Skip to content

Commit 2ea6b5b

Browse files
committed
refac: contiued refactors to shader interop & cleanup
1 parent 67a47b4 commit 2ea6b5b

22 files changed

+409
-495
lines changed

extensions/pl_renderer_ext.c

Lines changed: 118 additions & 144 deletions
Large diffs are not rendered by default.

extensions/pl_renderer_internal.c

Lines changed: 61 additions & 83 deletions
Large diffs are not rendered by default.

extensions/pl_renderer_internal.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ typedef struct _plEnvironmentProbeData
247247
plBindGroupHandle atLightingBindGroup[6];
248248

249249
// GPU buffers
250-
plBufferHandle atGlobalBuffers[PL_MAX_FRAMES_IN_FLIGHT];
250+
plBufferHandle atViewBuffers[PL_MAX_FRAMES_IN_FLIGHT];
251+
plBufferHandle atView2Buffers[PL_MAX_FRAMES_IN_FLIGHT];
251252

252253
// submitted drawables
253254
uint32_t* sbuVisibleDeferredEntities[6];
@@ -312,7 +313,8 @@ typedef struct _plView
312313
plBindGroupHandle tLightingBindGroup;
313314

314315
// GPU buffers
315-
plBufferHandle atGlobalBuffers[PL_MAX_FRAMES_IN_FLIGHT];
316+
plBufferHandle atViewBuffers[PL_MAX_FRAMES_IN_FLIGHT];
317+
plBufferHandle atView2Buffers[PL_MAX_FRAMES_IN_FLIGHT];
316318

317319
// submitted drawables
318320
uint32_t* sbtVisibleDrawables;
@@ -351,9 +353,10 @@ typedef struct _plScene
351353
uint32_t* sbuIndexBuffer;
352354
plGpuMaterial* sbtMaterialBuffer;
353355
plVec4* sbtSkinVertexDataBuffer;
354-
plGpuLight* sbtLightData;
356+
plGpuLight* sbtLightData;
355357

356358
// GPU buffers
359+
plBufferHandle atSceneBuffer[PL_MAX_FRAMES_IN_FLIGHT];
357360
plBufferHandle tVertexBuffer;
358361
plBufferHandle tIndexBuffer;
359362
plBufferHandle tStorageBuffer;
@@ -405,20 +408,20 @@ typedef struct _plScene
405408
uint32_t uCubeTextureIndexCount;
406409
plHashMap64 tTextureIndexHashmap; // texture handle <-> index
407410
plHashMap64 tCubeTextureIndexHashmap; // texture handle <-> index
408-
plBindGroupHandle atGlobalBindGroup[PL_MAX_FRAMES_IN_FLIGHT];
411+
plBindGroupHandle atBindGroups[PL_MAX_FRAMES_IN_FLIGHT];
409412

410413
// material hashmaps (material component <-> GPU material)
411414
plMaterialComponent* sbtMaterials;
412415
plHashMap64 tMaterialHashmap;
413416

414417
// shadows
415-
plBufferHandle atShadowCameraBuffers[PL_MAX_FRAMES_IN_FLIGHT];
416-
plBufferHandle atLightShadowDataBuffer[PL_MAX_FRAMES_IN_FLIGHT];
418+
plBufferHandle atShadowCameraBuffers[PL_MAX_FRAMES_IN_FLIGHT];
419+
plBufferHandle atLightShadowDataBuffer[PL_MAX_FRAMES_IN_FLIGHT];
417420
plGpuLightShadow* sbtLightShadowData;
418-
uint32_t uShadowOffset;
419-
uint32_t uShadowIndex;
420-
uint32_t uDShadowOffset;
421-
uint32_t uDShadowIndex;
421+
uint32_t uShadowOffset;
422+
uint32_t uShadowIndex;
423+
uint32_t uDShadowOffset;
424+
uint32_t uDShadowIndex;
422425

423426
// environment probes
424427
plEntity tProbeMesh;
@@ -462,7 +465,7 @@ typedef struct _plRefRendererData
462465
plRenderPassLayoutHandle tPickRenderPassLayout;
463466

464467
// bind group layouts
465-
plBindGroupLayoutHandle tSceneBGLayout;
468+
plBindGroupLayoutHandle tViewBGLayout;
466469
plBindGroupLayoutHandle tShadowGlobalBGLayout;
467470

