Blender V5.3
gpu_select_sample_query.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <cstdlib>
13
14#include "GPU_debug.hh"
15#include "GPU_framebuffer.hh"
16#include "GPU_select.hh"
17#include "GPU_state.hh"
18
19#include "BLI_rect.hh"
20
21#include "BLI_utildefines.hh"
22#include "BLI_vector.hh"
23
24#include "gpu_backend.hh"
25#include "gpu_query.hh"
26
27#include "gpu_select_private.hh"
28
29namespace blender {
30
31using namespace blender::gpu;
32
53
55
57 const rcti *input,
58 const GPUSelectMode mode,
59 int oldhits)
60{
61 GPU_debug_group_begin("Selection Queries");
62
64 g_query_state.buffer = buffer;
65 g_query_state.mode = mode;
67 g_query_state.oldhits = oldhits;
68
72
77
78 /* Write to color buffer. Seems to fix issues with selecting alpha blended geom (see #7997). */
79 GPU_color_mask(true, true, true, true);
80
81 /* In order to save some fill rate we minimize the viewport using rect.
82 * We need to get the region of the viewport so that our geometry doesn't
83 * get rejected before the depth test. Should probably cull rect against
84 * the viewport but this is a rare case I think */
85
86 const int viewport[4] = {
88
89 GPU_viewport(UNPACK4(viewport));
90 GPU_scissor(UNPACK4(viewport));
91 GPU_scissor_test(false);
92
93 /* occlusion queries operates on fragments that pass tests and since we are interested on all
94 * objects in the view frustum independently of their order, we need to disable the depth test */
95 if (mode == GPU_SELECT_ALL) {
96 /* #glQueries on Windows+Intel drivers only works with depth testing turned on.
97 * See #62947 for details */
99 GPU_depth_mask(true);
100 }
101 else if (mode == GPU_SELECT_NEAREST_FIRST_PASS) {
103 GPU_depth_mask(true);
104 GPU_clear_depth(1.0f);
105 }
106 else if (mode == GPU_SELECT_NEAREST_SECOND_PASS) {
108 GPU_depth_mask(false);
109 }
110}
111
113{
116 }
117
121
123 /* Second pass should never run if first pass fails,
124 * can read past `buffer_len` in this case. */
129 return true;
130 }
131 return false;
132 }
133 }
134 return true;
135}
136
138{
139 uint hits = 0;
140
143 }
144
148
149 for (int i = 0; i < result.size(); i++) {
150 if (result[i] != 0) {
152 GPUSelectResult hit_result{};
153 hit_result.id = ids[i];
154 hit_result.depth = 0xFFFF;
155 g_query_state.buffer->storage.append(hit_result);
156 hits++;
157 }
158 else {
159 int j;
160 /* search in buffer and make selected object first */
161 for (j = 0; j < g_query_state.oldhits; j++) {
162 if (g_query_state.buffer->storage[j].id == ids[i]) {
163 g_query_state.buffer->storage[j].depth = 0;
164 }
165 }
166 break;
167 }
168 }
169 }
170
171 delete g_query_state.queries;
172 delete g_query_state.ids;
173
177
179
180 return hits;
181}
182
183} // namespace blender
#define UNPACK2(a)
#define UNPACK4(a)
constexpr int64_t size() const
Definition BLI_span.hh:256
void append(const T &value)
virtual QueryPool * querypool_alloc()=0
static GPUBackend * get()
virtual void begin_query()=0
virtual void init(GPUQueryType type)=0
virtual void get_occlusion_result(MutableSpan< uint32_t > r_values)=0
virtual void end_query()=0
void append(const T &value)
@ GPU_QUERY_OCCLUSION
Definition gpu_query.hh:18
BMesh Iterators.
static GPUSelectQueryState g_query_state
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_scissor_test(bool enable)
Definition gpu_state.cc:173
GPUWriteMask GPU_write_mask_get()
Definition gpu_state.cc:217
unsigned int uint
Definition BKE_text.h:55
GPUDepthTest GPU_depth_test_get()
Definition gpu_state.cc:229
@ GPU_SELECT_NEAREST_SECOND_PASS
Definition GPU_select.hh:25
@ GPU_SELECT_ALL
Definition GPU_select.hh:22
@ GPU_SELECT_NEAREST_FIRST_PASS
Definition GPU_select.hh:24
void GPU_clear_depth(float depth)
bool gpu_select_query_load_id(uint id)
void GPU_depth_mask(bool depth)
Definition gpu_state.cc:107
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
@ GPU_DEPTH_LESS_EQUAL
Definition GPU_state.hh:135
@ GPU_DEPTH_ALWAYS
Definition GPU_state.hh:133
@ GPU_DEPTH_EQUAL
Definition GPU_state.hh:136
void GPU_scissor_get(int coords[4])
Definition gpu_state.cc:253
void GPU_color_mask(bool r, bool g, bool b, bool a)
Definition gpu_state.cc:95
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void GPU_debug_group_end()
Definition gpu_debug.cc:34
void GPU_scissor(int x, int y, int width, int height)
Definition gpu_state.cc:178
void gpu_select_query_begin(GPUSelectBuffer *buffer, const rcti *input, GPUSelectMode mode, int oldhits)
void GPU_viewport(int x, int y, int width, int height)
Definition gpu_state.cc:184
void GPU_viewport_size_get_i(int coords[4])
Definition gpu_state.cc:267
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
void GPU_write_mask(GPUWriteMask mask)
Definition gpu_state.cc:90
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
GPUSelectStorage storage
Definition GPU_select.hh:49
Vector< uint, QUERY_MIN_LEN > * ids