Blender V4.3
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
11#pragma once
12
13#include "GPU_common_types.hh"
14#include "MEM_guardedalloc.h"
15
17#include "mtl_texture.hh"
18#include <Metal/Metal.h>
19
20namespace blender::gpu {
21
22class MTLContext;
23
25 bool used;
27 union {
28 float color[4];
29 float depth;
32
38
39 /* If Array Length is larger than zero, use multilayered rendering. */
41};
42
47 private:
48 /* Context Handle. */
49 MTLContext *context_;
50
51 /* Metal Attachment properties. */
52 uint colour_attachment_count_;
53 MTLAttachment mtl_color_attachments_[GPU_FB_MAX_COLOR_ATTACHMENT];
54 MTLAttachment mtl_depth_attachment_;
55 MTLAttachment mtl_stencil_attachment_;
56 bool use_multilayered_rendering_ = false;
57
58 /* State. */
59
64 bool is_dirty_;
65
67 bool is_loadstore_dirty_;
68
73 MTLContext *dirty_state_ctx_;
74
80 bool has_pending_clear_;
81
92 typedef enum {
93 MTL_FB_CONFIG_CLEAR = 0,
94 MTL_FB_CONFIG_LOAD = 1,
95 MTL_FB_CONFIG_CUSTOM = 2
96 } MTL_FB_CONFIG;
97#define MTL_FB_CONFIG_MAX (MTL_FB_CONFIG_CUSTOM + 1)
98
99 MTLRenderPassDescriptor *framebuffer_descriptor_[MTL_FB_CONFIG_MAX];
100 MTLRenderPassColorAttachmentDescriptor
101 *colour_attachment_descriptors_[GPU_FB_MAX_COLOR_ATTACHMENT];
103 bool descriptor_dirty_[MTL_FB_CONFIG_MAX];
105 bool enabled_srgb_;
107 bool srgb_;
108
116 int default_width_ = 0;
117 int default_height_ = 0;
118
119 public:
123 MTLFrameBuffer(MTLContext *ctx, const char *name);
124
126
127 void bind(bool enabled_srgb) override;
128
129 bool check(char err_out[256]) override;
130
132 const float clear_col[4],
133 float clear_depth,
134 uint clear_stencil) override;
135 void clear_multi(const float (*clear_cols)[4]) override;
137 eGPUDataFormat data_format,
138 const void *clear_value) override;
139
141
142 void read(eGPUFrameBufferBits planes,
144 const int area[4],
145 int channel_len,
146 int slot,
147 void *r_data) override;
148
149 void blit_to(eGPUFrameBufferBits planes,
150 int src_slot,
151 FrameBuffer *dst,
152 int dst_slot,
153 int dst_offset_x,
154 int dst_offset_y) override;
155
156 protected:
157 void subpass_transition_impl(const GPUAttachmentState /*depth_attachment_state*/,
158 Span<GPUAttachmentState> color_attachment_states) override;
159
160 public:
161 void apply_state();
162
163 /* State. */
164 /* Flag MTLFramebuffer configuration as having changed. */
165 void mark_dirty();
167 /* Mark that a pending clear has been performed. */
168 void mark_cleared();
169 /* Mark that we have a pending clear. */
170 void mark_do_clear();
171
172 /* Attachment management. */
173 /* When dirty_attachments_ is true, we need to reprocess attachments to extract Metal
174 * information. */
176 void update_attachments(bool update_viewport);
177 bool add_color_attachment(gpu::MTLTexture *texture, uint slot, int miplevel, int layer);
178 bool add_depth_attachment(gpu::MTLTexture *texture, int miplevel, int layer);
179 bool add_stencil_attachment(gpu::MTLTexture *texture, int miplevel, int layer);
180 bool remove_color_attachment(uint slot);
185
186 /* Clear values -> Load/store actions. */
187 bool set_color_attachment_clear_color(uint slot, const float clear_color[4]);
188 bool set_depth_attachment_clear_value(float depth_clear);
189 bool set_stencil_attachment_clear_value(uint stencil_clear);
190 bool set_color_loadstore_op(uint slot, eGPULoadOp load_action, eGPUStoreOp store_action);
191 bool set_depth_loadstore_op(eGPULoadOp load_action, eGPUStoreOp store_action);
192 bool set_stencil_loadstore_op(eGPULoadOp load_action, eGPUStoreOp store_action);
193
194 /* Remove any pending clears - Ensure "load" configuration is used. */
195 bool reset_clear_state();
196
197 /* Fetch values */
198 bool has_attachment_at_slot(uint slot);
199 bool has_color_attachment_with_texture(gpu::MTLTexture *texture);
202 int get_color_attachment_slot_from_texture(gpu::MTLTexture *texture);
211
212 /* Metal API resources and validation. */
214 MTLRenderPassDescriptor *bake_render_pass_descriptor(bool load_contents);
215
216 /* Blitting. */
217 void blit(uint read_slot,
218 uint src_x_offset,
219 uint src_y_offset,
220 MTLFrameBuffer *metal_fb_write,
221 uint write_slot,
222 uint dst_x_offset,
223 uint dst_y_offset,
224 uint width,
225 uint height,
226 eGPUFrameBufferBits blit_buffers);
227
228 int get_width();
229 int get_height();
230 int get_default_width();
231 int get_default_height();
232
234 {
235 return is_dirty_ || is_loadstore_dirty_;
236 }
237
239 {
240 return has_pending_clear_;
241 }
242
244 {
245 return enabled_srgb_;
246 }
247
249 {
250 return srgb_;
251 }
252
253 inline void default_size_set(int w, int h)
254 {
255 default_width_ = w;
256 default_height_ = h;
257 }
258
259 private:
260 /* Clears a render target by force-opening a render pass. */
261 void force_clear();
262
263 MEM_CXX_CLASS_ALLOC_FUNCS("MTLFrameBuffer");
264};
265
266} // namespace blender::gpu
unsigned int uint
eGPUStoreOp
GPUAttachmentState
eGPUFrameBufferBits
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)
bool set_depth_loadstore_op(eGPULoadOp load_action, eGPUStoreOp store_action)
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, eGPUFrameBufferBits blit_buffers)
void read(eGPUFrameBufferBits planes, eGPUDataFormat format, const int area[4], int channel_len, int slot, void *r_data) override
int get_color_attachment_slot_from_texture(gpu::MTLTexture *texture)
void blit_to(eGPUFrameBufferBits planes, int src_slot, FrameBuffer *dst, int dst_slot, int dst_offset_x, int dst_offset_y) override
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)
void clear_multi(const float(*clear_cols)[4]) override
bool set_stencil_loadstore_op(eGPULoadOp load_action, eGPUStoreOp store_action)
bool set_color_loadstore_op(uint slot, eGPULoadOp load_action, eGPUStoreOp store_action)
bool add_depth_attachment(gpu::MTLTexture *texture, int miplevel, int layer)
void attachment_set_loadstore_op(GPUAttachmentType type, GPULoadStore ls) 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 clear(eGPUFrameBufferBits buffers, const float clear_col[4], float clear_depth, uint clear_stencil) 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
format
#define MTL_FB_CONFIG_MAX
union blender::gpu::MTLAttachment::@650 clear_value
char * buffers[2]