468471
// renderer specific log channel
@@ -474,12 +477,14 @@ typedef struct _plRefRendererData
474477
plDeviceMemoryAllocatorI* ptStagingUnCachedAllocator;
475478
plDeviceMemoryAllocatorI* ptStagingUnCachedBuddyAllocator;
476479
plDeviceMemoryAllocatorI* ptStagingCachedAllocator;
480+
481+
// samplers
482+
plSamplerHandle tSamplerLinearClamp;
483+
plSamplerHandle tSamplerNearestClamp;
484+
plSamplerHandle tSamplerLinearRepeat;
485+
plSamplerHandle tSamplerNearestRepeat;
477486

478-
// default textures & samplers & bindgroups
479-
plSamplerHandle tDefaultSampler;
480-
plSamplerHandle tSkyboxSampler;
481-
plSamplerHandle tShadowSampler;
482-
plSamplerHandle tEnvSampler;
487+
// default textures
483488
plTextureHandle tDummyTexture;
484489
plTextureHandle tDummyTextureCube;
485490

shaders/bg_scene.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef BG_SCENE_INC
2+
#define BG_SCENE_INC
3+
4+
#include "pl_shader_interop_renderer.h"
5+
#include "lights.glsl"
6+
#include "material_info.glsl"
7+
8+
//-----------------------------------------------------------------------------
9+
// [SECTION] bind group 0
10+
//-----------------------------------------------------------------------------
11+
12+
layout(std140, set = 0, binding = 0) buffer _tGpuScene
13+
{
14+
plGpuSceneData tData;
15+
} tGpuScene;
16+
17+
layout(std140, set = 0, binding = 1) readonly buffer _tVertexBuffer
18+
{
19+
vec4 atVertexData[];
20+
} tVertexBuffer;
21+
22+
layout(std140, set = 0, binding = 2) readonly buffer _tTransformBuffer
23+
{
24+
mat4 atTransform[];
25+
} tTransformBuffer;
26+
27+
layout(set = 0, binding = 3) readonly buffer plMaterialInfo
28+
{
29+
plGpuMaterial atMaterials[];
30+
} tMaterialInfo;
31+
32+
// samplers
33+
layout(set = 0, binding = 4) uniform sampler tSamplerLinearClamp;
34+
layout(set = 0, binding = 5) uniform sampler tSamplerNearestClamp;
35+
layout(set = 0, binding = 6) uniform sampler tSamplerLinearRepeat;
36+
layout(set = 0, binding = 7) uniform sampler tSamplerNearestRepeat;
37+
38+
layout(set = 0, binding = 8) uniform texture2D at2DTextures[PL_MAX_BINDLESS_TEXTURES];
39+
layout(set = 0, binding = PL_MAX_BINDLESS_CUBE_TEXTURE_SLOT) uniform textureCube atCubeTextures[PL_MAX_BINDLESS_TEXTURES];
40+
41+
#endif // BG_SCENE_INC

shaders/bg_view.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef BG_VIEW_INC
2+
#define BG_VIEW_INC
3+
4+
#include "pl_shader_interop_renderer.h"
5+
#include "lights.glsl"
6+
7+
#ifndef PL_VIEW_SET_LOCATION
8+
#define PL_VIEW_SET_LOCATION 1
9+
#endif
10+
11+
layout(set = PL_VIEW_SET_LOCATION, binding = 0) readonly uniform _plViewInfo
12+
{
13+
plGpuViewData data[];
14+
} tViewInfo;
15+
16+
layout(set = PL_VIEW_SET_LOCATION, binding = 1) readonly buffer _tViewInfo2
17+
{
18+
plGpuViewData data[];
19+
} tViewInfo2;
20+
21+
layout(set = PL_VIEW_SET_LOCATION, binding = 2) uniform _plLightInfo
22+
{
23+
plGpuLight atData[1];
24+
} tLightInfo;
25+
26+
layout(set = PL_VIEW_SET_LOCATION, binding = 3) readonly buffer plDShadowData
27+
{
28+
plGpuLightShadow atData[];
29+
} tDShadowData;
30+
31+
layout(set = PL_VIEW_SET_LOCATION, binding = 4) readonly buffer plShadowData
32+
{
33+
plGpuLightShadow atData[];
34+
} tShadowData;
35+
36+
layout(set = PL_VIEW_SET_LOCATION, binding = 5) readonly buffer plProbeData
37+
{
38+
plGpuProbe atData[];
39+
} tProbeData;
40+
41+
#endif // BG_VIEW_INC

