Blender V5.0
mtl_framebuffer.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 "GPU_common_types.hh"
14#include "MEM_guardedalloc.h"
15
17#include "mtl_texture.hh"
18
19#include <Metal/Metal.h>
20
21namespace blender::gpu {
22
23class MTLContext;
24
26 bool used = false;
28 union {
29 float color[4];
30 float depth;
33
36 uint mip = 0;
39
40 /* If Array Length is larger than zero, use multilayered rendering. */
42};
43
48 private:
49 /* Context Handle. */
50 MTLContext *context_;
51
52 /* Metal Attachment properties. */
53 uint colour_attachment_count_;
54 MTLAttachment mtl_color_attachments_[GPU_FB_MAX_COLOR_ATTACHMENT];
55 MTLAttachment mtl_depth_attachment_;
56 MTLAttachment mtl_stencil_attachment_;
57 bool use_multilayered_rendering_ = false;
58
59 /* State. */
60
65 bool is_dirty_;
66
68 bool is_loadstore_dirty_;
69
74 MTLContext *dirty_state_ctx_;
75
81 bool has_pending_clear_;
82
93 enum {
94 MTL_FB_CONFIG_CLEAR = 0,
95 MTL_FB_CONFIG_LOAD = 1,
96 MTL_FB_CONFIG_CUSTOM = 2,
97
98 MTL_FB_CONFIG_MAX = (MTL_FB_CONFIG_CUSTOM + 1),
99 };
100
101 MTLRenderPassDescriptor *framebuffer_descriptor_[MTL_FB_CONFIG_MAX];
102 MTLRenderPassColorAttachmentDescriptor
103 *colour_attachment_descriptors_[GPU_FB_MAX_COLOR_ATTACHMENT];
105 bool descriptor_dirty_[MTL_FB_CONFIG_MAX];
107 bool enabled_srgb_;
109 bool srgb_;
110
118 int default_width_ = 0;
119 int default_height_ = 0;
120
121 public:
125 MTLFrameBuffer(MTLContext *ctx, const char *name);
126
127 ~MTLFrameBuffer() override;
128
129 void bind(bool enabled_srgb) override;
130
131 bool check(char err_out[256]) override;
132
134 const float clear_col[4],
135 float clear_depth,
136 uint clear_stencil) override;
137 void clear_multi(const float (*clear_cols)[4]) override;
139 eGPUDataFormat data_format,
140 const void *clear_value) override;
141
143
144 void read(GPUFrameBufferBits planes,
146 const int area[4],
147 int channel_len,
148 int slot,
149 void *r_data) override;
150
151 void blit_to(GPUFrameBufferBits planes,
152 int src_slot,
153 FrameBuffer *dst,
154 int dst_slot,
155 int dst_offset_x,
156 int dst_offset_y) override;
157
158 protected:
159 void subpass_transition_impl(const GPUAttachmentState /*depth_attachment_state*/,
160 Span<GPUAttachmentState> color_attachment_states) override;
161
162 public:
163 void apply_state();
164
165 /* State. */
166 /* Flag MTLFramebuffer configuration as having changed. */
167 void mark_dirty();
169 /* Mark that a pending clear has been performed. */
170 void mark_cleared();
171 /* Mark that we have a pending clear. */
172 void mark_do_clear();
173
174 /* Attachment management. */
175 /* When dirty_attachments_ is true, we need to reprocess attachments to extract Metal
176 * information. */
178 void update_attachments(bool update_viewport);
179 bool add_color_attachment(gpu::MTLTexture *texture, uint slot, int miplevel, int layer);
180 bool add_depth_attachment(gpu::MTLTexture *texture, int miplevel, int layer);
181 bool add_stencil_attachment(gpu::MTLTexture *texture, int miplevel, int layer);
182 bool remove_color_attachment(uint slot);
187
188 /* Clear values -> Load/store actions. */
189 bool set_color_attachment_clear_color(uint slot, const float clear_color[4]);
190 bool set_depth_attachment_clear_value(float depth_clear);
191 bool set_stencil_attachment_clear_value(uint stencil_clear);
192 bool set_color_loadstore_op(uint slot, GPULoadOp load_action, GPUStoreOp store_action);
193 bool set_depth_loadstore_op(GPULoadOp load_action, GPUStoreOp store_action);
194 bool set_stencil_loadstore_op(GPULoadOp load_action, GPUStoreOp store_action);
195
196 /* Remove any pending clears - Ensure "load" configuration is used. */
197 bool reset_clear_state();
198
199 /* Fetch values */
200 bool has_attachment_at_slot(uint slot);
213
214 /* Metal API resources and validation. */
216 MTLRenderPassDescriptor *bake_render_pass_descriptor(bool load_contents);
217
218 /* Blitting. */
219 void blit(uint read_slot,
220 uint src_x_offset,
221 uint src_y_offset,
222 MTLFrameBuffer *metal_fb_write,
223 uint write_slot,
224 uint dst_x_offset,
225 uint dst_y_offset,
226 uint width,
227 uint height,
228 GPUFrameBufferBits blit_buffers);
229
230 int get_width();
231 int get_height();
232 int get_default_width();
233 int get_default_height();
234
236 {
237 return is_dirty_ || is_loadstore_dirty_;
238 }
239
241 {
242 return has_pending_clear_;
243 }
244
246 {
247 return enabled_srgb_;
248 }
249
251 {
252 return srgb_;
253 }
254
255 inline void default_size_set(int w, int h)
256 {
257 default_width_ = w;
258 default_height_ = h;
259 }
260
261 private:
262 /* Clears a render target by force-opening a render pass. */
263 void force_clear();
264
265 MEM_CXX_CLASS_ALLOC_FUNCS("MTLFrameBuffer");
266};
267
268} // namespace blender::gpu
unsigned int uint
@ GPU_LOADACTION_DONT_CARE
@ GPU_STOREACTION_DONT_CARE
GPUAttachmentState
GPUFrameBufferBits
eGPUDataFormat
Read Guarded memory(de)allocation.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
FrameBuffer(const char *name)
MTLAttachment get_color_attachment(uint slot)
void blit(uint read_slot, uint src_x_offset, uint src_y_offset, MTLFrameBuffer *metal_fb_write, uint write_slot, uint dst_x_offset, uint dst_y_offset, uint width, uint height, GPUFrameBufferBits blit_buffers)
bool set_stencil_loadstore_op(GPULoadOp load_action, GPUStoreOp store_action)
bool set_depth_loadstore_op(GPULoadOp load_action, GPUStoreOp store_action)
int get_color_attachment_slot_from_texture(gpu::MTLTexture *texture)
void default_size_set(int w, int h)
bool check(char err_out[256]) override
bool add_color_attachment(gpu::MTLTexture *texture, uint slot, int miplevel, int layer)
bool set_color_attachment_clear_color(uint slot, const float clear_color[4])
MTLFrameBuffer(MTLContext *ctx, const char *name)
bool set_depth_attachment_clear_value(float depth_clear)
void update_attachments(bool update_viewport)
MTLRenderPassDescriptor * bake_render_pass_descriptor(bool load_contents)
bool has_color_attachment_with_texture(gpu::MTLTexture *texture)
bool set_color_loadstore_op(uint slot, GPULoadOp load_action, GPUStoreOp store_action)
void clear_multi(const float(*clear_cols)[4]) override
void blit_to(GPUFrameBufferBits planes, int src_slot, FrameBuffer *dst, int dst_slot, int dst_offset_x, int dst_offset_y) override
bool add_depth_attachment(gpu::MTLTexture *texture, int miplevel, int layer)
void attachment_set_loadstore_op(GPUAttachmentType type, GPULoadStore ls) override
void clear(GPUFrameBufferBits buffers, const float clear_col[4], float clear_depth, uint clear_stencil) override
bool set_stencil_attachment_clear_value(uint stencil_clear)
bool add_stencil_attachment(gpu::MTLTexture *texture, int miplevel, int layer)
void clear_attachment(GPUAttachmentType type, eGPUDataFormat data_format, const void *clear_value) override
void bind(bool enabled_srgb) override
void subpass_transition_impl(const GPUAttachmentState, Span< GPUAttachmentState > color_attachment_states) override
#define GPU_FB_MAX_COLOR_ATTACHMENT
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
format
const char * name
union blender::gpu::MTLAttachment::@142227134026063347366002067303226104245171177125 clear_value
char * buffers[2]