Blender V5.3
eevee_gbuffer.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "DRW_gpu_wrapper.hh"
14#include "DRW_render.hh"
15#include "GPU_capabilities.hh"
16#include "GPU_platform.hh"
17
18#include "eevee_defines.hh"
19
20namespace blender::eevee {
21
22using namespace draw;
23
24class Instance;
25
130struct GBuffer {
131 public:
132 TextureFromPool header_tx = {"GBufferHeader"};
133 TextureFromPool closure_tx = {"GBufferClosure"};
134 TextureFromPool normal_tx = {"GBufferNormal"};
135
136 /* Expected number of layer written through the framebuffer. */
140
141 private:
142 /* References to optional GBuffer layers that are not always required or written to.
143 * These will point to either the dummy textures bellow or to a layer range view of the above
144 * textures. In the later case, these layers are written with imageStore instead of being part
145 * of the #Framebuffer. */
146 gpu::Texture *closure_opt_layers_ = nullptr;
147 gpu::Texture *normal_opt_layers_ = nullptr;
148 gpu::Texture *header_opt_layers_ = nullptr;
149
150 /* Textures used to fulfill the GBuffer optional layers binding when textures do not have enough
151 * layers for the optional layers image views. The shader are then expected to never write to
152 * them. */
153 TextureFromPool dummy_header_tx_ = {"GBufferDummyHeader"};
154 TextureFromPool dummy_closure_tx_ = {"GBufferDummyClosure"};
155 TextureFromPool dummy_normal_tx_ = {"GBufferDummyNormal"};
156
157 public:
158 void acquire(int2 extent, int header_count, int data_count, int normal_count)
159 {
160 /* Always allocate enough layers so that the frame-buffer attachments are always valid. */
161 header_count = max_ii(header_fb_layer_count, header_count);
162 data_count = max_ii(closure_fb_layer_count, data_count);
163 normal_count = max_ii(normal_fb_layer_count, normal_count);
164
166 dummy_header_tx_.acquire_2d_array({1, 1}, 1, gpu::TextureFormat::UINT_32, dummy_use);
167 dummy_closure_tx_.acquire_2d_array({1, 1}, 1, gpu::TextureFormat::UNORM_10_10_10_2, dummy_use);
168 dummy_normal_tx_.acquire_2d_array({1, 1}, 1, gpu::TextureFormat::UNORM_16_16, dummy_use);
169
172 header_tx.acquire_2d_array(extent, header_count, gpu::TextureFormat::UINT_32, usage);
173 closure_tx.acquire_2d_array(extent, data_count, gpu::TextureFormat::UNORM_10_10_10_2, usage);
174 normal_tx.acquire_2d_array(extent, normal_count, gpu::TextureFormat::UNORM_16_16, usage);
175
176 /* Ensure layer view for frame-buffer attachment. */
180 /* Ensure layer view for image store. */
181 auto range = [](int layer_count, int fb_layer_count, Texture &tx, Texture &dummy) {
182 return (layer_count > fb_layer_count) ?
183 tx.layer_range_view(fb_layer_count, layer_count - fb_layer_count) :
184 dummy;
185 };
186 header_opt_layers_ = range(header_count, header_fb_layer_count, header_tx, dummy_header_tx_);
187 closure_opt_layers_ = range(data_count, closure_fb_layer_count, closure_tx, dummy_closure_tx_);
188 normal_opt_layers_ = range(normal_count, normal_fb_layer_count, normal_tx, dummy_normal_tx_);
189 }
190
191 /* Bind the GBuffer frame-buffer correctly using the correct workarounds. */
192 void bind(Framebuffer &gbuffer_fb, bool clear_combined = false)
193 {
194 /* Workaround a Metal bug that is only showing up on ATI/Intel GPUs. */
197 {
199 GPU_framebuffer_bind(gbuffer_fb);
200 return;
201 }
202
204 /* Clearing custom load-store frame-buffers is invalid,
205 * clear the stencil as a regular frame-buffer first. */
206 GPU_framebuffer_bind(gbuffer_fb);
207 GPU_framebuffer_clear_stencil(gbuffer_fb, 0x0u);
208 }
209
210 GPULoadOp combined_load_action = clear_combined ? GPU_LOADACTION_CLEAR : GPU_LOADACTION_LOAD;
211
213 gbuffer_fb,
214 {
216 {combined_load_action, GPU_STOREACTION_STORE, {0}}, /* Combined. */
217 {GPU_LOADACTION_CLEAR, GPU_STOREACTION_STORE, {0}}, /* GBuf Header. */
219 {GPU_LOADACTION_DONT_CARE, GPU_STOREACTION_STORE}, /* GBuf Closure. */
220 {GPU_LOADACTION_DONT_CARE, GPU_STOREACTION_STORE}, /* GBuf Closure 2. */
221 });
222 }
223
224 void release()
225 {
229
230 dummy_header_tx_.release();
231 dummy_closure_tx_.release();
232 dummy_normal_tx_.release();
233
234 header_opt_layers_ = nullptr;
235 closure_opt_layers_ = nullptr;
236 normal_opt_layers_ = nullptr;
237 }
238
239 template<typename PassType> void bind_resources(PassType &pass)
240 {
241 pass.bind_texture("gbuf_header_tx", &header_tx);
242 pass.bind_texture("gbuf_closure_tx", &closure_tx);
243 pass.bind_texture("gbuf_normal_tx", &normal_tx);
244 }
245
246 template<typename PassType> void bind_optional_layers(PassType &pass)
247 {
248 pass.bind_image(GBUF_NORMAL_SLOT, &normal_opt_layers_);
249 pass.bind_image(GBUF_CLOSURE_SLOT, &closure_opt_layers_);
250 pass.bind_image(GBUF_HEADER_SLOT, &header_opt_layers_);
251 }
252};
253
254} // namespace blender::eevee
#define GPU_framebuffer_bind_ex(_fb,...)
bool acquire_2d_array(int2 extent, int layers, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
void clear(float4 values)
bool ensure_layer_views(bool cube_as_array=false)
A running instance of the engine.
#define GBUF_NORMAL_FB_LAYER_COUNT
#define GBUF_CLOSURE_SLOT
#define GBUF_HEADER_SLOT
#define GBUF_CLOSURE_FB_LAYER_COUNT
#define GBUF_NORMAL_SLOT
#define GBUF_HEADER_FB_LAYER_COUNT
PassType
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
unsigned int uint
Definition BKE_text.h:55
@ GPU_LOADACTION_DONT_CARE
VecBase< uint32_t, 4 > uint4
MINLINE int max_ii(int a, int b)
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
VecBase< int32_t, 2 > int2
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_ATTACHMENT
bool GPU_stencil_export_support()
@ GPU_DEVICE_INTEL_UHD
@ GPU_DEVICE_INTEL
void GPU_framebuffer_clear_stencil(gpu::FrameBuffer *fb, uint clear_stencil)
TextureFromPool header_tx
void acquire(int2 extent, int header_count, int data_count, int normal_count)
void bind_resources(PassType &pass)
TextureFromPool closure_tx
TextureFromPool normal_tx
void bind_optional_layers(PassType &pass)
void bind(Framebuffer &gbuffer_fb, bool clear_combined=false)