Skip to content

Commit

Permalink
Share struct headers between GLSL and C++
Browse files Browse the repository at this point in the history
Should be nicer to not keep two definitions in sync
  • Loading branch information
sndels committed Nov 15, 2024
1 parent d07770c commit a14e642
Show file tree
Hide file tree
Showing 97 changed files with 821 additions and 643 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ endif() # PROSPER_ALLOCATOR_DEBUG
# Set up sub-builds and sources
add_subdirectory(ext)
add_subdirectory(src)
add_subdirectory(res/shader/shared)

if(PROSPER_ALWAYS_O2_DEPENDENCIES)
# Only optimize the dependencies that have a noticeable impact
Expand Down Expand Up @@ -113,6 +114,7 @@ target_link_libraries(prosper
meshoptimizer
mikktspace
shaderc
shared_shader_structs
spirv_headers
stb
tomlcpp
Expand Down
14 changes: 5 additions & 9 deletions res/shader/deferred_shading.comp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "scene/lighting.glsl"
#include "scene/lights.glsl"
#include "scene/skybox.glsl"
#include "shared/shader_structs/push_constants/deferred_shading.h"

layout(set = STORAGE_SET, binding = 0) uniform readonly image2D
inAlbedoRoughness;
Expand All @@ -18,12 +19,7 @@ layout(set = STORAGE_SET, binding = 2) uniform texture2D inNonLinearDepth;
layout(set = STORAGE_SET, binding = 3) uniform image2D outColor;
layout(set = STORAGE_SET, binding = 4) uniform sampler depthSampler;

layout(push_constant) uniform DeferredShadingPC
{
uint DrawType;
uint ibl;
}
PC;
layout(push_constant) uniform PushConstants { DeferredShadingPC PC; };

layout(local_size_x = GROUP_X, local_size_y = GROUP_Y) in;
void main()
Expand Down Expand Up @@ -63,15 +59,15 @@ void main()
if (PC.ibl == 1)
color += evalIBL(surface);

if (PC.DrawType != DrawType_Default)
if (PC.drawType != DrawType_Default)
{
if (PC.DrawType == DrawType_Position)
if (PC.drawType == DrawType_Position)
{
DebugInputs di;
di.position = surface.positionWS;
imageStore(
outColor, coord,
vec4(commonDebugDraw(PC.DrawType, di, surface.material), 1));
vec4(commonDebugDraw(PC.drawType, di, surface.material), 1));
}
else
{
Expand Down
10 changes: 3 additions & 7 deletions res/shader/dof/dilate.comp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
// Based on A Life of a Bokeh by Guillaume Abadie
// https://advances.realtimerendering.com/s2018/index.htm

#include "../shared/shader_structs/push_constants/dof/dilate.h"

layout(set = 0, binding = 0) uniform texture2D inTileMinMaxCoC;
layout(set = 0, binding = 1) uniform writeonly image2D outDilatedTileMinMaxCoC;
layout(set = 0, binding = 2) uniform sampler nearestSampler;

layout(push_constant) uniform DilatePC
{
ivec2 res;
vec2 invRes;
int gatherRadius;
}
PC;
layout(push_constant) uniform PushConstants { DilatePC PC; };

layout(local_size_x = GROUP_X, local_size_y = GROUP_Y) in;
void main()
Expand Down
9 changes: 2 additions & 7 deletions res/shader/dof/gather.comp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "../common/math.glsl"
#include "../common/random.glsl"
#include "../shared/shader_structs/push_constants/dof/gather.h"

// Based on A Life of a Bokeh by Guillaume Abadie
// https://advances.realtimerendering.com/s2018/index.htm
Expand All @@ -16,13 +17,7 @@ layout(set = 0, binding = 3) uniform writeonly image2D
layout(set = 0, binding = 4) uniform sampler nearestSampler;
layout(set = 0, binding = 5) uniform sampler trilinearSampler;

layout(push_constant) uniform GatherPC
{
ivec2 halfResolution;
vec2 invHalfResolution;
uint frameIndex;
}
PC;
layout(push_constant) uniform PushConstants { GatherPC PC; };

int octawebRingSampleCount(int ring)
{
Expand Down
10 changes: 3 additions & 7 deletions res/shader/dof/reduce.comp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// https://advances.realtimerendering.com/s2018/index.htm
// implemented using https://github.com/GPUOpen-Effects/FidelityFX-SPD

#include "../shared/shader_structs/push_constants/dof/reduce.h"

layout(set = 0, binding = 0) uniform image2D imgSrc;
layout(set = 0, binding = 1) uniform coherent image2D imgDst[12];
layout(std430, set = 0, binding = 2) coherent buffer SpdGlobalAtomicBuffer
Expand All @@ -14,13 +16,7 @@ layout(std430, set = 0, binding = 2) coherent buffer SpdGlobalAtomicBuffer
}
spdGlobalAtomic;

layout(push_constant) uniform ReducePC
{
ivec2 topMipResolution;
uint numWorkGroupsPerSlice;
uint mips;
}
PC;
layout(push_constant) uniform PushConstants { ReducePC PC; };

#define A_GPU 1
#define A_GLSL 1
Expand Down
9 changes: 2 additions & 7 deletions res/shader/dof/setup.comp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "../common/math.glsl"
#include "../scene/camera.glsl"
#include "../shared/shader_structs/push_constants/dof/setup.h"
#include "bilateral.glsl"

layout(set = STORAGE_SET, binding = 0) uniform readonly image2D inIllumination;
Expand All @@ -18,13 +19,7 @@ layout(set = STORAGE_SET, binding = 2) uniform image2D outIllumination;
layout(set = STORAGE_SET, binding = 3) uniform image2D outCircleOfConfusion;
layout(set = STORAGE_SET, binding = 4) uniform sampler depthSampler;

layout(push_constant) uniform SetupPC
{
float focusDistance;
float maxBackgroundCoC;
float maxCoC;
}
PC;
layout(push_constant) uniform PushConstants { SetupPC PC; };

float circleOfConfusion(float viewDepth)
{
Expand Down
16 changes: 5 additions & 11 deletions res/shader/draw_list_culler.comp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "scene/camera.glsl"
#include "scene/geometry.glsl"
#include "scene/instances.glsl"
#include "shared/shader_structs/push_constants/draw_list_culler.h"

// TODO: Put in a header?
struct DrawMeshletInstance
Expand Down Expand Up @@ -51,19 +52,12 @@ layout(set = STORAGE_SET, binding = 4) uniform texture2D
// This should clamp to 1 on/beyond edges
layout(set = STORAGE_SET, binding = 5) uniform sampler depthSampler;

layout(push_constant) uniform DrawListCullerPC
{
uvec2 hizResolution;
vec2 hizUvScale;
// 0 means no hiz bound
uint hizMipCount;
uint outputSecondPhaseInput;
}
PC;
layout(push_constant) uniform PushConstants { DrawListCullerPC PC; };

float signedDistance(vec4 plane, vec3 p) { return dot(plane, vec4(p, 1)); }

void transformBounds(inout MeshletBounds bounds, Transforms trfn, float scale)
void transformBounds(
inout MeshletBounds bounds, ModelInstanceTransforms trfn, float scale)
{
// Need full SRT for center as it's the cluster center in model space
bounds.center = (vec4(bounds.center, 1.0) * trfn.modelToWorld).xyz;
Expand Down Expand Up @@ -219,7 +213,7 @@ void main()
MeshletInfo meshletInfo =
loadMeshletInfo(metadata, meshletInstance.meshletIndex);

Transforms trfn =
ModelInstanceTransforms trfn =
modelInstanceTransforms.instance[instance.modelInstanceIndex];

float scale = modelInstanceScales.instance[instance.modelInstanceIndex];
Expand Down
4 changes: 2 additions & 2 deletions res/shader/draw_list_generator.comp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "scene/geometry.glsl"
#include "scene/instances.glsl"
#include "scene/materials.glsl"
#include "shared/shader_structs/push_constants/draw_list_generator.h"

// TODO: Put in a header?
struct DrawMeshletInstance
Expand All @@ -24,8 +25,7 @@ layout(std430, set = STORAGE_SET, binding = 0) writeonly buffer DrawList
}
outDrawList;

layout(push_constant) uniform DrawListGeneratorPC { uint matchTransparents; }
PC;
layout(push_constant) uniform PushConstants { DrawListGeneratorPC PC; };

layout(local_size_x = GROUP_X) in;
void main()
Expand Down
10 changes: 6 additions & 4 deletions res/shader/forward.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
#include "brdf.glsl"
#include "common/random.glsl"
#include "debug.glsl"
#include "forward_pc.glsl"
#include "scene/camera.glsl"
#include "scene/instances.glsl"
#include "scene/light_clusters.glsl"
#include "scene/lighting.glsl"
#include "scene/lights.glsl"
#include "scene/materials.glsl"
#include "scene/skybox.glsl"
#include "shared/shader_structs/push_constants/forward.h"

layout(push_constant) uniform PushConstants { ForwardPC PC; };

layout(location = 0) in vec3 inPositionWorld;
layout(location = 1) in float inZCam;
Expand Down Expand Up @@ -90,9 +92,9 @@ void main()
// Write before debug to not break TAA
outVelocity = clamp(velocity, vec2(-1), vec2(1));

if (PC.DrawType >= DrawType_PrimitiveID)
if (PC.drawType >= DrawType_PrimitiveID)
{
if (PC.DrawType == DrawType_MeshletID)
if (PC.drawType == DrawType_MeshletID)
{
outColor = vec4(uintToColor(inMeshletIndex), 1);
return;
Expand All @@ -105,7 +107,7 @@ void main()
di.position = surface.positionWS;
di.shadingNormal = surface.normalWS;
di.texCoord0 = inTexCoord0;
outColor = vec4(commonDebugDraw(PC.DrawType, di, surface.material), 1);
outColor = vec4(commonDebugDraw(PC.drawType, di, surface.material), 1);
return;
}

Expand Down
14 changes: 8 additions & 6 deletions res/shader/forward.mesh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#extension GL_EXT_shader_8bit_storage : require

#ifdef USE_GBUFFER_PC
#include "gbuffer_pc.glsl"
#include "shared/shader_structs/push_constants/gbuffer.h"
layout(push_constant) uniform PushConstants { GBufferPC PC; };
#else // !USE_GBUFFER_PC
#include "forward_pc.glsl"
#include "shared/shader_structs/push_constants/forward.h"
layout(push_constant) uniform PushConstants { ForwardPC PC; };
#endif // USE_GBUFFER_PC

#include "common/math.glsl"
Expand Down Expand Up @@ -60,8 +62,8 @@ layout(location = 8) out flat uint outMeshletIndex[];

void writeTransformedVertex(
DrawInstance instance, MeshletInstance meshletInstance,
GeometryMetadata metadata, Transforms trfn, uint globalVertexIndex,
uint meshletVertexIndex)
GeometryMetadata metadata, ModelInstanceTransforms trfn,
uint globalVertexIndex, uint meshletVertexIndex)
{
Vertex vertexModel = loadVertex(metadata, globalVertexIndex);
Vertex vertexWorld = transform(vertexModel, trfn);
Expand All @@ -75,7 +77,7 @@ void writeTransformedVertex(
vec4 posNDC = camera.cameraToClip * posCam;
outPositionNDC[meshletVertexIndex] = posNDC;

Transforms prevTrfn = trfn;
ModelInstanceTransforms prevTrfn = trfn;
if (PC.previousTransformValid == 1)
prevTrfn = previousModelInstanceTransforms
.instance[instance.modelInstanceIndex];
Expand Down Expand Up @@ -106,7 +108,7 @@ void main()
GeometryMetadata metadata = geometryMetadatas.data[instance.meshIndex];
MeshletInfo meshletInfo =
loadMeshletInfo(metadata, meshletInstance.meshletIndex);
Transforms trfn =
ModelInstanceTransforms trfn =
modelInstanceTransforms.instance[instance.modelInstanceIndex];

if (threadIndex == 0)
Expand Down
13 changes: 0 additions & 13 deletions res/shader/forward_pc.glsl

This file was deleted.

4 changes: 3 additions & 1 deletion res/shader/gbuffer.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

#include "common/math.glsl"
#include "debug.glsl"
#include "gbuffer_pc.glsl"
#include "scene/camera.glsl"
#include "scene/instances.glsl"
#include "scene/materials.glsl"
#include "shared/shader_structs/push_constants/gbuffer.h"

layout(push_constant) uniform PushConstants { GBufferPC PC; };

layout(location = 0) in vec3 inPositionWorld;
layout(location = 1) in float inZCam;
Expand Down
12 changes: 0 additions & 12 deletions res/shader/gbuffer_pc.glsl

This file was deleted.

11 changes: 3 additions & 8 deletions res/shader/hiz_downsampler.comp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// https://advances.realtimerendering.com/s2018/index.htm
// implemented using https://github.com/GPUOpen-Effects/FidelityFX-SPD

#include "shared/shader_structs/push_constants/hiz_downsampler.h"

layout(set = 0, binding = 0) uniform texture2D depthSrc;
layout(set = 0, binding = 1) uniform sampler depthSampler;
layout(set = 0, binding = 2) uniform coherent image2D depthDst[12];
Expand All @@ -15,14 +17,7 @@ layout(std430, set = 0, binding = 3) coherent buffer SpdGlobalAtomicBuffer
}
spdGlobalAtomic;

layout(push_constant) uniform ReducePC
{
ivec2 inputResolution;
ivec2 topMipResolution;
uint numWorkGroupsPerSlice;
uint mips;
}
PC;
layout(push_constant) uniform PushConstants { HizDownsamplerPC PC; };

#define A_GPU 1
#define A_GLSL 1
Expand Down
14 changes: 7 additions & 7 deletions res/shader/ibl/prefilter_radiance.comp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#pragma shader_stage(compute)

layout(push_constant) uniform PrefilterRadiancePC { uint mipCount; }
PC;

layout(binding = 0) uniform samplerCube inEnvironment;
// 15 levels == 16k texture
layout(binding = 1, r11f_g11f_b10f) uniform writeonly imageCube outRadiance[15];

#include "../brdf.glsl"
#include "../common/math.glsl"
#include "../common/random.glsl"
#include "../common/sampling.glsl"
#include "../shared/shader_structs/push_constants/prefilter_radiance.h"

layout(push_constant) uniform PushConstants { PrefilterRadiancePC PC; };

layout(binding = 0) uniform samplerCube inEnvironment;
// 15 levels == 16k texture
layout(binding = 1, r11f_g11f_b10f) uniform writeonly imageCube outRadiance[15];

// From Real Shading in Unreal Engine 4
// by Brian Karis
Expand Down
4 changes: 2 additions & 2 deletions res/shader/light_clustering.comp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include "common/math.glsl"
#include "scene/camera.glsl"
#include "scene/lights.glsl"
#include "shared/shader_structs/push_constants/light_clustering.h"

#define WRITE_CULLING_BINDS
#include "scene/light_clusters.glsl"

layout(push_constant) uniform ClusteringPC { uvec2 resolution; }
clusteringPC;
layout(push_constant) uniform PushConstants { LightClusteringPC clusteringPC; };

const uint maxPointIndices = 128;
const uint maxSpotIndices = 128;
Expand Down
Loading

0 comments on commit a14e642

Please sign in to comment.