34 const char *name,
GLContext *ctx, GLenum target, GLuint fbo,
int w,
int h)
42 gl_attachments_[0] = target;
62 if (context_ ==
nullptr) {
73 glDeleteFramebuffers(1, &fbo_id_);
76 context_->fbo_free(fbo_id_);
79 if (context_->active_fb ==
this && context_->back_left !=
this) {
87void GLFrameBuffer::init()
92 glGenFramebuffers(1, &fbo_id_);
95 glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
110 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
112#define FORMAT_STATUS(X) \
128 case GL_FRAMEBUFFER_COMPLETE:
137 const char *format =
"gpu::FrameBuffer: %s status %s\n";
143 fprintf(stderr, format, this->
name_, err);
149void GLFrameBuffer::update_attachments()
160 GLenum gl_attachment =
to_gl(type);
164 first_attachment = (attach.
tex) ? type : first_attachment;
168 first_attachment = (attach.
tex) ? type : first_attachment;
171 if (attach.
tex ==
nullptr) {
172 glFramebufferTexture(GL_FRAMEBUFFER, gl_attachment, 0, 0);
175 GLuint gl_tex =
static_cast<GLTexture *
>(attach.
tex)->tex_id_;
180 GLenum gl_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + attach.
layer;
181 glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attachment, gl_target, gl_tex, attach.
mip);
183 else if (attach.
layer > -1) {
184 glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attachment, gl_tex, attach.
mip, attach.
layer);
188 glFramebufferTexture(GL_FRAMEBUFFER, gl_attachment, gl_tex, attach.
mip);
201 for (
int i =
ARRAY_SIZE(gl_attachments_) - 1; i >= 0; --i) {
204 if (attach.tex !=
nullptr) {
205 gl_tex =
static_cast<GLTexture *
>(attach.tex)->tex_id_;
207 else if (gl_tex != 0) {
208 GLenum gl_attachment =
to_gl(type);
209 gl_attachments_[i] = gl_attachment;
210 glFramebufferTexture(GL_FRAMEBUFFER, gl_attachment, gl_tex, 0);
224 glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH,
width_);
225 glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT,
height_);
230 glDrawBuffers(
ARRAY_SIZE(gl_attachments_), gl_attachments_);
242 bool any_read =
false;
243 for (
auto attachment : color_attachment_states.
index_range()) {
252 glFramebufferFetchBarrierEXT();
261 for (
int i : color_attachment_states.
index_range()) {
272 attachments[i] = (attach_tex && attach_write) ?
to_gl(type) : GL_NONE;
277 glDrawBuffers(
ARRAY_SIZE(attachments), attachments);
285 for (
int i : color_attachment_states.
index_range()) {
289 if (tmp_detached_[type].tex !=
nullptr) {
302 this->update_attachments();
313 if (tmp_detached_[type].tex !=
nullptr) {
326 glDrawBuffers(
ARRAY_SIZE(gl_attachments_), gl_attachments_);
331 this->update_attachments();
350 float viewports_f[GPU_MAX_VIEWPORTS][4];
351 for (
int i = 0; i < GPU_MAX_VIEWPORTS; i++) {
352 for (
int j = 0; j < 4; j++) {
356 glViewportArrayv(0, GPU_MAX_VIEWPORTS, viewports_f[0]);
361 glEnable(GL_SCISSOR_TEST);
364 glDisable(GL_SCISSOR_TEST);
378 if (!immutable_ && fbo_id_ == 0) {
383 BLI_assert_msg(0,
"Trying to use the same frame-buffer in multiple context");
387 if (context_->active_fb !=
this) {
388 glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
390 if (immutable_ && fbo_id_ == 0) {
391 glDrawBuffer(gl_attachments_[0]);
402 this->update_attachments();
407 if (context_->active_fb !=
this || enabled_srgb_ != enabled_srgb) {
408 enabled_srgb_ = enabled_srgb;
409 if (enabled_srgb && srgb_) {
410 glEnable(GL_FRAMEBUFFER_SRGB);
413 glDisable(GL_FRAMEBUFFER_SRGB);
418 if (context_->active_fb !=
this) {
419 context_->active_fb =
this;
420 state_manager_->active_fb =
this;
450 float(clear_col[0]),
float(clear_col[1]),
float(clear_col[2]),
float(clear_col[3]));
463 glClearDepth(clear_depth);
468 glClearStencil(clear_stencil);
471 context_->state_manager->apply_state();
477 GLbitfield
mask =
to_gl(glclear_buffers);
500 context_->state_manager->apply_state();
503 glClearDepth(
float(clear_value.x));
511 glClearBufferfv(GL_COLOR, slot, &
data.x);
515 glClearBufferiv(GL_COLOR, slot, &
data.x);
519 glClearBufferuiv(GL_COLOR, slot, &
data.x);
546 GLenum format, type, mode;
547 mode = gl_attachments_[slot];
548 type =
to_gl(data_format);
552 format = GL_DEPTH_COMPONENT;
556 "GPUFramebuffer: Error: Trying to read depth without a depth buffer attached.");
561 "GPUFramebuffer: Error: Trying to read a color slot without valid attachment.");
564 if (format == GL_RED && type == GL_UNSIGNED_INT) {
565 format = GL_RED_INTEGER;
569 fprintf(stderr,
"GPUFramebuffer: Error: Trying to read stencil bit. Unsupported.");
572 fprintf(stderr,
"GPUFramebuffer: Error: Trying to read more than one frame-buffer plane.");
576 glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo_id_);
578 glReadPixels(
UNPACK4(area), format, type, r_data);
595 glBindFramebuffer(GL_READ_FRAMEBUFFER, src->fbo_id_);
596 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->fbo_id_);
599 BLI_assert(src->immutable_ ==
false || src_slot == 0);
600 BLI_assert(dst->immutable_ ==
false || dst_slot == 0);
601 BLI_assert(src->gl_attachments_[src_slot] != GL_NONE);
602 BLI_assert(dst->gl_attachments_[dst_slot] != GL_NONE);
603 glReadBuffer(src->gl_attachments_[src_slot]);
604 glDrawBuffer(dst->gl_attachments_[dst_slot]);
607 context_->state_manager->apply_state();
612 glBlitFramebuffer(0, 0,
w, h,
x,
y,
x +
w,
y + h,
mask, GL_NEAREST);
614 if (!dst->immutable_) {
616 glDrawBuffers(
ARRAY_SIZE(dst->gl_attachments_), dst->gl_attachments_);
619 context_->active_fb = dst;
#define GPU_ATTACHMENT_NONE
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
constexpr IndexRange()=default
constexpr IndexRange index_range() const
StateManager * state_manager
void size_set(int width, int height)
void set_color_attachment_bit(GPUAttachmentType type, bool value)
char name_[DEBUG_NAME_LEN]
FrameBuffer(const char *name)
GPUAttachment attachments_[GPU_FB_MAX_ATTACHMENT]
int viewport_[GPU_MAX_VIEWPORTS][4]
void attachment_set(GPUAttachmentType type, const GPUAttachment &new_attachment)
static bool framebuffer_fetch_support
static bool unused_fb_slot_workaround
static bool texture_barrier_support
bool check(char err_out[256]) override
void attachment_set_loadstore_op(GPUAttachmentType type, GPULoadStore ls) override
void clear(GPUFrameBufferBits buffers, const double4 clear_col, float clear_depth, uint clear_stencil) override
void blit_to(GPUFrameBufferBits planes, int src_slot, FrameBuffer *dst, int dst_slot, int dst_offset_x, int dst_offset_y) override
void subpass_transition_impl(const GPUAttachmentState depth_attachment_state, Span< GPUAttachmentState > color_attachment_states) override
void clear_attachment(GPUAttachmentType type, const double4 clear_value) override
GLFrameBuffer(const char *name)
void bind(bool enabled_srgb) override
void read(GPUFrameBufferBits planes, eGPUDataFormat format, const int area[4], int channel_len, int slot, void *r_data) override
void clear_multi(Span< double4 > clear_cols) override
static void set_framebuffer_srgb_target(int use_srgb_to_linear)
GPUTextureFormatFlag format_flag_get() const
void detach_from(FrameBuffer *fb)
#define GPU_FB_MAX_COLOR_ATTACHMENT
int size(const AssetLibraryReference *library_reference)
void object_label(GLenum type, GLuint object, const char *name)
@ GPU_FORMAT_NORMALIZED_INTEGER
GLenum channel_len_to_gl(int channel_len)
GLenum to_gl(eGPUDataFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
gpu::TextureFormat GPU_texture_format(const gpu::Texture *texture)
VecBase< int32_t, 4 > int4
void GPU_framebuffer_restore()
GPUWriteMask GPU_write_mask_get()
GPUStencilTest GPU_stencil_test_get()
VecBase< uint32_t, 4 > uint4
void GPU_depth_mask(bool depth)
VecBase< float, 4 > float4
@ GPU_FB_DEPTH_STENCIL_ATTACHMENT
@ GPU_FB_DEPTH_ATTACHMENT
@ GPU_FB_COLOR_ATTACHMENT0
BMesh const char void * data
uint GPU_stencil_mask_get()
void GPU_color_mask(bool r, bool g, bool b, bool a)
bool GPU_texture_is_cube(const gpu::Texture *texture)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
void GPU_stencil_test(GPUStencilTest test)
void GPU_texture_bind_ex(gpu::Texture *texture, GPUSamplerState state, int unit)
bool GPU_texture_is_array(const gpu::Texture *texture)
void GPU_stencil_write_mask_set(uint write_mask)
VecBase< double, 4 > double4
void GPU_write_mask(GPUWriteMask mask)
void GPU_texture_get_mipmap_size(gpu::Texture *texture, int mip_level, int *r_size)
static constexpr GPUSamplerState default_sampler()