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) {
94 set_clip_distances(
state.clip_distances, current_.clip_distances);
100 set_logic_op(
state.logic_op_xor);
103 set_facing(
state.invert_facing);
109 set_shadow_bias(
state.shadow_bias);
112 set_clip_control(
state.clip_control);
117 if (
state.polygon_smooth) {
118 glEnable(GL_POLYGON_SMOOTH);
121 glDisable(GL_POLYGON_SMOOTH);
125 if (
state.line_smooth) {
126 glEnable(GL_LINE_SMOOTH);
129 glDisable(GL_LINE_SMOOTH);
138 GPUStateMutable changed =
state ^ current_mutable_;
142 if (
state.point_size > 0.0f) {
143 glEnable(GL_PROGRAM_POINT_SIZE);
146 glDisable(GL_PROGRAM_POINT_SIZE);
153 glLineWidth(
clamp_f(
state.line_width, line_width_range_[0], line_width_range_[1]));
161 if (changed.stencil_compare_mask != 0 || changed.stencil_reference != 0 ||
162 changed.stencil_write_mask != 0)
167 current_mutable_ =
state;
176void GLStateManager::set_write_mask(
const GPUWriteMask value)
185 glEnable(GL_RASTERIZER_DISCARD);
188 glDisable(GL_RASTERIZER_DISCARD);
192void GLStateManager::set_depth_test(
const GPUDepthTest value)
218 glEnable(GL_DEPTH_TEST);
222 glDisable(GL_DEPTH_TEST);
230 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
233 glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
234 glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR_WRAP);
237 glStencilOpSeparate(GL_BACK, GL_KEEP, GL_DECR_WRAP, GL_KEEP);
238 glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_INCR_WRAP, GL_KEEP);
242 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
246 glEnable(GL_STENCIL_TEST);
249 glDisable(GL_STENCIL_TEST);
269 glStencilFunc(GL_ALWAYS, 0x00, 0x00);
273 glStencilMask(
state.stencil_write_mask);
274 glStencilFunc(func,
state.stencil_reference,
state.stencil_compare_mask);
277void GLStateManager::set_clip_distances(
const int new_dist_len,
const int old_dist_len)
279 for (
int i = 0;
i < new_dist_len;
i++) {
280 glEnable(GL_CLIP_DISTANCE0 +
i);
282 for (
int i = new_dist_len;
i < old_dist_len;
i++) {
283 glDisable(GL_CLIP_DISTANCE0 +
i);
287void GLStateManager::set_logic_op(
const bool enable)
290 glEnable(GL_COLOR_LOGIC_OP);
294 glDisable(GL_COLOR_LOGIC_OP);
298void GLStateManager::set_facing(
const bool invert)
300 glFrontFace((
invert) ? GL_CW : GL_CCW);
306 glEnable(GL_CULL_FACE);
310 glDisable(GL_CULL_FACE);
317 GL_LAST_VERTEX_CONVENTION;
318 glProvokingVertex(value);
321void GLStateManager::set_shadow_bias(
const bool enable)
324 glEnable(GL_POLYGON_OFFSET_FILL);
325 glEnable(GL_POLYGON_OFFSET_LINE);
327 glPolygonOffset(2.0f, 1.0f);
330 glDisable(GL_POLYGON_OFFSET_FILL);
331 glDisable(GL_POLYGON_OFFSET_LINE);
335void GLStateManager::set_clip_control(
const bool enable)
339 glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
342 glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE);
346void GLStateManager::set_blend(
const GPUBlend value)
355 GLenum src_rgb, src_alpha, dst_rgb, dst_alpha;
359 src_rgb = GL_SRC_ALPHA;
360 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
362 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
367 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
369 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
374 src_rgb = GL_SRC_ALPHA;
390 src_rgb = GL_DST_COLOR;
392 src_alpha = GL_DST_ALPHA;
397 src_rgb = GL_ONE_MINUS_DST_COLOR;
407 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
411 src_rgb = GL_ONE_MINUS_DST_ALPHA;
412 dst_rgb = GL_SRC_ALPHA;
414 dst_alpha = GL_SRC_ALPHA;
418 src_rgb = GL_ONE_MINUS_DST_ALPHA;
420 src_alpha = GL_ONE_MINUS_DST_ALPHA;
426 dst_rgb = GL_SRC1_COLOR;
428 dst_alpha = GL_SRC1_ALPHA;
433 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
435 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
441 glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
444 glBlendEquation(GL_FUNC_ADD);
450 glBlendFuncSeparate(src_rgb, dst_rgb, src_alpha, dst_alpha);
473 if ((textures_[unit] == tex->tex_id_) &&
478 targets_[unit] = tex->target_;
479 textures_[unit] = tex->tex_id_;
481 tex->is_bound_ =
true;
482 dirty_texture_binds_ |= 1ULL << unit;
487 glActiveTexture(GL_TEXTURE0);
488 glBindTexture(tex->target_, tex->tex_id_);
490 dirty_texture_binds_ |= 1ULL;
499 if (!tex->is_bound_) {
503 GLuint tex_id = tex->tex_id_;
505 if (textures_[
i] == tex_id) {
508 dirty_texture_binds_ |= 1ULL <<
i;
511 tex->is_bound_ =
false;
517 if (textures_[
i] != 0) {
520 dirty_texture_binds_ |= 1ULL <<
i;
523 this->texture_bind_apply();
526void GLStateManager::texture_bind_apply()
528 if (dirty_texture_binds_ == 0) {
531 uint64_t dirty_bind = dirty_texture_binds_;
532 dirty_texture_binds_ = 0;
536 int count = last - first;
539 glBindTextures(first,
count, textures_ + first);
540 glBindSamplers(first,
count, samplers_ + first);
543 for (
int unit = first; unit < last; unit++) {
544 if ((dirty_bind >> unit) & 1UL) {
545 glActiveTexture(GL_TEXTURE0 + unit);
546 glBindTexture(targets_[unit], textures_[unit]);
547 glBindSampler(unit, samplers_[unit]);
555 texture_unpack_row_length_ =
len;
560 return texture_unpack_row_length_;
567 if (textures_[
i] != 0) {
568 bound_slots |= 1ULL <<
i;
588 images_[unit] = tex->tex_id_;
591 tex->is_bound_image_ =
true;
592 dirty_image_binds_ |= 1ULL << unit;
598 if (!tex->is_bound_image_) {
602 GLuint tex_id = tex->tex_id_;
604 if (images_[
i] == tex_id) {
607 dirty_image_binds_ |= 1ULL <<
i;
610 tex->is_bound_image_ =
false;
616 if (images_[
i] != 0) {
618 dirty_image_binds_ |= 1ULL <<
i;
622 this->image_bind_apply();
625void GLStateManager::image_bind_apply()
627 if (dirty_image_binds_ == 0) {
630 uint32_t dirty_bind = dirty_image_binds_;
631 dirty_image_binds_ = 0;
635 int count = last - first;
638 glBindImageTextures(first,
count, images_ + first);
641 for (
int unit = first; unit < last; unit++) {
642 if ((dirty_bind >> unit) & 1UL) {
643 glBindImageTexture(unit, images_[unit], 0, GL_TRUE, 0, GL_READ_WRITE, formats_[unit]);
651 uint8_t bound_slots = 0;
653 if (images_[
i] != 0) {
654 bound_slots |= 1ULL <<
i;
668 glMemoryBarrier(
to_gl(barrier_bits));
673 if (gl_sync_ !=
nullptr) {
674 glDeleteSync(gl_sync_);
683 glDeleteSync(gl_sync_);
686 gl_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
693 if (gl_sync_ ==
nullptr) {
696 glWaitSync(gl_sync_, 0, GL_TIMEOUT_IGNORED);
MINLINE float clamp_f(float value, float min, float max)
MINLINE unsigned int bitscan_forward_uint(unsigned int a)
MINLINE unsigned int float_as_uint(float f)
MINLINE unsigned int bitscan_reverse_uint(unsigned int a)
MINLINE unsigned int bitscan_forward_uint64(unsigned long long a)
MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a)
@ GPU_DEPTH_GREATER_EQUAL
@ GPU_BLEND_ADDITIVE_PREMULT
@ GPU_BLEND_ALPHA_UNDER_PREMUL
@ GPU_BLEND_ALPHA_PREMULT
@ GPU_BLEND_OVERLAY_MASK_FROM_ALPHA
@ GPU_STENCIL_OP_COUNT_DEPTH_FAIL
@ GPU_STENCIL_OP_COUNT_DEPTH_PASS
unsigned long long int uint64_t
static bool multi_bind_support
static bool multi_bind_image_support
void texture_bind_temp(GLTexture *tex)
GLFrameBuffer * active_fb
uint texture_unpack_row_length_get() const
void image_unbind(Texture *tex) override
void image_bind(Texture *tex, int unit) override
void apply_state() override
void texture_unpack_row_length_set(uint len) 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
CCL_NAMESPACE_BEGIN ccl_device float invert(const float color, const float factor)
GLenum to_gl_internal_format(TextureFormat format)
static GLenum to_gl(const GPUAttachmentType type)