-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jonathan hoffstadt
committed
May 3, 2024
1 parent
80c31e5
commit 6a17906
Showing
6 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#version 450 | ||
#extension GL_ARB_separate_shader_objects : enable | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] specialication constants | ||
//----------------------------------------------------------------------------- | ||
|
||
layout(constant_id = 0) const int iMeshVariantFlags = 0; | ||
layout(constant_id = 1) const int iDataStride = 0; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] bind group 0 | ||
//----------------------------------------------------------------------------- | ||
|
||
layout(set = 0, binding = 0) uniform _plGlobalInfo | ||
{ | ||
vec4 tCameraPos; | ||
mat4 tCameraView; | ||
mat4 tCameraProjection; | ||
mat4 tCameraViewProjection; | ||
} tGlobalInfo; | ||
|
||
layout(std140, set = 0, binding = 1) readonly buffer _tVertexBuffer | ||
{ | ||
vec4 atVertexData[]; | ||
} tVertexBuffer; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] dynamic bind group | ||
//----------------------------------------------------------------------------- | ||
|
||
layout(set = 1, binding = 0) uniform _plObjectInfo | ||
{ | ||
vec4 tColor; | ||
float fThickness; | ||
int iDataOffset; | ||
int iVertexOffset; | ||
mat4 tModel; | ||
} tObjectInfo; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] entry | ||
//----------------------------------------------------------------------------- | ||
|
||
// output | ||
layout(location = 0) out vec4 outColor; | ||
|
||
void | ||
main() | ||
{ | ||
outColor = tObjectInfo.tColor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#version 450 | ||
#extension GL_ARB_separate_shader_objects : enable | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] specialication constants | ||
//----------------------------------------------------------------------------- | ||
|
||
layout(constant_id = 0) const int iMeshVariantFlags = 0; | ||
layout(constant_id = 1) const int iDataStride = 0; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] defines | ||
//----------------------------------------------------------------------------- | ||
|
||
// iMeshVariantFlags | ||
const int PL_MESH_FORMAT_FLAG_HAS_POSITION = 1 << 0; | ||
const int PL_MESH_FORMAT_FLAG_HAS_NORMAL = 1 << 1; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] bind group 0 | ||
//----------------------------------------------------------------------------- | ||
|
||
layout(set = 0, binding = 0) uniform _plGlobalInfo | ||
{ | ||
vec4 tCameraPos; | ||
mat4 tCameraView; | ||
mat4 tCameraProjection; | ||
mat4 tCameraViewProjection; | ||
} tGlobalInfo; | ||
|
||
layout(std140, set = 0, binding = 1) readonly buffer _tVertexBuffer | ||
{ | ||
vec4 atVertexData[]; | ||
} tVertexBuffer; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] dynamic bind group | ||
//----------------------------------------------------------------------------- | ||
|
||
layout(set = 1, binding = 0) uniform _plObjectInfo | ||
{ | ||
vec4 tColor; | ||
float fThickness; | ||
int iDataOffset; | ||
int iVertexOffset; | ||
mat4 tModel; | ||
} tObjectInfo; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] entry | ||
//----------------------------------------------------------------------------- | ||
|
||
// input | ||
layout(location = 0) in vec3 inPos; | ||
|
||
|
||
void | ||
main() | ||
{ | ||
const mat4 tMVP = tGlobalInfo.tCameraViewProjection * tObjectInfo.tModel; | ||
vec4 inPosition = vec4(inPos, 1.0); | ||
vec3 inNormal = vec3(0.0, 0.0, 0.0); | ||
|
||
const uint iVertexDataOffset = iDataStride * (gl_VertexIndex - tObjectInfo.iVertexOffset) + tObjectInfo.iDataOffset; | ||
int iCurrentAttribute = 0; | ||
if(bool(iMeshVariantFlags & PL_MESH_FORMAT_FLAG_HAS_POSITION)) iCurrentAttribute++; | ||
if(bool(iMeshVariantFlags & PL_MESH_FORMAT_FLAG_HAS_NORMAL)) { inNormal = tVertexBuffer.atVertexData[iVertexDataOffset + iCurrentAttribute].xyz;} | ||
|
||
vec4 tPos = tMVP * inPosition; | ||
vec4 tNorm = normalize(tMVP * vec4(inNormal, 0.0)); | ||
tPos = vec4(tPos.xyz + tNorm.xyz * tObjectInfo.fThickness, tPos.w); | ||
gl_Position = tPos; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include <metal_stdlib> | ||
#include <simd/simd.h> | ||
|
||
using namespace metal; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] specialization constants | ||
//----------------------------------------------------------------------------- | ||
|
||
constant int iMeshVariantFlags [[ function_constant(0) ]]; | ||
constant int iDataStride [[ function_constant(1) ]]; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] defines & structs | ||
//----------------------------------------------------------------------------- | ||
|
||
// iMeshVariantFlags | ||
#define PL_MESH_FORMAT_FLAG_HAS_POSITION 1 << 0 | ||
#define PL_MESH_FORMAT_FLAG_HAS_NORMAL 1 << 1 | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] bind group 0 | ||
//----------------------------------------------------------------------------- | ||
|
||
struct BindGroupData_0 | ||
{ | ||
float4 tCameraPosition; | ||
float4x4 tCameraView; | ||
float4x4 tCameraProjection; | ||
float4x4 tCameraViewProjection; | ||
}; | ||
|
||
struct BindGroup_0 | ||
{ | ||
device BindGroupData_0 *data; | ||
device float4 *atVertexData; | ||
}; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] dynamic bind group | ||
//----------------------------------------------------------------------------- | ||
|
||
struct DynamicData | ||
{ | ||
float4 tColor; | ||
float fThickness; | ||
int iDataOffset; | ||
int iVertexOffset; | ||
int iPadding[1]; | ||
float4x4 tModel; | ||
}; | ||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] input | ||
//----------------------------------------------------------------------------- | ||
|
||
struct VertexIn { | ||
float3 tPosition [[attribute(0)]]; | ||
}; | ||
|
||
|
||
//----------------------------------------------------------------------------- | ||
// [SECTION] entry | ||
//----------------------------------------------------------------------------- | ||
|
||
struct VertexOut { | ||
float4 tPositionOut [[position]]; | ||
}; | ||
|
||
vertex VertexOut vertex_main( | ||
uint vertexID [[ vertex_id ]], | ||
VertexIn in [[stage_in]], | ||
device const BindGroup_0& bg0 [[ buffer(1) ]], | ||
device const DynamicData& tObjectInfo [[ buffer(2) ]] | ||
) | ||
{ | ||
|
||
const float4x4 tMVP = bg0.data->tCameraViewProjection * tObjectInfo.tModel; | ||
float4 inPosition = float4(in.tPosition, 1.0); | ||
float3 inNormal = float3(0.0, 0.0, 0.0); | ||
|
||
int iCurrentAttribute = 0; | ||
|
||
const uint iVertexDataOffset = iDataStride * (vertexID - tObjectInfo.iVertexOffset) + tObjectInfo.iDataOffset; | ||
|
||
if(iMeshVariantFlags & PL_MESH_FORMAT_FLAG_HAS_POSITION) { iCurrentAttribute++;} | ||
if(iMeshVariantFlags & PL_MESH_FORMAT_FLAG_HAS_NORMAL) { inNormal = bg0.atVertexData[iVertexDataOffset + iCurrentAttribute].xyz; iCurrentAttribute++;} | ||
|
||
float4 tPos = tMVP * inPosition; | ||
float4 tNorm = fast::normalize(tMVP * float4(inNormal, 0.0)); | ||
tPos = float4(tPos.xyz + tNorm.xyz * tObjectInfo.fThickness, tPos.w); | ||
|
||
VertexOut tOut; | ||
tOut.tPositionOut = tPos; | ||
tOut.tPositionOut.y = tOut.tPositionOut.y * -1.0; | ||
return tOut; | ||
} | ||
|
||
fragment float4 fragment_main( | ||
VertexOut in [[stage_in]], | ||
device const BindGroup_0& bg0 [[ buffer(1) ]], | ||
device const DynamicData& tObjectInfo [[ buffer(2) ]], | ||
bool front_facing [[front_facing]] | ||
) | ||
{ | ||
return tObjectInfo.tColor; | ||
} |