7 using namespace RenderCore;
16 uint32_t numMips, uint32_t layer = 0)
19 for (uint32 i = 1; i < numMips; ++i)
22 Format(
"Mip Gen {} {}", i, name), queue, 0u,
27 uint32_t dstMipLevel = i;
31 r->
BindTextureSRV(self, src,
"srcTexture", RHIPipelineStageBits::ComputeShader,
36 self, dst,
"srcTexture", RHIPipelineStageBits::ComputeShader,
40 self, dst,
"dstTexture", RHIPipelineStageBits::ComputeShader,
50 uint32_t mipIndex = i;
53 uint32_t w = std::max(1u, extent.x >> mipIndex);
54 uint32_t h = std::max(1u, extent.y >> mipIndex);
67 RHITextureAspectFlag dstAspect,
ResourceHandle srcSampler, uint32_t numMips, uint32_t numLayer = 1,
75 uint32_t numWorkGroups;
78 auto SpdSetup = [](
uint2& dispatchThreadGroupCountXY,
79 uint32_t& numWorkGroups,
83 uint32_t endIndexX = (extent[0] - 1) / 64;
84 uint32_t endIndexY = (extent[1] - 1) / 64;
86 dispatchThreadGroupCountXY[0] = endIndexX + 1;
87 dispatchThreadGroupCountXY[1] = endIndexY + 1;
89 numWorkGroups = (dispatchThreadGroupCountXY[0]) * (dispatchThreadGroupCountXY[1]);
92 Format(
"{} SPD Atomics", name),
94 .
usage = RHIBufferUsageBits::StorageBuffer | RHIBufferUsageBits::TransferDestination,
95 .size =
sizeof(uint32_t) * 6,
101 int reductionMode = 0;
104 case RHIDeviceSampler::SamplerDesc::Reduction::WeightedAverage:
107 case RHIDeviceSampler::SamplerDesc::Reduction::Min:
110 case RHIDeviceSampler::SamplerDesc::Reduction::Max:
118 r->
BindBufferUnordered(self, SpdCounter, RHIPipelineStageBits::ComputeShader,
"spdGlobalAtomic");
119 CHECK_MSG(numMips <= 12,
"Single Pass CS Mip version supports up to 12 mips.");
120 CHECK_MSG(numMips > 1,
"Single Pass CS Mip version requires at least 2 mips.");
121 r->
BindTextureSRV(self, src,
"imgSrc", RHIPipelineStageBits::ComputeShader,
124 .dimension = RHITextureDimension::E2DArray,
127 for (uint32_t mip = 1; mip <= 12; mip++)
129 uint32_t dstMipLevel = mip;
132 dstMipLevel = std::min(dstMipLevel, numMips - 1);
134 self, dst,
"imgDst", RHIPipelineStageBits::ComputeShader,
137 .dimension = RHITextureDimension::E2DArray,
142 self, dst,
"imgDst6", RHIPipelineStageBits::ComputeShader,
145 .dimension = RHITextureDimension::E2DArray,
153 PushConstants pc{.extents = {dstTex->mDesc.extent.x, dstTex->mDesc.extent.y}, .mips = numMips};
155 pc.extents *= 2, pc.mips++;
156 uint2 dispatchThreadGroupCountXY;
157 SpdSetup(dispatchThreadGroupCountXY, pc.numWorkGroups, pc.extents);
161 cmd->FillBuffer(ctr, 0u);
162 cmd->BeginTransition();
163 cmd->SetBufferTransition(
166 .srcAccess = RHIResourceAccessBits::TransferWrite,
167 .dstAccess = RHIResourceAccessBits::ShaderWrite,
168 .srcStage = RHIPipelineStageBits::ComputeShader | RHIPipelineStageBits::Transfer,
169 .dstStage = RHIPipelineStageBits::ComputeShader,
171 cmd->EndTransition();
175 cmd->Dispatch(dispatchThreadGroupCountXY.x, dispatchThreadGroupCountXY.y, 1);
#define CHECK_MSG(expr, format_str,...)
virtual String ResolveRelativePathBase(StringView path="") const =0
Retrives path relative to the application executable.
Definition Resource.hpp:57
Definition Command.hpp:42
Definition Resource.hpp:216
const RHITextureDesc mDesc
Definition Resource.hpp:221
Renderer implementing a Frame Graph system with automatic resource tracking and synchronization.
Definition Renderer.hpp:377
PassHandle CreatePass(StringView name, RHIDeviceQueueType queue, size_t priority, FSetup &&setup, FRecord &&record)
Create a render pass from a Setup(Renderer*, PassHandle) and Record(Renderer*, PassHandle,...
Definition Renderer.hpp:680
Variant< RHIBuffer *, RHITexture *, RHIAccelerationStructure * > DerefResource(const ResourceHandle handle) const
Dereference a resource handle to its underlying RHI resource.
Definition Renderer.hpp:1068
void BindTextureSRV(PassHandle pass, ResourceHandle texture, StringView bind_point, RHIPipelineStage stage, RHITextureViewDesc const &desc) const
Binds a texture as a Shader Resource View (read-only sampling / fetch).
Definition Renderer.cpp:351
void BindTextureSampler(PassHandle pass, ResourceHandle sampler, StringView bind_point) const
Binds a sampler to the shader.
Definition Renderer.cpp:303
const RHIApplication * GetApplication() const
Get the RHIApplication this Renderer was constructed with.
Definition Renderer.hpp:1034
void CmdDispatch(PassHandle pass, RHICommandList *cmd, RHIExtent3D thread_size) const
Helper that dispatches a compute shader with the specified THREAD count.
Definition Renderer.cpp:2272
void CmdSetPipeline(PassHandle pass, RHICommandList *cmd) const
Helper that sets the current pass's PSO and descriptor sets to the current command list.
Definition Renderer.cpp:2174
void BindTextureUAV(PassHandle pass, ResourceHandle texture, StringView bind_point, RHIPipelineStage stage, RHITextureViewDesc const &desc) const
Binds a texture for unordered (UAV) read-write access in shaders.
Definition Renderer.cpp:364
void BindPushConstant(PassHandle pass, RHIShaderStage stage, size_t offset, size_t size) const
Declares a range of Push Constant used in a stage.
Definition Renderer.cpp:241
void BindBufferUnordered(PassHandle pass, ResourceHandle buffer, RHIPipelineStage stage, StringView bind_point) const
Binds a buffer for unordered (UAV) access from shaders (read and/or write in any order).
Definition Renderer.cpp:270
void BindShader(PassHandle pass, RHIShaderStage stage, StringView entry_point, StringView shader_path, Span< const char > specializationData={}, uint32_t rtHitGroupIndex=0, RHIPipelineState::PipelineStateDesc::RayTracingHitGroupType rtHitGroupType=RHIPipelineState::PipelineStateDesc::RayTracingHitGroupType::Triangles) const
Binds shader file path to a certain pass at a certain stage.
Definition Renderer.cpp:221
void CmdSetPushConstant(PassHandle pass, RHICommandList *cmd, RHIShaderStage stage, size_t offset, T const &data)
Helper that sets a Push Constant range data with a single l-value.
Definition Renderer.hpp:1184
uint64_t GetFrame() const
Retrieves the current frame number.
Definition Renderer.hpp:1208
ResourceHandle CreateResource(StringView name, T const &desc)
Create a new resource to be used in the render graph.
Definition Renderer.hpp:705
constexpr String Format(fmt::format_string< Args... > format, Args &&... args)
String wrapper for Format().
Definition Container.hpp:126
Span< const T > AsSpan(T const &data)
Helper to construct one const r-value as a single element span.
Definition Container.hpp:73
std::basic_string_view< char > StringView
Alias for std::basic_string_view<char>
Definition Container.hpp:56
Span< const char > AsBytes(Span< T > data)
Definition Container.hpp:65
vec2 float2
Definition Math.hpp:27
uvec2 uint2
Definition Math.hpp:33
glm::vec< 3, uint32_t > RHIExtent3D
Definition Common.hpp:11
RHIDeviceQueueType
Definition Common.hpp:179
RHIResourceFormat
Definition Common.hpp:34
Handle ResourceHandle
Definition Renderer.hpp:22
Handle PassHandle
Definition Renderer.hpp:21
Definition CSClearBuffer.hpp:5
void createCSMipGenerationPasses(Renderer *renderer, StringView name, RHIDeviceQueueType queue, ResourceHandle src, ResourceHandle dst, RHIResourceFormat srcFormat, RHIResourceFormat dstFormat, RHITextureAspectFlagBits srcAspect, RHITextureAspectFlagBits dstAspect, ResourceHandle srcSampler, uint32_t numMips, uint32_t layer=0)
Generates full mip-chain with multiple compute dispatches.
Definition CSMipGeneration.hpp:12
void createCSMipGenerationSinglePass(Renderer *renderer, StringView name, RHIDeviceQueueType queue, ResourceHandle src, ResourceHandle dst, RHIResourceFormat srcFormat, RHIResourceFormat dstFormat, RHITextureAspectFlag srcAspect, RHITextureAspectFlag dstAspect, ResourceHandle srcSampler, uint32_t numMips, uint32_t numLayer=1, RHIDeviceSampler::SamplerDesc::Reduction reduction=RHIDeviceSampler::SamplerDesc::Reduction::WeightedAverage)
Generates full mip-chain with a single compute dispatch.
Definition CSMipGeneration.hpp:64
Definition Resource.hpp:28
RHIBufferUsage usage
How the buffer can be used by the device, initially.
Definition Resource.hpp:31
Reduction
Definition Device.hpp:154
RHIExtent3D extent
Definition Resource.hpp:146
static RHITextureSubresourceRange Create(RHITextureAspectFlag aspect=RHITextureAspectFlagBits::Color, uint32_t base_mip_level=0, uint32_t mip_count=1, uint32_t base_array_layer=0, uint32_t layer_count=1)
Helper function to create a Subresource Range with default parameters.
Definition Resource.hpp:191