Blender V5.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
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;
27 bool ignored = false;
30
33 uint mip = 0;
36
37 /* If Array Length is larger than zero, use multilayered rendering. */
39};
40
45 private:
46 /* Context Handle. */
47 MTLContext *context_;
48
49 /* Metal Attachment properties. */
50 uint colour_attachment_count_;
51 MTLAttachment mtl_color_attachments_[GPU_FB_MAX_COLOR_ATTACHMENT];
52 MTLAttachment mtl_depth_attachment_;
53 MTLAttachment mtl_stencil_attachment_;
54 bool use_multilayered_rendering_ = false;
55
56 /* State. */
57
62 bool is_dirty_;
63
65 bool is_loadstore_dirty_;
66
71 MTLContext *dirty_state_ctx_;
72
78 bool has_pending_clear_;
79
90 enum {
91 MTL_FB_CONFIG_CLEAR = 0,
92 MTL_FB_CONFIG_LOAD = 1,
93 MTL_FB_CONFIG_CUSTOM = 2,
94
95 MTL_FB_CONFIG_MAX = (MTL_FB_CONFIG_CUSTOM + 1),
96 };
97
98 MTLRenderPassDescriptor *framebuffer_descriptor_[MTL_FB_CONFIG_MAX];
99 MTLRenderPassColorAttachmentDescriptor
100 *colour_attachment_descriptors_[GPU_FB_MAX_COLOR_ATTACHMENT];
102 bool descriptor_dirty_[MTL_FB_CONFIG_MAX];
104 bool enabled_srgb_;
106 bool srgb_;
107
116 int attachment_width_ = 0;
117 int attachment_height_ = 0;
118
119 public:
123 MTLFrameBuffer(MTLContext *ctx, const char *name);
124
125 ~MTLFrameBuffer() override;
126
127 void bind(bool enabled_srgb) override;
128
129 bool check(char err_out[256]) override;
130
131 void clear(GPUFrameBufferBits buffers,
132 const double4 clear_col,
133 float clear_depth,
134 uint clear_stencil) override;
135 void clear_multi(Span<double4> clear_cols) override;
136 void clear_attachment(GPUAttachmentType type, const double4 clear_value) override;
137
139
140 void read(GPUFrameBufferBits planes,
141 eGPUDataFormat format,
142 const int area[4],
143 int channel_len,
144 int slot,
145 void *r_data) override;
146
147 void blit_to(GPUFrameBufferBits planes,
148 int src_slot,
149 FrameBuffer *dst,
150 int dst_slot,
151 int dst_offset_x,
152 int dst_offset_y) override;
153
154 protected:
155 void subpass_transition_impl(const GPUAttachmentState /*depth_attachment_state*/,
156 Span<GPUAttachmentState> color_attachment_states) override;
157
158 public:
159 void apply_state();
160
161 /* State. */
162 /* Flag MTLFramebuffer configuration as having changed. */
163 void mark_dirty();
165 /* Mark that a pending clear has been performed. */
166 void mark_cleared();
167 /* Mark that we have a pending clear. */
168 void mark_do_clear();
169
170 /* Attachment management. */
171 /* When dirty_attachments_ is true, we need to reprocess attachments to extract Metal
172 * information. */
174 void update_attachments(bool update_viewport);
175 bool add_color_attachment(gpu::MTLTexture *texture, uint slot, int miplevel, int layer);
176 bool add_depth_attachment(gpu::MTLTexture *texture, int miplevel, int layer);
177 bool add_stencil_attachment(gpu::MTLTexture *texture, int miplevel, int layer);
178 bool remove_color_attachment(uint slot);
183
184 /* Clear values -> Load/store actions. */
185 bool set_color_attachment_clear_color(uint slot, const double4 clear_color);
186 bool set_depth_attachment_clear_value(float depth_clear);
187 bool set_stencil_attachment_clear_value(uint stencil_clear);
188 bool set_color_loadstore_op(uint slot, GPULoadOp load_action, GPUStoreOp store_action);
189 bool set_depth_loadstore_op(GPULoadOp load_action, GPUStoreOp store_action);
190 bool set_stencil_loadstore_op(GPULoadOp load_action, GPUStoreOp store_action);
191
192 /* Remove any pending clears - Ensure "load" configuration is used. */
193 bool reset_clear_state();
194
195 /* Fetch values */
196 bool has_attachment_at_slot(uint slot);
209
210 /* Metal API resources and validation. */
212 MTLRenderPassDescriptor *bake_render_pass_descriptor(bool load_contents);
213
214 /* Blitting. */
215 void blit(uint read_slot,
216 uint src_x_offset,
217 uint src_y_offset,
218 MTLFrameBuffer *metal_fb_write,
219 uint write_slot,
220 uint dst_x_offset,
221 uint dst_y_offset,
222 uint width,
223 uint height,
224 GPUFrameBufferBits blit_buffers);
225
226 int get_width();
227 int get_height();
230 void attachment_size_set(int w, int h);
231
233 {
234 return is_dirty_ || is_loadstore_dirty_;
235 }
236
238 {
239 return has_pending_clear_;
240 }
241
243 {
244 return enabled_srgb_;
245 }
246
248 {
249 return srgb_;
250 }
251
252 private:
253 /* Clears a render target by force-opening a render pass. */
254 void force_clear();
255
256 MEM_CXX_CLASS_ALLOC_FUNCS("MTLFrameBuffer");
257};
258
259} // namespace blender::gpu
Read Blender memory allocation and freeing.
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 attachment_size_set(int w, int h)
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)
bool check(char err_out[256]) override
void read(GPUFrameBufferBits planes, eGPUDataFormat format, const int area[4], int channel_len, int slot, void *r_data) override
bool add_color_attachment(gpu::MTLTexture *texture, uint slot, int miplevel, int layer)
void clear_multi(Span< double4 > clear_cols) override
void clear(GPUFrameBufferBits buffers, const double4 clear_col, float clear_depth, uint clear_stencil) override
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 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
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, const double4 clear_value) override
bool set_color_attachment_clear_color(uint slot, const double4 clear_color)
void bind(bool enabled_srgb) override
void subpass_transition_impl(const GPUAttachmentState, Span< GPUAttachmentState > color_attachment_states) override
#define GPU_FB_MAX_COLOR_ATTACHMENT
#define layer
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
unsigned int uint
Definition BKE_text.h:55
@ GPU_LOADACTION_DONT_CARE
@ GPU_STOREACTION_DONT_CARE
VecBase< double, 4 > double4