Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
BloomPreRenderOperator.cpp
Go to the documentation of this file.
1
2
4
8
13
17
18namespace Divide {
19
20namespace {
22}
23
26{
27 ShaderModuleDescriptor vertModule = {};
29 vertModule._sourceFile = "baseVertexShaders.glsl";
30 vertModule._variant = "FullScreenQuad";
31
32 ShaderModuleDescriptor fragModule = {};
34 fragModule._sourceFile = "bloom.glsl";
35 fragModule._variant = "BloomCalc";
36
37 ShaderProgramDescriptor shaderDescriptor = {};
38 shaderDescriptor._modules.push_back(vertModule);
39 shaderDescriptor._modules.push_back(fragModule);
40 shaderDescriptor._globalDefines.emplace_back( "luminanceBias PushData0[0].x" );
41
42 ResourceDescriptor<ShaderProgram> bloomCalc("BloomCalc", shaderDescriptor );
43 bloomCalc.waitForReady(false);
44
45 _bloomCalc = CreateResource(bloomCalc);
46 {
47 PipelineDescriptor pipelineDescriptor;
48 pipelineDescriptor._stateBlock = _context.get2DStateBlock();
49 pipelineDescriptor._shaderProgramHandle = _bloomCalc;
51
52 _bloomCalcPipeline = _context.newPipeline( pipelineDescriptor );
53 }
54
55 fragModule._variant = "BloomApply";
56 shaderDescriptor = {};
57 shaderDescriptor._modules.push_back(vertModule);
58 shaderDescriptor._modules.push_back(fragModule);
59
60 ResourceDescriptor<ShaderProgram> bloomApply("BloomApply", shaderDescriptor );
61 bloomApply.waitForReady(false);
62 _bloomApply = CreateResource(bloomApply);
63 {
64 PipelineDescriptor pipelineDescriptor;
65 pipelineDescriptor._stateBlock = _context.get2DStateBlock();
66 pipelineDescriptor._shaderProgramHandle = _bloomApply;
68
69 _bloomApplyPipeline = _context.newPipeline( pipelineDescriptor );
70 }
71
72 const vec2<U16> res = parent.screenRT()._rt->getResolution();
73 if (res.height > 1440)
74 {
75 resolutionDownscaleFactor = 4.0f;
76 }
77
78 const auto& screenAtt = parent.screenRT()._rt->getAttachment(RTAttachmentType::COLOUR, GFXDevice::ScreenTargets::ALBEDO);
79 TextureDescriptor screenDescriptor = Get(screenAtt->texture())->descriptor();
80 screenDescriptor._mipMappingState = MipMappingState::OFF;
81
82 RenderTargetDescriptor desc = {};
83 desc._attachments =
84 {
85 InternalRTAttachmentDescriptor{ screenDescriptor, screenAtt->_descriptor._sampler, RTAttachmentType::COLOUR, RTColourAttachmentSlot::SLOT_0 }
86 };
87
88 desc._resolution = res;
89 desc._name = "Bloom_Blur_0";
91 desc._name = "Bloom_Blur_1";
93
94 desc._name = "Bloom";
95 desc._resolution = vec2<U16>(res / resolutionDownscaleFactor);
97
99}
100
102{
106 {
108 }
109
112}
113
114bool BloomPreRenderOperator::ready() const noexcept
115{
116 if (_bloomCalcPipeline != nullptr && _bloomApplyPipeline != nullptr)
117 {
119 }
120
121 return false;
122}
123
124void BloomPreRenderOperator::reshape(const U16 width, const U16 height)
125{
126 PreRenderOperator::reshape(width, height);
127
128 const U16 w = to_U16(width / resolutionDownscaleFactor);
129 const U16 h = to_U16(height / resolutionDownscaleFactor);
130 _bloomOutput._rt->resize(w, h);
131 _bloomBlurBuffer[0]._rt->resize(width, height);
132 _bloomBlurBuffer[1]._rt->resize(width, height);
133}
134
136{
137 _luminanceBias = val;
139 _context.context().config().changed(true);
140}
141
142// Order: luminance calc -> bloom -> toneMap
143bool BloomPreRenderOperator::execute([[maybe_unused]] const PlayerIndex idx, [[maybe_unused]] const CameraSnapshot& cameraSnapshot, const RenderTargetHandle& input, const RenderTargetHandle& output, GFX::CommandBuffer& bufferInOut)
144{
145 assert(input._targetID != output._targetID);
146
148 const auto& screenTex = Get(screenAtt->texture())->getView();
149
150 { // Step 1: generate bloom - render all of the "bright spots"
151 GFX::BeginRenderPassCommand beginRenderPassCmd{};
152 beginRenderPassCmd._target = _bloomOutput._targetID;
153 beginRenderPassCmd._name = "DO_BLOOM_PASS";
154 beginRenderPassCmd._descriptor = _screenOnlyDraw;
155 beginRenderPassCmd._clearDescriptor[to_base( RTColourAttachmentSlot::SLOT_0 )] = DEFAULT_CLEAR_ENTRY;
156 GFX::EnqueueCommand(bufferInOut, beginRenderPassCmd);
157
158 GFX::EnqueueCommand<GFX::BindPipelineCommand>( bufferInOut )->_pipeline = _bloomCalcPipeline;
159 PushConstantsStruct& params = GFX::EnqueueCommand<GFX::SendPushConstantsCommand>( bufferInOut )->_fastData;
160 params.data[0]._vec[0].x = luminanceBias();
161
162 auto cmd = GFX::EnqueueCommand<GFX::BindShaderResourcesCommand>( bufferInOut );
163 cmd->_usage = DescriptorSetUsage::PER_DRAW;
164 {
166 Set( binding._data, screenTex, screenAtt->_descriptor._sampler );
167 }
168 {
170 Set( binding._data, _parent.luminanceTex(), _parent.lumaSampler());
171 }
172
173
174 GFX::EnqueueCommand<GFX::DrawCommand>(bufferInOut)->_drawCommands.emplace_back();
175 GFX::EnqueueCommand<GFX::EndRenderPassCommand>(bufferInOut);
176 }
177 {// Step 2: blur bloom
183 10,
184 true,
185 1,
186 bufferInOut);
187 }
188 {// Step 3: apply bloom
189 const auto& bloomAtt = _bloomBlurBuffer[1]._rt->getAttachment(RTAttachmentType::COLOUR );
190 const auto& bloomTex = Get(bloomAtt->texture())->getView();
191
192 GFX::BeginRenderPassCommand beginRenderPassCmd{};
193 beginRenderPassCmd._target = output._targetID;
194 beginRenderPassCmd._descriptor = _screenOnlyDraw;
195 GFX::EnqueueCommand(bufferInOut, beginRenderPassCmd);
196
197 auto cmd = GFX::EnqueueCommand<GFX::BindShaderResourcesCommand>( bufferInOut );
198 cmd->_usage = DescriptorSetUsage::PER_DRAW;
199 {
201 Set( binding._data, screenTex, screenAtt->_descriptor._sampler );
202 }
203 {
205 Set( binding._data, bloomTex, bloomAtt->_descriptor._sampler );
206 }
207
208 GFX::EnqueueCommand<GFX::BindPipelineCommand>( bufferInOut )->_pipeline = _bloomApplyPipeline;
209
210 GFX::EnqueueCommand<GFX::DrawCommand>(bufferInOut)->_drawCommands.emplace_back();
211 GFX::EnqueueCommand<GFX::EndRenderPassCommand>(bufferInOut);
212 }
213
214 return true;
215}
216}
#define DIVIDE_UNEXPECTED_CALL()
bool ready() const noexcept override
Handle< ShaderProgram > _bloomApply
BloomPreRenderOperator(GFXDevice &context, PreRenderBatch &parent)
void reshape(U16 width, U16 height) override
bool execute(PlayerIndex idx, const CameraSnapshot &cameraSnapshot, const RenderTargetHandle &input, const RenderTargetHandle &output, GFX::CommandBuffer &bufferInOut) override
Return true if we rendered into "output".
RenderTargetHandle _bloomBlurBuffer[2]
Handle< ShaderProgram > _bloomCalc
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
void blurTarget(RenderTargetHandle &blurSource, RenderTargetHandle &blurBuffer, const RenderTargetHandle &blurTarget, RTAttachmentType att, RTColourAttachmentSlot slot, I32 kernelSize, bool gaussian, U8 layerCount, GFX::CommandBuffer &bufferInOut)
Definition: GFXDevice.cpp:1642
GFXRTPool & renderTargetPool() noexcept
Definition: GFXDevice.inl:133
Pipeline * newPipeline(const PipelineDescriptor &descriptor)
Create and return a new graphics pipeline. This is only used for caching and doesn't use the object a...
Definition: GFXDevice.cpp:3074
const RenderStateBlock & get2DStateBlock() const noexcept
Definition: GFXDevice.inl:123
RenderTargetHandle allocateRT(const RenderTargetDescriptor &descriptor)
Definition: GFXRTPool.cpp:17
bool deallocateRT(RenderTargetHandle &handle)
Definition: GFXRTPool.cpp:33
PlatformContext & context() noexcept
Configuration & config() noexcept
Handle< Texture > luminanceTex() const noexcept
virtual void reshape(U16 width, U16 height)
virtual bool ready() const noexcept
RTDrawDescriptor _screenOnlyDraw
RTAttachment * getAttachment(RTAttachmentType type, RTColourAttachmentSlot slot=RTColourAttachmentSlot::SLOT_0) const
bool resize(U16 width, U16 height)
Resize all attachments.
vec4< T > _vec[4]
Definition: MathMatrices.h:709
FORCE_INLINE T * EnqueueCommand(CommandBuffer &buffer)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
FORCE_INLINE void DestroyResource(Handle< T > &handle, const bool immediate=false)
constexpr U16 to_U16(const T value)
Project & parent
Definition: DefaultScene.h:41
uint16_t U16
void Set(DescriptorSetBindingData &dataInOut, ShaderBuffer *buffer, const BufferRange range) noexcept
DescriptorSetBinding & AddBinding(DescriptorSet &setInOut, U8 slot, U16 stageVisibilityMask)
FORCE_INLINE Handle< T > CreateResource(const ResourceDescriptor< T > &descriptor, bool &wasInCache, std::atomic_uint &taskCounter)
RTClearEntry DEFAULT_CLEAR_ENTRY
FORCE_INLINE T * Get(const Handle< T > handle)
constexpr auto to_base(const Type value) -> Type
F32 luminanceBias
struct Divide::Configuration::Rendering::PostFX::Bloom bloom
struct Divide::Configuration::Rendering::PostFX postFX
struct Divide::Configuration::Rendering rendering
DescriptorSetBindingData _data
static constexpr RTColourAttachmentSlot ALBEDO
Definition: GFXDevice.h:228
PrimitiveTopology _primitiveTopology
Definition: Pipeline.h:48
Handle< ShaderProgram > _shaderProgramHandle
Definition: Pipeline.h:47
RenderStateBlock _stateBlock
Definition: Pipeline.h:46
vector< ShaderModuleDescriptor > _modules
InternalRTAttachmentDescriptors _attachments
Definition: RenderTarget.h:52
RenderTargetID _targetID
Definition: RenderTarget.h:47