2018-06-26 15:17:31 -06:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup gpu
|
2018-06-26 15:17:31 -06:00
|
|
|
*/
|
|
|
|
|
2020-03-11 14:52:57 +11:00
|
|
|
#ifndef GPU_STANDALONE
|
|
|
|
# include "DNA_userdef_types.h"
|
|
|
|
# define PIXELSIZE (U.pixelsize)
|
|
|
|
#else
|
|
|
|
# define PIXELSIZE (1.0f)
|
|
|
|
#endif
|
2018-10-30 16:21:44 +01:00
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
#include "BLI_math_vector.h"
|
2019-06-06 10:06:54 +10:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
2019-03-23 23:47:12 +01:00
|
|
|
#include "BKE_global.h"
|
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "GPU_extensions.h"
|
2018-06-26 15:17:31 -06:00
|
|
|
#include "GPU_glew.h"
|
|
|
|
#include "GPU_state.h"
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
#include "gpu_context_private.hh"
|
|
|
|
|
2020-08-16 14:01:07 +02:00
|
|
|
#include "gpu_state_private.hh"
|
|
|
|
|
|
|
|
using namespace blender::gpu;
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
#define SET_STATE(_prefix, _state, _value) \
|
|
|
|
do { \
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateManager *stack = GPU_context_active_get()->state_manager; \
|
|
|
|
auto &state_object = stack->_prefix##state; \
|
2020-08-16 20:56:39 +02:00
|
|
|
state_object._state = _value; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define SET_IMMUTABLE_STATE(_state, _value) SET_STATE(, _state, _value)
|
|
|
|
#define SET_MUTABLE_STATE(_state, _value) SET_STATE(mutable_, _state, _value)
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Immutable state Setters
|
|
|
|
* \{ */
|
|
|
|
|
2020-08-16 15:38:34 +02:00
|
|
|
void GPU_blend(eGPUBlend blend)
|
2018-06-26 15:17:31 -06:00
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_IMMUTABLE_STATE(blend, blend);
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
|
|
|
|
2020-08-16 14:01:07 +02:00
|
|
|
void GPU_face_culling(eGPUFaceCullTest culling)
|
2020-07-17 20:04:37 +02:00
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_IMMUTABLE_STATE(culling_test, culling);
|
2020-07-17 20:04:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-17 20:13:11 +02:00
|
|
|
void GPU_front_facing(bool invert)
|
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_IMMUTABLE_STATE(invert_facing, invert);
|
2020-07-17 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-17 20:26:12 +02:00
|
|
|
void GPU_provoking_vertex(eGPUProvokingVertex vert)
|
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_IMMUTABLE_STATE(provoking_vert, vert);
|
2018-10-30 15:31:32 -03:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
/* TODO explicit depth test. */
|
2018-06-26 15:17:31 -06:00
|
|
|
void GPU_depth_test(bool enable)
|
|
|
|
{
|
2020-08-16 23:20:59 +02:00
|
|
|
SET_IMMUTABLE_STATE(depth_test, (enable) ? GPU_DEPTH_LESS_EQUAL : GPU_DEPTH_NONE);
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_line_smooth(bool enable)
|
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_IMMUTABLE_STATE(line_smooth, enable);
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_polygon_smooth(bool enable)
|
2018-06-26 15:17:31 -06:00
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_IMMUTABLE_STATE(polygon_smooth, enable);
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_logic_op_xor_set(bool enable)
|
2018-06-26 15:17:31 -06:00
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_IMMUTABLE_STATE(logic_op_xor, enable);
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
|
|
|
|
2020-08-16 23:20:59 +02:00
|
|
|
void GPU_write_mask(eGPUWriteMask mask)
|
|
|
|
{
|
|
|
|
SET_IMMUTABLE_STATE(write_mask, mask);
|
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_color_mask(bool r, bool g, bool b, bool a)
|
2018-06-26 15:17:31 -06:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateManager *stack = GPU_context_active_get()->state_manager;
|
|
|
|
auto &state = stack->state;
|
2020-08-16 20:56:39 +02:00
|
|
|
eGPUWriteMask write_mask = state.write_mask;
|
|
|
|
SET_FLAG_FROM_TEST(write_mask, r, GPU_WRITE_RED);
|
|
|
|
SET_FLAG_FROM_TEST(write_mask, g, GPU_WRITE_GREEN);
|
|
|
|
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;
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_depth_mask(bool depth)
|
2019-05-28 17:14:22 +02:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateManager *stack = GPU_context_active_get()->state_manager;
|
|
|
|
auto &state = stack->state;
|
2020-08-16 20:56:39 +02:00
|
|
|
eGPUWriteMask write_mask = state.write_mask;
|
|
|
|
SET_FLAG_FROM_TEST(write_mask, depth, GPU_WRITE_DEPTH);
|
|
|
|
state.write_mask = write_mask;
|
2019-05-28 17:14:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_clip_distances(int distances_enabled)
|
2020-07-17 19:21:33 +02:00
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_IMMUTABLE_STATE(clip_distances, distances_enabled);
|
2020-07-17 19:21:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
/** \} */
|
2018-06-26 15:17:31 -06:00
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Mutable State Setters
|
|
|
|
* \{ */
|
2020-07-17 19:03:30 +02:00
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_depth_range(float near, float far)
|
2018-06-26 15:17:31 -06:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateManager *stack = GPU_context_active_get()->state_manager;
|
|
|
|
auto &state = stack->mutable_state;
|
2020-08-16 20:56:39 +02:00
|
|
|
copy_v2_fl2(state.depth_range, near, far);
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_line_width(float width)
|
2018-06-26 15:17:31 -06:00
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_MUTABLE_STATE(line_width, width * PIXELSIZE);
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_point_size(float size)
|
2018-06-26 15:17:31 -06:00
|
|
|
{
|
2020-08-16 20:56:39 +02:00
|
|
|
SET_MUTABLE_STATE(point_size, size * PIXELSIZE);
|
2018-06-26 15:17:31 -06:00
|
|
|
}
|
2018-10-31 12:28:59 +01:00
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
/* Programmable point size
|
|
|
|
* - shaders set their own point size when enabled
|
|
|
|
* - use glPointSize when disabled */
|
|
|
|
/* TODO remove and use program point size everywhere */
|
|
|
|
void GPU_program_point_size(bool enable)
|
2018-10-31 12:28:59 +01:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateManager *stack = GPU_context_active_get()->state_manager;
|
|
|
|
auto &state = stack->mutable_state;
|
2020-08-16 20:56:39 +02:00
|
|
|
/* Set point size sign negative to disable. */
|
|
|
|
state.point_size = fabsf(state.point_size) * (enable ? 1 : -1);
|
2018-10-31 12:28:59 +01:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_scissor_test(bool enable)
|
2018-10-31 12:28:59 +01:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateManager *stack = GPU_context_active_get()->state_manager;
|
|
|
|
auto &state = stack->mutable_state;
|
2020-08-16 20:56:39 +02:00
|
|
|
/* Set point size sign negative to disable. */
|
|
|
|
state.scissor_rect[2] = abs(state.scissor_rect[2]) * (enable ? 1 : -1);
|
2018-11-04 10:08:55 +11:00
|
|
|
}
|
2019-07-02 12:30:55 +10:00
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_scissor(int x, int y, int width, int height)
|
2020-07-17 18:51:26 +02:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateManager *stack = GPU_context_active_get()->state_manager;
|
|
|
|
auto &state = stack->mutable_state;
|
2020-08-16 20:56:39 +02:00
|
|
|
int scissor_rect[4] = {x, y, width, height};
|
|
|
|
copy_v4_v4_int(state.scissor_rect, scissor_rect);
|
2020-07-17 18:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_viewport(int x, int y, int width, int height)
|
2019-07-02 12:30:55 +10:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateManager *stack = GPU_context_active_get()->state_manager;
|
|
|
|
auto &state = stack->mutable_state;
|
2020-08-16 20:56:39 +02:00
|
|
|
int viewport_rect[4] = {x, y, width, height};
|
|
|
|
copy_v4_v4_int(state.viewport_rect, viewport_rect);
|
2019-07-02 12:30:55 +10:00
|
|
|
}
|
2020-03-11 14:52:57 +11:00
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name State Getters
|
|
|
|
* \{ */
|
|
|
|
|
2020-08-16 23:20:59 +02:00
|
|
|
eGPUBlend GPU_blend_get()
|
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUState &state = GPU_context_active_get()->state_manager->state;
|
2020-08-16 23:20:59 +02:00
|
|
|
return state.blend;
|
|
|
|
}
|
|
|
|
|
|
|
|
eGPUWriteMask GPU_write_mask_get()
|
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUState &state = GPU_context_active_get()->state_manager->state;
|
2020-08-16 23:20:59 +02:00
|
|
|
return state.write_mask;
|
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
bool GPU_depth_test_enabled()
|
2020-07-16 04:16:10 +02:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUState &state = GPU_context_active_get()->state_manager->state;
|
2020-08-16 20:56:39 +02:00
|
|
|
return state.depth_test != GPU_DEPTH_NONE;
|
2020-07-16 04:16:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_scissor_get(int coords[4])
|
2020-07-16 04:16:10 +02:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateMutable &state = GPU_context_active_get()->state_manager->mutable_state;
|
2020-08-16 20:56:39 +02:00
|
|
|
copy_v4_v4_int(coords, state.scissor_rect);
|
2020-07-16 04:16:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_viewport_size_get_f(float coords[4])
|
2020-07-16 04:16:10 +02:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateMutable &state = GPU_context_active_get()->state_manager->mutable_state;
|
2020-08-16 20:56:39 +02:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
coords[i] = state.viewport_rect[i];
|
|
|
|
}
|
2020-07-16 04:16:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
void GPU_viewport_size_get_i(int coords[4])
|
2020-07-16 04:16:10 +02:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUStateMutable &state = GPU_context_active_get()->state_manager->mutable_state;
|
2020-08-16 20:56:39 +02:00
|
|
|
copy_v4_v4_int(coords, state.viewport_rect);
|
2020-07-16 04:16:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
bool GPU_depth_mask_get(void)
|
2020-07-17 23:00:55 +02:00
|
|
|
{
|
2020-08-17 00:34:06 +02:00
|
|
|
GPUState &state = GPU_context_active_get()->state_manager->state;
|
2020-08-16 20:56:39 +02:00
|
|
|
return (state.write_mask & GPU_WRITE_DEPTH) != 0;
|
2020-07-17 23:00:55 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 18:13:19 +02:00
|
|
|
bool GPU_mipmap_enabled(void)
|
|
|
|
{
|
|
|
|
/* TODO(fclem) this used to be a userdef option. */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-16 20:56:39 +02:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Context Utils
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
void GPU_flush(void)
|
|
|
|
{
|
|
|
|
glFlush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_finish(void)
|
|
|
|
{
|
|
|
|
glFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPU_unpack_row_length_set(uint len)
|
|
|
|
{
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
2020-08-16 14:01:07 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
2020-08-16 23:20:59 +02:00
|
|
|
/** \name Default OpenGL State
|
2020-05-15 14:29:27 +02:00
|
|
|
*
|
|
|
|
* This is called on startup, for opengl offscreen render.
|
|
|
|
* Generally we should always return to this state when
|
|
|
|
* temporarily modifying the state for drawing, though that are (undocumented)
|
2020-08-16 23:20:59 +02:00
|
|
|
* exceptions that we should try to get rid of.
|
|
|
|
* \{ */
|
2020-05-15 14:29:27 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-07-17 15:58:33 +02:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
2020-07-17 18:51:26 +02:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
2020-07-17 15:58:33 +02:00
|
|
|
|
2020-05-15 14:29:27 +02:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 14:52:57 +11:00
|
|
|
/** \} */
|