Descriptors now work in metal hopefully

This commit is contained in:
Dynamitos
2024-09-16 13:00:53 +02:00
parent 49e94d3b74
commit 6417ab940d
45 changed files with 435 additions and 476 deletions
+2 -2
View File
@@ -25,9 +25,9 @@ void computeFrustums(ComputeShaderInput in)
frustum.sides[1] = computePlane(origin, corners[1], corners[3]);
frustum.sides[2] = computePlane(origin, corners[0], corners[1]);
frustum.sides[3] = computePlane(origin, corners[3], corners[2]);
if(in.dispatchThreadID.x < pDispatchParams.numThreads.x && in.dispatchThreadID.y < pDispatchParams.numThreads.y)
if(in.dispatchThreadID.x < pDispatchParams.p.numThreads.x && in.dispatchThreadID.y < pDispatchParams.p.numThreads.y)
{
uint index = in.dispatchThreadID.x + (in.dispatchThreadID.y * pDispatchParams.numThreads.x);
uint index = in.dispatchThreadID.x + (in.dispatchThreadID.y * pDispatchParams.p.numThreads.x);
pDispatchParams.frustums[index] = frustum;
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ Params vertexMain(
DebugVertex vert
){
Params result;
result.pos = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, float4(vert.position, 1)));
result.pos = mul(pViewParams.c.projectionMatrix, mul(pViewParams.c.viewMatrix, float4(vert.position, 1)));
result.color = vert.color;
return result;
}
+5 -5
View File
@@ -30,7 +30,7 @@ groupshared bool meshVisible;
ParameterBlock<DepthData> pDepthAttachment;
bool isBoxVisible(AABB bounding)
{
int2 mipDimensions = int2(int(pViewParams.screenDimensions.x), int(pViewParams.screenDimensions.y));
int2 mipDimensions = int2(int(pViewParams.c.screenDimensions.x), int(pViewParams.c.screenDimensions.y));
// now we calculate what mip level we need to only sample up to 4 texels covering the entire meshlet
int2 screenCornerMin = mipDimensions;
int2 screenCornerMax = int2(0, 0);
@@ -91,14 +91,14 @@ void taskMain(
p.instanceId = pOffsets.instanceOffset + groupID;
p.meshletOffset = mesh.meshletOffset;
p.cullingOffset = pScene.cullingOffsets[p.instanceId];
modelViewProjection = mul(mul(pViewParams.projectionMatrix, pViewParams.viewMatrix), instance.transformMatrix);
modelViewProjection = mul(mul(pViewParams.c.projectionMatrix, pViewParams.c.viewMatrix), instance.transformMatrix);
float3 origin = viewToModel(instance.inverseTransformMatrix, float4(0, 0, 0, 1)).xyz;
const float offset = 0.0f;
float3 corners[4] = {
screenToModel(instance.inverseTransformMatrix, float4(offset, offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(pViewParams.screenDimensions.x - offset, offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(offset, pViewParams.screenDimensions.y - offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(pViewParams.screenDimensions - float2(offset, offset), -1.0f, 1.0f)).xyz
screenToModel(instance.inverseTransformMatrix, float4(pViewParams.c.screenDimensions.x - offset, offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(offset, pViewParams.c.screenDimensions.y - offset, -1.0f, 1.0f)).xyz,
screenToModel(instance.inverseTransformMatrix, float4(pViewParams.c.screenDimensions - float2(offset, offset), -1.0f, 1.0f)).xyz
};
viewFrustum.sides[0] = computePlane(origin, corners[2], corners[0]);
viewFrustum.sides[1] = computePlane(origin, corners[1], corners[3]);
+2 -2
View File
@@ -47,9 +47,9 @@ void reduceLevel(
void sourceCopy(
uint2 dispatchID: SV_DispatchThreadID
) {
if(dispatchID.x >= pViewParams.screenDimensions.x || dispatchID.y >= pViewParams.screenDimensions.y)
if(dispatchID.x >= pViewParams.c.screenDimensions.x || dispatchID.y >= pViewParams.c.screenDimensions.y)
{
return;
}
pDepthAttachment.buffer[dispatchID.x + (dispatchID.y * uint(pViewParams.screenDimensions.x))] = pDepthAttachment.texture[uint2(dispatchID)];
pDepthAttachment.buffer[dispatchID.x + (dispatchID.y * uint(pViewParams.c.screenDimensions.x))] = pDepthAttachment.texture[uint2(dispatchID)];
}
+1 -1
View File
@@ -72,7 +72,7 @@ void cullLights(ComputeShaderInput in)
uMaxDepth = 0x0;
oLightCount = 0;
tLightCount = 0;
groupFrustum = pDispatchParams.frustums[in.groupID.x + (in.groupID.y * pDispatchParams.numThreadGroups.x)];
groupFrustum = pDispatchParams.frustums[in.groupID.x + (in.groupID.y * pDispatchParams.p.numThreadGroups.x)];
}
GroupMemoryBarrierWithGroupSync();
+2 -2
View File
@@ -73,10 +73,10 @@ VertexShaderOutput vertexMain(
};
VertexShaderOutput output;
float3x3 cameraRotation = float3x3(pViewParams.viewMatrix);
float3x3 cameraRotation = float3x3(pViewParams.c.viewMatrix);
float4 worldPos = float4(mul(cameraRotation, vertices[vertexIndex]), 1.0f);
//clip(dot(worldPos, clipPlane));
output.clipPos = mul(pViewParams.projectionMatrix, worldPos);
output.clipPos = mul(pViewParams.c.projectionMatrix, worldPos);
output.texCoords = normalize(vertices[vertexIndex]);
return output;
}
+1 -1
View File
@@ -66,7 +66,7 @@ VertexOutput vertexMain(VertexInput input)
float4 vertex = coordinates[input.vertexId];
VertexOutput output;
output.texCoords = vertex.zw;
output.position = mul(pViewParams.projectionMatrix, float4(vertex.xy, 0, 1));
output.position = mul(pViewParams.c.projectionMatrix, float4(vertex.xy, 0, 1));
output.glyphIndex = pRender.instances[input.instanceId].glyphIndex;
return output;
}
+2 -2
View File
@@ -34,7 +34,7 @@ void meshMain(
out vertices WaterVertex vertices[TILE_VERTS],
out indices uint3 indices[TILE_PRIMS],
) {
float4x4 vp = mul(pViewParams.projectionMatrix, pViewParams.viewMatrix);
float4x4 vp = mul(pViewParams.c.projectionMatrix, pViewParams.c.viewMatrix);
SetMeshOutputCounts(TILE_VERTS, TILE_PRIMS);
for(uint i = threadID; i < TILE_PRIMS; i += MESH_GROUP_SIZE)
@@ -48,7 +48,7 @@ void meshMain(
float3 worldPos = params.offset + objectPos * params.extent + float3(groupID.x * params.extent, 0, groupID.y * params.extent);
float lodDisplacement = 0;
float3 camPos = pViewParams.cameraPos_WS.xyz;
float3 camPos = pViewParams.c.cameraPos_WS.xyz;
float cameraDistance = distance(worldPos, camPos);
float threshold = 0;
if(params.numMeshes == 3)
+1 -1
View File
@@ -23,7 +23,7 @@ float Beckmann(float ndoth, float roughness) {
[shader("pixel")]
float4 fragmentMain(WaterVertex vert) : SV_TARGET {
float3 lightDir = -normalize(pWaterMaterial.sunDirection);
float3 viewDir = normalize(pViewParams.cameraPos_WS.xyz - vert.position_WS);
float3 viewDir = normalize(pViewParams.c.cameraPos_WS.xyz - vert.position_WS);
float3 halfwayDir = normalize(lightDir + viewDir);
float depth = vert.depth;
float LdotH = clamp(dot(lightDir, halfwayDir), 0, 1);
+1 -1
View File
@@ -15,7 +15,7 @@ void taskMain(
bounding.minCorner = float3(tile.location.x, tile.height, tile.location.y) * tile.extent;
bounding.maxCorner = float3(tile.location.x + 1, tile.height, tile.location.y + 1) * tile.extent;
float3 median = (bounding.minCorner + bounding.maxCorner) / 2;
float distance = distance(median, pViewParams.cameraPos_WS.xyz);
float distance = distance(median, pViewParams.c.cameraPos_WS.xyz);
float tileDistance = distance / tile.extent;
uint numMeshes = groupID.y + 1;
+1 -1
View File
@@ -67,7 +67,7 @@ struct AABB
{
float4 clipCorner = mul(mvp, corners[i]);
float4 screenCorner = clipToScreen(clipCorner);
int2 screenCoords = int2(clamp(int(screenCorner.x), 0, int(pViewParams.screenDimensions.x)), clamp(int(screenCorner.y), 0, int(pViewParams.screenDimensions.y)));
int2 screenCoords = int2(clamp(int(screenCorner.x), 0, int(pViewParams.c.screenDimensions.x)), clamp(int(screenCorner.y), 0, int(pViewParams.c.screenDimensions.y)));
screenCornerMin = int2(min(screenCornerMin.x, screenCoords.x), min(screenCornerMin.y, screenCoords.y));
screenCornerMax = int2(max(screenCornerMax.x, screenCoords.x), max(screenCornerMax.y, screenCoords.y));
maxDepth = max(maxDepth, screenCorner.z);
+11 -7
View File
@@ -12,9 +12,13 @@ struct ViewParameter
float4x4 inverseProjection;
float4 cameraPos_WS;
float2 screenDimensions;
}
layout(set=0)
ParameterBlock<ViewParameter> pViewParams;
};
struct ViewParamWrapper
{
ParameterBlock<ViewParameter> c;
};
layout(set = 0)
ParameterBlock<ViewParamWrapper> pViewParams;
float4 worldToModel(float4x4 inverseTransform, float4 world)
{
@@ -27,7 +31,7 @@ float4 worldToModel(float4x4 inverseTransform, float4 world)
float4 viewToWorld(float4 view)
{
float4 world = mul(pViewParams.inverseViewMatrix, view);
float4 world = mul(pViewParams.c.inverseViewMatrix, view);
world = world / world.w;
@@ -43,7 +47,7 @@ float4 viewToModel(float4x4 inverseTransform, float4 view)
float4 clipToView(float4 clip)
{
float4 view = mul(pViewParams.inverseProjection, clip);
float4 view = mul(pViewParams.c.inverseProjection, clip);
view = view / view.w;
@@ -59,7 +63,7 @@ float4 clipToWorld(float4 clip)
float4 screenToView(float4 screen)
{
float2 texCoord = screen.xy / pViewParams.screenDimensions;
float2 texCoord = screen.xy / pViewParams.c.screenDimensions;
// Convert to clip space
float4 clip = float4( float2( texCoord.x, 1.0f-texCoord.y ) * 2.0f - 1.0f, screen.z, screen.w);
@@ -88,7 +92,7 @@ float4 clipToScreen(float4 clip)
float oz = 1;
float pz = 0 - 1;
float zf = pz * ndc.z + oz;
return float4(float2(texCoords.x, 1 - texCoords.y) * pViewParams.screenDimensions, zf, 1.0f);
return float4(float2(texCoords.x, 1 - texCoords.y) * pViewParams.c.screenDimensions, zf, 1.0f);
}
struct Plane
+7 -2
View File
@@ -6,6 +6,11 @@ struct DispatchParams
uint pad0;
uint3 numThreads;
uint pad1;
RWStructuredBuffer<Frustum> frustums;
};
struct DispatchParamWrapper
{
ParameterBlock<DispatchParams> p;
RWStructuredBuffer<Frustum> frustums;
}
ParameterBlock<DispatchParams> pDispatchParams;
ParameterBlock<DispatchParamWrapper> pDispatchParams;
+3 -6
View File
@@ -21,7 +21,6 @@ struct FragmentParameter
{
float4 position_CS : SV_Position;
#ifndef POS_ONLY
float3 cameraPos_WS: POSITION0;
float3 normal_WS : NORMAL0;
float3 tangent_WS : TANGENT0;
float3 biTangent_WS : TANGENT1;
@@ -52,7 +51,7 @@ struct FragmentParameter
float3x3 tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
result.tbn = tbn;
result.position_TS = mul(tbn, position_WS);
result.viewDir_TS = mul(tbn, normalize(cameraPos_WS - position_WS));
result.viewDir_TS = mul(tbn, normalize(pViewParams.c.cameraPos_WS.xyz - position_WS));
result.normal_TS = mul(tbn, normal_WS);
return result;
}
@@ -67,7 +66,6 @@ struct FragmentParameter
FragmentParameter result;
result.position_CS = f0.position_CS * barycentricCoords.x + f1.position_CS * barycentricCoords.y + f2.position_CS * barycentricCoords.z;
#ifndef POS_ONLY
result.cameraPos_WS = f0.cameraPos_WS * barycentricCoords.x + f1.cameraPos_WS * barycentricCoords.y + f2.cameraPos_WS * barycentricCoords.z;
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
result.biTangent_WS = f0.biTangent_WS * barycentricCoords.x + f1.biTangent_WS * barycentricCoords.y + f2.biTangent_WS * barycentricCoords.z;
@@ -104,8 +102,8 @@ struct VertexAttributes
{
float4 modelPos = float4(position_MS, 1);
float4 worldPos = mul(transformMatrix, modelPos);
float4 viewPos = mul(pViewParams.viewMatrix, worldPos);
float4 clipPos = mul(pViewParams.projectionMatrix, viewPos);
float4 viewPos = mul(pViewParams.c.viewMatrix, worldPos);
float4 clipPos = mul(pViewParams.c.projectionMatrix, viewPos);
FragmentParameter result;
result.position_CS = clipPos;
#ifndef POS_ONLY
@@ -113,7 +111,6 @@ struct VertexAttributes
float3 T = mul(normalMatrix, tangent_MS);
float3 N = mul(normalMatrix, normal_MS);
float3 B = mul(normalMatrix, biTangent_MS);
result.cameraPos_WS = pViewParams.cameraPos_WS.xyz;
result.normal_WS = N;
result.tangent_WS = T;
result.biTangent_WS = B;
+10 -10
View File
@@ -20,8 +20,8 @@ struct MeshData
uint32_t numIndices;
};
static const uint32_t MAX_VERTICES = 256;
static const uint32_t MAX_PRIMITIVES = 256;
static const uint32_t MAX_VERTICES = 64;
static const uint32_t MAX_PRIMITIVES = 126;
static const uint32_t MAX_MESHLETS_PER_INSTANCE = 2048;
struct InstanceData
@@ -39,19 +39,19 @@ struct MeshletCullingInfo
}
};
struct DrawCallOffsets
cbuffer DrawCallOffsets
{
uint instanceOffset;
uint textureOffset;
uint samplerOffset;
uint floatOffset;
};
#ifdef RAY_TRACING
layout(shaderRecordEXT)
#else
layout(push_constant)
#endif
ConstantBuffer<DrawCallOffsets> pOffsets;
} pOffsets;
//#ifdef RAY_TRACING
//layout(shaderRecordEXT)
//#else
//layout(push_constant)
//#endif
//ConstantBuffer<DrawCallOffsets> pOffsets;
struct Scene
{
+3 -3
View File
@@ -10,11 +10,11 @@ void raygen()
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
const float2 inUV = pixelCenter / float2(LaunchSize.xy);
float2 d = float2(inUV.x, 1 - inUV.y) * 2.0 - 1.0;
float4 target = mul(pViewParams.inverseProjection, float4(d.x, d.y, 1, 1));
float4 target = mul(pViewParams.c.inverseProjection, float4(d.x, d.y, 1, 1));
RayDesc rayDesc;
rayDesc.Origin = mul(pViewParams.inverseViewMatrix, float4(0, 0, 0, 1)).xyz;
rayDesc.Direction = mul(pViewParams.inverseViewMatrix, float4(normalize(target.xyz), 0)).xyz;
rayDesc.Origin = mul(pViewParams.c.inverseViewMatrix, float4(0, 0, 0, 1)).xyz;
rayDesc.Direction = mul(pViewParams.c.inverseViewMatrix, float4(normalize(target.xyz), 0)).xyz;
rayDesc.TMin = 0.001;
rayDesc.TMax = 10000.0;