shaders/deferred_lighting.frag

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#extension GL_ARB_separate_shader_objects : enable
33
#extension GL_EXT_nonuniform_qualifier : enable
44

5-
#include "global.inc"
5+
#include "bg_scene.inc"
6+
#include "bg_view.inc"
67
#include "math.glsl"
78

89
//-----------------------------------------------------------------------------
@@ -13,45 +14,14 @@ layout(constant_id = 0) const int iRenderingFlags = 0;
1314
layout(constant_id = 1) const int iLightCount = 0;
1415
layout(constant_id = 2) const int iProbeCount = 0;
1516

16-
//-----------------------------------------------------------------------------
17-
// [SECTION] bind group 1
18-
//-----------------------------------------------------------------------------
19-
20-
layout(input_attachment_index = 1, set = 1, binding = 0) uniform subpassInput tAlbedoSampler;
21-
layout(input_attachment_index = 2, set = 1, binding = 1) uniform subpassInput tNormalTexture;
22-
layout(input_attachment_index = 3, set = 1, binding = 2) uniform subpassInput tAOMetalRoughnessTexture;
23-
layout(input_attachment_index = 0, set = 1, binding = 3) uniform subpassInput tDepthSampler;
24-
2517
//-----------------------------------------------------------------------------
2618
// [SECTION] bind group 2
2719
//-----------------------------------------------------------------------------
2820

29-
layout(set = 2, binding = 0) readonly buffer _plGlobalInfo
30-
{
31-
plGpuGlobalData data[];
32-
} tGlobalInfo;
33-
34-
layout(set = 2, binding = 1) uniform _plLightInfo
35-
{
36-
plGpuLight atData[1];
37-
} tLightInfo;
38-
39-
layout(set = 2, binding = 2) readonly buffer plDShadowData
40-
{
41-
plGpuLightShadow atData[];
42-
} tDShadowData;
43-
44-
layout(set = 2, binding = 3) readonly buffer plShadowData
45-
{
46-
plGpuLightShadow atData[];
47-
} tShadowData;
48-
49-
layout(set = 2, binding = 4) readonly buffer plProbeData
50-
{
51-
plGpuProbe atData[];
52-
} tProbeData;
53-
54-
layout(set = 2, binding = 5) uniform sampler tShadowSampler;
21+
layout(input_attachment_index = 1, set = 2, binding = 0) uniform subpassInput tAlbedoSampler;
22+
layout(input_attachment_index = 2, set = 2, binding = 1) uniform subpassInput tNormalTexture;
23+
layout(input_attachment_index = 3, set = 2, binding = 2) uniform subpassInput tAOMetalRoughnessTexture;
24+
layout(input_attachment_index = 0, set = 2, binding = 3) uniform subpassInput tDepthSampler;
5525

5626
//-----------------------------------------------------------------------------
5727
// [SECTION] dynamic bind group
@@ -82,19 +52,19 @@ void main()
8252
vec3 color = vec3(0);
8353

8454
float depth = subpassLoad(tDepthSampler).r;
85-
vec3 ndcSpace = vec3((gl_FragCoord.x - tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tViewportInfo.x) / tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tViewportSize.x, (gl_FragCoord.y - tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tViewportInfo.y) / tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tViewportSize.y, depth);
55+
vec3 ndcSpace = vec3(gl_FragCoord.x / tViewInfo2.data[tObjectInfo.tData.uGlobalIndex].tViewportSize.x, gl_FragCoord.y / tViewInfo2.data[tObjectInfo.tData.uGlobalIndex].tViewportSize.y, depth);
8656

8757
vec3 clipSpace = ndcSpace;
8858
clipSpace.xy = clipSpace.xy * 2.0 - 1.0;
89-
vec4 homoLocation = inverse(tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tCameraProjection) * vec4(clipSpace, 1.0);
59+
vec4 homoLocation = inverse(tViewInfo2.data[tObjectInfo.tData.uGlobalIndex].tCameraProjection) * vec4(clipSpace, 1.0);
9060

