GPUState: Only apply state before drawing

This commit is contained in:
Clément Foucault 2020-08-17 18:08:47 +02:00
parent 482a51aabf
commit 536c2e0ec9
Notes: blender-bot 2023-02-14 01:57:12 +01:00
Referenced by commit f12b0373f3, BGL: Workaround broken bgl usage caused by GPU refactor
Referenced by issue #81289, Python gpu bgl image drawing alpha problem
Referenced by issue #80169, Node wrangler highlighting artifacts
7 changed files with 20 additions and 18 deletions

View File

@ -282,6 +282,8 @@ void GPU_batch_draw_advanced(GPUBatch *batch, int v_first, int v_count, int i_fi
/* just draw some vertices and let shader place them where we want. */
void GPU_draw_primitive(GPUPrimType prim_type, int v_count)
{
GPU_context_active_get()->state_manager->apply_state();
/* we cannot draw without vao ... annoying ... */
glBindVertexArray(GPU_vao_default());

View File

@ -622,6 +622,8 @@ void GPU_framebuffer_clear(GPUFrameBuffer *fb,
{
CHECK_FRAMEBUFFER_IS_BOUND(fb);
GPU_context_active_get()->state_manager->apply_state();
if (buffers & GPU_COLOR_BIT) {
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glClearColor(clear_col[0], clear_col[1], clear_col[2], clear_col[3]);
@ -786,6 +788,8 @@ void GPU_framebuffer_blit(GPUFrameBuffer *fb_read,
GLbitfield mask = convert_buffer_bits_to_gl(blit_buffers);
GPU_context_active_get()->state_manager->apply_state();
glBlitFramebuffer(0,
0,
fb_read->width,
@ -1043,6 +1047,8 @@ void GPU_offscreen_draw_to_screen(GPUOffScreen *ofs, int x, int y)
const int w = GPU_texture_width(ofs->color);
const int h = GPU_texture_height(ofs->color);
GPU_context_active_get()->state_manager->apply_state();
GPUFrameBuffer *ofs_fb = gpu_offscreen_fb_get(ofs);
glBindFramebuffer(GL_READ_FRAMEBUFFER, ofs_fb->object);
@ -1107,6 +1113,7 @@ void GPU_clear_depth(float depth)
void GPU_clear(eGPUFrameBufferBits flags)
{
GPU_context_active_get()->state_manager->apply_state();
glClear(convert_buffer_bits_to_gl(flags));
}

View File

@ -210,6 +210,8 @@ static bool vertex_count_makes_sense_for_primitive(uint vertex_len, GPUPrimType
void immBegin(GPUPrimType prim_type, uint vertex_len)
{
GPU_context_active_get()->state_manager->apply_state();
#if TRUST_NO_ONE
assert(initialized);
assert(imm.prim_type == GPU_PRIM_NONE); /* make sure we haven't already begun */

View File

@ -45,8 +45,6 @@ using namespace blender::gpu;
GPUStateManager *stack = GPU_context_active_get()->state_manager; \
auto &state_object = stack->_prefix##state; \
state_object._state = _value; \
/* TODO remove this and only push state at draw time. */ \
stack->set_##_prefix##state(state_object); \
} while (0)
#define SET_IMMUTABLE_STATE(_state, _value) SET_STATE(, _state, _value)
@ -112,8 +110,6 @@ void GPU_color_mask(bool r, bool g, bool b, bool a)
SET_FLAG_FROM_TEST(write_mask, b, GPU_WRITE_BLUE);
SET_FLAG_FROM_TEST(write_mask, a, GPU_WRITE_ALPHA);
state.write_mask = write_mask;
/* TODO remove this and only push state at draw time. */
stack->set_state(state);
}
void GPU_depth_mask(bool depth)
@ -123,8 +119,6 @@ void GPU_depth_mask(bool depth)
eGPUWriteMask write_mask = state.write_mask;
SET_FLAG_FROM_TEST(write_mask, depth, GPU_WRITE_DEPTH);
state.write_mask = write_mask;
/* TODO remove this and only push state at draw time. */
stack->set_state(state);
}
void GPU_clip_distances(int distances_enabled)
@ -143,8 +137,6 @@ void GPU_depth_range(float near, float far)
GPUStateManager *stack = GPU_context_active_get()->state_manager;
auto &state = stack->mutable_state;
copy_v2_fl2(state.depth_range, near, far);
/* TODO remove this and only push state at draw time. */
stack->set_mutable_state(state);
}
void GPU_line_width(float width)
@ -167,8 +159,6 @@ void GPU_program_point_size(bool enable)
auto &state = stack->mutable_state;
/* Set point size sign negative to disable. */
state.point_size = fabsf(state.point_size) * (enable ? 1 : -1);
/* TODO remove this and only push state at draw time. */
stack->set_mutable_state(state);
}
void GPU_scissor_test(bool enable)
@ -177,8 +167,6 @@ void GPU_scissor_test(bool enable)
auto &state = stack->mutable_state;
/* Set point size sign negative to disable. */
state.scissor_rect[2] = abs(state.scissor_rect[2]) * (enable ? 1 : -1);
/* TODO remove this and only push state at draw time. */
stack->set_mutable_state(state);
}
void GPU_scissor(int x, int y, int width, int height)
@ -187,8 +175,6 @@ void GPU_scissor(int x, int y, int width, int height)
auto &state = stack->mutable_state;
int scissor_rect[4] = {x, y, width, height};
copy_v4_v4_int(state.scissor_rect, scissor_rect);
/* TODO remove this and only push state at draw time. */
stack->set_mutable_state(state);
}
void GPU_viewport(int x, int y, int width, int height)
@ -197,8 +183,6 @@ void GPU_viewport(int x, int y, int width, int height)
auto &state = stack->mutable_state;
int viewport_rect[4] = {x, y, width, height};
copy_v4_v4_int(state.viewport_rect, viewport_rect);
/* TODO remove this and only push state at draw time. */
stack->set_mutable_state(state);
}
/** \} */

View File

@ -138,6 +138,12 @@ class GPUStateManager {
virtual void set_state(const GPUState &state) = 0;
virtual void set_mutable_state(const GPUStateMutable &state) = 0;
inline void apply_state(void)
{
this->set_state(this->state);
this->set_mutable_state(this->mutable_state);
};
};
} // namespace gpu

View File

@ -300,6 +300,8 @@ GLBatch::~GLBatch()
void GLBatch::bind(int i_first)
{
GPU_context_active_get()->state_manager->apply_state();
if (flag & GPU_BATCH_DIRTY) {
vao_cache_.clear();
}

View File

@ -102,9 +102,8 @@ void GLStateManager::set_mutable_state(const GPUStateMutable &state)
if ((changed.scissor_rect[0] != 0) || (changed.scissor_rect[1] != 0) ||
(changed.scissor_rect[2] != 0) || (changed.scissor_rect[3] != 0)) {
glScissor(UNPACK4(state.scissor_rect));
if ((state.scissor_rect[2] > 0)) {
glScissor(UNPACK4(state.scissor_rect));
glEnable(GL_SCISSOR_TEST);
}
else {