Skip to content

Commit b293a05

Browse files
committed
Move material system stuff from common_cp to material_cp
Previously only the material system used compute shaders at all, but now that other systems will be using them, all this stuff needs to be moved to another shader. Otherwise the other shaders would fail to compile, or all of them would require a bunch of macros to check for it.
1 parent ee892c3 commit b293a05

File tree

6 files changed

+88
-51
lines changed

6 files changed

+88
-51
lines changed

src.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ set(GLSLSOURCELIST
147147
${ENGINE_DIR}/renderer/glsl_source/cull_cp.glsl
148148
${ENGINE_DIR}/renderer/glsl_source/depthReduction_cp.glsl
149149
${ENGINE_DIR}/renderer/glsl_source/processSurfaces_cp.glsl
150+
${ENGINE_DIR}/renderer/glsl_source/material_cp.glsl
150151
${ENGINE_DIR}/renderer/glsl_source/material_vp.glsl
151152
${ENGINE_DIR}/renderer/glsl_source/material_fp.glsl
152153
${ENGINE_DIR}/renderer/glsl_source/skybox_vp.glsl

src/engine/renderer/glsl_source/common_cp.glsl

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,41 +54,9 @@ array must be in the form of uvec4 array[] */
5454
+ gl_GlobalInvocationID.y * gl_NumWorkGroups.x * gl_WorkGroupSize.x\
5555
+ gl_GlobalInvocationID.x )
5656

57-
/* Common structs */
57+
/* Macro combinations for subgroup ops */
5858

59-
struct BoundingSphere {
60-
vec3 origin;
61-
float radius;
62-
};
63-
64-
struct SurfaceDescriptor {
65-
BoundingSphere boundingSphere;
66-
uint surfaceCommandIDs[MAX_SURFACE_COMMANDS];
67-
};
68-
69-
struct PortalSurface {
70-
BoundingSphere boundingSphere;
71-
72-
uint drawSurfID;
73-
float distance;
74-
vec2 padding;
75-
};
76-
77-
struct GLIndirectCommand {
78-
uint count;
79-
uint instanceCount;
80-
uint firstIndex;
81-
int baseVertex;
82-
uint baseInstance;
83-
};
84-
85-
struct IndirectCompactCommand {
86-
uint count;
87-
uint firstIndex;
88-
uint baseInstance;
89-
};
90-
91-
struct SurfaceCommand {
92-
bool enabled;
93-
IndirectCompactCommand drawCommand;
94-
};
59+
#if defined(HAVE_KHR_shader_subgroup_basic) && defined(HAVE_KHR_shader_subgroup_arithmetic)\
60+
&& defined(HAVE_KHR_shader_subgroup_ballot) && defined(HAVE_ARB_shader_atomic_counter_ops)
61+
#define SUBGROUP_STREAM_COMPACTION
62+
#endif

src/engine/renderer/glsl_source/cull_cp.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3535
/* cull_cp.glsl */
3636

3737
#insert common_cp
38+
#insert material_cp
3839

3940
// Keep this to 64 because we don't want extra shared mem etc. to be allocated, and to minimize wasted lanes
4041
layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
===========================================================================
3+
4+
Daemon BSD Source Code
5+
Copyright (c) 2025 Daemon Developers
6+
All rights reserved.
7+
8+
This file is part of the Daemon BSD Source Code (Daemon Source Code).
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright
13+
notice, this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright
15+
notice, this list of conditions and the following disclaimer in the
16+
documentation and/or other materials provided with the distribution.
17+
* Neither the name of the Daemon developers nor the
18+
names of its contributors may be used to endorse or promote products
19+
derived from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
25+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
===========================================================================
33+
*/
34+
35+
/* material_cp.glsl */
36+
37+
struct BoundingSphere {
38+
vec3 origin;
39+
float radius;
40+
};
41+
42+
struct SurfaceDescriptor {
43+
BoundingSphere boundingSphere;
44+
uint surfaceCommandIDs[MAX_SURFACE_COMMANDS];
45+
};
46+
47+
struct PortalSurface {
48+
BoundingSphere boundingSphere;
49+
50+
uint drawSurfID;
51+
float distance;
52+
vec2 padding;
53+
};
54+
55+
struct GLIndirectCommand {
56+
uint count;
57+
uint instanceCount;
58+
uint firstIndex;
59+
int baseVertex;
60+
uint baseInstance;
61+
};
62+
63+
struct IndirectCompactCommand {
64+
uint count;
65+
uint firstIndex;
66+
uint baseInstance;
67+
};
68+
69+
struct SurfaceCommand {
70+
bool enabled;
71+
IndirectCompactCommand drawCommand;
72+
};

src/engine/renderer/glsl_source/processSurfaces_cp.glsl

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3535
/* processSurfaces_cp.glsl */
3636