9161
vec4 tViewPosition = homoLocation;
9262
tViewPosition.xyz = tViewPosition.xyz / tViewPosition.w;
9363
tViewPosition.x = tViewPosition.x;
9464
tViewPosition.y = tViewPosition.y;
9565
tViewPosition.z = tViewPosition.z;
9666
tViewPosition.w = 1.0;
97-
vec4 tWorldPosition = inverse(tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tCameraView) * tViewPosition;
67+
vec4 tWorldPosition = inverse(tViewInfo2.data[tObjectInfo.tData.uGlobalIndex].tCameraView) * tViewPosition;
9868
vec3 n = Decode(subpassLoad(tNormalTexture).xy);
9969

10070
MaterialInfo materialInfo;
@@ -123,7 +93,7 @@ void main()
12393
vec3 f_metal_brdf_ibl = vec3(0.0);
12494
vec3 f_emissive = vec3(0.0);
12595

126-
vec3 v = normalize(tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tCameraPos.xyz - tWorldPosition.xyz);
96+
vec3 v = normalize(tViewInfo2.data[tObjectInfo.tData.uGlobalIndex].tCameraPos.xyz - tWorldPosition.xyz);
12797

12898
// Calculate lighting contribution from image based lighting source (IBL)
12999
if(bool(iRenderingFlags & PL_RENDERING_FLAG_USE_IBL) && iProbeCount > 0)
@@ -146,7 +116,7 @@ void main()
146116
{
147117
f_diffuse = getDiffuseLight(n, iProbeIndex) * tBaseColor.rgb;
148118

149-
int iMips = textureQueryLevels(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tEnvSampler));
119+
int iMips = textureQueryLevels(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tSamplerNearestRepeat));
150120
f_specular_metal = getIBLRadianceGGX(n, v, materialInfo.perceptualRoughness, iMips, tWorldPosition.xyz, iProbeIndex);
151121
f_specular_dielectric = f_specular_metal;
152122

shaders/deferred_lighting.vert

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
#extension GL_ARB_separate_shader_objects : enable
33
#extension GL_EXT_nonuniform_qualifier : enable
44

5-
#include "global.inc"
5+
#include "bg_scene.inc"
6+
7+
layout(constant_id = 0) const int iRenderingFlags = 0;
8+
layout(constant_id = 1) const int iLightCount = 0;
9+
layout(constant_id = 2) const int iProbeCount = 0;
10+
611
//-----------------------------------------------------------------------------
712
// [SECTION] dynamic bind group
813
//-----------------------------------------------------------------------------

shaders/forward.frag

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#extension GL_ARB_separate_shader_objects : enable
33
#extension GL_EXT_nonuniform_qualifier : enable
44

5-
#include "global.inc"
5+
#include "bg_scene.inc"
6+
#include "bg_view.inc"
67

78
//-----------------------------------------------------------------------------
89
// [SECTION] specialication constants
@@ -16,37 +17,6 @@ layout(constant_id = 4) const int iRenderingFlags = 0;
1617
layout(constant_id = 5) const int iLightCount = 0;
1718
layout(constant_id = 6) const int iProbeCount = 0;
1819

19-
//-----------------------------------------------------------------------------
20-
// [SECTION] bind group 1
21-
//-----------------------------------------------------------------------------
22-
23-
layout(set = 1, binding = 0) readonly buffer _plGlobalInfo
24-
{
25-
plGpuGlobalData data[];
26-
} tGlobalInfo;
27-
28-
layout(set = 1, binding = 1) uniform _plLightInfo
29-
{
30-
plGpuLight atData[1];
31-
} tLightInfo;
32-
33-
layout(set = 1, binding = 2) readonly buffer plDShadowData
34-
{
35-
plGpuLightShadow atData[];
36-
} tDShadowData;
37-
38-
layout(set = 1, binding = 3) readonly buffer plShadowData
39-
{
40-
plGpuLightShadow atData[];
41-
} tShadowData;
42-
43-
layout(set = 1, binding = 4) readonly buffer plProbeData
44-
{
45-
plGpuProbe atData[];
46-
} tProbeData;
47-
48-
layout(set = 1, binding = 5) uniform sampler tShadowSampler;
49-
5020
//-----------------------------------------------------------------------------
5121
// [SECTION] dynamic bind group
5222
//-----------------------------------------------------------------------------
@@ -128,7 +98,7 @@ void main()
12898
vec3 f_metal_brdf_ibl = vec3(0.0);
12999
vec3 f_emissive = vec3(0.0);
130100

