Foundation
Loading...
Searching...
No Matches
CSMipGeneration.hpp
Go to the documentation of this file.
1#pragma once
4{
5 using namespace RenderCore;
9 uint32_t maxMips = 16, uint32_t layer = 0
10 )
11 {
12 using namespace Math;
13#pragma pack(push, 1)
14 struct PushConstant
15 {
17 uint filter;
18 };
19#pragma pack(pop)
20 for (uint32 i = 0; i < maxMips; ++i)
21 {
23 renderer, fmt::format("Mip Gen {} {}", i, name), queue,
24 [=](PassHandle self, Renderer* r)
25 {
26 r->BindShader(self, RHIShaderStageBits::Compute, "csMain", "data/shaders/CSMipGeneration.spv");
27 if (i == 0)
28 r->BindTextureSRV(
29 self, src, "srcTexture", RHIPipelineStageBits::ComputeShader,
30 {
31 .format = srcFormat,
32 .range = RHITextureSubresourceRange::Create(srcAspect, 0, 1, layer, 1)
33 });
34 else
35 r->BindTextureSRV(
36 self, dst, "srcTexture", RHIPipelineStageBits::ComputeShader,
37 {
38 .format = dstFormat,
39 .range = RHITextureSubresourceRange::Create(dstAspect, i - 1, 1, layer, 1)
40 });
41 r->BindTextureUAV(
42 self, dst, "dstTexture", RHIPipelineStageBits::ComputeShader,
43 {
44 .format = dstFormat,
45 .range = RHITextureSubresourceRange::Create(dstAspect, i, 1, layer, 1)
46 });
47 r->BindPushConstant(self, RHIShaderStageBits::Compute, 0, sizeof(PushConstant));
48 },
50 {
51 RHITexture* srcTex = r->DerefResource(src).Get<RHITexture*>();
52 RHITexture* dstTex = r->DerefResource(dst).Get<RHITexture*>();
53 RHIExtent3D extent;
54 CHECK_MSG((extent = srcTex->mDesc.extent) == dstTex->mDesc.extent, "Extent of source images mismatch");
55 r->CmdSetPipeline(self,cmd);
56 uint32_t w = std::max(1u, extent.x >> i);
57 uint32_t h = std::max(1u, extent.y >> i);
58 r->CmdSetPushConstant(self, cmd, RHIShaderStageBits::Compute, 0, PushConstant{
59 .srcExtent = { w, h },
60 .filter = 0 // box
61 });
62 r->CmdDispatch(self, cmd, {w,h,1});
63 },
64 [=](PassHandle self, Renderer* r)
65 {
66 if (i == 0 && src == dst)
67 return true;
68 RHITexture* srcTex = r->DerefResource(src).Get<RHITexture*>();
69 return i > srcTex->mDesc.mipLevels - 1;
70 });
71 }
72 }
73} // namespace Foundation::Rendering
#define CHECK_MSG(expr, format_str,...)
Definition Logging.hpp:31
Definition Command.hpp:34
Definition Resource.hpp:146
const RHITextureDesc mDesc
Definition Resource.hpp:150
Renderer implementing a Frame Graph system with automatic resource tracking and synchronization.
Definition Renderer.hpp:78
void BindShader(PassHandle pass, RHIShaderStage stage, StringView entry_point, Native::Path const &shader_path) const
Binds shader file path to a certain pass at a certain stage.
Definition Renderer.cpp:98
std::basic_string_view< char > StringView
Alias for std::basic_string_view<char>
Definition Container.hpp:53
std::unique_ptr< T, StlDeleter< T > > UniquePtr
std::unique_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:161
glm::vec< 3, uint32_t > RHIExtent3D
Definition Common.hpp:11
RHIDeviceQueueType
Definition Common.hpp:69
RHIResourceFormat
Definition Common.hpp:20
LambdaPass< FSetup, FRecord, FSkip > * createPass(Renderer *r, StringView name, RHIDeviceQueueType queue, FSetup &&setup, FRecord &&record, FSkip &&skip={})
Convenient functional wrapper to create a pass from Setup/Record lambdas.
Definition Renderer.hpp:1000
size_t ResourceHandle
Definition RenderPass.hpp:12
size_t PassHandle
Definition RenderPass.hpp:11
Reference implementations of real-time rendering routines.
Definition Application.hpp:18
void createCSMipGenerationPasses(Renderer *renderer, StringView name, RHIDeviceQueueType queue, ResourceHandle src, ResourceHandle dst, RHITextureAspectFlagBits srcAspect, RHIResourceFormat srcFormat, RHITextureAspectFlagBits dstAspect, RHIResourceFormat dstFormat, uint32_t maxMips=16, uint32_t layer=0)
Definition CSMipGeneration.hpp:6
uint32_t mipLevels
Definition Resource.hpp:86
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:122