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_);
57 this->set_state(this->
state);
59 this->texture_bind_apply();
60 this->image_bind_apply();
69 uint32_t clip_distances = current_.clip_distances;
70 current_ =
~this->state;
71 current_.clip_distances = clip_distances;
72 current_mutable_ =
~this->mutable_state;
73 this->set_state(this->
state);
81 if (changed.
blend != 0) {
95 set_clip_distances(
state.clip_distances, current_.clip_distances);
101 set_logic_op(
state.logic_op_xor);
104 set_facing(
state.invert_facing);
110 set_shadow_bias(
state.shadow_bias);
113 set_clip_control(
state.clip_control);
118 if (
state.polygon_smooth) {
119 glEnable(GL_POLYGON_SMOOTH);
122 glDisable(GL_POLYGON_SMOOTH);
126 if (
state.line_smooth) {
127 glEnable(GL_LINE_SMOOTH);
130 glDisable(GL_LINE_SMOOTH);
139 GPUStateMutable changed =
state ^ current_mutable_;
143 if (
state.point_size > 0.0f) {
144 glEnable(GL_PROGRAM_POINT_SIZE);
147 glDisable(GL_PROGRAM_POINT_SIZE);
154 glLineWidth(
clamp_f(
state.line_width, line_width_range_[0], line_width_range_[1]));
162 if (changed.stencil_compare_mask != 0 || changed.stencil_reference != 0 ||
163 changed.stencil_write_mask != 0)
168 current_mutable_ =
state;
177void GLStateManager::set_write_mask(
const eGPUWriteMask value)
186 glEnable(GL_RASTERIZER_DISCARD);
189 glDisable(GL_RASTERIZER_DISCARD);
193void GLStateManager::set_depth_test(
const eGPUDepthTest value)
219 glEnable(GL_DEPTH_TEST);
223 glDisable(GL_DEPTH_TEST);
231 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
234 glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
235 glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR_WRAP);
238 glStencilOpSeparate(GL_BACK, GL_KEEP, GL_DECR_WRAP, GL_KEEP);
239 glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_INCR_WRAP, GL_KEEP);
243 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
247 glEnable(GL_STENCIL_TEST);
250 glDisable(GL_STENCIL_TEST);
270 glStencilFunc(GL_ALWAYS, 0x00, 0x00);
274 glStencilMask(
state.stencil_write_mask);
275 glStencilFunc(func,
state.stencil_reference,
state.stencil_compare_mask);
278void GLStateManager::set_clip_distances(
const int new_dist_len,
const int old_dist_len)
280 for (
int i = 0;
i < new_dist_len;
i++) {
281 glEnable(GL_CLIP_DISTANCE0 +
i);
283 for (
int i = new_dist_len;
i < old_dist_len;
i++) {
284 glDisable(GL_CLIP_DISTANCE0 +
i);
288void GLStateManager::set_logic_op(
const bool enable)
291 glEnable(GL_COLOR_LOGIC_OP);
295 glDisable(GL_COLOR_LOGIC_OP);
299void GLStateManager::set_facing(
const bool invert)
301 glFrontFace((
invert) ? GL_CW : GL_CCW);
307 glEnable(GL_CULL_FACE);
311 glDisable(GL_CULL_FACE);
318 GL_LAST_VERTEX_CONVENTION;
319 glProvokingVertex(value);
322void GLStateManager::set_shadow_bias(
const bool enable)
325 glEnable(GL_POLYGON_OFFSET_FILL);
326 glEnable(GL_POLYGON_OFFSET_LINE);
328 glPolygonOffset(2.0f, 1.0f);
331 glDisable(GL_POLYGON_OFFSET_FILL);
332 glDisable(GL_POLYGON_OFFSET_LINE);
336void GLStateManager::set_clip_control(
const bool enable)
341 glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
344 glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE);
349void GLStateManager::set_blend(
const eGPUBlend value)
358 GLenum src_rgb, src_alpha, dst_rgb, dst_alpha;
362 src_rgb = GL_SRC_ALPHA;
363 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
365 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
370 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
372 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
377 src_rgb = GL_SRC_ALPHA;
393 src_rgb = GL_DST_COLOR;
395 src_alpha = GL_DST_ALPHA;
400 src_rgb = GL_ONE_MINUS_DST_COLOR;
410 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
414 src_rgb = GL_ONE_MINUS_DST_ALPHA;
415 dst_rgb = GL_SRC_ALPHA;
417 dst_alpha = GL_SRC_ALPHA;
421 src_rgb = GL_ONE_MINUS_DST_ALPHA;
423 src_alpha = GL_ONE_MINUS_DST_ALPHA;
429 dst_rgb = GL_SRC1_COLOR;
431 dst_alpha = GL_SRC1_ALPHA;
436 dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
438 dst_alpha = GL_ONE_MINUS_SRC_ALPHA;
444 glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
447 glBlendEquation(GL_FUNC_ADD);
453 glBlendFuncSeparate(src_rgb, dst_rgb, src_alpha, dst_alpha);
476 if ((textures_[unit] == tex->tex_id_) &&
481 targets_[unit] = tex->target_;
482 textures_[unit] = tex->tex_id_;
484 tex->is_bound_ =
true;
485 dirty_texture_binds_ |= 1ULL << unit;
490 glActiveTexture(GL_TEXTURE0);
491 glBindTexture(tex->target_, tex->tex_id_);
493 dirty_texture_binds_ |= 1ULL;
502 if (!tex->is_bound_) {
506 GLuint tex_id = tex->tex_id_;
508 if (textures_[
i] == tex_id) {
511 dirty_texture_binds_ |= 1ULL <<
i;
514 tex->is_bound_ =
false;
520 if (textures_[
i] != 0) {
523 dirty_texture_binds_ |= 1ULL <<
i;
526 this->texture_bind_apply();
529void GLStateManager::texture_bind_apply()
531 if (dirty_texture_binds_ == 0) {
534 uint64_t dirty_bind = dirty_texture_binds_;
535 dirty_texture_binds_ = 0;
539 int count = last - first;
542 glBindTextures(first,
count, textures_ + first);
543 glBindSamplers(first,
count, samplers_ + first);
546 for (
int unit = first; unit < last; unit++) {
547 if ((dirty_bind >> unit) & 1UL) {
548 glActiveTexture(GL_TEXTURE0 + unit);
549 glBindTexture(targets_[unit], textures_[unit]);
550 glBindSampler(unit, samplers_[unit]);
558 glPixelStorei(GL_UNPACK_ROW_LENGTH,
len);
565 if (textures_[
i] != 0) {
566 bound_slots |= 1ULL <<
i;
586 images_[unit] = tex->tex_id_;
588 tex->is_bound_image_ =
true;
589 dirty_image_binds_ |= 1ULL << unit;
595 if (!tex->is_bound_image_) {
599 GLuint tex_id = tex->tex_id_;
601 if (images_[
i] == tex_id) {
603 dirty_image_binds_ |= 1ULL <<
i;
606 tex->is_bound_image_ =
false;
612 if (images_[
i] != 0) {
614 dirty_image_binds_ |= 1ULL <<
i;
617 this->image_bind_apply();
620void GLStateManager::image_bind_apply()
622 if (dirty_image_binds_ == 0) {
625 uint32_t dirty_bind = dirty_image_binds_;
626 dirty_image_binds_ = 0;
630 int count = last - first;
633 glBindImageTextures(first,
count, images_ + first);
636 for (
int unit = first; unit < last; unit++) {
637 if ((dirty_bind >> unit) & 1UL) {
638 glBindImageTexture(unit, images_[unit], 0, GL_TRUE, 0, GL_READ_WRITE, formats_[unit]);
646 uint8_t bound_slots = 0;
648 if (images_[
i] != 0) {
649 bound_slots |= 1ULL <<
i;
663 glMemoryBarrier(
to_gl(barrier_bits));
668 if (gl_sync_ !=
nullptr) {
669 glDeleteSync(gl_sync_);
678 glDeleteSync(gl_sync_);
681 gl_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
688 if (gl_sync_ ==
nullptr) {
691 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_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
@ GPU_DEPTH_GREATER_EQUAL
unsigned long long int uint64_t
static bool multi_bind_support
static bool clip_control_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_unpack_row_length_set(uint len) override
void issue_barrier(eGPUBarrier barrier_bits) override
void texture_unbind_all() override
uint8_t bound_image_slots()
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
eGPUTextureFormat format_
CCL_NAMESPACE_BEGIN ccl_device float invert(const float color, const float factor)
GLenum to_gl_internal_format(eGPUTextureFormat format)
static GLenum to_gl(const GPUAttachmentType type)