Blender V5.3
gpu_state.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifndef GPU_STANDALONE
10# include "DNA_userdef_types.h"
11# define PIXELSIZE (U.pixelsize)
12#else
13# define PIXELSIZE (1.0f)
14#endif
15
16#include "BLI_math_vector_c.hh"
17#include "BLI_utildefines.hh"
18
19#include "GPU_state.hh"
20
21#include "gpu_backend.hh"
23
24#include "gpu_state_private.hh"
25
26namespace blender {
27
28using namespace blender::gpu;
29
30#define SET_STATE(_prefix, _state, _value) \
31 do { \
32 StateManager *stack = Context::get()->state_manager; \
33 auto &state_object = stack->_prefix##state; \
34 state_object._state = (_value); \
35 } while (0)
36
37#define SET_IMMUTABLE_STATE(_state, _value) SET_STATE(, _state, _value)
38#define SET_MUTABLE_STATE(_state, _value) SET_STATE(mutable_, _state, _value)
39
40/* -------------------------------------------------------------------- */
43
45{
46 SET_IMMUTABLE_STATE(blend, blend);
47}
48
50{
51 SET_IMMUTABLE_STATE(culling_test, culling);
52}
53
59
61{
62 SET_IMMUTABLE_STATE(invert_facing, invert);
63}
64
66{
67 SET_IMMUTABLE_STATE(provoking_vert, vert);
68}
69
71{
72 SET_IMMUTABLE_STATE(depth_test, test);
73}
74
76{
77 SET_IMMUTABLE_STATE(stencil_test, test);
78}
79
80void GPU_line_smooth(bool enable)
81{
82 SET_IMMUTABLE_STATE(line_smooth, enable);
83}
84
85void GPU_polygon_smooth(bool enable)
86{
87 SET_IMMUTABLE_STATE(polygon_smooth, enable);
88}
89
94
95void GPU_color_mask(bool r, bool g, bool b, bool a)
96{
98 auto &state = stack->state;
99 uint32_t write_mask = state.write_mask;
104 state.write_mask = write_mask;
105}
106
107void GPU_depth_mask(bool depth)
108{
110 auto &state = stack->state;
111 uint32_t write_mask = state.write_mask;
112 SET_FLAG_FROM_TEST(write_mask, depth, uint32_t(GPU_WRITE_DEPTH));
113 state.write_mask = write_mask;
114}
115
116void GPU_clip_distances(int distances_enabled)
117{
118 SET_IMMUTABLE_STATE(clip_distances, distances_enabled);
119}
120
122 GPUBlend blend,
123 GPUFaceCullTest culling_test,
124 GPUDepthTest depth_test,
125 GPUStencilTest stencil_test,
126 GPUStencilOp stencil_op,
127 GPUProvokingVertex provoking_vert)
128{
130 auto &state = stack->state;
131 state.write_mask = uint32_t(write_mask);
132 state.blend = uint32_t(blend);
133 state.culling_test = uint32_t(culling_test);
134 state.depth_test = uint32_t(depth_test);
135 state.stencil_test = uint32_t(stencil_test);
136 state.stencil_op = uint32_t(stencil_op);
137 state.provoking_vert = uint32_t(provoking_vert);
138}
139
141{
143}
144
146
147/* -------------------------------------------------------------------- */
150
151void GPU_line_width(float width)
152{
153 width = max_ff(1.0f, width * PIXELSIZE);
154 SET_MUTABLE_STATE(line_width, width);
155}
156
157void GPU_point_size(float size)
158{
160 auto &state = stack->mutable_state;
161 /* Keep the sign of point_size since it represents the enable state. */
162 state.point_size = size * ((state.point_size > 0.0) ? 1.0f : -1.0f);
163}
164
165void GPU_program_point_size(bool enable)
166{
168 auto &state = stack->mutable_state;
169 /* Set point size sign negative to disable. */
170 state.point_size = fabsf(state.point_size) * (enable ? 1 : -1);
171}
172
173void GPU_scissor_test(bool enable)
174{
176}
177
178void GPU_scissor(int x, int y, int width, int height)
179{
180 int scissor_rect[4] = {x, y, width, height};
181 Context::get()->active_fb->scissor_set(scissor_rect);
182}
183
184void GPU_viewport(int x, int y, int width, int height)
185{
186 int viewport_rect[4] = {x, y, width, height};
187 Context::get()->active_fb->viewport_set(viewport_rect);
188}
189
191{
192 SET_MUTABLE_STATE(stencil_reference, uint8_t(reference));
193}
194
196{
197 SET_MUTABLE_STATE(stencil_write_mask, uint8_t(write_mask));
198}
199
201{
202 SET_MUTABLE_STATE(stencil_compare_mask, uint8_t(compare_mask));
203}
204
206
207/* -------------------------------------------------------------------- */
210
216
222
224{
226 return state.stencil_write_mask;
227}
228
234
240
242{
244 return state.line_width;
245}
246
248{
250 return bool(state.line_smooth);
251}
252
253void GPU_scissor_get(int coords[4])
254{
256}
257
258void GPU_viewport_size_get_f(float coords[4])
259{
260 int viewport[4];
261 Context::get()->active_fb->viewport_get(viewport);
262 for (int i = 0; i < 4; i++) {
263 coords[i] = viewport[i];
264 }
265}
266
267void GPU_viewport_size_get_i(int coords[4])
268{
270}
271
273{
275 return (state.write_mask & GPU_WRITE_DEPTH) != 0;
276}
277
279
280/* -------------------------------------------------------------------- */
283
285{
286 Context::get()->flush();
287}
288
290{
291 Context::get()->finish();
292}
293
298
300
301/* -------------------------------------------------------------------- */
304
309
311{
312 Fence *fence = GPUBackend::get()->fence_alloc();
313 return wrap(fence);
314}
315
316void GPU_fence_free(GPUFence *fence)
317{
318 delete unwrap(fence);
319}
320
321void GPU_fence_signal(GPUFence *fence)
322{
323 unwrap(fence)->signal();
324}
325
326void GPU_fence_wait(GPUFence *fence)
327{
328 unwrap(fence)->wait();
329}
330
332
333/* -------------------------------------------------------------------- */
336
338{
339 /* Set default state. */
340 state.write_mask = GPU_WRITE_COLOR;
341 state.blend = GPU_BLEND_NONE;
342 state.culling_test = GPU_CULL_NONE;
343 state.depth_test = GPU_DEPTH_NONE;
344 state.stencil_test = GPU_STENCIL_NONE;
345 state.stencil_op = GPU_STENCIL_OP_NONE;
346 state.provoking_vert = GPU_VERTEX_LAST;
347
348 state.invert_facing = false;
349 state.clip_distances = 0;
350 state.clip_control = false;
351 state.polygon_smooth = false;
352 state.line_smooth = false;
353
354 mutable_state.point_size = -1.0f; /* Negative is not using point size. */
355 mutable_state.line_width = 1.0f;
356 mutable_state.stencil_write_mask = 0x00;
357 mutable_state.stencil_compare_mask = 0x00;
358 mutable_state.stencil_reference = 0x00;
359
361}
362
364
365} // namespace blender
#define SET_FLAG_FROM_TEST(value, test, flag)
static Context * get()
virtual void finish()=0
virtual void flush()=0
void viewport_set(const int viewport[4])
void scissor_set(const int scissor[4])
void scissor_get(int r_scissor[4]) const
void viewport_get(int r_viewport[4]) const
static GPUBackend * get()
virtual Fence * fence_alloc()=0
virtual void issue_barrier(GPUBarrier barrier_bits)=0
virtual void apply_state()=0
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats
#define clip_control
void barrier()
float2x2 invert(float2x2 mat)
#define SET_MUTABLE_STATE(_state, _value)
Definition gpu_state.cc:38
#define PIXELSIZE
Definition gpu_state.cc:11
#define SET_IMMUTABLE_STATE(_state, _value)
Definition gpu_state.cc:37
const int state
BLI_INLINE MTLTexture * unwrap(Texture *tex)
BLI_INLINE Texture * wrap(MTLTexture *texture)
BMesh Iterators.
void GPU_provoking_vertex(GPUProvokingVertex vert)
Definition gpu_state.cc:65
void GPU_scissor_test(bool enable)
Definition gpu_state.cc:173
@ GPU_CULL_NONE
Definition GPU_state.hh:157
@ GPU_STENCIL_OP_NONE
Definition GPU_state.hh:149
void GPU_fence_free(GPUFence *fence)
Definition gpu_state.cc:316
void GPU_state_set(GPUWriteMask write_mask, GPUBlend blend, GPUFaceCullTest culling_test, GPUDepthTest depth_test, GPUStencilTest stencil_test, GPUStencilOp stencil_op, GPUProvokingVertex provoking_vert)
Definition gpu_state.cc:121
GPUWriteMask GPU_write_mask_get()
Definition gpu_state.cc:217
unsigned int uint
Definition BKE_text.h:55
void GPU_polygon_smooth(bool enable)
Definition gpu_state.cc:85
void GPU_clip_distances(int distances_enabled)
Definition gpu_state.cc:116
GPUDepthTest GPU_depth_test_get()
Definition gpu_state.cc:229
GPUStencilTest GPU_stencil_test_get()
Definition gpu_state.cc:235
void GPU_point_size(float size)
Definition gpu_state.cc:157
void GPU_line_smooth(bool enable)
Definition gpu_state.cc:80
void GPU_depth_mask(bool depth)
Definition gpu_state.cc:107
void GPU_fence_wait(GPUFence *fence)
Definition gpu_state.cc:326
GPUFaceCullTest GPU_face_culling_get()
Definition gpu_state.cc:54
void GPU_apply_state()
Definition gpu_state.cc:294
void GPU_line_width(float width)
Definition gpu_state.cc:151
void GPU_stencil_reference_set(uint reference)
Definition gpu_state.cc:190
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
GPUFence * GPU_fence_create()
Definition gpu_state.cc:310
float GPU_line_width_get()
Definition gpu_state.cc:241
unsigned int uint32_t
Definition BKE_text.h:81
@ GPU_DEPTH_NONE
Definition GPU_state.hh:132
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
uint GPU_stencil_mask_get()
Definition gpu_state.cc:223
void GPU_scissor_get(int coords[4])
Definition gpu_state.cc:253
bool GPU_depth_mask_get()
Definition gpu_state.cc:272
unsigned char uint8_t
Definition BKE_text.h:79
void GPU_color_mask(bool r, bool g, bool b, bool a)
Definition gpu_state.cc:95
void GPU_stencil_test(GPUStencilTest test)
Definition gpu_state.cc:75
void GPU_program_point_size(bool enable)
Definition gpu_state.cc:165
@ GPU_STENCIL_NONE
Definition GPU_state.hh:142
void GPU_scissor(int x, int y, int width, int height)
Definition gpu_state.cc:178
void GPU_flush()
Definition gpu_state.cc:284
bool GPU_line_smooth_get()
Definition gpu_state.cc:247
GPUBlend GPU_blend_get()
Definition gpu_state.cc:211
MINLINE float max_ff(float a, float b)
@ GPU_WRITE_ALPHA
Definition GPU_state.hh:24
@ GPU_WRITE_COLOR
Definition GPU_state.hh:27
@ GPU_WRITE_RED
Definition GPU_state.hh:21
@ GPU_WRITE_BLUE
Definition GPU_state.hh:23
@ GPU_WRITE_GREEN
Definition GPU_state.hh:22
@ GPU_WRITE_DEPTH
Definition GPU_state.hh:25
void GPU_viewport(int x, int y, int width, int height)
Definition gpu_state.cc:184
void GPU_clip_control_unit_range(bool enable)
Definition gpu_state.cc:140
void GPU_viewport_size_get_i(int coords[4])
Definition gpu_state.cc:267
void GPU_face_culling(GPUFaceCullTest culling)
Definition gpu_state.cc:49
GPUProvokingVertex
Definition GPU_state.hh:162
@ GPU_VERTEX_LAST
Definition GPU_state.hh:163
void GPU_front_facing(bool invert)
Definition gpu_state.cc:60
void GPU_stencil_write_mask_set(uint write_mask)
Definition gpu_state.cc:195
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void GPU_fence_signal(GPUFence *fence)
Definition gpu_state.cc:321
void GPU_finish()
Definition gpu_state.cc:289
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
void GPU_stencil_compare_mask_set(uint compare_mask)
Definition gpu_state.cc:200
void GPU_write_mask(GPUWriteMask mask)
Definition gpu_state.cc:90
#define fabsf