Compute shaders
This commit is contained in:
1185
31_compute_shader.cpp
Normal file
1185
31_compute_shader.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -106,7 +106,7 @@ endfunction ()
|
|||||||
function (add_slang_shader_target TARGET)
|
function (add_slang_shader_target TARGET)
|
||||||
cmake_parse_arguments ("SHADER" "" "CHAPTER_NAME" "SOURCES" ${ARGN})
|
cmake_parse_arguments ("SHADER" "" "CHAPTER_NAME" "SOURCES" ${ARGN})
|
||||||
set (SHADERS_DIR ${CHAPTER_NAME}/shaders)
|
set (SHADERS_DIR ${CHAPTER_NAME}/shaders)
|
||||||
file(GLOB HAS_COMPUTE shaders/${CHAPTER_SHADER}.comp)
|
file(GLOB HAS_COMPUTE shaders/${CHAPTER_SHADER}.comp.slang)
|
||||||
set (ENTRY_POINTS -entry vertMain -entry fragMain)
|
set (ENTRY_POINTS -entry vertMain -entry fragMain)
|
||||||
if(HAS_COMPUTE)
|
if(HAS_COMPUTE)
|
||||||
list(APPEND ENTRY_POINTS -entry compMain)
|
list(APPEND ENTRY_POINTS -entry compMain)
|
||||||
@@ -155,7 +155,7 @@ function (add_chapter CHAPTER_NAME)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
set (CHAPTER_SHADER_SLANG_TARGET ${CHAPTER_NAME}_slang_shader)
|
set (CHAPTER_SHADER_SLANG_TARGET ${CHAPTER_NAME}_slang_shader)
|
||||||
file (GLOB SHADER_SLANG_SOURCES shaders/${CHAPTER_SHADER}.slang)
|
file (GLOB SHADER_SLANG_SOURCES shaders/${CHAPTER_SHADER}*.slang)
|
||||||
if(SHADER_SLANG_SOURCES)
|
if(SHADER_SLANG_SOURCES)
|
||||||
add_slang_shader_target( ${CHAPTER_SHADER_SLANG_TARGET} CHAPTER_NAME ${CHAPTER_NAME} SOURCES ${SHADER_SLANG_SOURCES})
|
add_slang_shader_target( ${CHAPTER_SHADER_SLANG_TARGET} CHAPTER_NAME ${CHAPTER_NAME} SOURCES ${SHADER_SLANG_SOURCES})
|
||||||
add_dependencies(${CHAPTER_NAME} ${CHAPTER_SHADER_SLANG_TARGET})
|
add_dependencies(${CHAPTER_NAME} ${CHAPTER_SHADER_SLANG_TARGET})
|
||||||
@@ -275,10 +275,10 @@ add_chapter (30_multisampling
|
|||||||
TEXTURES images/viking_room.png
|
TEXTURES images/viking_room.png
|
||||||
LIBS glm::glm tinyobjloader::tinyobjloader)
|
LIBS glm::glm tinyobjloader::tinyobjloader)
|
||||||
|
|
||||||
# add_chapter (31_compute_shader
|
add_chapter (31_compute_shader
|
||||||
# SHADER 31_shader_compute
|
SHADER 31_shader_compute
|
||||||
# LIBS glm::glm)
|
LIBS glm::glm)
|
||||||
#
|
|
||||||
# add_chapter (32_ecosystem_utilities
|
# add_chapter (32_ecosystem_utilities
|
||||||
# SHADER 27_shader_depth
|
# SHADER 27_shader_depth
|
||||||
# MODELS models/viking_room.obj
|
# MODELS models/viking_room.obj
|
||||||
|
|||||||
66
shaders/31_shader_compute.comp.slang
Normal file
66
shaders/31_shader_compute.comp.slang
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
struct VSInput {
|
||||||
|
float2 inPosition;
|
||||||
|
float3 inColor;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VSOutput {
|
||||||
|
float4 pos : SV_Position;
|
||||||
|
float pointSize : SV_PointSize;
|
||||||
|
float3 fragColor : COLOR0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PSInput {
|
||||||
|
float4 pos : SV_Position;
|
||||||
|
float3 fragColor : COLOR0;
|
||||||
|
float2 pointCoord : SV_PointCoord;
|
||||||
|
};
|
||||||
|
|
||||||
|
[shader("vertex")]
|
||||||
|
VSOutput vertMain(VSInput input) {
|
||||||
|
VSOutput output;
|
||||||
|
output.pointSize = 14.0;
|
||||||
|
output.pos = float4(input.inPosition, 1.0, 1.0);
|
||||||
|
output.fragColor = input.inColor.rgb;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[shader("fragment")]
|
||||||
|
float4 fragMain(PSInput input) : SV_Target {
|
||||||
|
float2 coord = input.pointCoord - float2(0.5);
|
||||||
|
return float4(input.fragColor, 0.5 - length(coord));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Particle {
|
||||||
|
float2 position;
|
||||||
|
float2 velocity;
|
||||||
|
float4 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UniformBuffer {
|
||||||
|
float deltaTime;
|
||||||
|
};
|
||||||
|
ConstantBuffer<UniformBuffer> ubo;
|
||||||
|
|
||||||
|
struct ParticleSSBO {
|
||||||
|
Particle particles;
|
||||||
|
};
|
||||||
|
StructuredBuffer<ParticleSSBO> particlesIn;
|
||||||
|
RWStructuredBuffer<ParticleSSBO> particlesOut;
|
||||||
|
|
||||||
|
[shader("compute")]
|
||||||
|
[numthreads(256, 1, 1)]
|
||||||
|
void compMain(uint3 threadId : SV_DispatchThreadID) {
|
||||||
|
uint index = threadId.x;
|
||||||
|
|
||||||
|
particlesOut[index].particles.position = particlesIn[index].particles.position +
|
||||||
|
particlesIn[index].particles.velocity.xy * ubo.deltaTime;
|
||||||
|
particlesOut[index].particles.velocity = particlesIn[index].particles.velocity;
|
||||||
|
|
||||||
|
// Flip movement at window border
|
||||||
|
if ((particlesOut[index].particles.position.x <= -1.0) || (particlesOut[index].particles.position.x >= 1.0)) {
|
||||||
|
particlesOut[index].particles.velocity.x = -particlesOut[index].particles.velocity.x;
|
||||||
|
}
|
||||||
|
if ((particlesOut[index].particles.position.y <= -1.0) || (particlesOut[index].particles.position.y >= 1.0)) {
|
||||||
|
particlesOut[index].particles.velocity.y = -particlesOut[index].particles.velocity.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user