3737
#insert common_cp
38+
#insert material_cp
3839

3940
// Keep this to 64 because we don't want extra shared mem etc. to be allocated, and to minimize wasted lanes
4041
layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
@@ -59,15 +60,10 @@ uniform uint u_Frame;
5960
uniform uint u_ViewID;
6061
uniform uint u_SurfaceCommandsOffset;
6162

62-
#if defined(HAVE_KHR_shader_subgroup_basic) && defined(HAVE_KHR_shader_subgroup_arithmetic)\
63-
&& defined(HAVE_KHR_shader_subgroup_ballot) && defined(HAVE_ARB_shader_atomic_counter_ops)
64-
#define HAVE_processSurfaces_subgroup
65-
#endif
66-
6763
void AddDrawCommand( in uint commandID, in uvec2 materialID ) {
6864
SurfaceCommand command = surfaceCommands[commandID + u_SurfaceCommandsOffset];
6965

70-
#if defined(HAVE_processSurfaces_subgroup)
66+
#if defined(SUBGROUP_STREAM_COMPACTION)
7167
const uint count = subgroupBallotBitCount( subgroupBallot( command.enabled ) );
7268
// Exclusive scan so we can determine the offset for each lane without any synchronization
7369
const uint subgroupOffset = subgroupExclusiveAdd( command.enabled ? 1 : 0 );
@@ -83,23 +79,20 @@ void AddDrawCommand( in uint commandID, in uvec2 materialID ) {
8379
#endif
8480

8581
if( command.enabled ) {
86-
// materialID.x is the global ID of the material
87-
// materialID.y is the offset for the memory allocated to the material's culled commands
88-
#if !defined(HAVE_processSurfaces_subgroup)
89-
const uint atomicCmdID = atomicCounterIncrement( atomicCommandCounters[materialID.x
90-
+ MAX_COMMAND_COUNTERS * ( MAX_VIEWS * u_Frame + u_ViewID )] );
91-
#endif
92-
9382
GLIndirectCommand indirectCommand;
9483
indirectCommand.count = command.drawCommand.count;
9584
indirectCommand.instanceCount = 1;
9685
indirectCommand.firstIndex = command.drawCommand.firstIndex;
9786
indirectCommand.baseVertex = 0;
9887
indirectCommand.baseInstance = command.drawCommand.baseInstance;
9988

100-
#if defined(HAVE_processSurfaces_subgroup)
89+
// materialID.x is the global ID of the material
90+
// materialID.y is the offset for the memory allocated to the material's culled commands
91+
#if defined(SUBGROUP_STREAM_COMPACTION)
10192
culledCommands[atomicCmdID + subgroupOffset + materialID.y * MAX_COMMAND_COUNTERS + u_SurfaceCommandsOffset] = indirectCommand;
10293
#else
94+
const uint atomicCmdID = atomicCounterIncrement( atomicCommandCounters[materialID.x
95+
+ MAX_COMMAND_COUNTERS * ( MAX_VIEWS * u_Frame + u_ViewID )] );
10396
culledCommands[atomicCmdID + materialID.y * MAX_COMMAND_COUNTERS + u_SurfaceCommandsOffset] = indirectCommand;
10497
#endif
10598
}

src/engine/renderer/shaders.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "shadowFill_fp.glsl.h"
5656
#include "shadowFill_vp.glsl.h"
5757
#include "skybox_fp.glsl.h"
58+
#include "material_cp.glsl.h"
5859
#include "material_vp.glsl.h"
5960
#include "material_fp.glsl.h"
6061
#include "common.glsl.h"
@@ -107,6 +108,7 @@ std::unordered_map<std::string, std::string> shadermap({
107108
{ "lighttile_vp.glsl", std::string(reinterpret_cast<const char*>(lighttile_vp_glsl), sizeof(lighttile_vp_glsl)) },
108109
{ "liquid_fp.glsl", std::string(reinterpret_cast<const char*>(liquid_fp_glsl), sizeof(liquid_fp_glsl)) },
109110
{ "liquid_vp.glsl", std::string(reinterpret_cast<const char*>(liquid_vp_glsl), sizeof(liquid_vp_glsl)) },
111+
{ "material_cp.glsl", std::string( reinterpret_cast< const char* >( material_cp_glsl ), sizeof( material_cp_glsl ) ) },
110112
{ "material_vp.glsl", std::string( reinterpret_cast< const char* >( material_vp_glsl ), sizeof( material_vp_glsl ) ) },
111113
{ "material_fp.glsl", std::string( reinterpret_cast< const char* >( material_fp_glsl ), sizeof( material_fp_glsl ) ) },
112114
{ "motionblur_fp.glsl", std::string(reinterpret_cast<const char*>(motionblur_fp_glsl), sizeof(motionblur_fp_glsl)) },

0 commit comments

Comments
 (0)