Skip to content

Commit 2b5ef6d

Browse files
SaschaWillemsmarty-johnson59charles-lunarg
authored
Add a new compute shader bonus chapter (#320)
* Started working on compute shader bonus chapter * Continued work on compute shader bonus chapter * Continued work on compute shader bonus chapter * Added code for compute sample work-in-progress * Added code for compute sample Per-frame ssbos Compute shader is now integrated and updating particles work-in-progress * Added code for compute sample Nicer visuals, circular initial particle positions work-in-progress * Added code for compute sample work-in-progress * Tutorial compute queue families, submission and synchronization chapter * Added paragraphs on sync, submit and queue familes Rewording some paragraphs * Rewording Added image for workgroup and invocations compute space * Sample for work group and dispatch counts * Note and spec reference on queue families * Minor additions * Sample code uses SSBO per frame Code cleanup Tutotorial changed * Added image for SSBOs per-frame reads and writes * Conclusion * Added note on signaled fences * Update 11_Compute_Shader.md * Use persistent mapped buffers * Added links to advanced topics (tutorials, samples) * Fixed typo * Add note for the storage image chapter * Added note on how to handle uneven no. of invocations using gl_GlobalInvocationID * Added compute fence resize to tutorial Was already present in code * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Update en/11_Compute_Shader.md Co-authored-by: Charles Giessen <[email protected]> * Name the actual compute related physical device limits * Clarfiy how work group and invocation counts are specified * Show how initial particle data is generated for the shader storage buffer s * Restructure SSBO setup * Indentation * Minor touches * fixed transposition * Added note on std140 memory layout qualifier --------- Co-authored-by: Marty Johnson <[email protected]> Co-authored-by: Charles Giessen <[email protected]>
1 parent f869c95 commit 2b5ef6d

10 files changed

+2140
-0
lines changed

code/31_compute_shader.cpp

+1,414
Large diffs are not rendered by default.

code/31_shader_compute.comp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#version 450
2+
3+
struct Particle {
4+
vec2 position;
5+
vec2 velocity;
6+
vec4 color;
7+
};
8+
9+
layout (binding = 0) uniform ParameterUBO {
10+
float deltaTime;
11+
} ubo;
12+
13+
layout(std140, binding = 1) readonly buffer ParticleSSBOIn {
14+
Particle particlesIn[ ];
15+
};
16+
17+
layout(std140, binding = 2) buffer ParticleSSBOOut {
18+
Particle particlesOut[ ];
19+
};
20+
21+
layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
22+
23+
void main()
24+
{
25+
uint index = gl_GlobalInvocationID.x;
26+
27+
Particle particleIn = particlesIn[index];
28+
29+
particlesOut[index].position = particleIn.position + particleIn.velocity.xy * ubo.deltaTime;
30+
particlesOut[index].velocity = particleIn.velocity;
31+
32+
// Flip movement at window border
33+
if ((particlesOut[index].position.x <= -1.0) || (particlesOut[index].position.x >= 1.0)) {
34+
particlesOut[index].velocity.x = -particlesOut[index].velocity.x;
35+
}
36+
if ((particlesOut[index].position.y <= -1.0) || (particlesOut[index].position.y >= 1.0)) {
37+
particlesOut[index].velocity.y = -particlesOut[index].velocity.y;
38+
}
39+
40+
}

code/31_shader_compute.frag

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 450
2+
3+
layout(location = 0) in vec3 fragColor;
4+
5+
layout(location = 0) out vec4 outColor;
6+
7+
void main() {
8+
9+
vec2 coord = gl_PointCoord - vec2(0.5);
10+
outColor = vec4(fragColor, 0.5 - length(coord));
11+
}

code/31_shader_compute.vert

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#version 450
2+
3+
layout(location = 0) in vec2 inPosition;
4+
layout(location = 1) in vec4 inColor;
5+
6+
layout(location = 0) out vec3 fragColor;
7+
8+
void main() {
9+
10+
gl_PointSize = 14.0;
11+
gl_Position = vec4(inPosition.xy, 1.0, 1.0);
12+
fragColor = inColor.rgb;
13+
}

code/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,7 @@ add_chapter (30_multisampling
173173
MODELS ../resources/viking_room.obj
174174
TEXTURES ../resources/viking_room.png
175175
LIBS glm::glm tinyobjloader::tinyobjloader)
176+
177+
add_chapter (31_compute_shader
178+
SHADER 31_shader_compute
179+
LIBS glm::glm)

en/11_Compute_Shader.md

+650
Large diffs are not rendered by default.

images/compute_shader_particles.png

652 KB
Loading

images/compute_space.svg

+4
Loading

images/compute_ssbo_read_write.svg

+4
Loading
47 KB
Loading

0 commit comments

Comments
 (0)