Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
DoFPreRenderOperator.cpp
Go to the documentation of this file.
1
2
4
12
14
15namespace Divide {
16
17namespace {
18 constexpr U8 g_samplesOnFirstRing = 3;
19 constexpr U8 g_ringCount = 4;
20}
21
24{
25 ShaderModuleDescriptor vertModule = {};
27 vertModule._sourceFile = "baseVertexShaders.glsl";
28 vertModule._variant = "FullScreenQuad";
29
30 ShaderModuleDescriptor fragModule = {};
32 fragModule._sourceFile = "DepthOfField.glsl";
33 //blur the depth buffer?
34 fragModule._defines.emplace_back("USE_DEPTH_BLUR");
35 //use noise instead of pattern for sample dithering
36 fragModule._defines.emplace_back("USER_NOISE");
37 //use pentagon as bokeh shape?
38 //fragModule._defines.emplace_back("USE_PENTAGON");
39 fragModule._defines.emplace_back(Util::StringFormat("RING_COUNT {}", g_ringCount));
40 fragModule._defines.emplace_back(Util::StringFormat("FIRST_RING_SAMPLES {}", g_samplesOnFirstRing));
41 fragModule._defines.emplace_back( Util::StringFormat( "FIRST_RING_SAMPLES {}", g_samplesOnFirstRing ) );
42
43 fragModule._defines.emplace_back("size PushData0[0].xy");
44 // autofocus point on screen (0.0,0.0 - left lower corner, 1.0,1.0 - upper right)
45 fragModule._defines.emplace_back("focus PushData0[0].zw");
46 fragModule._defines.emplace_back("_zPlanes PushData0[1].xy");
47 //focal distance value in meters, but you may use autofocus option below
48 fragModule._defines.emplace_back("focalDepth PushData0[1].z");
49 //focal length in mm
50 fragModule._defines.emplace_back("focalLength PushData0[1].w");
51 fragModule._defines.emplace_back("fstop PushData0[2].x");
52 //near dof blur start
53 fragModule._defines.emplace_back("ndofstart PushData0[2].y");
54 //near dof blur falloff distance
55 fragModule._defines.emplace_back("ndofdist PushData0[2].z");
56 //far dof blur start
57 fragModule._defines.emplace_back("fdofstart PushData0[2].w");
58 //far dof blur falloff distance
59 fragModule._defines.emplace_back("fdofdist PushData0[3].x");
60 //vignetting outer border
61 fragModule._defines.emplace_back("vignout PushData0[3].y");
62 //vignetting inner border
63 fragModule._defines.emplace_back("vignin PushData0[3].z");
64 //show debug focus point and focal range (red = focal point, green = focal range)
65 fragModule._defines.emplace_back("showFocus uint(PushData0[3].w)");
66 //manual dof calculation
67 fragModule._defines.emplace_back("manualdof uint(PushData1[0].x)");
68 //use optical lens vignetting?
69 fragModule._defines.emplace_back("vignetting uint(PushData1[0].y)");
70 //use autofocus in shader? disable if you use external focalDepth value
71 fragModule._defines.emplace_back("autofocus uint(PushData1[0].z)");
72
73 ShaderProgramDescriptor shaderDescriptor = {};
74 shaderDescriptor._modules.push_back(vertModule);
75 shaderDescriptor._modules.push_back(fragModule);
76
77 ResourceDescriptor<ShaderProgram> dof("DepthOfField", shaderDescriptor );
78 dof.waitForReady(false);
79
81 PipelineDescriptor pipelineDescriptor = {};
82 pipelineDescriptor._stateBlock = _context.get2DStateBlock();
83 pipelineDescriptor._shaderProgramHandle = _dofShader;
85
86 _pipeline = _context.newPipeline( pipelineDescriptor );
87
88 const vec2<U16> resolution = _parent.screenRT()._rt->getResolution();
89 _constants.data[0]._vec[0].xy.set(resolution.width, resolution.height );
90
92}
93
95{
97}
98
99bool DoFPreRenderOperator::ready() const noexcept
100{
102}
103
105{
106 const auto& params = _context.context().config().rendering.postFX.dof;
107
108 _constants.data[0]._vec[0].zw = params.focalPoint;
109 _constants.data[0]._vec[1].z = params.focalDepth;
110 _constants.data[0]._vec[1].w = params.focalLength;
112 _constants.data[0]._vec[2].y = params.ndofstart;
113 _constants.data[0]._vec[2].z = params.ndofdist;
114 _constants.data[0]._vec[2].w = params.fdofstart;
115 _constants.data[0]._vec[3].x = params.fdofdist;
116 _constants.data[0]._vec[3].y = params.vignout;
117 _constants.data[0]._vec[3].z = params.vignin;
118 _constants.data[0]._vec[3].w = params.debugFocus ? 1.f : 0.f;
119 _constants.data[1]._vec[0].x = params.manualdof ? 1.f : 0.f;
120 _constants.data[1]._vec[0].y = params.vignetting ? 1.f : 0.f;
121 _constants.data[1]._vec[0].z = params.autoFocus ? 1.f : 0.f;
122}
123
124void DoFPreRenderOperator::reshape(const U16 width, const U16 height)
125{
126 PreRenderOperator::reshape(width, height);
127 _constants.data[0]._vec[0].xy.set(width, height);
128}
129
130bool DoFPreRenderOperator::execute([[maybe_unused]] const PlayerIndex idx, const CameraSnapshot& cameraSnapshot, const RenderTargetHandle& input, const RenderTargetHandle& output, GFX::CommandBuffer& bufferInOut)
131{
132 _constants.data[0]._vec[1].xy = cameraSnapshot._zPlanes;
133
136 const auto& screenTex = Get(screenAtt->texture())->getView();
137 const auto& extraTex = Get(extraAtt->texture())->getView();
138
139 auto cmd = GFX::EnqueueCommand<GFX::BindShaderResourcesCommand>(bufferInOut);
140 cmd->_usage = DescriptorSetUsage::PER_DRAW;
141 {
143 Set( binding._data, screenTex, screenAtt->_descriptor._sampler );
144 }
145 {
147 Set( binding._data, extraTex, extraAtt->_descriptor._sampler );
148 }
149
150 GFX::BeginRenderPassCommand beginRenderPassCmd{};
151 beginRenderPassCmd._target = output._targetID;
152 beginRenderPassCmd._descriptor = _screenOnlyDraw;
153 beginRenderPassCmd._name = "DO_DOF_PASS";
154 beginRenderPassCmd._clearDescriptor[to_base( RTColourAttachmentSlot::SLOT_0 )] = DEFAULT_CLEAR_ENTRY;
155 GFX::EnqueueCommand(bufferInOut, beginRenderPassCmd);
156
157 GFX::EnqueueCommand<GFX::BindPipelineCommand>(bufferInOut)->_pipeline = _pipeline;
158 GFX::EnqueueCommand<GFX::SendPushConstantsCommand>(bufferInOut)->_fastData = _constants;
159
160
161 GFX::EnqueueCommand<GFX::DrawCommand>(bufferInOut)->_drawCommands.emplace_back();
162 GFX::EnqueueCommand<GFX::EndRenderPassCommand>(bufferInOut);
163
164 return true;
165}
166}
bool ready() const noexcept 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".
DoFPreRenderOperator(GFXDevice &context, PreRenderBatch &parent)
void reshape(U16 width, U16 height) override
Handle< ShaderProgram > _dofShader
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
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
PlatformContext & context() noexcept
Configuration & config() noexcept
RenderTargetHandle screenRT() const noexcept
RenderTargetHandle getLinearDepthRT() 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
vec2< U16 > getResolution() const noexcept
vec4< T > _vec[4]
Definition: MathMatrices.h:709
void set(const T *v) noexcept
set the 2 components of the vector manually using a source pointer to a (large enough) array
Definition: MathVectors.h:335
vec2< T > xy
Definition: MathVectors.h:1366
vec2< T > zw
Definition: MathVectors.h:1366
FORCE_INLINE T * EnqueueCommand(CommandBuffer &buffer)
FStops StringToFStops(const string &name)
Definition: Camera.cpp:21
Str StringFormat(const char *fmt, Args &&...args)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
static constexpr F32 g_FStopValues[to_base(FStops::COUNT)]
Definition: Camera.h:69
FORCE_INLINE void DestroyResource(Handle< T > &handle, const bool immediate=false)
uint8_t U8
@ RES_LOADED
The resource is available for usage.
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
struct Divide::Configuration::Rendering::PostFX::DOF dof
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
RenderTargetID _targetID
Definition: RenderTarget.h:47