From adca09b643d05572321b5015809245493b8dd762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Mon, 17 Aug 2020 20:32:20 +0200 Subject: GPUState: Port default state to StateManager constructor --- source/blender/draw/intern/draw_manager_exec.c | 5 +- source/blender/gpu/intern/gpu_state.cc | 67 ++++++++++++++------------ source/blender/gpu/intern/gpu_state_private.hh | 17 +++++++ source/blender/gpu/opengl/gl_state.cc | 32 ++++++++++-- source/blender/gpu/opengl/gl_state.hh | 2 + 5 files changed, 87 insertions(+), 36 deletions(-) diff --git a/source/blender/draw/intern/draw_manager_exec.c b/source/blender/draw/intern/draw_manager_exec.c index f08c3231e8c..47892b958d0 100644 --- a/source/blender/draw/intern/draw_manager_exec.c +++ b/source/blender/draw/intern/draw_manager_exec.c @@ -136,18 +136,21 @@ void drw_state_set(DRWState state) switch (state & DRW_STATE_WRITE_STENCIL_ENABLED) { case DRW_STATE_WRITE_STENCIL: stencil_op = GPU_STENCIL_OP_REPLACE; + GPU_stencil_write_mask_set(0xFF); break; case DRW_STATE_WRITE_STENCIL_SHADOW_PASS: stencil_op = GPU_STENCIL_OP_COUNT_DEPTH_PASS; + GPU_stencil_write_mask_set(0xFF); break; case DRW_STATE_WRITE_STENCIL_SHADOW_FAIL: stencil_op = GPU_STENCIL_OP_COUNT_DEPTH_FAIL; + GPU_stencil_write_mask_set(0xFF); break; default: stencil_op = GPU_STENCIL_OP_NONE; + GPU_stencil_write_mask_set(0x00); break; } - GPU_stencil_write_mask_set(0xFF); switch (state & DRW_STATE_STENCIL_TEST_ENABLED) { case DRW_STATE_STENCIL_ALWAYS: diff --git a/source/blender/gpu/intern/gpu_state.cc b/source/blender/gpu/intern/gpu_state.cc index 619f52da852..796146abdde 100644 --- a/source/blender/gpu/intern/gpu_state.cc +++ b/source/blender/gpu/intern/gpu_state.cc @@ -310,40 +310,43 @@ void GPU_unpack_row_length_set(uint len) * exceptions that we should try to get rid of. * \{ */ +GPUStateManager::GPUStateManager(void) +{ + /* Set default state. */ + state.write_mask = GPU_WRITE_COLOR; + state.blend = GPU_BLEND_NONE; + state.culling_test = GPU_CULL_NONE; + state.depth_test = GPU_DEPTH_NONE; + state.stencil_test = GPU_STENCIL_NONE; + state.stencil_op = GPU_STENCIL_OP_NONE; + state.provoking_vert = GPU_VERTEX_LAST; + state.logic_op_xor = false; + state.invert_facing = false; + state.shadow_bias = false; + state.polygon_smooth = false; + state.clip_distances = 0; + + /* TODO: We should have better default for viewport and scissors. + * For now it's not important since they are overwritten at soon as a framebuffer is bound. */ + mutable_state.viewport_rect[0] = 0; + mutable_state.viewport_rect[1] = 0; + mutable_state.viewport_rect[2] = 10; + mutable_state.viewport_rect[3] = 10; + mutable_state.scissor_rect[0] = 0; + mutable_state.scissor_rect[1] = 0; + mutable_state.scissor_rect[2] = -10; /* Disable */ + mutable_state.scissor_rect[3] = 10; + mutable_state.depth_range[0] = 0.0f; + mutable_state.depth_range[1] = 1.0f; + mutable_state.point_size = 1.0f; + mutable_state.line_width = 1.0f; + mutable_state.stencil_write_mask = 0x00; + mutable_state.stencil_compare_mask = 0x00; + mutable_state.stencil_reference = 0x00; +} + void GPU_state_init(void) { - GPU_program_point_size(false); - - glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); - - glDisable(GL_BLEND); - glDisable(GL_DEPTH_TEST); - glDisable(GL_COLOR_LOGIC_OP); - glDisable(GL_STENCIL_TEST); - glDisable(GL_DITHER); - - glDepthFunc(GL_LEQUAL); - glDepthRange(0.0, 1.0); - - glFrontFace(GL_CCW); - glCullFace(GL_BACK); - glDisable(GL_CULL_FACE); - - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); - - /* Is default but better be explicit. */ - glEnable(GL_MULTISAMPLE); - - /* This is a bit dangerous since addons could change this. */ - glEnable(GL_PRIMITIVE_RESTART); - glPrimitiveRestartIndex((GLuint)0xFFFFFFFF); - - /* TODO: Should become default. But needs at least GL 4.3 */ - if (GLEW_ARB_ES3_compatibility) { - /* Takes predecence over GL_PRIMITIVE_RESTART */ - glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX); - } } /** \} */ diff --git a/source/blender/gpu/intern/gpu_state_private.hh b/source/blender/gpu/intern/gpu_state_private.hh index 3324cf6934f..99f580de15e 100644 --- a/source/blender/gpu/intern/gpu_state_private.hh +++ b/source/blender/gpu/intern/gpu_state_private.hh @@ -76,6 +76,13 @@ inline GPUState operator^(const GPUState &a, const GPUState &b) return r; } +inline GPUState operator~(const GPUState &a) +{ + GPUState r; + r.data = ~a.data; + return r; +} + /* Mutable state that does not require pipeline change. */ union GPUStateMutable { struct { @@ -128,12 +135,22 @@ inline GPUStateMutable operator^(const GPUStateMutable &a, const GPUStateMutable return r; } +inline GPUStateMutable operator~(const GPUStateMutable &a) +{ + GPUStateMutable r; + for (int i = 0; i < ARRAY_SIZE(a.data); i++) { + r.data[i] = ~a.data[i]; + } + return r; +} + class GPUStateManager { public: GPUState state; GPUStateMutable mutable_state; public: + GPUStateManager(); virtual ~GPUStateManager(){}; virtual void set_state(const GPUState &state) = 0; diff --git a/source/blender/gpu/opengl/gl_state.cc b/source/blender/gpu/opengl/gl_state.cc index f85fb804317..3e3695e0b48 100644 --- a/source/blender/gpu/opengl/gl_state.cc +++ b/source/blender/gpu/opengl/gl_state.cc @@ -34,6 +34,32 @@ using namespace blender::gpu; /** \name GLStateManager * \{ */ +GLStateManager::GLStateManager(void) : GPUStateManager() +{ + /* Set other states that never change. */ + glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); + glEnable(GL_MULTISAMPLE); + glEnable(GL_PRIMITIVE_RESTART); + + glDisable(GL_DITHER); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + + glPrimitiveRestartIndex((GLuint)0xFFFFFFFF); + /* TODO: Should become default. But needs at least GL 4.3 */ + if (GLEW_ARB_ES3_compatibility) { + /* Takes predecence over GL_PRIMITIVE_RESTART */ + glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX); + } + + /* Force update using default state. */ + current_ = ~state; + current_mutable_ = ~mutable_state; + set_state(state); + set_mutable_state(mutable_state); +} + void GLStateManager::set_state(const GPUState &state) { GPUState changed = state ^ current_; @@ -276,12 +302,12 @@ void GLStateManager::set_facing(const bool invert) void GLStateManager::set_backface_culling(const eGPUFaceCullTest test) { if (test != GPU_CULL_NONE) { - glDisable(GL_CULL_FACE); - } - else { glEnable(GL_CULL_FACE); glCullFace((test == GPU_CULL_FRONT) ? GL_FRONT : GL_BACK); } + else { + glDisable(GL_CULL_FACE); + } } void GLStateManager::set_provoking_vert(const eGPUProvokingVertex vert) diff --git a/source/blender/gpu/opengl/gl_state.hh b/source/blender/gpu/opengl/gl_state.hh index 5b8e687425d..b05fcc5d044 100644 --- a/source/blender/gpu/opengl/gl_state.hh +++ b/source/blender/gpu/opengl/gl_state.hh @@ -38,6 +38,8 @@ class GLStateManager : public GPUStateManager { GPUStateMutable current_mutable_; public: + GLStateManager(); + void set_state(const GPUState &state) override; void set_mutable_state(const GPUStateMutable &state) override; -- cgit v1.2.3