31 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
32 glEnable(GL_MULTISAMPLE);
36 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
37 glPixelStorei(GL_PACK_ALIGNMENT, 1);
38 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
42 glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
45 glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, line_width_range_);
56 this->set_state(this->
state);
58 this->texture_bind_apply();
59 this->image_bind_apply();
68 uint32_t clip_distances = current_.clip_distances;
69 current_ = ~this->state;
70 current_.clip_distances = clip_distances;
71 current_mutable_ = ~this->mutable_state;
72 this->set_state(this->
state);
80 if (changed.blend != 0) {
83 if (changed.write_mask != 0) {
86 if (changed.depth_test != 0) {
89 if (changed.stencil_test != 0 || changed.stencil_op != 0) {
93 if (changed.clip_distances != 0) {
94 set_clip_distances(
state.clip_distances, current_.clip_distances);
96 if (changed.culling_test != 0) {
100 if (changed.invert_facing != 0) {
101 set_facing(
state.invert_facing);
103 if (changed.provoking_vert != 0) {
106 if (changed.clip_control != 0) {
107 set_clip_control(
state.clip_control);
111 if (changed.polygon_smooth) {
112 if (
state.polygon_smooth) {
113 glEnable(GL_POLYGON_SMOOTH);
116 glDisable(GL_POLYGON_SMOOTH);
119 if (changed.line_smooth) {
120 if (
state.line_smooth) {
121 glEnable(GL_LINE_SMOOTH);
124 glDisable(GL_LINE_SMOOTH);
133 GPUStateMutable changed =
state ^ current_mutable_;
137 if (
state.point_size > 0.0f) {
138 glEnable(GL_PROGRAM_POINT_SIZE);
141 glDisable(GL_PROGRAM_POINT_SIZE);
148 glLineWidth(
clamp_f(
state.line_width, line_width_range_[0], line_width_range_[1]));
151 if (changed.stencil_compare_mask != 0 || changed.stencil_reference != 0 ||
152 changed.stencil_write_mask != 0)
157 current_mutable_ =
state;
166void GLStateManager::set_write_mask(
const GPUWriteMask value)
175 glEnable(GL_RASTERIZER_DISCARD);
178 glDisable(GL_RASTERIZER_DISCARD);
182void GLStateManager::set_depth_test(
const GPUDepthTest value)
208 glEnable(GL_DEPTH_TEST);
212 glDisable(GL_DEPTH_TEST);
220 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
223 glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
224 glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR_WRAP);
227 glStencilOpSeparate(GL_BACK, GL_KEEP, GL_DECR_WRAP, GL_KEEP);
228 glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_INCR_WRAP, GL_KEEP);
232 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
236 glEnable(GL_STENCIL_TEST);
239 glDisable(GL_STENCIL_TEST);
259 glStencilFunc(GL_ALWAYS, 0x00, 0x00);
263 glStencilMask(
state.stencil_write_mask);
264 glStencilFunc(func,
state.stencil_reference,
state.stencil_compare_mask);
267void GLStateManager::set_clip_distances(
const int new_dist_len,
const int old_dist_len)
269 for (
int i = 0; i < new_dist_len; i++) {
270 glEnable(GL_CLIP_DISTANCE0 + i);
272 for (
int i = new_dist_len; i < old_dist_len; i++) {
273 glDisable(GL_CLIP_DISTANCE0 + i);
277void GLStateManager::set_facing(
const bool invert)
279 glFrontFace((
invert) ? GL_CW : GL_CCW);
285 glEnable(GL_CULL_FACE);
289 glDisable(GL_CULL_FACE);
296 GL_LAST_VERTEX_CONVENTION;
297 glProvokingVertex(value);
300void GLStateManager::set_clip_control(
const bool enable)
304 glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
307 glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE);
311void GLStateManager::set_blend(
const GPUBlend value)
320 GLenum src_rgb, src_alpha, dst_rgb, dst_alpha;
324 src_rgb = GL_SRC_ALPHA;
325 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
327 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
332 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
334 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
339 src_rgb = GL_SRC_ALPHA;
358 src_rgb = GL_DST_COLOR;
360 src_alpha = GL_DST_ALPHA;
365 src_rgb = GL_ONE_MINUS_DST_COLOR;
375 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
379 src_rgb = GL_ONE_MINUS_DST_ALPHA;
380 dst_rgb = GL_SRC_ALPHA;
382 dst_alpha = GL_SRC_ALPHA;
386 src_rgb = GL_ONE_MINUS_DST_ALPHA;
388 src_alpha = GL_ONE_MINUS_DST_ALPHA;
394 dst_rgb = GL_SRC1_COLOR;
396 dst_alpha = GL_SRC1_ALPHA;
401 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
403 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
408 dst_rgb = GL_SRC_ALPHA;
410 dst_alpha = GL_SRC_ALPHA;
416 glBlendEquation(GL_MIN);
419 glBlendEquation(GL_MAX);
422 glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
425 glBlendEquation(GL_FUNC_ADD);
431 glBlendFuncSeparate(src_rgb, dst_rgb, src_alpha, dst_alpha);
454 if ((textures_[unit] == tex->tex_id_) &&
459 targets_[unit] = tex->target_;
460 textures_[unit] = tex->tex_id_;
462 tex->is_bound_ =
true;
463 dirty_texture_binds_ |= 1ULL << unit;
468 glActiveTexture(GL_TEXTURE0);
469 glBindTexture(tex->target_, tex->tex_id_);
471 dirty_texture_binds_ |= 1ULL;
480 if (!tex->is_bound_) {
484 GLuint tex_id = tex->tex_id_;
485 for (
int i = 0; i <
ARRAY_SIZE(textures_); i++) {
486 if (textures_[i] == tex_id) {
489 dirty_texture_binds_ |= 1ULL << i;
492 tex->is_bound_ =
false;
497 for (
int i = 0; i <
ARRAY_SIZE(textures_); i++) {
498 if (textures_[i] != 0) {
501 dirty_texture_binds_ |= 1ULL << i;
504 this->texture_bind_apply();
507void GLStateManager::texture_bind_apply()
509 if (dirty_texture_binds_ == 0) {
512 uint64_t dirty_bind = dirty_texture_binds_;
513 dirty_texture_binds_ = 0;
517 int count = last - first;
520 glBindTextures(first, count, textures_ + first);
521 glBindSamplers(first, count, samplers_ + first);
524 for (
int unit = first; unit < last; unit++) {
525 if ((dirty_bind >> unit) & 1UL) {
526 glActiveTexture(GL_TEXTURE0 + unit);
527 glBindTexture(targets_[unit], textures_[unit]);
528 glBindSampler(unit, samplers_[unit]);
537 for (
int i = 0; i <
ARRAY_SIZE(textures_); i++) {
538 if (textures_[i] != 0) {
539 bound_slots |= 1ULL << i;
559 images_[unit] = tex->tex_id_;
562 tex->is_bound_image_ =
true;
563 dirty_image_binds_ |= 1ULL << unit;
569 if (!tex->is_bound_image_) {
573 GLuint tex_id = tex->tex_id_;
574 for (
int i = 0; i <
ARRAY_SIZE(images_); i++) {
575 if (images_[i] == tex_id) {
578 dirty_image_binds_ |= 1ULL << i;
581 tex->is_bound_image_ =
false;
586 for (
int i = 0; i <
ARRAY_SIZE(images_); i++) {
587 if (images_[i] != 0) {
589 dirty_image_binds_ |= 1ULL << i;
593 this->image_bind_apply();
596void GLStateManager::image_bind_apply()
598 if (dirty_image_binds_ == 0) {
601 uint32_t dirty_bind = dirty_image_binds_;
602 dirty_image_binds_ = 0;
606 int count = last - first;
609 glBindImageTextures(first, count, images_ + first);
612 for (
int unit = first; unit < last; unit++) {
613 if ((dirty_bind >> unit) & 1UL) {
614 glBindImageTexture(unit, images_[unit], 0, GL_TRUE, 0, GL_READ_WRITE, formats_[unit]);
623 for (
int i = 0; i <
ARRAY_SIZE(images_); i++) {
624 if (images_[i] != 0) {
625 bound_slots |= 1ULL << i;
639 glMemoryBarrier(
to_gl(barrier_bits));
644 if (gl_sync_ !=
nullptr) {
645 glDeleteSync(gl_sync_);
654 glDeleteSync(gl_sync_);
657 gl_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
664 if (gl_sync_ ==
nullptr) {
667 glWaitSync(gl_sync_, 0, GL_TIMEOUT_IGNORED);
static bool multi_bind_support
static bool multi_bind_image_support
void texture_bind_temp(GLTexture *tex)
GLFrameBuffer * active_fb
void image_unbind(Texture *tex) override
void image_bind(Texture *tex, int unit) override
void apply_state() override
void texture_unbind_all() override
uint8_t bound_image_slots()
void issue_barrier(GPUBarrier barrier_bits) override
void force_state() override
uint64_t bound_texture_slots()
void texture_bind(Texture *tex, GPUSamplerState sampler, int unit) override
void image_unbind_all() override
void texture_unbind(Texture *tex) override
static GLuint get_sampler(const GPUSamplerState &sampler_state)
void check_feedback_loop()
GPUStateMutable mutable_state
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats
TextureFormat format_get() const
float2x2 invert(float2x2 mat)
GLenum to_gl_internal_format(TextureFormat format)
GLenum to_gl(eGPUDataFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
@ GPU_STENCIL_OP_COUNT_DEPTH_FAIL
@ GPU_STENCIL_OP_COUNT_DEPTH_PASS
MINLINE unsigned int bitscan_forward_uint(unsigned int a)
MINLINE unsigned int float_as_uint(float f)
@ GPU_BLEND_OVERLAY_MASK_FROM_ALPHA
@ GPU_BLEND_ALPHA_UNDER_PREMUL
@ GPU_BLEND_ADDITIVE_PREMULT
@ GPU_BLEND_ALPHA_PREMULT
@ GPU_DEPTH_GREATER_EQUAL
MINLINE unsigned int bitscan_reverse_uint(unsigned int a)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
MINLINE unsigned int bitscan_forward_uint64(unsigned long long a)
MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a)
MINLINE float clamp_f(float value, float min, float max)
void vert(const int vert_id, float4 &position)