131-
vec3 v = normalize(tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tCameraPos.xyz - tShaderIn.tWorldPosition.xyz);
101+
vec3 v = normalize(tViewInfo2.data[tObjectInfo.tData.uGlobalIndex].tCameraPos.xyz - tShaderIn.tWorldPosition.xyz);
132102

133103
// Calculate lighting contribution from image based lighting source (IBL)
134104
if(bool(iRenderingFlags & PL_RENDERING_FLAG_USE_IBL) && iProbeCount > 0)
@@ -151,7 +121,7 @@ void main()
151121
{
152122
f_diffuse = getDiffuseLight(n, iProbeIndex) * tBaseColor.rgb;
153123

154-
int iMips = textureQueryLevels(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tEnvSampler));
124+
int iMips = textureQueryLevels(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tSamplerNearestRepeat));
155125
f_specular_metal = getIBLRadianceGGX(n, v, materialInfo.perceptualRoughness, iMips, tShaderIn.tWorldPosition.xyz, iProbeIndex);
156126
f_specular_dielectric = f_specular_metal;
157127

@@ -173,7 +143,7 @@ void main()
173143
{
174144
float u_OcclusionStrength = 1.0;
175145
float ao = 1.0;
176-
ao = texture(sampler2D(at2DTextures[nonuniformEXT(material.iOcclusionTexIdx)], tDefaultSampler), tShaderIn.tUV[material.iOcclusionUVSet]).r;
146+
ao = texture(sampler2D(at2DTextures[nonuniformEXT(material.iOcclusionTexIdx)], tSamplerLinearRepeat), tShaderIn.tUV[material.iOcclusionUVSet]).r;
177147
color = color * (1.0 + u_OcclusionStrength * (ao - 1.0));
178148
}
179149

@@ -208,7 +178,7 @@ void main()
208178

209179
// Get cascade index for the current fragment's view position
210180

211-
vec4 inViewPos = tGlobalInfo.data[tObjectInfo.tData.uGlobalIndex].tCameraView * vec4(tShaderIn.tWorldPosition.xyz, 1.0);
181+
vec4 inViewPos = tViewInfo2.data[tObjectInfo.tData.uGlobalIndex].tCameraView * vec4(tShaderIn.tWorldPosition.xyz, 1.0);
212182
for(uint j = 0; j < tLightData.iCascadeCount - 1; ++j)
213183
{
214184
if(inViewPos.z > tShadowData.cascadeSplits[j])
@@ -294,7 +264,7 @@ void main()
294264
vec2(1, 2),
295265
};
296266
vec2 sampleLocation = vec2(tShadowData.fXOffset, tShadowData.fYOffset) + (result.xy * tShadowData.fFactor) + (faceoffsets[int(result.z)] * tShadowData.fFactor);
297-
float dist = texture(sampler2D(at2DTextures[nonuniformEXT(tShadowData.iShadowMapTexIdx)], tShadowSampler), sampleLocation).r;
267+
float dist = texture(sampler2D(at2DTextures[nonuniformEXT(tShadowData.iShadowMapTexIdx)], tSamplerNearestClamp), sampleLocation).r;
298268
float fDist = shadowCoord.z;
299269
dist = dist * shadowCoord.w;
300270
// dist = 1 - dist * shadowCoord.w;
@@ -413,7 +383,7 @@ void main()
413383
f_emissive = material.tEmissiveFactor;
414384
if(bool(iTextureMappingFlags & PL_HAS_EMISSIVE_MAP))
415385
{
416-
f_emissive *= pl_srgb_to_linear(texture(sampler2D(at2DTextures[nonuniformEXT(material.iEmissiveTexIdx)], tDefaultSampler), tShaderIn.tUV[material.iEmissiveUVSet]).rgb);
386+
f_emissive *= pl_srgb_to_linear(texture(sampler2D(at2DTextures[nonuniformEXT(material.iEmissiveTexIdx)], tSamplerLinearRepeat), tShaderIn.tUV[material.iEmissiveUVSet]).rgb);
417387
}
418388

419389
color = f_emissive + color;

0 commit comments

Comments
 (0)