From 365fb36108b986509c38f188177755a2f971a292 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Sun, 1 May 2022 02:30:32 +0200 Subject: LibGL: Split up GLContext implementation into logical units This allows for faster rebuilds when the implementation changes. --- Userland/Libraries/LibGL/CMakeLists.txt | 7 + Userland/Libraries/LibGL/ContextParameter.cpp | 535 ++++ Userland/Libraries/LibGL/GLContext.cpp | 3956 +++++-------------------- Userland/Libraries/LibGL/GLContext.h | 35 + Userland/Libraries/LibGL/Lighting.cpp | 539 ++++ Userland/Libraries/LibGL/Lists.cpp | 164 + Userland/Libraries/LibGL/Matrix.cpp | 171 ++ Userland/Libraries/LibGL/Stencil.cpp | 160 + Userland/Libraries/LibGL/Textures.cpp | 666 +++++ Userland/Libraries/LibGL/Vertex.cpp | 336 +++ 10 files changed, 3335 insertions(+), 3234 deletions(-) create mode 100644 Userland/Libraries/LibGL/ContextParameter.cpp create mode 100644 Userland/Libraries/LibGL/Lighting.cpp create mode 100644 Userland/Libraries/LibGL/Lists.cpp create mode 100644 Userland/Libraries/LibGL/Matrix.cpp create mode 100644 Userland/Libraries/LibGL/Stencil.cpp create mode 100644 Userland/Libraries/LibGL/Textures.cpp create mode 100644 Userland/Libraries/LibGL/Vertex.cpp (limited to 'Userland') diff --git a/Userland/Libraries/LibGL/CMakeLists.txt b/Userland/Libraries/LibGL/CMakeLists.txt index b0347cbb4a..e4bfda9114 100644 --- a/Userland/Libraries/LibGL/CMakeLists.txt +++ b/Userland/Libraries/LibGL/CMakeLists.txt @@ -1,8 +1,15 @@ set(SOURCES + ContextParameter.cpp GLAPI.cpp GLContext.cpp + Matrix.cpp + Lighting.cpp + Lists.cpp + Stencil.cpp Tex/NameAllocator.cpp Tex/Texture2D.cpp + Textures.cpp + Vertex.cpp ) serenity_lib(LibGL gl) diff --git a/Userland/Libraries/LibGL/ContextParameter.cpp b/Userland/Libraries/LibGL/ContextParameter.cpp new file mode 100644 index 0000000000..758760de58 --- /dev/null +++ b/Userland/Libraries/LibGL/ContextParameter.cpp @@ -0,0 +1,535 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * Copyright (c) 2021, Stephan Unverwerth + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace GL { + +Optional GLContext::get_context_parameter(GLenum name) +{ + switch (name) { + case GL_ALPHA_BITS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; + case GL_ALPHA_TEST: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_alpha_test_enabled } }; + case GL_BLEND: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_blend_enabled } }; + case GL_BLEND_DST_ALPHA: + return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_blend_destination_factor) } }; + case GL_BLEND_SRC_ALPHA: + return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_blend_source_factor) } }; + case GL_BLUE_BITS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; + case GL_COLOR_MATERIAL: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_color_material_enabled } }; + case GL_COLOR_MATERIAL_FACE: + return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_color_material_face) } }; + case GL_COLOR_MATERIAL_MODE: + return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_color_material_mode) } }; + case GL_CULL_FACE: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_cull_faces } }; + case GL_DEPTH_BITS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; + case GL_DEPTH_TEST: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_test_enabled } }; + case GL_DITHER: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_dither_enabled } }; + case GL_DOUBLEBUFFER: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = true } }; + case GL_FOG: { + auto fog_enabled = m_rasterizer->options().fog_enabled; + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = fog_enabled } }; + } + case GL_GREEN_BITS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; + case GL_LIGHTING: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_lighting_enabled } }; + case GL_MAX_LIGHTS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_device_info.num_lights) } }; + case GL_MAX_MODELVIEW_STACK_DEPTH: + return ContextParameter { .type = GL_INT, .value = { .integer_value = MODELVIEW_MATRIX_STACK_LIMIT } }; + case GL_MAX_PROJECTION_STACK_DEPTH: + return ContextParameter { .type = GL_INT, .value = { .integer_value = PROJECTION_MATRIX_STACK_LIMIT } }; + case GL_MAX_TEXTURE_SIZE: + return ContextParameter { .type = GL_INT, .value = { .integer_value = 4096 } }; + case GL_MAX_TEXTURE_STACK_DEPTH: + return ContextParameter { .type = GL_INT, .value = { .integer_value = TEXTURE_MATRIX_STACK_LIMIT } }; + case GL_MAX_TEXTURE_UNITS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_texture_units.size()) } }; + case GL_NORMALIZE: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_normalize } }; + case GL_PACK_ALIGNMENT: + return ContextParameter { .type = GL_INT, .value = { .integer_value = m_pack_alignment } }; + case GL_PACK_IMAGE_HEIGHT: + return ContextParameter { .type = GL_BOOL, .value = { .integer_value = 0 } }; + case GL_PACK_LSB_FIRST: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } }; + case GL_PACK_ROW_LENGTH: + return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; + case GL_PACK_SKIP_PIXELS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; + case GL_PACK_SKIP_ROWS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; + case GL_PACK_SWAP_BYTES: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } }; + case GL_POLYGON_OFFSET_FILL: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_offset_enabled } }; + case GL_RED_BITS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; + case GL_SCISSOR_BOX: { + auto scissor_box = m_rasterizer->options().scissor_box; + return ContextParameter { + .type = GL_INT, + .count = 4, + .value = { + .integer_list = { + scissor_box.x(), + scissor_box.y(), + scissor_box.width(), + scissor_box.height(), + } } + }; + } break; + case GL_SCISSOR_TEST: { + auto scissor_enabled = m_rasterizer->options().scissor_enabled; + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = scissor_enabled } }; + } + case GL_STENCIL_BITS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = m_device_info.stencil_bits } }; + case GL_STENCIL_CLEAR_VALUE: + return ContextParameter { .type = GL_INT, .value = { .integer_value = m_clear_stencil } }; + case GL_STENCIL_TEST: + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_stencil_test_enabled } }; + case GL_TEXTURE_1D: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_1d_enabled() } }; + case GL_TEXTURE_2D: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_2d_enabled() } }; + case GL_TEXTURE_3D: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_3d_enabled() } }; + case GL_TEXTURE_CUBE_MAP: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_cube_map_enabled() } }; + case GL_TEXTURE_GEN_Q: + case GL_TEXTURE_GEN_R: + case GL_TEXTURE_GEN_S: + case GL_TEXTURE_GEN_T: { + auto generation_enabled = texture_coordinate_generation(m_active_texture_unit_index, name).enabled; + return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = generation_enabled } }; + } + case GL_UNPACK_ALIGNMENT: + return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_alignment } }; + case GL_UNPACK_IMAGE_HEIGHT: + return ContextParameter { .type = GL_BOOL, .value = { .integer_value = 0 } }; + case GL_UNPACK_LSB_FIRST: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } }; + case GL_UNPACK_ROW_LENGTH: + return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_row_length } }; + case GL_UNPACK_SKIP_PIXELS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; + case GL_UNPACK_SKIP_ROWS: + return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; + case GL_UNPACK_SWAP_BYTES: + return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } }; + case GL_VIEWPORT: + return ContextParameter { + .type = GL_INT, + .count = 4, + .value = { + .integer_list = { + m_viewport.x(), + m_viewport.y(), + m_viewport.width(), + m_viewport.height(), + } } + }; + default: + dbgln_if(GL_DEBUG, "get_context_parameter({:#x}): unknown context parameter", name); + return {}; + } +} + +void GLContext::gl_disable(GLenum capability) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_disable, capability); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + auto rasterizer_options = m_rasterizer->options(); + bool update_rasterizer_options = false; + + switch (capability) { + case GL_COLOR_MATERIAL: + m_color_material_enabled = false; + break; + case GL_CULL_FACE: + m_cull_faces = false; + rasterizer_options.enable_culling = false; + update_rasterizer_options = true; + break; + case GL_DEPTH_TEST: + m_depth_test_enabled = false; + rasterizer_options.enable_depth_test = false; + update_rasterizer_options = true; + break; + case GL_BLEND: + m_blend_enabled = false; + rasterizer_options.enable_blending = false; + update_rasterizer_options = true; + break; + case GL_ALPHA_TEST: + m_alpha_test_enabled = false; + rasterizer_options.enable_alpha_test = false; + update_rasterizer_options = true; + break; + case GL_DITHER: + m_dither_enabled = false; + break; + case GL_FOG: + rasterizer_options.fog_enabled = false; + update_rasterizer_options = true; + break; + case GL_LIGHTING: + m_lighting_enabled = false; + rasterizer_options.lighting_enabled = false; + update_rasterizer_options = true; + break; + case GL_LIGHT0: + case GL_LIGHT1: + case GL_LIGHT2: + case GL_LIGHT3: + case GL_LIGHT4: + case GL_LIGHT5: + case GL_LIGHT6: + case GL_LIGHT7: + m_light_states.at(capability - GL_LIGHT0).is_enabled = false; + m_light_state_is_dirty = true; + break; + case GL_NORMALIZE: + m_normalize = false; + rasterizer_options.normalization_enabled = false; + update_rasterizer_options = true; + break; + case GL_POLYGON_OFFSET_FILL: + m_depth_offset_enabled = false; + rasterizer_options.depth_offset_enabled = false; + update_rasterizer_options = true; + break; + case GL_SCISSOR_TEST: + rasterizer_options.scissor_enabled = false; + update_rasterizer_options = true; + break; + case GL_STENCIL_TEST: + m_stencil_test_enabled = false; + rasterizer_options.enable_stencil_test = false; + update_rasterizer_options = true; + break; + case GL_TEXTURE_1D: + m_active_texture_unit->set_texture_1d_enabled(false); + m_sampler_config_is_dirty = true; + break; + case GL_TEXTURE_2D: + m_active_texture_unit->set_texture_2d_enabled(false); + m_sampler_config_is_dirty = true; + break; + case GL_TEXTURE_3D: + m_active_texture_unit->set_texture_3d_enabled(false); + m_sampler_config_is_dirty = true; + break; + case GL_TEXTURE_CUBE_MAP: + m_active_texture_unit->set_texture_cube_map_enabled(false); + m_sampler_config_is_dirty = true; + break; + case GL_TEXTURE_GEN_Q: + case GL_TEXTURE_GEN_R: + case GL_TEXTURE_GEN_S: + case GL_TEXTURE_GEN_T: + texture_coordinate_generation(m_active_texture_unit_index, capability).enabled = false; + m_texcoord_generation_dirty = true; + break; + default: + dbgln_if(GL_DEBUG, "gl_disable({:#x}): unknown parameter", capability); + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); + } + + if (update_rasterizer_options) + m_rasterizer->set_options(rasterizer_options); +} + +void GLContext::gl_disable_client_state(GLenum cap) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + switch (cap) { + case GL_COLOR_ARRAY: + m_client_side_color_array_enabled = false; + break; + case GL_NORMAL_ARRAY: + m_client_side_normal_array_enabled = false; + break; + case GL_TEXTURE_COORD_ARRAY: + m_client_side_texture_coord_array_enabled[m_client_active_texture] = false; + break; + case GL_VERTEX_ARRAY: + m_client_side_vertex_array_enabled = false; + break; + default: + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); + } +} + +void GLContext::gl_enable(GLenum capability) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_enable, capability); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + auto rasterizer_options = m_rasterizer->options(); + bool update_rasterizer_options = false; + + switch (capability) { + case GL_COLOR_MATERIAL: + m_color_material_enabled = true; + break; + case GL_CULL_FACE: + m_cull_faces = true; + rasterizer_options.enable_culling = true; + update_rasterizer_options = true; + break; + case GL_DEPTH_TEST: + m_depth_test_enabled = true; + rasterizer_options.enable_depth_test = true; + update_rasterizer_options = true; + break; + case GL_BLEND: + m_blend_enabled = true; + rasterizer_options.enable_blending = true; + update_rasterizer_options = true; + break; + case GL_ALPHA_TEST: + m_alpha_test_enabled = true; + rasterizer_options.enable_alpha_test = true; + update_rasterizer_options = true; + break; + case GL_DITHER: + m_dither_enabled = true; + break; + case GL_FOG: + rasterizer_options.fog_enabled = true; + update_rasterizer_options = true; + break; + case GL_LIGHTING: + m_lighting_enabled = true; + rasterizer_options.lighting_enabled = true; + update_rasterizer_options = true; + break; + case GL_NORMALIZE: + m_normalize = true; + rasterizer_options.normalization_enabled = true; + update_rasterizer_options = true; + break; + case GL_POLYGON_OFFSET_FILL: + m_depth_offset_enabled = true; + rasterizer_options.depth_offset_enabled = true; + update_rasterizer_options = true; + break; + case GL_SCISSOR_TEST: + rasterizer_options.scissor_enabled = true; + update_rasterizer_options = true; + break; + case GL_STENCIL_TEST: + m_stencil_test_enabled = true; + rasterizer_options.enable_stencil_test = true; + update_rasterizer_options = true; + break; + case GL_TEXTURE_1D: + m_active_texture_unit->set_texture_1d_enabled(true); + m_sampler_config_is_dirty = true; + break; + case GL_TEXTURE_2D: + m_active_texture_unit->set_texture_2d_enabled(true); + m_sampler_config_is_dirty = true; + break; + case GL_TEXTURE_3D: + m_active_texture_unit->set_texture_3d_enabled(true); + m_sampler_config_is_dirty = true; + break; + case GL_TEXTURE_CUBE_MAP: + m_active_texture_unit->set_texture_cube_map_enabled(true); + m_sampler_config_is_dirty = true; + break; + case GL_LIGHT0: + case GL_LIGHT1: + case GL_LIGHT2: + case GL_LIGHT3: + case GL_LIGHT4: + case GL_LIGHT5: + case GL_LIGHT6: + case GL_LIGHT7: + m_light_states.at(capability - GL_LIGHT0).is_enabled = true; + m_light_state_is_dirty = true; + break; + case GL_TEXTURE_GEN_Q: + case GL_TEXTURE_GEN_R: + case GL_TEXTURE_GEN_S: + case GL_TEXTURE_GEN_T: + texture_coordinate_generation(m_active_texture_unit_index, capability).enabled = true; + m_texcoord_generation_dirty = true; + break; + default: + dbgln_if(GL_DEBUG, "gl_enable({:#x}): unknown parameter", capability); + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); + } + + if (update_rasterizer_options) + m_rasterizer->set_options(rasterizer_options); +} + +void GLContext::gl_enable_client_state(GLenum cap) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + switch (cap) { + case GL_COLOR_ARRAY: + m_client_side_color_array_enabled = true; + break; + case GL_NORMAL_ARRAY: + m_client_side_normal_array_enabled = true; + break; + case GL_TEXTURE_COORD_ARRAY: + m_client_side_texture_coord_array_enabled[m_client_active_texture] = true; + break; + case GL_VERTEX_ARRAY: + m_client_side_vertex_array_enabled = true; + break; + default: + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); + } +} + +void GLContext::gl_get_booleanv(GLenum pname, GLboolean* data) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + auto optional_parameter = get_context_parameter(pname); + RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM); + auto parameter = optional_parameter.release_value(); + + switch (parameter.type) { + case GL_BOOL: + *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE; + break; + case GL_DOUBLE: + *data = (parameter.value.double_value == 0.0) ? GL_FALSE : GL_TRUE; + break; + case GL_INT: + *data = (parameter.value.integer_value == 0) ? GL_FALSE : GL_TRUE; + break; + default: + VERIFY_NOT_REACHED(); + } +} + +void GLContext::gl_get_doublev(GLenum pname, GLdouble* params) +{ + get_floating_point(pname, params); +} + +template +void GLContext::get_floating_point(GLenum pname, T* params) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // Handle matrix retrieval first + auto flatten_and_assign_matrix = [¶ms](FloatMatrix4x4 const& matrix) { + auto elements = matrix.elements(); + for (size_t i = 0; i < 4; ++i) { + for (size_t j = 0; j < 4; ++j) { + // Return transposed matrix since OpenGL defines them as column-major + params[i * 4 + j] = static_cast(elements[j][i]); + } + } + }; + switch (pname) { + case GL_MODELVIEW_MATRIX: + flatten_and_assign_matrix(m_model_view_matrix); + return; + case GL_PROJECTION_MATRIX: + flatten_and_assign_matrix(m_projection_matrix); + return; + } + + // Regular parameters + auto optional_parameter = get_context_parameter(pname); + RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM); + auto parameter = optional_parameter.release_value(); + + switch (parameter.type) { + case GL_BOOL: + *params = parameter.value.boolean_value ? GL_TRUE : GL_FALSE; + break; + case GL_DOUBLE: + for (size_t i = 0; i < parameter.count; ++i) + params[i] = parameter.value.double_list[i]; + break; + case GL_INT: + for (size_t i = 0; i < parameter.count; ++i) + params[i] = parameter.value.integer_list[i]; + break; + default: + VERIFY_NOT_REACHED(); + } +} + +void GLContext::gl_get_floatv(GLenum pname, GLfloat* params) +{ + get_floating_point(pname, params); +} + +void GLContext::gl_get_integerv(GLenum pname, GLint* data) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + auto optional_parameter = get_context_parameter(pname); + RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM); + auto parameter = optional_parameter.release_value(); + + switch (parameter.type) { + case GL_BOOL: + *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE; + break; + case GL_DOUBLE: { + double const int_range = static_cast(NumericLimits::max()) - NumericLimits::min(); + for (size_t i = 0; i < parameter.count; ++i) { + double const result_factor = (clamp(parameter.value.double_list[i], -1.0, 1.0) + 1.0) / 2.0; + data[i] = static_cast(NumericLimits::min() + result_factor * int_range); + } + break; + } + case GL_INT: + for (size_t i = 0; i < parameter.count; ++i) + data[i] = parameter.value.integer_list[i]; + break; + default: + VERIFY_NOT_REACHED(); + } +} + +GLboolean GLContext::gl_is_enabled(GLenum capability) +{ + RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0); + + auto optional_parameter = get_context_parameter(capability); + RETURN_VALUE_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM, 0); + + auto parameter = optional_parameter.release_value(); + RETURN_VALUE_WITH_ERROR_IF(!parameter.is_capability, GL_INVALID_ENUM, 0); + + return parameter.value.boolean_value; +} + +} diff --git a/Userland/Libraries/LibGL/GLContext.cpp b/Userland/Libraries/LibGL/GLContext.cpp index 4618383689..77f70936cc 100644 --- a/Userland/Libraries/LibGL/GLContext.cpp +++ b/Userland/Libraries/LibGL/GLContext.cpp @@ -9,58 +9,18 @@ #include #include #include -#include -#include -#include -#include #include #include #include #include #include #include -#include -#include +#include __attribute__((visibility("hidden"))) GL::GLContext* g_gl_context; namespace GL { -static constexpr size_t MODELVIEW_MATRIX_STACK_LIMIT = 64; -static constexpr size_t PROJECTION_MATRIX_STACK_LIMIT = 8; -static constexpr size_t TEXTURE_MATRIX_STACK_LIMIT = 8; - -#define APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(name, ...) \ - if (should_append_to_listing()) { \ - append_to_listing<&GLContext::name>(__VA_ARGS__); \ - if (!should_execute_after_appending_to_listing()) \ - return; \ - } - -#define APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(name, arg) \ - if (should_append_to_listing()) { \ - auto ptr = store_in_listing(arg); \ - append_to_listing<&GLContext::name>(*ptr); \ - if (!should_execute_after_appending_to_listing()) \ - return; \ - } - -#define RETURN_WITH_ERROR_IF(condition, error) \ - if (condition) { \ - dbgln_if(GL_DEBUG, "{}(): error {:#x}", __func__, error); \ - if (m_error == GL_NO_ERROR) \ - m_error = error; \ - return; \ - } - -#define RETURN_VALUE_WITH_ERROR_IF(condition, error, return_value) \ - if (condition) { \ - dbgln_if(GL_DEBUG, "{}(): error {:#x}", __func__, error); \ - if (m_error == GL_NO_ERROR) \ - m_error = error; \ - return return_value; \ - } - GLContext::GLContext(RefPtr driver, NonnullOwnPtr device, Gfx::Bitmap& frontbuffer) : m_viewport { frontbuffer.rect() } , m_frontbuffer { frontbuffer } @@ -120,148 +80,6 @@ GLContext::~GLContext() make_context_current(nullptr); } -Optional GLContext::get_context_parameter(GLenum name) -{ - switch (name) { - case GL_ALPHA_BITS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; - case GL_ALPHA_TEST: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_alpha_test_enabled } }; - case GL_BLEND: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_blend_enabled } }; - case GL_BLEND_DST_ALPHA: - return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_blend_destination_factor) } }; - case GL_BLEND_SRC_ALPHA: - return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_blend_source_factor) } }; - case GL_BLUE_BITS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; - case GL_COLOR_MATERIAL: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_color_material_enabled } }; - case GL_COLOR_MATERIAL_FACE: - return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_color_material_face) } }; - case GL_COLOR_MATERIAL_MODE: - return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_color_material_mode) } }; - case GL_CULL_FACE: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_cull_faces } }; - case GL_DEPTH_BITS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; - case GL_DEPTH_TEST: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_test_enabled } }; - case GL_DITHER: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_dither_enabled } }; - case GL_DOUBLEBUFFER: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = true } }; - case GL_FOG: { - auto fog_enabled = m_rasterizer->options().fog_enabled; - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = fog_enabled } }; - } - case GL_GREEN_BITS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; - case GL_LIGHTING: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_lighting_enabled } }; - case GL_MAX_LIGHTS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_device_info.num_lights) } }; - case GL_MAX_MODELVIEW_STACK_DEPTH: - return ContextParameter { .type = GL_INT, .value = { .integer_value = MODELVIEW_MATRIX_STACK_LIMIT } }; - case GL_MAX_PROJECTION_STACK_DEPTH: - return ContextParameter { .type = GL_INT, .value = { .integer_value = PROJECTION_MATRIX_STACK_LIMIT } }; - case GL_MAX_TEXTURE_SIZE: - return ContextParameter { .type = GL_INT, .value = { .integer_value = 4096 } }; - case GL_MAX_TEXTURE_STACK_DEPTH: - return ContextParameter { .type = GL_INT, .value = { .integer_value = TEXTURE_MATRIX_STACK_LIMIT } }; - case GL_MAX_TEXTURE_UNITS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = static_cast(m_texture_units.size()) } }; - case GL_NORMALIZE: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_normalize } }; - case GL_PACK_ALIGNMENT: - return ContextParameter { .type = GL_INT, .value = { .integer_value = m_pack_alignment } }; - case GL_PACK_IMAGE_HEIGHT: - return ContextParameter { .type = GL_BOOL, .value = { .integer_value = 0 } }; - case GL_PACK_LSB_FIRST: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } }; - case GL_PACK_ROW_LENGTH: - return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; - case GL_PACK_SKIP_PIXELS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; - case GL_PACK_SKIP_ROWS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; - case GL_PACK_SWAP_BYTES: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } }; - case GL_POLYGON_OFFSET_FILL: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_depth_offset_enabled } }; - case GL_RED_BITS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = sizeof(float) * 8 } }; - case GL_SCISSOR_BOX: { - auto scissor_box = m_rasterizer->options().scissor_box; - return ContextParameter { - .type = GL_INT, - .count = 4, - .value = { - .integer_list = { - scissor_box.x(), - scissor_box.y(), - scissor_box.width(), - scissor_box.height(), - } } - }; - } break; - case GL_SCISSOR_TEST: { - auto scissor_enabled = m_rasterizer->options().scissor_enabled; - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = scissor_enabled } }; - } - case GL_STENCIL_BITS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = m_device_info.stencil_bits } }; - case GL_STENCIL_CLEAR_VALUE: - return ContextParameter { .type = GL_INT, .value = { .integer_value = m_clear_stencil } }; - case GL_STENCIL_TEST: - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = m_stencil_test_enabled } }; - case GL_TEXTURE_1D: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_1d_enabled() } }; - case GL_TEXTURE_2D: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_2d_enabled() } }; - case GL_TEXTURE_3D: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_3d_enabled() } }; - case GL_TEXTURE_CUBE_MAP: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = m_active_texture_unit->texture_cube_map_enabled() } }; - case GL_TEXTURE_GEN_Q: - case GL_TEXTURE_GEN_R: - case GL_TEXTURE_GEN_S: - case GL_TEXTURE_GEN_T: { - auto generation_enabled = texture_coordinate_generation(m_active_texture_unit_index, name).enabled; - return ContextParameter { .type = GL_BOOL, .is_capability = true, .value = { .boolean_value = generation_enabled } }; - } - case GL_UNPACK_ALIGNMENT: - return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_alignment } }; - case GL_UNPACK_IMAGE_HEIGHT: - return ContextParameter { .type = GL_BOOL, .value = { .integer_value = 0 } }; - case GL_UNPACK_LSB_FIRST: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } }; - case GL_UNPACK_ROW_LENGTH: - return ContextParameter { .type = GL_INT, .value = { .integer_value = m_unpack_row_length } }; - case GL_UNPACK_SKIP_PIXELS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; - case GL_UNPACK_SKIP_ROWS: - return ContextParameter { .type = GL_INT, .value = { .integer_value = 0 } }; - case GL_UNPACK_SWAP_BYTES: - return ContextParameter { .type = GL_BOOL, .value = { .boolean_value = false } }; - case GL_VIEWPORT: - return ContextParameter { - .type = GL_INT, - .count = 4, - .value = { - .integer_list = { - m_viewport.x(), - m_viewport.y(), - m_viewport.width(), - m_viewport.height(), - } } - }; - default: - dbgln_if(GL_DEBUG, "get_context_parameter({:#x}): unknown context parameter", name); - return {}; - } -} - void GLContext::gl_begin(GLenum mode) { APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_begin, mode); @@ -309,27 +127,6 @@ void GLContext::gl_clear_depth(GLdouble depth) m_clear_depth = clamp(static_cast(depth), 0.f, 1.f); } -void GLContext::gl_clear_stencil(GLint s) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_stencil, s); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - m_clear_stencil = static_cast(s & ((1 << m_device_info.stencil_bits) - 1)); -} - -void GLContext::gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color, r, g, b, a); - - m_current_vertex_color = { - static_cast(r), - static_cast(g), - static_cast(b), - static_cast(a), - }; -} - void GLContext::gl_end() { APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_end); @@ -385,52 +182,6 @@ void GLContext::gl_end() m_vertex_list.clear_with_capacity(); } -void GLContext::gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_frustum, left, right, bottom, top, near_val, far_val); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(near_val < 0 || far_val < 0, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE); - - // Let's do some math! - auto a = static_cast((right + left) / (right - left)); - auto b = static_cast((top + bottom) / (top - bottom)); - auto c = static_cast(-((far_val + near_val) / (far_val - near_val))); - auto d = static_cast(-((2 * far_val * near_val) / (far_val - near_val))); - - FloatMatrix4x4 frustum { - static_cast(2 * near_val / (right - left)), 0, a, 0, - 0, static_cast(2 * near_val / (top - bottom)), b, 0, - 0, 0, c, d, - 0, 0, -1, 0 - }; - *m_current_matrix = *m_current_matrix * frustum; -} - -void GLContext::gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_ortho, left, right, bottom, top, near_val, far_val); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE); - - auto rl = right - left; - auto tb = top - bottom; - auto fn = far_val - near_val; - auto tx = -(right + left) / rl; - auto ty = -(top + bottom) / tb; - auto tz = -(far_val + near_val) / fn; - - FloatMatrix4x4 projection { - static_cast(2 / rl), 0, 0, static_cast(tx), - 0, static_cast(2 / tb), 0, static_cast(ty), - 0, 0, static_cast(-2 / fn), static_cast(tz), - 0, 0, 0, 1 - }; - *m_current_matrix = *m_current_matrix * projection; -} - GLenum GLContext::gl_get_error() { if (m_in_draw_state) @@ -464,3273 +215,1010 @@ GLubyte* GLContext::gl_get_string(GLenum name) RETURN_VALUE_WITH_ERROR_IF(true, GL_INVALID_ENUM, nullptr); } -void GLContext::gl_load_identity() -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_load_identity); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - *m_current_matrix = FloatMatrix4x4::identity(); -} - -void GLContext::gl_load_matrix(FloatMatrix4x4 const& matrix) +void GLContext::gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height) { - APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_load_matrix, matrix); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - *m_current_matrix = matrix; -} + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_viewport, x, y, width, height); -void GLContext::gl_matrix_mode(GLenum mode) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_matrix_mode, mode); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(mode < GL_MODELVIEW || mode > GL_TEXTURE, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE); - m_current_matrix_mode = mode; - switch (mode) { - case GL_MODELVIEW: - m_current_matrix = &m_model_view_matrix; - m_current_matrix_stack = &m_model_view_matrix_stack; - break; - case GL_PROJECTION: - m_current_matrix = &m_projection_matrix; - m_current_matrix_stack = &m_projection_matrix_stack; - break; - case GL_TEXTURE: - m_current_matrix = &m_texture_matrix; - m_current_matrix_stack = &m_texture_matrix_stack; - break; - default: - VERIFY_NOT_REACHED(); - } -} + m_viewport = { x, y, width, height }; -static constexpr size_t matrix_stack_limit(GLenum matrix_mode) -{ - switch (matrix_mode) { - case GL_MODELVIEW: - return MODELVIEW_MATRIX_STACK_LIMIT; - case GL_PROJECTION: - return PROJECTION_MATRIX_STACK_LIMIT; - case GL_TEXTURE: - return TEXTURE_MATRIX_STACK_LIMIT; - } - VERIFY_NOT_REACHED(); + auto rasterizer_options = m_rasterizer->options(); + rasterizer_options.viewport = m_viewport; + m_rasterizer->set_options(rasterizer_options); } -void GLContext::gl_push_matrix() +void GLContext::gl_front_face(GLenum face) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_matrix); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF((*m_current_matrix_stack).size() >= matrix_stack_limit(m_current_matrix_mode), GL_STACK_OVERFLOW); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_front_face, face); - (*m_current_matrix_stack).append(*m_current_matrix); -} + RETURN_WITH_ERROR_IF(face < GL_CW || face > GL_CCW, GL_INVALID_ENUM); -void GLContext::gl_pop_matrix() -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_matrix); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF((*m_current_matrix_stack).is_empty(), GL_STACK_UNDERFLOW); + m_front_face = face; - *m_current_matrix = (*m_current_matrix_stack).take_last(); + auto rasterizer_options = m_rasterizer->options(); + rasterizer_options.front_face = (face == GL_CW) ? GPU::WindingOrder::Clockwise : GPU::WindingOrder::CounterClockwise; + m_rasterizer->set_options(rasterizer_options); } -void GLContext::gl_mult_matrix(FloatMatrix4x4 const& matrix) +void GLContext::gl_cull_face(GLenum cull_mode) { - APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_mult_matrix, matrix); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_cull_face, cull_mode); - *m_current_matrix = *m_current_matrix * matrix; -} + RETURN_WITH_ERROR_IF(cull_mode < GL_FRONT || cull_mode > GL_FRONT_AND_BACK, GL_INVALID_ENUM); -void GLContext::gl_rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rotate, angle, x, y, z); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + m_culled_sides = cull_mode; - FloatVector3 axis = { x, y, z }; - if (axis.length() > 0.f) - axis.normalize(); - auto rotation_mat = Gfx::rotation_matrix(axis, angle * static_cast(M_PI * 2 / 360)); - *m_current_matrix = *m_current_matrix * rotation_mat; + auto rasterizer_options = m_rasterizer->options(); + rasterizer_options.cull_back = cull_mode == GL_BACK || cull_mode == GL_FRONT_AND_BACK; + rasterizer_options.cull_front = cull_mode == GL_FRONT || cull_mode == GL_FRONT_AND_BACK; + m_rasterizer->set_options(rasterizer_options); } -void GLContext::gl_scale(GLdouble x, GLdouble y, GLdouble z) +void GLContext::gl_flush() { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scale, x, y, z); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - auto scale_matrix = Gfx::scale_matrix(FloatVector3 { static_cast(x), static_cast(y), static_cast(z) }); - *m_current_matrix = *m_current_matrix * scale_matrix; + // No-op since GLContext is completely synchronous at the moment } -void GLContext::gl_translate(GLdouble x, GLdouble y, GLdouble z) +void GLContext::gl_finish() { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_translate, x, y, z); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - auto translation_matrix = Gfx::translation_matrix(FloatVector3 { static_cast(x), static_cast(y), static_cast(z) }); - *m_current_matrix = *m_current_matrix * translation_matrix; + // No-op since GLContext is completely synchronous at the moment } -void GLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) +void GLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_vertex, x, y, z, w); - - GPU::Vertex vertex; + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor); - vertex.position = { static_cast(x), static_cast(y), static_cast(z), static_cast(w) }; - vertex.color = m_current_vertex_color; - for (size_t i = 0; i < m_device_info.num_texture_units; ++i) - vertex.tex_coords[i] = m_current_vertex_tex_coord[i]; - vertex.normal = m_current_vertex_normal; + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - m_vertex_list.append(vertex); -} + // FIXME: The list of allowed enums differs between API versions + // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc -void GLContext::gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_coord, s, t, r, q); + RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO + || src_factor == GL_ONE + || src_factor == GL_SRC_COLOR + || src_factor == GL_ONE_MINUS_SRC_COLOR + || src_factor == GL_DST_COLOR + || src_factor == GL_ONE_MINUS_DST_COLOR + || src_factor == GL_SRC_ALPHA + || src_factor == GL_ONE_MINUS_SRC_ALPHA + || src_factor == GL_DST_ALPHA + || src_factor == GL_ONE_MINUS_DST_ALPHA + || src_factor == GL_CONSTANT_COLOR + || src_factor == GL_ONE_MINUS_CONSTANT_COLOR + || src_factor == GL_CONSTANT_ALPHA + || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA + || src_factor == GL_SRC_ALPHA_SATURATE), + GL_INVALID_ENUM); - m_current_vertex_tex_coord[0] = { s, t, r, q }; -} + RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO + || dst_factor == GL_ONE + || dst_factor == GL_SRC_COLOR + || dst_factor == GL_ONE_MINUS_SRC_COLOR + || dst_factor == GL_DST_COLOR + || dst_factor == GL_ONE_MINUS_DST_COLOR + || dst_factor == GL_SRC_ALPHA + || dst_factor == GL_ONE_MINUS_SRC_ALPHA + || dst_factor == GL_DST_ALPHA + || dst_factor == GL_ONE_MINUS_DST_ALPHA + || dst_factor == GL_CONSTANT_COLOR + || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR + || dst_factor == GL_CONSTANT_ALPHA + || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA), + GL_INVALID_ENUM); -void GLContext::gl_multi_tex_coord(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_multi_tex_coord, target, s, t, r, q); + m_blend_source_factor = src_factor; + m_blend_destination_factor = dst_factor; - RETURN_WITH_ERROR_IF(target < GL_TEXTURE0 || target >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM); + auto map_gl_blend_factor_to_device = [](GLenum factor) constexpr + { + switch (factor) { + case GL_ZERO: + return GPU::BlendFactor::Zero; + case GL_ONE: + return GPU::BlendFactor::One; + case GL_SRC_ALPHA: + return GPU::BlendFactor::SrcAlpha; + case GL_ONE_MINUS_SRC_ALPHA: + return GPU::BlendFactor::OneMinusSrcAlpha; + case GL_SRC_COLOR: + return GPU::BlendFactor::SrcColor; + case GL_ONE_MINUS_SRC_COLOR: + return GPU::BlendFactor::OneMinusSrcColor; + case GL_DST_ALPHA: + return GPU::BlendFactor::DstAlpha; + case GL_ONE_MINUS_DST_ALPHA: + return GPU::BlendFactor::OneMinusDstAlpha; + case GL_DST_COLOR: + return GPU::BlendFactor::DstColor; + case GL_ONE_MINUS_DST_COLOR: + return GPU::BlendFactor::OneMinusDstColor; + case GL_SRC_ALPHA_SATURATE: + return GPU::BlendFactor::SrcAlphaSaturate; + default: + VERIFY_NOT_REACHED(); + } + }; - m_current_vertex_tex_coord[target - GL_TEXTURE0] = { s, t, r, q }; + auto options = m_rasterizer->options(); + options.blend_source_factor = map_gl_blend_factor_to_device(m_blend_source_factor); + options.blend_destination_factor = map_gl_blend_factor_to_device(m_blend_destination_factor); + m_rasterizer->set_options(options); } -void GLContext::gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height) +void GLContext::gl_alpha_func(GLenum func, GLclampf ref) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_viewport, x, y, width, height); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_alpha_func, func, ref); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE); - - m_viewport = { x, y, width, height }; - - auto rasterizer_options = m_rasterizer->options(); - rasterizer_options.viewport = m_viewport; - m_rasterizer->set_options(rasterizer_options); -} - -void GLContext::gl_enable(GLenum capability) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_enable, capability); + RETURN_WITH_ERROR_IF(func < GL_NEVER || func > GL_ALWAYS, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + m_alpha_test_func = func; + m_alpha_test_ref_value = ref; - auto rasterizer_options = m_rasterizer->options(); - bool update_rasterizer_options = false; + auto options = m_rasterizer->options(); - switch (capability) { - case GL_COLOR_MATERIAL: - m_color_material_enabled = true; - break; - case GL_CULL_FACE: - m_cull_faces = true; - rasterizer_options.enable_culling = true; - update_rasterizer_options = true; - break; - case GL_DEPTH_TEST: - m_depth_test_enabled = true; - rasterizer_options.enable_depth_test = true; - update_rasterizer_options = true; - break; - case GL_BLEND: - m_blend_enabled = true; - rasterizer_options.enable_blending = true; - update_rasterizer_options = true; - break; - case GL_ALPHA_TEST: - m_alpha_test_enabled = true; - rasterizer_options.enable_alpha_test = true; - update_rasterizer_options = true; - break; - case GL_DITHER: - m_dither_enabled = true; - break; - case GL_FOG: - rasterizer_options.fog_enabled = true; - update_rasterizer_options = true; - break; - case GL_LIGHTING: - m_lighting_enabled = true; - rasterizer_options.lighting_enabled = true; - update_rasterizer_options = true; - break; - case GL_NORMALIZE: - m_normalize = true; - rasterizer_options.normalization_enabled = true; - update_rasterizer_options = true; - break; - case GL_POLYGON_OFFSET_FILL: - m_depth_offset_enabled = true; - rasterizer_options.depth_offset_enabled = true; - update_rasterizer_options = true; - break; - case GL_SCISSOR_TEST: - rasterizer_options.scissor_enabled = true; - update_rasterizer_options = true; + switch (func) { + case GL_NEVER: + options.alpha_test_func = GPU::AlphaTestFunction::Never; break; - case GL_STENCIL_TEST: - m_stencil_test_enabled = true; - rasterizer_options.enable_stencil_test = true; - update_rasterizer_options = true; + case GL_ALWAYS: + options.alpha_test_func = GPU::AlphaTestFunction::Always; break; - case GL_TEXTURE_1D: - m_active_texture_unit->set_texture_1d_enabled(true); - m_sampler_config_is_dirty = true; + case GL_LESS: + options.alpha_test_func = GPU::AlphaTestFunction::Less; break; - case GL_TEXTURE_2D: - m_active_texture_unit->set_texture_2d_enabled(true); - m_sampler_config_is_dirty = true; + case GL_LEQUAL: + options.alpha_test_func = GPU::AlphaTestFunction::LessOrEqual; break; - case GL_TEXTURE_3D: - m_active_texture_unit->set_texture_3d_enabled(true); - m_sampler_config_is_dirty = true; + case GL_EQUAL: + options.alpha_test_func = GPU::AlphaTestFunction::Equal; break; - case GL_TEXTURE_CUBE_MAP: - m_active_texture_unit->set_texture_cube_map_enabled(true); - m_sampler_config_is_dirty = true; + case GL_NOTEQUAL: + options.alpha_test_func = GPU::AlphaTestFunction::NotEqual; break; - case GL_LIGHT0: - case GL_LIGHT1: - case GL_LIGHT2: - case GL_LIGHT3: - case GL_LIGHT4: - case GL_LIGHT5: - case GL_LIGHT6: - case GL_LIGHT7: - m_light_states.at(capability - GL_LIGHT0).is_enabled = true; - m_light_state_is_dirty = true; + case GL_GEQUAL: + options.alpha_test_func = GPU::AlphaTestFunction::GreaterOrEqual; break; - case GL_TEXTURE_GEN_Q: - case GL_TEXTURE_GEN_R: - case GL_TEXTURE_GEN_S: - case GL_TEXTURE_GEN_T: - texture_coordinate_generation(m_active_texture_unit_index, capability).enabled = true; - m_texcoord_generation_dirty = true; + case GL_GREATER: + options.alpha_test_func = GPU::AlphaTestFunction::Greater; break; default: - dbgln_if(GL_DEBUG, "gl_enable({:#x}): unknown parameter", capability); - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); + VERIFY_NOT_REACHED(); } - if (update_rasterizer_options) - m_rasterizer->set_options(rasterizer_options); + options.alpha_test_ref_value = m_alpha_test_ref_value; + m_rasterizer->set_options(options); } -void GLContext::gl_disable(GLenum capability) +void GLContext::gl_hint(GLenum target, GLenum mode) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_disable, capability); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_hint, target, mode); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - auto rasterizer_options = m_rasterizer->options(); - bool update_rasterizer_options = false; - - switch (capability) { - case GL_COLOR_MATERIAL: - m_color_material_enabled = false; - break; - case GL_CULL_FACE: - m_cull_faces = false; - rasterizer_options.enable_culling = false; - update_rasterizer_options = true; - break; - case GL_DEPTH_TEST: - m_depth_test_enabled = false; - rasterizer_options.enable_depth_test = false; - update_rasterizer_options = true; - break; - case GL_BLEND: - m_blend_enabled = false; - rasterizer_options.enable_blending = false; - update_rasterizer_options = true; - break; - case GL_ALPHA_TEST: - m_alpha_test_enabled = false; - rasterizer_options.enable_alpha_test = false; - update_rasterizer_options = true; - break; - case GL_DITHER: - m_dither_enabled = false; - break; - case GL_FOG: - rasterizer_options.fog_enabled = false; - update_rasterizer_options = true; - break; - case GL_LIGHTING: - m_lighting_enabled = false; - rasterizer_options.lighting_enabled = false; - update_rasterizer_options = true; - break; - case GL_LIGHT0: - case GL_LIGHT1: - case GL_LIGHT2: - case GL_LIGHT3: - case GL_LIGHT4: - case GL_LIGHT5: - case GL_LIGHT6: - case GL_LIGHT7: - m_light_states.at(capability - GL_LIGHT0).is_enabled = false; - m_light_state_is_dirty = true; - break; - case GL_NORMALIZE: - m_normalize = false; - rasterizer_options.normalization_enabled = false; - update_rasterizer_options = true; - break; - case GL_POLYGON_OFFSET_FILL: - m_depth_offset_enabled = false; - rasterizer_options.depth_offset_enabled = false; - update_rasterizer_options = true; - break; - case GL_SCISSOR_TEST: - rasterizer_options.scissor_enabled = false; - update_rasterizer_options = true; - break; - case GL_STENCIL_TEST: - m_stencil_test_enabled = false; - rasterizer_options.enable_stencil_test = false; - update_rasterizer_options = true; - break; - case GL_TEXTURE_1D: - m_active_texture_unit->set_texture_1d_enabled(false); - m_sampler_config_is_dirty = true; - break; - case GL_TEXTURE_2D: - m_active_texture_unit->set_texture_2d_enabled(false); - m_sampler_config_is_dirty = true; - break; - case GL_TEXTURE_3D: - m_active_texture_unit->set_texture_3d_enabled(false); - m_sampler_config_is_dirty = true; - break; - case GL_TEXTURE_CUBE_MAP: - m_active_texture_unit->set_texture_cube_map_enabled(false); - m_sampler_config_is_dirty = true; - break; - case GL_TEXTURE_GEN_Q: - case GL_TEXTURE_GEN_R: - case GL_TEXTURE_GEN_S: - case GL_TEXTURE_GEN_T: - texture_coordinate_generation(m_active_texture_unit_index, capability).enabled = false; - m_texcoord_generation_dirty = true; - break; - default: - dbgln_if(GL_DEBUG, "gl_disable({:#x}): unknown parameter", capability); - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); - } + RETURN_WITH_ERROR_IF(target != GL_PERSPECTIVE_CORRECTION_HINT + && target != GL_POINT_SMOOTH_HINT + && target != GL_LINE_SMOOTH_HINT + && target != GL_POLYGON_SMOOTH_HINT + && target != GL_FOG_HINT + && target != GL_GENERATE_MIPMAP_HINT + && target != GL_TEXTURE_COMPRESSION_HINT, + GL_INVALID_ENUM); - if (update_rasterizer_options) - m_rasterizer->set_options(rasterizer_options); -} - -GLboolean GLContext::gl_is_enabled(GLenum capability) -{ - RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0); - - auto optional_parameter = get_context_parameter(capability); - RETURN_VALUE_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM, 0); - - auto parameter = optional_parameter.release_value(); - RETURN_VALUE_WITH_ERROR_IF(!parameter.is_capability, GL_INVALID_ENUM, 0); - - return parameter.value.boolean_value; -} - -void GLContext::gl_gen_textures(GLsizei n, GLuint* textures) -{ - RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - m_name_allocator.allocate(n, textures); - - // Initialize all texture names with a nullptr - for (auto i = 0; i < n; ++i) { - GLuint name = textures[i]; - m_allocated_textures.set(name, nullptr); - } -} - -void GLContext::gl_delete_textures(GLsizei n, GLuint const* textures) -{ - RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - for (auto i = 0; i < n; i++) { - GLuint name = textures[i]; - if (name == 0) - continue; - - m_name_allocator.free(name); - - auto texture_object = m_allocated_textures.find(name); - if (texture_object == m_allocated_textures.end() || texture_object->value.is_null()) - continue; - auto texture = texture_object->value; - - // Check all texture units - for (auto& texture_unit : m_texture_units) { - if (texture->is_texture_2d() && texture_unit.texture_2d_target_texture() == texture) { - // If a texture that is currently bound is deleted, the binding reverts to 0 (the default texture) - texture_unit.set_texture_2d_target_texture(get_default_texture(GL_TEXTURE_2D)); - } - } - - m_allocated_textures.remove(name); - } -} - -void GLContext::gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLvoid const* data) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // We only support GL_TEXTURE_2D for now - RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); - - // Internal format can also be a number between 1 and 4. Symbolic formats were only added with EXT_texture, promoted to core in OpenGL 1.1 - if (internal_format == 1) - internal_format = GL_ALPHA; - else if (internal_format == 2) - internal_format = GL_LUMINANCE_ALPHA; - else if (internal_format == 3) - internal_format = GL_RGB; - else if (internal_format == 4) - internal_format = GL_RGBA; - - // We only support symbolic constants for now - RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA || internal_format == GL_LUMINANCE8 || internal_format == GL_LUMINANCE8_ALPHA8), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE); - // Check if width and height are a power of 2 - if (!m_device_info.supports_npot_textures) { - RETURN_WITH_ERROR_IF(!is_power_of_two(width), GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(!is_power_of_two(height), GL_INVALID_VALUE); - } - RETURN_WITH_ERROR_IF(border != 0, GL_INVALID_VALUE); - - auto texture_2d = m_active_texture_unit->texture_2d_target_texture(); - VERIFY(!texture_2d.is_null()); - - if (level == 0) { - // FIXME: OpenGL has the concept of texture and mipmap completeness. A texture has to fulfill certain criteria to be considered complete. - // Trying to render while an incomplete texture is bound will result in an error. - // Here we simply create a complete device image when mipmap level 0 is attached to the texture object. This has the unfortunate side effect - // that constructing GL textures in any but the default mipmap order, going from level 0 upwards will cause mip levels to stay uninitialized. - // To be spec compliant we should create the device image once the texture has become complete and is used for rendering the first time. - // All images that were attached before the device image was created need to be stored somewhere to be used to initialize the device image once complete. - texture_2d->set_device_image(m_rasterizer->create_image(GPU::ImageFormat::BGRA8888, width, height, 1, 999, 1)); - m_sampler_config_is_dirty = true; - } - - texture_2d->upload_texture_data(level, internal_format, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment); -} - -void GLContext::gl_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid const* data) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // We only support GL_TEXTURE_2D for now - RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); - - // We only support symbolic constants for now - RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_RGB), GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE); - - // A 2D texture array must have been defined by a previous glTexImage2D operation - auto texture_2d = m_active_texture_unit->texture_2d_target_texture(); - RETURN_WITH_ERROR_IF(texture_2d.is_null(), GL_INVALID_OPERATION); - - RETURN_WITH_ERROR_IF(xoffset < 0 || yoffset < 0 || xoffset + width > texture_2d->width_at_lod(level) || yoffset + height > texture_2d->height_at_lod(level), GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(mode != GL_DONT_CARE + && mode != GL_FASTEST + && mode != GL_NICEST, + GL_INVALID_ENUM); - texture_2d->replace_sub_texture_data(level, xoffset, yoffset, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment); + // According to the spec implementors are free to ignore glHint. So we do. } -void GLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param) +void GLContext::gl_read_buffer(GLenum mode) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameter, target, pname, param); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_read_buffer, mode); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter) - RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); - - // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter) - RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_MIN_FILTER - || pname == GL_TEXTURE_MAG_FILTER - || pname == GL_TEXTURE_WRAP_S - || pname == GL_TEXTURE_WRAP_T), + // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here + // plus any aux buffer between 0 and GL_AUX_BUFFERS + RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT + && mode != GL_FRONT_RIGHT + && mode != GL_BACK_LEFT + && mode != GL_BACK_RIGHT + && mode != GL_FRONT + && mode != GL_BACK + && mode != GL_LEFT + && mode != GL_RIGHT, GL_INVALID_ENUM); - // We assume GL_TEXTURE_2D (see above) - auto texture_2d = m_active_texture_unit->texture_2d_target_texture(); - if (texture_2d.is_null()) - return; - - switch (pname) { - case GL_TEXTURE_MIN_FILTER: - RETURN_WITH_ERROR_IF(!(param == GL_NEAREST - || param == GL_LINEAR - || param == GL_NEAREST_MIPMAP_NEAREST - || param == GL_LINEAR_MIPMAP_NEAREST - || param == GL_NEAREST_MIPMAP_LINEAR - || param == GL_LINEAR_MIPMAP_LINEAR), - GL_INVALID_ENUM); - - texture_2d->sampler().set_min_filter(param); - break; - - case GL_TEXTURE_MAG_FILTER: - RETURN_WITH_ERROR_IF(!(param == GL_NEAREST - || param == GL_LINEAR), - GL_INVALID_ENUM); - - texture_2d->sampler().set_mag_filter(param); - break; - - case GL_TEXTURE_WRAP_S: - RETURN_WITH_ERROR_IF(!(param == GL_CLAMP - || param == GL_CLAMP_TO_BORDER - || param == GL_CLAMP_TO_EDGE - || param == GL_MIRRORED_REPEAT - || param == GL_REPEAT), - GL_INVALID_ENUM); - - texture_2d->sampler().set_wrap_s_mode(param); - break; - - case GL_TEXTURE_WRAP_T: - RETURN_WITH_ERROR_IF(!(param == GL_CLAMP - || param == GL_CLAMP_TO_BORDER - || param == GL_CLAMP_TO_EDGE - || param == GL_MIRRORED_REPEAT - || param == GL_REPEAT), - GL_INVALID_ENUM); - - texture_2d->sampler().set_wrap_t_mode(param); - break; - - default: - VERIFY_NOT_REACHED(); - } + // FIXME: We do not currently have aux buffers, so make it an invalid + // operation to select anything but front or back buffers. Also we do + // not allow selecting the stereoscopic RIGHT buffers since we do not + // have them configured. + RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT + && mode != GL_FRONT + && mode != GL_BACK_LEFT + && mode != GL_BACK + && mode != GL_FRONT + && mode != GL_BACK + && mode != GL_LEFT, + GL_INVALID_OPERATION); - m_sampler_config_is_dirty = true; + m_current_read_buffer = mode; } -void GLContext::gl_tex_parameterfv(GLenum target, GLenum pname, GLfloat const* params) +void GLContext::gl_draw_buffer(GLenum buffer) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameterfv, target, pname, params); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter) - RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); - - // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter) - RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_BORDER_COLOR), GL_INVALID_ENUM); - - // We assume GL_TEXTURE_2D (see above) - auto texture_2d = m_active_texture_unit->texture_2d_target_texture(); - RETURN_WITH_ERROR_IF(texture_2d.is_null(), GL_INVALID_OPERATION); - - switch (pname) { - case GL_TEXTURE_BORDER_COLOR: - texture_2d->sampler().set_border_color(params[0], params[1], params[2], params[3]); - break; - default: - VERIFY_NOT_REACHED(); - } - - m_sampler_config_is_dirty = true; -} - -void GLContext::gl_front_face(GLenum face) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_front_face, face); + // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here + // plus any aux buffer between 0 and GL_AUX_BUFFERS + RETURN_WITH_ERROR_IF(buffer != GL_NONE + && buffer != GL_FRONT_LEFT + && buffer != GL_FRONT_RIGHT + && buffer != GL_BACK_LEFT + && buffer != GL_BACK_RIGHT + && buffer != GL_FRONT + && buffer != GL_BACK + && buffer != GL_LEFT + && buffer != GL_RIGHT, + GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(face < GL_CW || face > GL_CCW, GL_INVALID_ENUM); + // FIXME: We do not currently have aux buffers, so make it an invalid + // operation to select anything but front or back buffers. Also we do + // not allow selecting the stereoscopic RIGHT buffers since we do not + // have them configured. + RETURN_WITH_ERROR_IF(buffer != GL_NONE + && buffer != GL_FRONT_LEFT + && buffer != GL_FRONT + && buffer != GL_BACK_LEFT + && buffer != GL_BACK + && buffer != GL_FRONT + && buffer != GL_BACK + && buffer != GL_LEFT, + GL_INVALID_OPERATION); - m_front_face = face; + m_current_draw_buffer = buffer; auto rasterizer_options = m_rasterizer->options(); - rasterizer_options.front_face = (face == GL_CW) ? GPU::WindingOrder::Clockwise : GPU::WindingOrder::CounterClockwise; + // FIXME: We only have a single draw buffer in SoftGPU at the moment, + // so we simply disable color writes if GL_NONE is selected + rasterizer_options.enable_color_write = m_current_draw_buffer != GL_NONE; m_rasterizer->set_options(rasterizer_options); } -void GLContext::gl_cull_face(GLenum cull_mode) +void GLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_cull_face, cull_mode); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(cull_mode < GL_FRONT || cull_mode > GL_FRONT_AND_BACK, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(format != GL_COLOR_INDEX + && format != GL_STENCIL_INDEX + && format != GL_DEPTH_COMPONENT + && format != GL_RED + && format != GL_GREEN + && format != GL_BLUE + && format != GL_ALPHA + && format != GL_RGB + && format != GL_RGBA + && format != GL_LUMINANCE + && format != GL_LUMINANCE_ALPHA, + GL_INVALID_ENUM); - m_culled_sides = cull_mode; + RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE + && type != GL_BYTE + && type != GL_BITMAP + && type != GL_UNSIGNED_SHORT + && type != GL_SHORT + && type != GL_BLUE + && type != GL_UNSIGNED_INT + && type != GL_INT + && type != GL_FLOAT, + GL_INVALID_ENUM); - auto rasterizer_options = m_rasterizer->options(); - rasterizer_options.cull_back = cull_mode == GL_BACK || cull_mode == GL_FRONT_AND_BACK; - rasterizer_options.cull_front = cull_mode == GL_FRONT || cull_mode == GL_FRONT_AND_BACK; - m_rasterizer->set_options(rasterizer_options); -} + // FIXME: We only support RGBA buffers for now. + // Once we add support for indexed color modes do the correct check here + RETURN_WITH_ERROR_IF(format == GL_COLOR_INDEX, GL_INVALID_OPERATION); -GLuint GLContext::gl_gen_lists(GLsizei range) -{ - RETURN_VALUE_WITH_ERROR_IF(range <= 0, GL_INVALID_VALUE, 0); - RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0); + // FIXME: We do not have stencil buffers yet + // Once we add support for stencil buffers do the correct check here + RETURN_WITH_ERROR_IF(format == GL_STENCIL_INDEX, GL_INVALID_OPERATION); - auto initial_entry = m_listings.size(); - m_listings.resize(range + initial_entry); - return initial_entry + 1; -} + if (format == GL_DEPTH_COMPONENT) { + // FIXME: This check needs to be a bit more sophisticated. Currently the buffers + // are hardcoded. Once we add proper structures for them we need to correct this check -void GLContext::invoke_list(size_t list_index) -{ - auto& listing = m_listings[list_index - 1]; - for (auto& entry : listing.entries) { - entry.function.visit([&](auto& function) { - entry.arguments.visit([&](auto& arguments) { - auto apply = [&](Args&&... args) { - if constexpr (requires { (this->*function)(forward(args)...); }) - (this->*function)(forward(args)...); - }; - - arguments.apply_as_args(apply); - }); - }); + // Error because only back buffer has a depth buffer + RETURN_WITH_ERROR_IF(m_current_read_buffer == GL_FRONT + || m_current_read_buffer == GL_FRONT_LEFT + || m_current_read_buffer == GL_FRONT_RIGHT, + GL_INVALID_OPERATION); } -} - -void GLContext::gl_call_list(GLuint list) -{ - if (m_gl_call_depth > max_allowed_gl_call_depth) - return; - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_list, list); - - if (m_listings.size() < list) - return; - - TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 }; + // Some helper functions for converting float values to integer types + auto float_to_i8 = [](float f) -> GLchar { + return static_cast((0x7f * min(max(f, 0.0f), 1.0f) - 1) / 2); + }; - invoke_list(list); -} + auto float_to_i16 = [](float f) -> GLshort { + return static_cast((0x7fff * min(max(f, 0.0f), 1.0f) - 1) / 2); + }; -void GLContext::gl_call_lists(GLsizei n, GLenum type, void const* lists) -{ - if (m_gl_call_depth > max_allowed_gl_call_depth) - return; + auto float_to_i32 = [](float f) -> GLint { + return static_cast((0x7fffffff * min(max(f, 0.0f), 1.0f) - 1) / 2); + }; - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_lists, n, type, lists); - - RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(!(type == GL_BYTE - || type == GL_UNSIGNED_BYTE - || type == GL_SHORT - || type == GL_UNSIGNED_SHORT - || type == GL_INT - || type == GL_UNSIGNED_INT - || type == GL_FLOAT - || type == GL_2_BYTES - || type == GL_3_BYTES - || type == GL_4_BYTES), - GL_INVALID_ENUM); + auto float_to_u8 = [](float f) -> GLubyte { + return static_cast(0xff * min(max(f, 0.0f), 1.0f)); + }; - TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 }; + auto float_to_u16 = [](float f) -> GLushort { + return static_cast(0xffff * min(max(f, 0.0f), 1.0f)); + }; - auto invoke_all_lists = [&](T const* lists) { - for (int i = 0; i < n; ++i) { - auto list = static_cast(lists[i]); - invoke_list(m_list_base + list); - } + auto float_to_u32 = [](float f) -> GLuint { + return static_cast(0xffffffff * min(max(f, 0.0f), 1.0f)); }; + + u8 component_size = 0; switch (type) { case GL_BYTE: - invoke_all_lists(static_cast(lists)); - break; case GL_UNSIGNED_BYTE: - invoke_all_lists(static_cast(lists)); + component_size = 1; break; case GL_SHORT: - invoke_all_lists(static_cast(lists)); - break; case GL_UNSIGNED_SHORT: - invoke_all_lists(static_cast(lists)); + component_size = 2; break; case GL_INT: - invoke_all_lists(static_cast(lists)); - break; case GL_UNSIGNED_INT: - invoke_all_lists(static_cast(lists)); - break; case GL_FLOAT: - invoke_all_lists(static_cast(lists)); - break; - case GL_2_BYTES: - case GL_3_BYTES: - case GL_4_BYTES: - dbgln("GLContext FIXME: unimplemented glCallLists() with type {}", type); + component_size = 4; break; - default: - VERIFY_NOT_REACHED(); } -} - -void GLContext::gl_delete_lists(GLuint list, GLsizei range) -{ - if (m_listings.size() < list || m_listings.size() <= list + range) - return; - - for (auto& entry : m_listings.span().slice(list - 1, range)) - entry.entries.clear_with_capacity(); -} - -void GLContext::gl_list_base(GLuint base) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_list_base, base); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - m_list_base = base; -} - -void GLContext::gl_end_list() -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(!m_current_listing_index.has_value(), GL_INVALID_OPERATION); - - m_listings[m_current_listing_index->index] = move(m_current_listing_index->listing); - m_current_listing_index.clear(); -} + if (format == GL_DEPTH_COMPONENT) { + auto const row_stride = (width * component_size + m_pack_alignment - 1) / m_pack_alignment * m_pack_alignment; -void GLContext::gl_new_list(GLuint list, GLenum mode) -{ - RETURN_WITH_ERROR_IF(list == 0, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(mode != GL_COMPILE && mode != GL_COMPILE_AND_EXECUTE, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(m_current_listing_index.has_value(), GL_INVALID_OPERATION); + // Read from depth buffer + for (GLsizei i = 0; i < height; ++i) { + for (GLsizei j = 0; j < width; ++j) { + float depth = m_rasterizer->get_depthbuffer_value(x + j, y + i); + auto char_ptr = reinterpret_cast(pixels) + i * row_stride + j * component_size; - if (m_listings.size() < list) - return; - - m_current_listing_index = CurrentListing { {}, static_cast(list - 1), mode }; -} - -GLboolean GLContext::gl_is_list(GLuint list) -{ - RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE); - - return list < m_listings.size() ? GL_TRUE : GL_FALSE; -} - -void GLContext::gl_flush() -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // No-op since GLContext is completely synchronous at the moment -} - -void GLContext::gl_finish() -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // No-op since GLContext is completely synchronous at the moment -} - -void GLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: The list of allowed enums differs between API versions - // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc - - RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO - || src_factor == GL_ONE - || src_factor == GL_SRC_COLOR - || src_factor == GL_ONE_MINUS_SRC_COLOR - || src_factor == GL_DST_COLOR - || src_factor == GL_ONE_MINUS_DST_COLOR - || src_factor == GL_SRC_ALPHA - || src_factor == GL_ONE_MINUS_SRC_ALPHA - || src_factor == GL_DST_ALPHA - || src_factor == GL_ONE_MINUS_DST_ALPHA - || src_factor == GL_CONSTANT_COLOR - || src_factor == GL_ONE_MINUS_CONSTANT_COLOR - || src_factor == GL_CONSTANT_ALPHA - || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA - || src_factor == GL_SRC_ALPHA_SATURATE), - GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO - || dst_factor == GL_ONE - || dst_factor == GL_SRC_COLOR - || dst_factor == GL_ONE_MINUS_SRC_COLOR - || dst_factor == GL_DST_COLOR - || dst_factor == GL_ONE_MINUS_DST_COLOR - || dst_factor == GL_SRC_ALPHA - || dst_factor == GL_ONE_MINUS_SRC_ALPHA - || dst_factor == GL_DST_ALPHA - || dst_factor == GL_ONE_MINUS_DST_ALPHA - || dst_factor == GL_CONSTANT_COLOR - || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR - || dst_factor == GL_CONSTANT_ALPHA - || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA), - GL_INVALID_ENUM); - - m_blend_source_factor = src_factor; - m_blend_destination_factor = dst_factor; - - auto map_gl_blend_factor_to_device = [](GLenum factor) constexpr - { - switch (factor) { - case GL_ZERO: - return GPU::BlendFactor::Zero; - case GL_ONE: - return GPU::BlendFactor::One; - case GL_SRC_ALPHA: - return GPU::BlendFactor::SrcAlpha; - case GL_ONE_MINUS_SRC_ALPHA: - return GPU::BlendFactor::OneMinusSrcAlpha; - case GL_SRC_COLOR: - return GPU::BlendFactor::SrcColor; - case GL_ONE_MINUS_SRC_COLOR: - return GPU::BlendFactor::OneMinusSrcColor; - case GL_DST_ALPHA: - return GPU::BlendFactor::DstAlpha; - case GL_ONE_MINUS_DST_ALPHA: - return GPU::BlendFactor::OneMinusDstAlpha; - case GL_DST_COLOR: - return GPU::BlendFactor::DstColor; - case GL_ONE_MINUS_DST_COLOR: - return GPU::BlendFactor::OneMinusDstColor; - case GL_SRC_ALPHA_SATURATE: - return GPU::BlendFactor::SrcAlphaSaturate; - default: - VERIFY_NOT_REACHED(); + switch (type) { + case GL_BYTE: + *reinterpret_cast(char_ptr) = float_to_i8(depth); + break; + case GL_SHORT: + *reinterpret_cast(char_ptr) = float_to_i16(depth); + break; + case GL_INT: + *reinterpret_cast(char_ptr) = float_to_i32(depth); + break; + case GL_UNSIGNED_BYTE: + *reinterpret_cast(char_ptr) = float_to_u8(depth); + break; + case GL_UNSIGNED_SHORT: + *reinterpret_cast(char_ptr) = float_to_u16(depth); + break; + case GL_UNSIGNED_INT: + *reinterpret_cast(char_ptr) = float_to_u32(depth); + break; + case GL_FLOAT: + *reinterpret_cast(char_ptr) = min(max(depth, 0.0f), 1.0f); + break; + } + } } - }; - - auto options = m_rasterizer->options(); - options.blend_source_factor = map_gl_blend_factor_to_device(m_blend_source_factor); - options.blend_destination_factor = map_gl_blend_factor_to_device(m_blend_destination_factor); - m_rasterizer->set_options(options); -} - -void GLContext::gl_shade_model(GLenum mode) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_shade_model, mode); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(mode != GL_FLAT && mode != GL_SMOOTH, GL_INVALID_ENUM); - - auto options = m_rasterizer->options(); - options.shade_smooth = (mode == GL_SMOOTH); - m_rasterizer->set_options(options); -} - -void GLContext::gl_alpha_func(GLenum func, GLclampf ref) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_alpha_func, func, ref); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(func < GL_NEVER || func > GL_ALWAYS, GL_INVALID_ENUM); - - m_alpha_test_func = func; - m_alpha_test_ref_value = ref; + return; + } - auto options = m_rasterizer->options(); + bool write_red = false; + bool write_green = false; + bool write_blue = false; + bool write_alpha = false; + size_t component_count = 0; + size_t red_offset = 0; + size_t green_offset = 0; + size_t blue_offset = 0; + size_t alpha_offset = 0; + char* red_ptr = nullptr; + char* green_ptr = nullptr; + char* blue_ptr = nullptr; + char* alpha_ptr = nullptr; - switch (func) { - case GL_NEVER: - options.alpha_test_func = GPU::AlphaTestFunction::Never; - break; - case GL_ALWAYS: - options.alpha_test_func = GPU::AlphaTestFunction::Always; - break; - case GL_LESS: - options.alpha_test_func = GPU::AlphaTestFunction::Less; + switch (format) { + case GL_RGB: + write_red = true; + write_green = true; + write_blue = true; + component_count = 3; + red_offset = 2; + green_offset = 1; + blue_offset = 0; break; - case GL_LEQUAL: - options.alpha_test_func = GPU::AlphaTestFunction::LessOrEqual; + case GL_RGBA: + write_red = true; + write_green = true; + write_blue = true; + write_alpha = true; + component_count = 4; + red_offset = 3; + green_offset = 2; + blue_offset = 1; + alpha_offset = 0; break; - case GL_EQUAL: - options.alpha_test_func = GPU::AlphaTestFunction::Equal; + case GL_RED: + write_red = true; + component_count = 1; + red_offset = 0; break; - case GL_NOTEQUAL: - options.alpha_test_func = GPU::AlphaTestFunction::NotEqual; + case GL_GREEN: + write_green = true; + component_count = 1; + green_offset = 0; break; - case GL_GEQUAL: - options.alpha_test_func = GPU::AlphaTestFunction::GreaterOrEqual; + case GL_BLUE: + write_blue = true; + component_count = 1; + blue_offset = 0; break; - case GL_GREATER: - options.alpha_test_func = GPU::AlphaTestFunction::Greater; + case GL_ALPHA: + write_alpha = true; + component_count = 1; + alpha_offset = 0; break; - default: - VERIFY_NOT_REACHED(); } - options.alpha_test_ref_value = m_alpha_test_ref_value; - m_rasterizer->set_options(options); -} + auto const pixel_bytes = component_size * component_count; + auto const row_alignment_bytes = (m_pack_alignment - ((width * pixel_bytes) % m_pack_alignment)) % m_pack_alignment; -void GLContext::gl_hint(GLenum target, GLenum mode) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_hint, target, mode); + char* out_ptr = reinterpret_cast(pixels); + for (int i = 0; i < (int)height; ++i) { + for (int j = 0; j < (int)width; ++j) { + Gfx::ARGB32 color {}; + if (m_current_read_buffer == GL_FRONT || m_current_read_buffer == GL_LEFT || m_current_read_buffer == GL_FRONT_LEFT) { + if (y + i >= m_frontbuffer->width() || x + j >= m_frontbuffer->height()) + color = 0; + else + color = m_frontbuffer->scanline(y + i)[x + j]; + } else { + color = m_rasterizer->get_color_buffer_pixel(x + j, y + i); + } - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + float red = ((color >> 24) & 0xff) / 255.0f; + float green = ((color >> 16) & 0xff) / 255.0f; + float blue = ((color >> 8) & 0xff) / 255.0f; + float alpha = (color & 0xff) / 255.0f; - RETURN_WITH_ERROR_IF(target != GL_PERSPECTIVE_CORRECTION_HINT - && target != GL_POINT_SMOOTH_HINT - && target != GL_LINE_SMOOTH_HINT - && target != GL_POLYGON_SMOOTH_HINT - && target != GL_FOG_HINT - && target != GL_GENERATE_MIPMAP_HINT - && target != GL_TEXTURE_COMPRESSION_HINT, - GL_INVALID_ENUM); + // FIXME: Set up write pointers based on selected endianness (glPixelStore) + red_ptr = out_ptr + (component_size * red_offset); + green_ptr = out_ptr + (component_size * green_offset); + blue_ptr = out_ptr + (component_size * blue_offset); + alpha_ptr = out_ptr + (component_size * alpha_offset); - RETURN_WITH_ERROR_IF(mode != GL_DONT_CARE - && mode != GL_FASTEST - && mode != GL_NICEST, - GL_INVALID_ENUM); - - // According to the spec implementors are free to ignore glHint. So we do. -} - -void GLContext::gl_read_buffer(GLenum mode) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_read_buffer, mode); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here - // plus any aux buffer between 0 and GL_AUX_BUFFERS - RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT - && mode != GL_FRONT_RIGHT - && mode != GL_BACK_LEFT - && mode != GL_BACK_RIGHT - && mode != GL_FRONT - && mode != GL_BACK - && mode != GL_LEFT - && mode != GL_RIGHT, - GL_INVALID_ENUM); - - // FIXME: We do not currently have aux buffers, so make it an invalid - // operation to select anything but front or back buffers. Also we do - // not allow selecting the stereoscopic RIGHT buffers since we do not - // have them configured. - RETURN_WITH_ERROR_IF(mode != GL_FRONT_LEFT - && mode != GL_FRONT - && mode != GL_BACK_LEFT - && mode != GL_BACK - && mode != GL_FRONT - && mode != GL_BACK - && mode != GL_LEFT, - GL_INVALID_OPERATION); - - m_current_read_buffer = mode; -} - -void GLContext::gl_draw_buffer(GLenum buffer) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_buffer, buffer); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: Also allow aux buffers GL_AUX0 through GL_AUX3 here - // plus any aux buffer between 0 and GL_AUX_BUFFERS - RETURN_WITH_ERROR_IF(buffer != GL_NONE - && buffer != GL_FRONT_LEFT - && buffer != GL_FRONT_RIGHT - && buffer != GL_BACK_LEFT - && buffer != GL_BACK_RIGHT - && buffer != GL_FRONT - && buffer != GL_BACK - && buffer != GL_LEFT - && buffer != GL_RIGHT, - GL_INVALID_ENUM); - - // FIXME: We do not currently have aux buffers, so make it an invalid - // operation to select anything but front or back buffers. Also we do - // not allow selecting the stereoscopic RIGHT buffers since we do not - // have them configured. - RETURN_WITH_ERROR_IF(buffer != GL_NONE - && buffer != GL_FRONT_LEFT - && buffer != GL_FRONT - && buffer != GL_BACK_LEFT - && buffer != GL_BACK - && buffer != GL_FRONT - && buffer != GL_BACK - && buffer != GL_LEFT, - GL_INVALID_OPERATION); - - m_current_draw_buffer = buffer; - - auto rasterizer_options = m_rasterizer->options(); - // FIXME: We only have a single draw buffer in SoftGPU at the moment, - // so we simply disable color writes if GL_NONE is selected - rasterizer_options.enable_color_write = m_current_draw_buffer != GL_NONE; - m_rasterizer->set_options(rasterizer_options); -} - -void GLContext::gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE); - - RETURN_WITH_ERROR_IF(format != GL_COLOR_INDEX - && format != GL_STENCIL_INDEX - && format != GL_DEPTH_COMPONENT - && format != GL_RED - && format != GL_GREEN - && format != GL_BLUE - && format != GL_ALPHA - && format != GL_RGB - && format != GL_RGBA - && format != GL_LUMINANCE - && format != GL_LUMINANCE_ALPHA, - GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF(type != GL_UNSIGNED_BYTE - && type != GL_BYTE - && type != GL_BITMAP - && type != GL_UNSIGNED_SHORT - && type != GL_SHORT - && type != GL_BLUE - && type != GL_UNSIGNED_INT - && type != GL_INT - && type != GL_FLOAT, - GL_INVALID_ENUM); - - // FIXME: We only support RGBA buffers for now. - // Once we add support for indexed color modes do the correct check here - RETURN_WITH_ERROR_IF(format == GL_COLOR_INDEX, GL_INVALID_OPERATION); - - // FIXME: We do not have stencil buffers yet - // Once we add support for stencil buffers do the correct check here - RETURN_WITH_ERROR_IF(format == GL_STENCIL_INDEX, GL_INVALID_OPERATION); - - if (format == GL_DEPTH_COMPONENT) { - // FIXME: This check needs to be a bit more sophisticated. Currently the buffers - // are hardcoded. Once we add proper structures for them we need to correct this check - - // Error because only back buffer has a depth buffer - RETURN_WITH_ERROR_IF(m_current_read_buffer == GL_FRONT - || m_current_read_buffer == GL_FRONT_LEFT - || m_current_read_buffer == GL_FRONT_RIGHT, - GL_INVALID_OPERATION); - } - - // Some helper functions for converting float values to integer types - auto float_to_i8 = [](float f) -> GLchar { - return static_cast((0x7f * min(max(f, 0.0f), 1.0f) - 1) / 2); - }; - - auto float_to_i16 = [](float f) -> GLshort { - return static_cast((0x7fff * min(max(f, 0.0f), 1.0f) - 1) / 2); - }; - - auto float_to_i32 = [](float f) -> GLint { - return static_cast((0x7fffffff * min(max(f, 0.0f), 1.0f) - 1) / 2); - }; - - auto float_to_u8 = [](float f) -> GLubyte { - return static_cast(0xff * min(max(f, 0.0f), 1.0f)); - }; - - auto float_to_u16 = [](float f) -> GLushort { - return static_cast(0xffff * min(max(f, 0.0f), 1.0f)); - }; - - auto float_to_u32 = [](float f) -> GLuint { - return static_cast(0xffffffff * min(max(f, 0.0f), 1.0f)); - }; - - u8 component_size = 0; - switch (type) { - case GL_BYTE: - case GL_UNSIGNED_BYTE: - component_size = 1; - break; - case GL_SHORT: - case GL_UNSIGNED_SHORT: - component_size = 2; - break; - case GL_INT: - case GL_UNSIGNED_INT: - case GL_FLOAT: - component_size = 4; - break; - } - - if (format == GL_DEPTH_COMPONENT) { - auto const row_stride = (width * component_size + m_pack_alignment - 1) / m_pack_alignment * m_pack_alignment; - - // Read from depth buffer - for (GLsizei i = 0; i < height; ++i) { - for (GLsizei j = 0; j < width; ++j) { - float depth = m_rasterizer->get_depthbuffer_value(x + j, y + i); - auto char_ptr = reinterpret_cast(pixels) + i * row_stride + j * component_size; - - switch (type) { - case GL_BYTE: - *reinterpret_cast(char_ptr) = float_to_i8(depth); - break; - case GL_SHORT: - *reinterpret_cast(char_ptr) = float_to_i16(depth); - break; - case GL_INT: - *reinterpret_cast(char_ptr) = float_to_i32(depth); - break; - case GL_UNSIGNED_BYTE: - *reinterpret_cast(char_ptr) = float_to_u8(depth); - break; - case GL_UNSIGNED_SHORT: - *reinterpret_cast(char_ptr) = float_to_u16(depth); - break; - case GL_UNSIGNED_INT: - *reinterpret_cast(char_ptr) = float_to_u32(depth); - break; - case GL_FLOAT: - *reinterpret_cast(char_ptr) = min(max(depth, 0.0f), 1.0f); - break; - } - } - } - return; - } - - bool write_red = false; - bool write_green = false; - bool write_blue = false; - bool write_alpha = false; - size_t component_count = 0; - size_t red_offset = 0; - size_t green_offset = 0; - size_t blue_offset = 0; - size_t alpha_offset = 0; - char* red_ptr = nullptr; - char* green_ptr = nullptr; - char* blue_ptr = nullptr; - char* alpha_ptr = nullptr; - - switch (format) { - case GL_RGB: - write_red = true; - write_green = true; - write_blue = true; - component_count = 3; - red_offset = 2; - green_offset = 1; - blue_offset = 0; - break; - case GL_RGBA: - write_red = true; - write_green = true; - write_blue = true; - write_alpha = true; - component_count = 4; - red_offset = 3; - green_offset = 2; - blue_offset = 1; - alpha_offset = 0; - break; - case GL_RED: - write_red = true; - component_count = 1; - red_offset = 0; - break; - case GL_GREEN: - write_green = true; - component_count = 1; - green_offset = 0; - break; - case GL_BLUE: - write_blue = true; - component_count = 1; - blue_offset = 0; - break; - case GL_ALPHA: - write_alpha = true; - component_count = 1; - alpha_offset = 0; - break; - } - - auto const pixel_bytes = component_size * component_count; - auto const row_alignment_bytes = (m_pack_alignment - ((width * pixel_bytes) % m_pack_alignment)) % m_pack_alignment; - - char* out_ptr = reinterpret_cast(pixels); - for (int i = 0; i < (int)height; ++i) { - for (int j = 0; j < (int)width; ++j) { - Gfx::ARGB32 color {}; - if (m_current_read_buffer == GL_FRONT || m_current_read_buffer == GL_LEFT || m_current_read_buffer == GL_FRONT_LEFT) { - if (y + i >= m_frontbuffer->width() || x + j >= m_frontbuffer->height()) - color = 0; - else - color = m_frontbuffer->scanline(y + i)[x + j]; - } else { - color = m_rasterizer->get_color_buffer_pixel(x + j, y + i); - } - - float red = ((color >> 24) & 0xff) / 255.0f; - float green = ((color >> 16) & 0xff) / 255.0f; - float blue = ((color >> 8) & 0xff) / 255.0f; - float alpha = (color & 0xff) / 255.0f; - - // FIXME: Set up write pointers based on selected endianness (glPixelStore) - red_ptr = out_ptr + (component_size * red_offset); - green_ptr = out_ptr + (component_size * green_offset); - blue_ptr = out_ptr + (component_size * blue_offset); - alpha_ptr = out_ptr + (component_size * alpha_offset); - - switch (type) { - case GL_BYTE: - if (write_red) - *reinterpret_cast(red_ptr) = float_to_i8(red); - if (write_green) - *reinterpret_cast(green_ptr) = float_to_i8(green); - if (write_blue) - *reinterpret_cast(blue_ptr) = float_to_i8(blue); - if (write_alpha) - *reinterpret_cast(alpha_ptr) = float_to_i8(alpha); - break; - case GL_UNSIGNED_BYTE: - if (write_red) - *reinterpret_cast(red_ptr) = float_to_u8(red); - if (write_green) - *reinterpret_cast(green_ptr) = float_to_u8(green); - if (write_blue) - *reinterpret_cast(blue_ptr) = float_to_u8(blue); - if (write_alpha) - *reinterpret_cast(alpha_ptr) = float_to_u8(alpha); - break; - case GL_SHORT: - if (write_red) - *reinterpret_cast(red_ptr) = float_to_i16(red); - if (write_green) - *reinterpret_cast(green_ptr) = float_to_i16(green); - if (write_blue) - *reinterpret_cast(blue_ptr) = float_to_i16(blue); - if (write_alpha) - *reinterpret_cast(alpha_ptr) = float_to_i16(alpha); - break; - case GL_UNSIGNED_SHORT: - if (write_red) - *reinterpret_cast(red_ptr) = float_to_u16(red); - if (write_green) - *reinterpret_cast(green_ptr) = float_to_u16(green); - if (write_blue) - *reinterpret_cast(blue_ptr) = float_to_u16(blue); - if (write_alpha) - *reinterpret_cast(alpha_ptr) = float_to_u16(alpha); - break; - case GL_INT: - if (write_red) - *reinterpret_cast(red_ptr) = float_to_i32(red); - if (write_green) - *reinterpret_cast(green_ptr) = float_to_i32(green); - if (write_blue) - *reinterpret_cast(blue_ptr) = float_to_i32(blue); - if (write_alpha) - *reinterpret_cast(alpha_ptr) = float_to_i32(alpha); - break; - case GL_UNSIGNED_INT: - if (write_red) - *reinterpret_cast(red_ptr) = float_to_u32(red); - if (write_green) - *reinterpret_cast(green_ptr) = float_to_u32(green); - if (write_blue) - *reinterpret_cast(blue_ptr) = float_to_u32(blue); - if (write_alpha) - *reinterpret_cast(alpha_ptr) = float_to_u32(alpha); - break; - case GL_FLOAT: - if (write_red) - *reinterpret_cast(red_ptr) = min(max(red, 0.0f), 1.0f); - if (write_green) - *reinterpret_cast(green_ptr) = min(max(green, 0.0f), 1.0f); - if (write_blue) - *reinterpret_cast(blue_ptr) = min(max(blue, 0.0f), 1.0f); - if (write_alpha) - *reinterpret_cast(alpha_ptr) = min(max(alpha, 0.0f), 1.0f); - break; - } - - out_ptr += pixel_bytes; - } - - out_ptr += row_alignment_bytes; - } -} - -void GLContext::gl_bind_texture(GLenum target, GLuint texture) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(target != GL_TEXTURE_1D - && target != GL_TEXTURE_2D - && target != GL_TEXTURE_3D - && target != GL_TEXTURE_1D_ARRAY - && target != GL_TEXTURE_2D_ARRAY - && target != GL_TEXTURE_CUBE_MAP, - GL_INVALID_ENUM); - - // FIXME: We only support GL_TEXTURE_2D for now - if (target != GL_TEXTURE_2D) { - dbgln("gl_bind_texture(target = {:#x}): currently only GL_TEXTURE_2D is supported", target); - return; - } - - RefPtr texture_2d; - - if (texture == 0) { - // Texture name 0 refers to the default texture - texture_2d = get_default_texture(target); - } else { - // Find this texture name in our previously allocated textures - auto it = m_allocated_textures.find(texture); - if (it != m_allocated_textures.end()) { - auto texture_object = it->value; - if (!texture_object.is_null()) { - // Texture must have been created with the same target - RETURN_WITH_ERROR_IF(!texture_object->is_texture_2d(), GL_INVALID_OPERATION); - texture_2d = static_cast(texture_object.ptr()); - } - } - - // OpenGL 1.x supports binding texture names that were not previously generated by glGenTextures. - // If there is not an allocated texture, meaning it was not previously generated by glGenTextures, - // we can keep texture_object null to both allocate and bind the texture with the passed in texture name. - // FIXME: Later OpenGL versions such as 4.x enforce that texture names being bound were previously generated - // by glGenTextures. - if (!texture_2d) { - texture_2d = adopt_ref(*new Texture2D()); - m_allocated_textures.set(texture, texture_2d); - } - } - - m_active_texture_unit->set_texture_2d_target_texture(texture_2d); - m_sampler_config_is_dirty = true; -} - -GLboolean GLContext::gl_is_texture(GLuint texture) -{ - RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE); - - if (texture == 0) - return GL_FALSE; - - auto it = m_allocated_textures.find(texture); - if (it == m_allocated_textures.end()) - return GL_FALSE; - - return it->value.is_null() ? GL_FALSE : GL_TRUE; -} - -void GLContext::gl_active_texture(GLenum texture) -{ - RETURN_WITH_ERROR_IF(texture < GL_TEXTURE0 || texture >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM); - - m_active_texture_unit_index = texture - GL_TEXTURE0; - m_active_texture_unit = &m_texture_units.at(m_active_texture_unit_index); -} - -void GLContext::gl_get_booleanv(GLenum pname, GLboolean* data) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - auto optional_parameter = get_context_parameter(pname); - RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM); - auto parameter = optional_parameter.release_value(); - - switch (parameter.type) { - case GL_BOOL: - *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE; - break; - case GL_DOUBLE: - *data = (parameter.value.double_value == 0.0) ? GL_FALSE : GL_TRUE; - break; - case GL_INT: - *data = (parameter.value.integer_value == 0) ? GL_FALSE : GL_TRUE; - break; - default: - VERIFY_NOT_REACHED(); - } -} - -void GLContext::gl_get_doublev(GLenum pname, GLdouble* params) -{ - get_floating_point(pname, params); -} - -void GLContext::gl_get_floatv(GLenum pname, GLfloat* params) -{ - get_floating_point(pname, params); -} - -template -void GLContext::get_floating_point(GLenum pname, T* params) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // Handle matrix retrieval first - auto flatten_and_assign_matrix = [¶ms](FloatMatrix4x4 const& matrix) { - auto elements = matrix.elements(); - for (size_t i = 0; i < 4; ++i) { - for (size_t j = 0; j < 4; ++j) { - // Return transposed matrix since OpenGL defines them as column-major - params[i * 4 + j] = static_cast(elements[j][i]); - } - } - }; - switch (pname) { - case GL_MODELVIEW_MATRIX: - flatten_and_assign_matrix(m_model_view_matrix); - return; - case GL_PROJECTION_MATRIX: - flatten_and_assign_matrix(m_projection_matrix); - return; - } - - // Regular parameters - auto optional_parameter = get_context_parameter(pname); - RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM); - auto parameter = optional_parameter.release_value(); - - switch (parameter.type) { - case GL_BOOL: - *params = parameter.value.boolean_value ? GL_TRUE : GL_FALSE; - break; - case GL_DOUBLE: - for (size_t i = 0; i < parameter.count; ++i) - params[i] = parameter.value.double_list[i]; - break; - case GL_INT: - for (size_t i = 0; i < parameter.count; ++i) - params[i] = parameter.value.integer_list[i]; - break; - default: - VERIFY_NOT_REACHED(); - } -} - -void GLContext::gl_get_integerv(GLenum pname, GLint* data) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - auto optional_parameter = get_context_parameter(pname); - RETURN_WITH_ERROR_IF(!optional_parameter.has_value(), GL_INVALID_ENUM); - auto parameter = optional_parameter.release_value(); - - switch (parameter.type) { - case GL_BOOL: - *data = parameter.value.boolean_value ? GL_TRUE : GL_FALSE; - break; - case GL_DOUBLE: { - double const int_range = static_cast(NumericLimits::max()) - NumericLimits::min(); - for (size_t i = 0; i < parameter.count; ++i) { - double const result_factor = (clamp(parameter.value.double_list[i], -1.0, 1.0) + 1.0) / 2.0; - data[i] = static_cast(NumericLimits::min() + result_factor * int_range); - } - break; - } - case GL_INT: - for (size_t i = 0; i < parameter.count; ++i) - data[i] = parameter.value.integer_list[i]; - break; - default: - VERIFY_NOT_REACHED(); - } -} - -void GLContext::gl_depth_mask(GLboolean flag) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_mask, flag); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - auto options = m_rasterizer->options(); - options.enable_depth_write = (flag != GL_FALSE); - m_rasterizer->set_options(options); -} - -void GLContext::gl_clip_plane(GLenum plane, [[maybe_unused]] GLdouble const* equation) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clip_plane, plane, equation); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF((plane < GL_CLIP_PLANE0) || (plane > GL_CLIP_PLANE5), GL_INVALID_ENUM); - - dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_clip_plane() (equation = [{} {} {} {}])", equation[0], equation[1], equation[2], equation[3]); -} - -void GLContext::gl_enable_client_state(GLenum cap) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - switch (cap) { - case GL_COLOR_ARRAY: - m_client_side_color_array_enabled = true; - break; - case GL_NORMAL_ARRAY: - m_client_side_normal_array_enabled = true; - break; - case GL_TEXTURE_COORD_ARRAY: - m_client_side_texture_coord_array_enabled[m_client_active_texture] = true; - break; - case GL_VERTEX_ARRAY: - m_client_side_vertex_array_enabled = true; - break; - default: - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); - } -} - -void GLContext::gl_disable_client_state(GLenum cap) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - switch (cap) { - case GL_COLOR_ARRAY: - m_client_side_color_array_enabled = false; - break; - case GL_NORMAL_ARRAY: - m_client_side_normal_array_enabled = false; - break; - case GL_TEXTURE_COORD_ARRAY: - m_client_side_texture_coord_array_enabled[m_client_active_texture] = false; - break; - case GL_VERTEX_ARRAY: - m_client_side_vertex_array_enabled = false; - break; - default: - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); - } -} - -void GLContext::gl_client_active_texture(GLenum target) -{ - RETURN_WITH_ERROR_IF(target < GL_TEXTURE0 || target >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM); - - m_client_active_texture = target - GL_TEXTURE0; -} - -void GLContext::gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(!(size == 2 || size == 3 || size == 4), GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE); - - m_client_vertex_pointer = { .size = size, .type = type, .stride = stride, .pointer = pointer }; -} - -void GLContext::gl_color_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(!(size == 3 || size == 4), GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(type != GL_BYTE - && type != GL_UNSIGNED_BYTE - && type != GL_SHORT - && type != GL_UNSIGNED_SHORT - && type != GL_INT - && type != GL_UNSIGNED_INT - && type != GL_FLOAT - && type != GL_DOUBLE, - GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE); - - m_client_color_pointer = { .size = size, .type = type, .stride = stride, .pointer = pointer }; -} - -void GLContext::gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(!(size == 1 || size == 2 || size == 3 || size == 4), GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE); - - auto& tex_coord_pointer = m_client_tex_coord_pointer[m_client_active_texture]; - tex_coord_pointer = { .size = size, .type = type, .stride = stride, .pointer = pointer }; -} - -void GLContext::gl_normal_pointer(GLenum type, GLsizei stride, void const* pointer) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(type != GL_BYTE - && type != GL_SHORT - && type != GL_INT - && type != GL_FLOAT - && type != GL_DOUBLE, - GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE); - - m_client_normal_pointer = { .size = 3, .type = type, .stride = stride, .pointer = pointer }; -} - -void GLContext::gl_tex_env(GLenum target, GLenum pname, GLfloat param) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_env, target, pname, param); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: We currently only support a subset of possible target values. Implement the rest! - RETURN_WITH_ERROR_IF(target != GL_TEXTURE_ENV, GL_INVALID_ENUM); - - // FIXME: We currently only support a subset of possible pname values. Implement the rest! - RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_ENV_MODE, GL_INVALID_ENUM); - - auto param_enum = static_cast(param); - - switch (param_enum) { - case GL_MODULATE: - case GL_REPLACE: - case GL_DECAL: - case GL_ADD: - m_active_texture_unit->set_env_mode(param_enum); - m_sampler_config_is_dirty = true; - break; - default: - // FIXME: We currently only support a subset of possible param values. Implement the rest! - dbgln_if(GL_DEBUG, "gl_tex_env({:#x}, {:#x}, {}): param unimplemented", target, pname, param); - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); - } -} - -void GLContext::gl_draw_arrays(GLenum mode, GLint first, GLsizei count) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_arrays, mode, first, count); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES) - RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP - || mode == GL_TRIANGLE_FAN - || mode == GL_TRIANGLES - || mode == GL_QUADS - || mode == GL_QUAD_STRIP - || mode == GL_POLYGON), - GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE); - - // At least the vertex array needs to be enabled - if (!m_client_side_vertex_array_enabled) - return; - - auto last = first + count; - gl_begin(mode); - for (int i = first; i < last; i++) { - if (m_client_side_color_array_enabled) { - float color[4] { 0, 0, 0, 1 }; - read_from_vertex_attribute_pointer(m_client_color_pointer, i, color); - gl_color(color[0], color[1], color[2], color[3]); - } - - for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) { - if (m_client_side_texture_coord_array_enabled[t]) { - float tex_coords[4] { 0, 0, 0, 0 }; - read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords); - gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]); - } - } - - if (m_client_side_normal_array_enabled) { - float normal[3]; - read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal); - gl_normal(normal[0], normal[1], normal[2]); - } - - float vertex[4] { 0, 0, 0, 1 }; - read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex); - gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]); - } - gl_end(); -} - -void GLContext::gl_draw_elements(GLenum mode, GLsizei count, GLenum type, void const* indices) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_elements, mode, count, type, indices); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES) - RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP - || mode == GL_TRIANGLE_FAN - || mode == GL_TRIANGLES - || mode == GL_QUADS - || mode == GL_QUAD_STRIP - || mode == GL_POLYGON), - GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE - || type == GL_UNSIGNED_SHORT - || type == GL_UNSIGNED_INT), - GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE); - - // At least the vertex array needs to be enabled - if (!m_client_side_vertex_array_enabled) - return; - - gl_begin(mode); - for (int index = 0; index < count; index++) { - int i = 0; - switch (type) { - case GL_UNSIGNED_BYTE: - i = reinterpret_cast(indices)[index]; - break; - case GL_UNSIGNED_SHORT: - i = reinterpret_cast(indices)[index]; - break; - case GL_UNSIGNED_INT: - i = reinterpret_cast(indices)[index]; - break; - } - - if (m_client_side_color_array_enabled) { - float color[4] { 0, 0, 0, 1 }; - read_from_vertex_attribute_pointer(m_client_color_pointer, i, color); - gl_color(color[0], color[1], color[2], color[3]); - } - - for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) { - if (m_client_side_texture_coord_array_enabled[t]) { - float tex_coords[4] { 0, 0, 0, 0 }; - read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords); - gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]); - } - } - - if (m_client_side_normal_array_enabled) { - float normal[3]; - read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal); - gl_normal(normal[0], normal[1], normal[2]); - } - - float vertex[4] { 0, 0, 0, 1 }; - read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex); - gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]); - } - gl_end(); -} - -void GLContext::gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, void const* data) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_pixels, width, height, format, type, data); - - RETURN_WITH_ERROR_IF(format < GL_COLOR_INDEX || format > GL_BGRA, GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF((type < GL_BYTE || type > GL_FLOAT) - && (type < GL_UNSIGNED_BYTE_3_3_2 || type > GL_UNSIGNED_INT_10_10_10_2) - && (type < GL_UNSIGNED_BYTE_2_3_3_REV || type > GL_UNSIGNED_INT_2_10_10_10_REV), - GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF(type == GL_BITMAP && !(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX), GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE); - - // FIXME: GL_INVALID_OPERATION is generated if format is GL_STENCIL_INDEX and there is no stencil buffer - // FIXME: GL_INVALID_OPERATION is generated if format is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA, - // GL_BGR, GL_BGRA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA, and the GL is in color index mode - - RETURN_WITH_ERROR_IF(format != GL_RGB - && (type == GL_UNSIGNED_BYTE_3_3_2 - || type == GL_UNSIGNED_BYTE_2_3_3_REV - || type == GL_UNSIGNED_SHORT_5_6_5 - || type == GL_UNSIGNED_SHORT_5_6_5_REV), - GL_INVALID_OPERATION); - - RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_BGRA) - && (type == GL_UNSIGNED_SHORT_4_4_4_4 - || type == GL_UNSIGNED_SHORT_4_4_4_4_REV - || type == GL_UNSIGNED_SHORT_5_5_5_1 - || type == GL_UNSIGNED_SHORT_1_5_5_5_REV - || type == GL_UNSIGNED_INT_8_8_8_8 - || type == GL_UNSIGNED_INT_8_8_8_8_REV - || type == GL_UNSIGNED_INT_10_10_10_2 - || type == GL_UNSIGNED_INT_2_10_10_10_REV), - GL_INVALID_OPERATION); - - // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER - // target and the buffer object's data store is currently mapped. - // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER - // target and the data would be unpacked from the buffer object such that the memory reads required would - // exceed the data store size. - // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER - // target and data is not evenly divisible into the number of bytes needed to store in memory a datum - // indicated by type. - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: we only support RGBA + UNSIGNED_BYTE and DEPTH_COMPONENT + UNSIGNED_SHORT, implement all combinations! - if (!((format == GL_RGBA && type == GL_UNSIGNED_BYTE) || (format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_SHORT))) { - dbgln_if(GL_DEBUG, "gl_draw_pixels(): support for format {:#x} and/or type {:#x} not implemented", format, type); - return; - } - - // FIXME: implement support for pixel parameters such as GL_UNPACK_ALIGNMENT - - if (format == GL_RGBA) { - auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height }); - RETURN_WITH_ERROR_IF(bitmap_or_error.is_error(), GL_OUT_OF_MEMORY); - auto bitmap = bitmap_or_error.release_value(); - - auto pixel_data = static_cast(data); - for (int y = 0; y < height; ++y) - for (int x = 0; x < width; ++x) - bitmap->set_pixel(x, y, Color::from_argb(*(pixel_data++))); - - m_rasterizer->blit_to_color_buffer_at_raster_position(bitmap); - } else if (format == GL_DEPTH_COMPONENT) { - Vector depth_values; - depth_values.ensure_capacity(width * height); - - auto depth_data = static_cast(data); - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - auto u16_value = *(depth_data++); - auto float_value = static_cast(u16_value) / NumericLimits::max(); - depth_values.append(float_value); - } - } - - m_rasterizer->blit_to_depth_buffer_at_raster_position(depth_values, width, height); - } else { - VERIFY_NOT_REACHED(); - } -} - -void GLContext::gl_array_element(GLint i) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_array_element, i); - RETURN_WITH_ERROR_IF(i < 0, GL_INVALID_VALUE); - - // This is effectively the same as `gl_draw_elements`, except we only output a single - // vertex (this is done between a `gl_begin/end` call) that is to be rendered. - if (!m_client_side_vertex_array_enabled) - return; - - if (m_client_side_color_array_enabled) { - float color[4] { 0, 0, 0, 1 }; - read_from_vertex_attribute_pointer(m_client_color_pointer, i, color); - gl_color(color[0], color[1], color[2], color[3]); - } - - for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) { - if (m_client_side_texture_coord_array_enabled[t]) { - float tex_coords[4] { 0, 0, 0, 0 }; - read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords); - gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]); - } - } - - if (m_client_side_normal_array_enabled) { - float normal[3]; - read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal); - gl_normal(normal[0], normal[1], normal[2]); - } - - float vertex[4] { 0, 0, 0, 1 }; - read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex); - gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]); -} - -void GLContext::gl_depth_range(GLdouble min, GLdouble max) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_range, min, max); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - auto options = m_rasterizer->options(); - options.depth_min = clamp(min, 0.f, 1.f); - options.depth_max = clamp(max, 0.f, 1.f); - m_rasterizer->set_options(options); -} - -void GLContext::gl_depth_func(GLenum func) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func); - - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - RETURN_WITH_ERROR_IF(!(func == GL_NEVER - || func == GL_LESS - || func == GL_EQUAL - || func == GL_LEQUAL - || func == GL_GREATER - || func == GL_NOTEQUAL - || func == GL_GEQUAL - || func == GL_ALWAYS), - GL_INVALID_ENUM); - - auto options = m_rasterizer->options(); - - switch (func) { - case GL_NEVER: - options.depth_func = GPU::DepthTestFunction::Never; - break; - case GL_ALWAYS: - options.depth_func = GPU::DepthTestFunction::Always; - break; - case GL_LESS: - options.depth_func = GPU::DepthTestFunction::Less; - break; - case GL_LEQUAL: - options.depth_func = GPU::DepthTestFunction::LessOrEqual; - break; - case GL_EQUAL: - options.depth_func = GPU::DepthTestFunction::Equal; - break; - case GL_NOTEQUAL: - options.depth_func = GPU::DepthTestFunction::NotEqual; - break; - case GL_GEQUAL: - options.depth_func = GPU::DepthTestFunction::GreaterOrEqual; - break; - case GL_GREATER: - options.depth_func = GPU::DepthTestFunction::Greater; - break; - default: - VERIFY_NOT_REACHED(); - } - - m_rasterizer->set_options(options); -} - -// General helper function to read arbitrary vertex attribute data into a float array -void GLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements) -{ - auto byte_ptr = reinterpret_cast(attrib.pointer); - auto normalize = attrib.normalize; - size_t stride = attrib.stride; - - switch (attrib.type) { - case GL_BYTE: { - if (stride == 0) - stride = sizeof(GLbyte) * attrib.size; - - for (int i = 0; i < attrib.size; i++) { - elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); - if (normalize) - elements[i] /= 0x80; - } - break; - } - case GL_UNSIGNED_BYTE: { - if (stride == 0) - stride = sizeof(GLubyte) * attrib.size; - - for (int i = 0; i < attrib.size; i++) { - elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); - if (normalize) - elements[i] /= 0xff; - } - break; - } - case GL_SHORT: { - if (stride == 0) - stride = sizeof(GLshort) * attrib.size; - - for (int i = 0; i < attrib.size; i++) { - elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); - if (normalize) - elements[i] /= 0x8000; - } - break; - } - case GL_UNSIGNED_SHORT: { - if (stride == 0) - stride = sizeof(GLushort) * attrib.size; - - for (int i = 0; i < attrib.size; i++) { - elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); - if (normalize) - elements[i] /= 0xffff; - } - break; - } - case GL_INT: { - if (stride == 0) - stride = sizeof(GLint) * attrib.size; - - for (int i = 0; i < attrib.size; i++) { - elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); - if (normalize) - elements[i] /= 0x80000000; - } - break; - } - case GL_UNSIGNED_INT: { - if (stride == 0) - stride = sizeof(GLuint) * attrib.size; - - for (int i = 0; i < attrib.size; i++) { - elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); - if (normalize) - elements[i] /= 0xffffffff; - } - break; - } - case GL_FLOAT: { - if (stride == 0) - stride = sizeof(GLfloat) * attrib.size; - - for (int i = 0; i < attrib.size; i++) - elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); - break; - } - case GL_DOUBLE: { - if (stride == 0) - stride = sizeof(GLdouble) * attrib.size; - - for (int i = 0; i < attrib.size; i++) - elements[i] = static_cast(*(reinterpret_cast(byte_ptr + stride * index) + i)); - break; - } - } -} - -void GLContext::gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) -{ - auto options = m_rasterizer->options(); - auto mask = options.color_mask; - - if (!red) - mask &= ~0x000000ff; - else - mask |= 0x000000ff; - - if (!green) - mask &= ~0x0000ff00; - else - mask |= 0x0000ff00; - - if (!blue) - mask &= ~0x00ff0000; - else - mask |= 0x00ff0000; - - if (!alpha) - mask &= ~0xff000000; - else - mask |= 0xff000000; - - options.color_mask = mask; - m_rasterizer->set_options(options); -} - -void GLContext::gl_polygon_mode(GLenum face, GLenum mode) -{ - RETURN_WITH_ERROR_IF(!(face == GL_BACK || face == GL_FRONT || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(mode == GL_POINT || mode == GL_LINE || mode == GL_FILL), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - auto options = m_rasterizer->options(); - - // FIXME: This must support different polygon modes for front- and backside - if (face == GL_BACK) { - dbgln_if(GL_DEBUG, "gl_polygon_mode(GL_BACK, {:#x}): unimplemented", mode); - return; - } - - auto map_mode = [](GLenum mode) -> GPU::PolygonMode { - switch (mode) { - case GL_FILL: - return GPU::PolygonMode::Fill; - case GL_LINE: - return GPU::PolygonMode::Line; - case GL_POINT: - return GPU::PolygonMode::Point; - default: - VERIFY_NOT_REACHED(); - } - }; - - options.polygon_mode = map_mode(mode); - m_rasterizer->set_options(options); -} - -void GLContext::gl_polygon_offset(GLfloat factor, GLfloat units) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_polygon_offset, factor, units); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - auto rasterizer_options = m_rasterizer->options(); - rasterizer_options.depth_offset_factor = factor; - rasterizer_options.depth_offset_constant = units; - m_rasterizer->set_options(rasterizer_options); -} - -void GLContext::gl_fogfv(GLenum pname, GLfloat* params) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogfv, pname, params); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - auto options = m_rasterizer->options(); - - switch (pname) { - case GL_FOG_COLOR: - options.fog_color = { params[0], params[1], params[2], params[3] }; - break; - default: - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); - } - - m_rasterizer->set_options(options); -} - -void GLContext::gl_fogf(GLenum pname, GLfloat param) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogf, pname, param); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(param < 0.0f, GL_INVALID_VALUE); - - auto options = m_rasterizer->options(); - - switch (pname) { - case GL_FOG_DENSITY: - options.fog_density = param; - break; - case GL_FOG_END: - options.fog_end = param; - break; - case GL_FOG_START: - options.fog_start = param; - break; - default: - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); - } - - m_rasterizer->set_options(options); -} - -void GLContext::gl_fogi(GLenum pname, GLint param) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogi, pname, param); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(param != GL_LINEAR && param != GL_EXP && param != GL_EXP2, GL_INVALID_ENUM); - - auto options = m_rasterizer->options(); - - switch (pname) { - case GL_FOG_MODE: - switch (param) { - case GL_LINEAR: - options.fog_mode = GPU::FogMode::Linear; - break; - case GL_EXP: - options.fog_mode = GPU::FogMode::Exp; - break; - case GL_EXP2: - options.fog_mode = GPU::FogMode::Exp2; - break; - } - break; - default: - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); - } - - m_rasterizer->set_options(options); -} - -void GLContext::gl_pixel_storei(GLenum pname, GLint param) -{ - // FIXME: Implement missing parameters - switch (pname) { - case GL_PACK_ALIGNMENT: - RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE); - m_pack_alignment = param; - break; - case GL_UNPACK_ROW_LENGTH: - RETURN_WITH_ERROR_IF(param < 0, GL_INVALID_VALUE); - m_unpack_row_length = static_cast(param); - break; - case GL_UNPACK_ALIGNMENT: - RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE); - m_unpack_alignment = param; - break; - default: - RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); - break; - } -} - -void GLContext::gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scissor, x, y, width, height); - RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE); - - auto options = m_rasterizer->options(); - options.scissor_box = { x, y, width, height }; - m_rasterizer->set_options(options); -} - -void GLContext::gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_func_separate, face, func, ref, mask); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); - - RETURN_WITH_ERROR_IF(!(func == GL_NEVER - || func == GL_LESS - || func == GL_LEQUAL - || func == GL_GREATER - || func == GL_GEQUAL - || func == GL_EQUAL - || func == GL_NOTEQUAL - || func == GL_ALWAYS), - GL_INVALID_ENUM); - - ref = clamp(ref, 0, (1 << m_device_info.stencil_bits) - 1); - - StencilFunctionOptions new_options = { func, ref, mask }; - if (face == GL_FRONT || face == GL_FRONT_AND_BACK) - m_stencil_function[Face::Front] = new_options; - if (face == GL_BACK || face == GL_FRONT_AND_BACK) - m_stencil_function[Face::Back] = new_options; - - m_stencil_configuration_dirty = true; -} - -void GLContext::gl_stencil_mask_separate(GLenum face, GLuint mask) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_mask_separate, face, mask); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - if (face == GL_FRONT || face == GL_FRONT_AND_BACK) - m_stencil_operation[Face::Front].write_mask = mask; - if (face == GL_BACK || face == GL_FRONT_AND_BACK) - m_stencil_operation[Face::Back].write_mask = mask; - - m_stencil_configuration_dirty = true; -} - -void GLContext::gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_op_separate, face, sfail, dpfail, dppass); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); - - auto is_valid_op = [](GLenum op) -> bool { - return op == GL_KEEP || op == GL_ZERO || op == GL_REPLACE || op == GL_INCR || op == GL_INCR_WRAP - || op == GL_DECR || op == GL_DECR_WRAP || op == GL_INVERT; - }; - RETURN_WITH_ERROR_IF(!is_valid_op(sfail), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!is_valid_op(dpfail), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!is_valid_op(dppass), GL_INVALID_ENUM); - - auto update_stencil_operation = [&](Face face, GLenum sfail, GLenum dpfail, GLenum dppass) { - auto& stencil_operation = m_stencil_operation[face]; - stencil_operation.op_fail = sfail; - stencil_operation.op_depth_fail = dpfail; - stencil_operation.op_pass = dppass; - }; - if (face == GL_FRONT || face == GL_FRONT_AND_BACK) - update_stencil_operation(Face::Front, sfail, dpfail, dppass); - if (face == GL_BACK || face == GL_FRONT_AND_BACK) - update_stencil_operation(Face::Back, sfail, dpfail, dppass); - - m_stencil_configuration_dirty = true; -} - -void GLContext::gl_normal(GLfloat nx, GLfloat ny, GLfloat nz) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_normal, nx, ny, nz); - - m_current_vertex_normal = { nx, ny, nz }; -} - -void GLContext::gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_raster_pos, x, y, z, w); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - m_rasterizer->set_raster_position({ x, y, z, w }, m_model_view_matrix, m_projection_matrix); -} - -void GLContext::gl_line_width(GLfloat width) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_line_width, width); - - RETURN_WITH_ERROR_IF(width <= 0, GL_INVALID_VALUE); - - m_line_width = width; -} - -void GLContext::gl_push_attrib(GLbitfield mask) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_attrib, mask); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: implement - dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_push_attrib({})", mask); -} - -void GLContext::gl_pop_attrib() -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_attrib); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: implement - dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_pop_attrib()"); -} - -void GLContext::gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GLfloat w) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_light_model, pname, x, y, z, w); - - RETURN_WITH_ERROR_IF(pname != GL_LIGHT_MODEL_AMBIENT - && pname != GL_LIGHT_MODEL_COLOR_CONTROL - && pname != GL_LIGHT_MODEL_LOCAL_VIEWER - && pname != GL_LIGHT_MODEL_TWO_SIDE, - GL_INVALID_ENUM); - - auto lighting_params = m_rasterizer->light_model(); - - switch (pname) { - case GL_LIGHT_MODEL_AMBIENT: - lighting_params.scene_ambient_color = { x, y, z, w }; - break; - case GL_LIGHT_MODEL_COLOR_CONTROL: { - GLenum color_control = static_cast(x); - RETURN_WITH_ERROR_IF(color_control != GL_SINGLE_COLOR && color_control != GL_SEPARATE_SPECULAR_COLOR, GL_INVALID_ENUM); - lighting_params.color_control = (color_control == GL_SINGLE_COLOR) ? GPU::ColorControl::SingleColor : GPU::ColorControl::SeparateSpecularColor; - break; - } - case GL_LIGHT_MODEL_LOCAL_VIEWER: - // 0 means the viewer is at infinity - // 1 means they're in local (eye) space - lighting_params.viewer_at_infinity = (x != 1.0f); - break; - case GL_LIGHT_MODEL_TWO_SIDE: - VERIFY(y == 0.0f && z == 0.0f && w == 0.0f); - lighting_params.two_sided_lighting = x; - break; - default: - VERIFY_NOT_REACHED(); - } - - m_rasterizer->set_light_model_params(lighting_params); -} - -void GLContext::gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_bitmap, width, height, xorig, yorig, xmove, ymove, bitmap); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - if (bitmap != nullptr) { - // FIXME: implement - dbgln_if(GL_DEBUG, "gl_bitmap({}, {}, {}, {}, {}, {}, {}): unimplemented", width, height, xorig, yorig, xmove, ymove, bitmap); - } - - auto raster_position = m_rasterizer->raster_position(); - raster_position.window_coordinates += { xmove, ymove, 0.f, 0.f }; - m_rasterizer->set_raster_position(raster_position); -} - -void GLContext::gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border) -{ - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_image_2d, target, level, internalformat, x, y, width, height, border); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - - // FIXME: implement - dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_copy_tex_image_2d({:#x}, {}, {:#x}, {}, {}, {}, {}, {})", - target, level, internalformat, x, y, width, height, border); -} - -void GLContext::gl_get_tex_parameter_integerv(GLenum target, GLint level, GLenum pname, GLint* params) -{ - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - // FIXME: support targets other than GL_TEXTURE_2D - RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); - // FIXME: support other parameter names - RETURN_WITH_ERROR_IF(pname < GL_TEXTURE_WIDTH || pname > GL_TEXTURE_HEIGHT, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE); - // FIXME: GL_INVALID_VALUE is generated if target is GL_TEXTURE_BUFFER and level is not zero - // FIXME: GL_INVALID_OPERATION is generated if GL_TEXTURE_COMPRESSED_IMAGE_SIZE is queried on texture images with an uncompressed internal format or on proxy targets + switch (type) { + case GL_BYTE: + if (write_red) + *reinterpret_cast(red_ptr) = float_to_i8(red); + if (write_green) + *reinterpret_cast(green_ptr) = float_to_i8(green); + if (write_blue) + *reinterpret_cast(blue_ptr) = float_to_i8(blue); + if (write_alpha) + *reinterpret_cast(alpha_ptr) = float_to_i8(alpha); + break; + case GL_UNSIGNED_BYTE: + if (write_red) + *reinterpret_cast(red_ptr) = float_to_u8(red); + if (write_green) + *reinterpret_cast(green_ptr) = float_to_u8(green); + if (write_blue) + *reinterpret_cast(blue_ptr) = float_to_u8(blue); + if (write_alpha) + *reinterpret_cast(alpha_ptr) = float_to_u8(alpha); + break; + case GL_SHORT: + if (write_red) + *reinterpret_cast(red_ptr) = float_to_i16(red); + if (write_green) + *reinterpret_cast(green_ptr) = float_to_i16(green); + if (write_blue) + *reinterpret_cast(blue_ptr) = float_to_i16(blue); + if (write_alpha) + *reinterpret_cast(alpha_ptr) = float_to_i16(alpha); + break; + case GL_UNSIGNED_SHORT: + if (write_red) + *reinterpret_cast(red_ptr) = float_to_u16(red); + if (write_green) + *reinterpret_cast(green_ptr) = float_to_u16(green); + if (write_blue) + *reinterpret_cast(blue_ptr) = float_to_u16(blue); + if (write_alpha) + *reinterpret_cast(alpha_ptr) = float_to_u16(alpha); + break; + case GL_INT: + if (write_red) + *reinterpret_cast(red_ptr) = float_to_i32(red); + if (write_green) + *reinterpret_cast(green_ptr) = float_to_i32(green); + if (write_blue) + *reinterpret_cast(blue_ptr) = float_to_i32(blue); + if (write_alpha) + *reinterpret_cast(alpha_ptr) = float_to_i32(alpha); + break; + case GL_UNSIGNED_INT: + if (write_red) + *reinterpret_cast(red_ptr) = float_to_u32(red); + if (write_green) + *reinterpret_cast(green_ptr) = float_to_u32(green); + if (write_blue) + *reinterpret_cast(blue_ptr) = float_to_u32(blue); + if (write_alpha) + *reinterpret_cast(alpha_ptr) = float_to_u32(alpha); + break; + case GL_FLOAT: + if (write_red) + *reinterpret_cast(red_ptr) = min(max(red, 0.0f), 1.0f); + if (write_green) + *reinterpret_cast(green_ptr) = min(max(green, 0.0f), 1.0f); + if (write_blue) + *reinterpret_cast(blue_ptr) = min(max(blue, 0.0f), 1.0f); + if (write_alpha) + *reinterpret_cast(alpha_ptr) = min(max(alpha, 0.0f), 1.0f); + break; + } - VERIFY(!m_active_texture_unit->texture_2d_target_texture().is_null()); - auto const texture_2d = m_active_texture_unit->texture_2d_target_texture(); + out_ptr += pixel_bytes; + } - switch (pname) { - case GL_TEXTURE_HEIGHT: - *params = texture_2d->height_at_lod(level); - break; - case GL_TEXTURE_WIDTH: - *params = texture_2d->width_at_lod(level); - break; + out_ptr += row_alignment_bytes; } } -void GLContext::gl_rect(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) +void GLContext::gl_depth_mask(GLboolean flag) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rect, x1, y1, x2, y2); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_mask, flag); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - gl_begin(GL_POLYGON); - gl_vertex(x1, y1, 0.0, 1.0); - gl_vertex(x2, y1, 0.0, 1.0); - gl_vertex(x2, y2, 0.0, 1.0); - gl_vertex(x1, y2, 0.0, 1.0); - gl_end(); + auto options = m_rasterizer->options(); + options.enable_depth_write = (flag != GL_FALSE); + m_rasterizer->set_options(options); } -void GLContext::gl_tex_gen(GLenum coord, GLenum pname, GLint param) +void GLContext::gl_clip_plane(GLenum plane, [[maybe_unused]] GLdouble const* equation) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen, coord, pname, param); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clip_plane, plane, equation); - RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR - && param != GL_OBJECT_LINEAR - && param != GL_SPHERE_MAP - && param != GL_NORMAL_MAP - && param != GL_REFLECTION_MAP, - GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF((plane < GL_CLIP_PLANE0) || (plane > GL_CLIP_PLANE5), GL_INVALID_ENUM); - GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S); - texture_coordinate_generation(m_active_texture_unit_index, capability).generation_mode = param; - m_texcoord_generation_dirty = true; + dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_clip_plane() (equation = [{} {} {} {}])", equation[0], equation[1], equation[2], equation[3]); } -void GLContext::gl_tex_gen_floatv(GLenum coord, GLenum pname, GLfloat const* params) +void GLContext::gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, void const* data) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen_floatv, coord, pname, params); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_pixels, width, height, format, type, data); + + RETURN_WITH_ERROR_IF(format < GL_COLOR_INDEX || format > GL_BGRA, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE - && pname != GL_OBJECT_PLANE - && pname != GL_EYE_PLANE, + RETURN_WITH_ERROR_IF((type < GL_BYTE || type > GL_FLOAT) + && (type < GL_UNSIGNED_BYTE_3_3_2 || type > GL_UNSIGNED_INT_10_10_10_2) + && (type < GL_UNSIGNED_BYTE_2_3_3_REV || type > GL_UNSIGNED_INT_2_10_10_10_REV), GL_INVALID_ENUM); - GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S); + RETURN_WITH_ERROR_IF(type == GL_BITMAP && !(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX), GL_INVALID_ENUM); - switch (pname) { - case GL_TEXTURE_GEN_MODE: { - auto param = static_cast(params[0]); - RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR - && param != GL_OBJECT_LINEAR - && param != GL_SPHERE_MAP - && param != GL_NORMAL_MAP - && param != GL_REFLECTION_MAP, - GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM); - - texture_coordinate_generation(m_active_texture_unit_index, capability).generation_mode = param; - break; - } - case GL_OBJECT_PLANE: - texture_coordinate_generation(m_active_texture_unit_index, capability).object_plane_coefficients = { params[0], params[1], params[2], params[3] }; - break; - case GL_EYE_PLANE: { - auto const& inverse_model_view = m_model_view_matrix.inverse(); - auto input_coefficients = FloatVector4 { params[0], params[1], params[2], params[3] }; + RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE); - // Note: we are allowed to store transformed coefficients here, according to the documentation on - // `glGetTexGen`: - // - // "The returned values are those maintained in eye coordinates. They are not equal to the values - // specified using glTexGen, unless the modelview matrix was identity when glTexGen was called." + // FIXME: GL_INVALID_OPERATION is generated if format is GL_STENCIL_INDEX and there is no stencil buffer + // FIXME: GL_INVALID_OPERATION is generated if format is GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA, + // GL_BGR, GL_BGRA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA, and the GL is in color index mode - texture_coordinate_generation(m_active_texture_unit_index, capability).eye_plane_coefficients = inverse_model_view * input_coefficients; - break; - } - default: - VERIFY_NOT_REACHED(); - } + RETURN_WITH_ERROR_IF(format != GL_RGB + && (type == GL_UNSIGNED_BYTE_3_3_2 + || type == GL_UNSIGNED_BYTE_2_3_3_REV + || type == GL_UNSIGNED_SHORT_5_6_5 + || type == GL_UNSIGNED_SHORT_5_6_5_REV), + GL_INVALID_OPERATION); - m_texcoord_generation_dirty = true; -} + RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_BGRA) + && (type == GL_UNSIGNED_SHORT_4_4_4_4 + || type == GL_UNSIGNED_SHORT_4_4_4_4_REV + || type == GL_UNSIGNED_SHORT_5_5_5_1 + || type == GL_UNSIGNED_SHORT_1_5_5_5_REV + || type == GL_UNSIGNED_INT_8_8_8_8 + || type == GL_UNSIGNED_INT_8_8_8_8_REV + || type == GL_UNSIGNED_INT_10_10_10_2 + || type == GL_UNSIGNED_INT_2_10_10_10_REV), + GL_INVALID_OPERATION); -void GLContext::present() -{ - m_rasterizer->blit_color_buffer_to(*m_frontbuffer); -} + // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER + // target and the buffer object's data store is currently mapped. + // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER + // target and the data would be unpacked from the buffer object such that the memory reads required would + // exceed the data store size. + // FIXME: GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER + // target and data is not evenly divisible into the number of bytes needed to store in memory a datum + // indicated by type. -void GLContext::sync_device_config() -{ - sync_device_sampler_config(); - sync_device_texcoord_config(); - sync_light_state(); - sync_stencil_configuration(); -} + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); -void GLContext::sync_device_sampler_config() -{ - if (!m_sampler_config_is_dirty) + // FIXME: we only support RGBA + UNSIGNED_BYTE and DEPTH_COMPONENT + UNSIGNED_SHORT, implement all combinations! + if (!((format == GL_RGBA && type == GL_UNSIGNED_BYTE) || (format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_SHORT))) { + dbgln_if(GL_DEBUG, "gl_draw_pixels(): support for format {:#x} and/or type {:#x} not implemented", format, type); return; + } - m_sampler_config_is_dirty = false; - - for (unsigned i = 0; i < m_texture_units.size(); ++i) { - auto const& texture_unit = m_texture_units[i]; - - if (!texture_unit.texture_2d_enabled()) - continue; - - GPU::SamplerConfig config; - - auto texture_2d = texture_unit.texture_2d_target_texture(); - if (texture_2d.is_null()) { - config.bound_image = nullptr; - m_rasterizer->set_sampler_config(i, config); - continue; - } + // FIXME: implement support for pixel parameters such as GL_UNPACK_ALIGNMENT - config.bound_image = texture_2d->device_image(); + if (format == GL_RGBA) { + auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height }); + RETURN_WITH_ERROR_IF(bitmap_or_error.is_error(), GL_OUT_OF_MEMORY); + auto bitmap = bitmap_or_error.release_value(); - auto const& sampler = texture_2d->sampler(); + auto pixel_data = static_cast(data); + for (int y = 0; y < height; ++y) + for (int x = 0; x < width; ++x) + bitmap->set_pixel(x, y, Color::from_argb(*(pixel_data++))); - switch (sampler.min_filter()) { - case GL_NEAREST: - config.texture_min_filter = GPU::TextureFilter::Nearest; - config.mipmap_filter = GPU::MipMapFilter::None; - break; - case GL_LINEAR: - config.texture_min_filter = GPU::TextureFilter::Linear; - config.mipmap_filter = GPU::MipMapFilter::None; - break; - case GL_NEAREST_MIPMAP_NEAREST: - config.texture_min_filter = GPU::TextureFilter::Nearest; - config.mipmap_filter = GPU::MipMapFilter::Nearest; - break; - case GL_LINEAR_MIPMAP_NEAREST: - config.texture_min_filter = GPU::TextureFilter::Linear; - config.mipmap_filter = GPU::MipMapFilter::Nearest; - break; - case GL_NEAREST_MIPMAP_LINEAR: - config.texture_min_filter = GPU::TextureFilter::Nearest; - config.mipmap_filter = GPU::MipMapFilter::Linear; - break; - case GL_LINEAR_MIPMAP_LINEAR: - config.texture_min_filter = GPU::TextureFilter::Linear; - config.mipmap_filter = GPU::MipMapFilter::Linear; - break; - default: - VERIFY_NOT_REACHED(); - } + m_rasterizer->blit_to_color_buffer_at_raster_position(bitmap); + } else if (format == GL_DEPTH_COMPONENT) { + Vector depth_values; + depth_values.ensure_capacity(width * height); - switch (sampler.mag_filter()) { - case GL_NEAREST: - config.texture_mag_filter = GPU::TextureFilter::Nearest; - break; - case GL_LINEAR: - config.texture_mag_filter = GPU::TextureFilter::Linear; - break; - default: - VERIFY_NOT_REACHED(); + auto depth_data = static_cast(data); + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + auto u16_value = *(depth_data++); + auto float_value = static_cast(u16_value) / NumericLimits::max(); + depth_values.append(float_value); + } } - switch (sampler.wrap_s_mode()) { - case GL_CLAMP: - config.texture_wrap_u = GPU::TextureWrapMode::Clamp; - break; - case GL_CLAMP_TO_BORDER: - config.texture_wrap_u = GPU::TextureWrapMode::ClampToBorder; - break; - case GL_CLAMP_TO_EDGE: - config.texture_wrap_u = GPU::TextureWrapMode::ClampToEdge; - break; - case GL_REPEAT: - config.texture_wrap_u = GPU::TextureWrapMode::Repeat; - break; - case GL_MIRRORED_REPEAT: - config.texture_wrap_u = GPU::TextureWrapMode::MirroredRepeat; - break; - default: - VERIFY_NOT_REACHED(); - } + m_rasterizer->blit_to_depth_buffer_at_raster_position(depth_values, width, height); + } else { + VERIFY_NOT_REACHED(); + } +} - switch (sampler.wrap_t_mode()) { - case GL_CLAMP: - config.texture_wrap_v = GPU::TextureWrapMode::Clamp; - break; - case GL_CLAMP_TO_BORDER: - config.texture_wrap_v = GPU::TextureWrapMode::ClampToBorder; - break; - case GL_CLAMP_TO_EDGE: - config.texture_wrap_v = GPU::TextureWrapMode::ClampToEdge; - break; - case GL_REPEAT: - config.texture_wrap_v = GPU::TextureWrapMode::Repeat; - break; - case GL_MIRRORED_REPEAT: - config.texture_wrap_v = GPU::TextureWrapMode::MirroredRepeat; - break; - default: - VERIFY_NOT_REACHED(); - } +void GLContext::gl_depth_range(GLdouble min, GLdouble max) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_range, min, max); - switch (texture_unit.env_mode()) { - case GL_MODULATE: - config.fixed_function_texture_env_mode = GPU::TextureEnvMode::Modulate; - break; - case GL_REPLACE: - config.fixed_function_texture_env_mode = GPU::TextureEnvMode::Replace; - break; - case GL_DECAL: - config.fixed_function_texture_env_mode = GPU::TextureEnvMode::Decal; - break; - case GL_ADD: - config.fixed_function_texture_env_mode = GPU::TextureEnvMode::Add; - break; - default: - VERIFY_NOT_REACHED(); - } + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - config.border_color = sampler.border_color(); - m_rasterizer->set_sampler_config(i, config); - } + auto options = m_rasterizer->options(); + options.depth_min = clamp(min, 0.f, 1.f); + options.depth_max = clamp(max, 0.f, 1.f); + m_rasterizer->set_options(options); } -void GLContext::sync_light_state() +void GLContext::gl_depth_func(GLenum func) { - if (!m_light_state_is_dirty) - return; + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_depth_func, func); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - m_light_state_is_dirty = false; + RETURN_WITH_ERROR_IF(!(func == GL_NEVER + || func == GL_LESS + || func == GL_EQUAL + || func == GL_LEQUAL + || func == GL_GREATER + || func == GL_NOTEQUAL + || func == GL_GEQUAL + || func == GL_ALWAYS), + GL_INVALID_ENUM); auto options = m_rasterizer->options(); - options.color_material_enabled = m_color_material_enabled; - switch (m_color_material_face) { - case GL_BACK: - options.color_material_face = GPU::ColorMaterialFace::Back; + + switch (func) { + case GL_NEVER: + options.depth_func = GPU::DepthTestFunction::Never; break; - case GL_FRONT: - options.color_material_face = GPU::ColorMaterialFace::Front; + case GL_ALWAYS: + options.depth_func = GPU::DepthTestFunction::Always; break; - case GL_FRONT_AND_BACK: - options.color_material_face = GPU::ColorMaterialFace::FrontAndBack; + case GL_LESS: + options.depth_func = GPU::DepthTestFunction::Less; break; - default: - VERIFY_NOT_REACHED(); - } - switch (m_color_material_mode) { - case GL_AMBIENT: - options.color_material_mode = GPU::ColorMaterialMode::Ambient; + case GL_LEQUAL: + options.depth_func = GPU::DepthTestFunction::LessOrEqual; break; - case GL_AMBIENT_AND_DIFFUSE: - options.color_material_mode = GPU::ColorMaterialMode::Ambient; - options.color_material_mode = GPU::ColorMaterialMode::Diffuse; + case GL_EQUAL: + options.depth_func = GPU::DepthTestFunction::Equal; break; - case GL_DIFFUSE: - options.color_material_mode = GPU::ColorMaterialMode::Diffuse; + case GL_NOTEQUAL: + options.depth_func = GPU::DepthTestFunction::NotEqual; break; - case GL_EMISSION: - options.color_material_mode = GPU::ColorMaterialMode::Emissive; + case GL_GEQUAL: + options.depth_func = GPU::DepthTestFunction::GreaterOrEqual; break; - case GL_SPECULAR: - options.color_material_mode = GPU::ColorMaterialMode::Specular; + case GL_GREATER: + options.depth_func = GPU::DepthTestFunction::Greater; break; default: VERIFY_NOT_REACHED(); } - m_rasterizer->set_options(options); - - for (auto light_id = 0u; light_id < m_device_info.num_lights; light_id++) { - auto const& current_light_state = m_light_states.at(light_id); - m_rasterizer->set_light_state(light_id, current_light_state); - } - m_rasterizer->set_material_state(GPU::Face::Front, m_material_states[Face::Front]); - m_rasterizer->set_material_state(GPU::Face::Back, m_material_states[Face::Back]); + m_rasterizer->set_options(options); } -void GLContext::sync_device_texcoord_config() +void GLContext::gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) { - if (!m_texcoord_generation_dirty) - return; - m_texcoord_generation_dirty = false; - auto options = m_rasterizer->options(); + auto mask = options.color_mask; + + if (!red) + mask &= ~0x000000ff; + else + mask |= 0x000000ff; + + if (!green) + mask &= ~0x0000ff00; + else + mask |= 0x0000ff00; - for (size_t i = 0; i < m_device_info.num_texture_units; ++i) { + if (!blue) + mask &= ~0x00ff0000; + else + mask |= 0x00ff0000; - u8 enabled_coordinates = GPU::TexCoordGenerationCoordinate::None; - for (GLenum capability = GL_TEXTURE_GEN_S; capability <= GL_TEXTURE_GEN_Q; ++capability) { - auto const context_coordinate_config = texture_coordinate_generation(i, capability); - if (!context_coordinate_config.enabled) - continue; + if (!alpha) + mask &= ~0xff000000; + else + mask |= 0xff000000; - GPU::TexCoordGenerationConfig* texcoord_generation_config; - switch (capability) { - case GL_TEXTURE_GEN_S: - enabled_coordinates |= GPU::TexCoordGenerationCoordinate::S; - texcoord_generation_config = &options.texcoord_generation_config[i][0]; - break; - case GL_TEXTURE_GEN_T: - enabled_coordinates |= GPU::TexCoordGenerationCoordinate::T; - texcoord_generation_config = &options.texcoord_generation_config[i][1]; - break; - case GL_TEXTURE_GEN_R: - enabled_coordinates |= GPU::TexCoordGenerationCoordinate::R; - texcoord_generation_config = &options.texcoord_generation_config[i][2]; - break; - case GL_TEXTURE_GEN_Q: - enabled_coordinates |= GPU::TexCoordGenerationCoordinate::Q; - texcoord_generation_config = &options.texcoord_generation_config[i][3]; - break; - default: - VERIFY_NOT_REACHED(); - } + options.color_mask = mask; + m_rasterizer->set_options(options); +} - switch (context_coordinate_config.generation_mode) { - case GL_OBJECT_LINEAR: - texcoord_generation_config->mode = GPU::TexCoordGenerationMode::ObjectLinear; - texcoord_generation_config->coefficients = context_coordinate_config.object_plane_coefficients; - break; - case GL_EYE_LINEAR: - texcoord_generation_config->mode = GPU::TexCoordGenerationMode::EyeLinear; - texcoord_generation_config->coefficients = context_coordinate_config.eye_plane_coefficients; - break; - case GL_SPHERE_MAP: - texcoord_generation_config->mode = GPU::TexCoordGenerationMode::SphereMap; - break; - case GL_REFLECTION_MAP: - texcoord_generation_config->mode = GPU::TexCoordGenerationMode::ReflectionMap; - break; - case GL_NORMAL_MAP: - texcoord_generation_config->mode = GPU::TexCoordGenerationMode::NormalMap; - break; - } - } - options.texcoord_generation_enabled_coordinates[i] = enabled_coordinates; - } +void GLContext::gl_polygon_mode(GLenum face, GLenum mode) +{ + RETURN_WITH_ERROR_IF(!(face == GL_BACK || face == GL_FRONT || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(mode == GL_POINT || mode == GL_LINE || mode == GL_FILL), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - m_rasterizer->set_options(options); -} + auto options = m_rasterizer->options(); -void GLContext::sync_stencil_configuration() -{ - if (!m_stencil_configuration_dirty) + // FIXME: This must support different polygon modes for front- and backside + if (face == GL_BACK) { + dbgln_if(GL_DEBUG, "gl_polygon_mode(GL_BACK, {:#x}): unimplemented", mode); return; - m_stencil_configuration_dirty = false; - - auto set_device_stencil = [&](GPU::Face face, StencilFunctionOptions func, StencilOperationOptions op) { - GPU::StencilConfiguration device_configuration; - - // Stencil test function - auto map_func = [](GLenum func) -> GPU::StencilTestFunction { - switch (func) { - case GL_ALWAYS: - return GPU::StencilTestFunction::Always; - case GL_EQUAL: - return GPU::StencilTestFunction::Equal; - case GL_GEQUAL: - return GPU::StencilTestFunction::GreaterOrEqual; - case GL_GREATER: - return GPU::StencilTestFunction::Greater; - case GL_LESS: - return GPU::StencilTestFunction::Less; - case GL_LEQUAL: - return GPU::StencilTestFunction::LessOrEqual; - case GL_NEVER: - return GPU::StencilTestFunction::Never; - case GL_NOTEQUAL: - return GPU::StencilTestFunction::NotEqual; - } - VERIFY_NOT_REACHED(); - }; - device_configuration.test_function = map_func(func.func); - device_configuration.reference_value = func.reference_value; - device_configuration.test_mask = func.mask; - - // Stencil operation - auto map_operation = [](GLenum operation) -> GPU::StencilOperation { - switch (operation) { - case GL_DECR: - return GPU::StencilOperation::Decrement; - case GL_DECR_WRAP: - return GPU::StencilOperation::DecrementWrap; - case GL_INCR: - return GPU::StencilOperation::Increment; - case GL_INCR_WRAP: - return GPU::StencilOperation::IncrementWrap; - case GL_INVERT: - return GPU::StencilOperation::Invert; - case GL_KEEP: - return GPU::StencilOperation::Keep; - case GL_REPLACE: - return GPU::StencilOperation::Replace; - case GL_ZERO: - return GPU::StencilOperation::Zero; - } - VERIFY_NOT_REACHED(); - }; - device_configuration.on_stencil_test_fail = map_operation(op.op_fail); - device_configuration.on_depth_test_fail = map_operation(op.op_depth_fail); - device_configuration.on_pass = map_operation(op.op_pass); - device_configuration.write_mask = op.write_mask; + } - m_rasterizer->set_stencil_configuration(face, device_configuration); + auto map_mode = [](GLenum mode) -> GPU::PolygonMode { + switch (mode) { + case GL_FILL: + return GPU::PolygonMode::Fill; + case GL_LINE: + return GPU::PolygonMode::Line; + case GL_POINT: + return GPU::PolygonMode::Point; + default: + VERIFY_NOT_REACHED(); + } }; - set_device_stencil(GPU::Face::Front, m_stencil_function[Face::Front], m_stencil_operation[Face::Front]); - set_device_stencil(GPU::Face::Back, m_stencil_function[Face::Back], m_stencil_operation[Face::Back]); + + options.polygon_mode = map_mode(mode); + m_rasterizer->set_options(options); } -void GLContext::build_extension_string() +void GLContext::gl_polygon_offset(GLfloat factor, GLfloat units) { - Vector extensions; - - // FIXME: npot texture support became a required core feature starting with OpenGL 2.0 (https://www.khronos.org/opengl/wiki/NPOT_Texture) - // Ideally we would verify if the selected device adheres to the requested OpenGL context version before context creation - // and refuse to create a context if it doesn't. - if (m_device_info.supports_npot_textures) - extensions.append("GL_ARB_texture_non_power_of_two"); - - if (m_device_info.num_texture_units > 1) - extensions.append("GL_ARB_multitexture"); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_polygon_offset, factor, units); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - m_extensions = String::join(" ", extensions); + auto rasterizer_options = m_rasterizer->options(); + rasterizer_options.depth_offset_factor = factor; + rasterizer_options.depth_offset_constant = units; + m_rasterizer->set_options(rasterizer_options); } -void GLContext::gl_lightf(GLenum light, GLenum pname, GLfloat param) +void GLContext::gl_fogfv(GLenum pname, GLfloat* params) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightf, light, pname, param); - + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogfv, pname, params); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname != GL_SPOT_EXPONENT || pname != GL_SPOT_CUTOFF), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(param < 0.f, GL_INVALID_VALUE); - auto& light_state = m_light_states.at(light - GL_LIGHT0); + auto options = m_rasterizer->options(); switch (pname) { - case GL_CONSTANT_ATTENUATION: - light_state.constant_attenuation = param; - break; - case GL_LINEAR_ATTENUATION: - light_state.linear_attenuation = param; - break; - case GL_QUADRATIC_ATTENUATION: - light_state.quadratic_attenuation = param; - break; - case GL_SPOT_EXPONENT: - RETURN_WITH_ERROR_IF(param > 128.f, GL_INVALID_VALUE); - light_state.spotlight_exponent = param; - break; - case GL_SPOT_CUTOFF: - RETURN_WITH_ERROR_IF(param > 90.f && param != 180.f, GL_INVALID_VALUE); - light_state.spotlight_cutoff_angle = param; + case GL_FOG_COLOR: + options.fog_color = { params[0], params[1], params[2], params[3] }; break; default: - VERIFY_NOT_REACHED(); + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); } - m_light_state_is_dirty = true; + m_rasterizer->set_options(options); } -void GLContext::gl_lightfv(GLenum light, GLenum pname, GLfloat const* params) +void GLContext::gl_fogf(GLenum pname, GLfloat param) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightfv, light, pname, params); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogf, pname, param); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_POSITION || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname == GL_SPOT_CUTOFF || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_DIRECTION), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(param < 0.0f, GL_INVALID_VALUE); - auto& light_state = m_light_states.at(light - GL_LIGHT0); + auto options = m_rasterizer->options(); switch (pname) { - case GL_AMBIENT: - light_state.ambient_intensity = { params[0], params[1], params[2], params[3] }; - break; - case GL_DIFFUSE: - light_state.diffuse_intensity = { params[0], params[1], params[2], params[3] }; - break; - case GL_SPECULAR: - light_state.specular_intensity = { params[0], params[1], params[2], params[3] }; - break; - case GL_POSITION: - light_state.position = { params[0], params[1], params[2], params[3] }; - light_state.position = m_model_view_matrix * light_state.position; - break; - case GL_CONSTANT_ATTENUATION: - RETURN_WITH_ERROR_IF(params[0] < 0.f, GL_INVALID_VALUE); - light_state.constant_attenuation = params[0]; - break; - case GL_LINEAR_ATTENUATION: - RETURN_WITH_ERROR_IF(params[0] < 0.f, GL_INVALID_VALUE); - light_state.linear_attenuation = params[0]; - break; - case GL_QUADRATIC_ATTENUATION: - RETURN_WITH_ERROR_IF(params[0] < 0.f, GL_INVALID_VALUE); - light_state.quadratic_attenuation = params[0]; - break; - case GL_SPOT_EXPONENT: { - auto exponent = params[0]; - RETURN_WITH_ERROR_IF(exponent < 0.f || exponent > 128.f, GL_INVALID_VALUE); - light_state.spotlight_exponent = exponent; + case GL_FOG_DENSITY: + options.fog_density = param; break; - } - case GL_SPOT_CUTOFF: { - auto cutoff = params[0]; - RETURN_WITH_ERROR_IF((cutoff < 0.f || cutoff > 90.f) && cutoff != 180.f, GL_INVALID_VALUE); - light_state.spotlight_cutoff_angle = cutoff; + case GL_FOG_END: + options.fog_end = param; break; - } - case GL_SPOT_DIRECTION: { - FloatVector4 direction_vector = { params[0], params[1], params[2], 0.f }; - direction_vector = m_model_view_matrix * direction_vector; - light_state.spotlight_direction = direction_vector.xyz(); + case GL_FOG_START: + options.fog_start = param; break; - } default: - VERIFY_NOT_REACHED(); + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); } - m_light_state_is_dirty = true; + m_rasterizer->set_options(options); } -void GLContext::gl_lightiv(GLenum light, GLenum pname, GLint const* params) +void GLContext::gl_fogi(GLenum pname, GLint param) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightiv, light, pname, params); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_fogi, pname, param); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_POSITION || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname == GL_SPOT_CUTOFF || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_DIRECTION), GL_INVALID_ENUM); - - auto& light_state = m_light_states[light - GL_LIGHT0]; + RETURN_WITH_ERROR_IF(param != GL_LINEAR && param != GL_EXP && param != GL_EXP2, GL_INVALID_ENUM); - auto const to_float_vector = [](GLfloat x, GLfloat y, GLfloat z, GLfloat w) { - return FloatVector4(x, y, z, w); - }; + auto options = m_rasterizer->options(); switch (pname) { - case GL_AMBIENT: - light_state.ambient_intensity = to_float_vector(params[0], params[1], params[2], params[3]); - break; - case GL_DIFFUSE: - light_state.diffuse_intensity = to_float_vector(params[0], params[1], params[2], params[3]); - break; - case GL_SPECULAR: - light_state.specular_intensity = to_float_vector(params[0], params[1], params[2], params[3]); - break; - case GL_POSITION: - light_state.position = to_float_vector(params[0], params[1], params[2], params[3]); - light_state.position = m_model_view_matrix * light_state.position; - break; - case GL_CONSTANT_ATTENUATION: - RETURN_WITH_ERROR_IF(params[0] < 0, GL_INVALID_VALUE); - light_state.constant_attenuation = static_cast(params[0]); - break; - case GL_LINEAR_ATTENUATION: - RETURN_WITH_ERROR_IF(params[0] < 0, GL_INVALID_VALUE); - light_state.linear_attenuation = static_cast(params[0]); - break; - case GL_QUADRATIC_ATTENUATION: - RETURN_WITH_ERROR_IF(params[0] < 0, GL_INVALID_VALUE); - light_state.quadratic_attenuation = static_cast(params[0]); - break; - case GL_SPOT_EXPONENT: { - auto exponent = static_cast(params[0]); - RETURN_WITH_ERROR_IF(exponent < 0.f || exponent > 128.f, GL_INVALID_VALUE); - light_state.spotlight_exponent = exponent; - break; - } - case GL_SPOT_CUTOFF: { - auto cutoff = static_cast(params[0]); - RETURN_WITH_ERROR_IF((cutoff < 0.f || cutoff > 90.f) && cutoff != 180.f, GL_INVALID_VALUE); - light_state.spotlight_cutoff_angle = cutoff; - break; - } - case GL_SPOT_DIRECTION: { - auto direction_vector = to_float_vector(params[0], params[1], params[2], 0.0f); - direction_vector = m_model_view_matrix * direction_vector; - light_state.spotlight_direction = direction_vector.xyz(); + case GL_FOG_MODE: + switch (param) { + case GL_LINEAR: + options.fog_mode = GPU::FogMode::Linear; + break; + case GL_EXP: + options.fog_mode = GPU::FogMode::Exp; + break; + case GL_EXP2: + options.fog_mode = GPU::FogMode::Exp2; + break; + } break; - } default: - VERIFY_NOT_REACHED(); + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); } - m_light_state_is_dirty = true; + m_rasterizer->set_options(options); } -void GLContext::gl_materialf(GLenum face, GLenum pname, GLfloat param) +void GLContext::gl_pixel_storei(GLenum pname, GLint param) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialf, face, pname, param); - RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(pname != GL_SHININESS, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(param > 128.0f, GL_INVALID_VALUE); - - switch (face) { - case GL_FRONT: - m_material_states[Face::Front].shininess = param; + // FIXME: Implement missing parameters + switch (pname) { + case GL_PACK_ALIGNMENT: + RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE); + m_pack_alignment = param; break; - case GL_BACK: - m_material_states[Face::Back].shininess = param; + case GL_UNPACK_ROW_LENGTH: + RETURN_WITH_ERROR_IF(param < 0, GL_INVALID_VALUE); + m_unpack_row_length = static_cast(param); break; - case GL_FRONT_AND_BACK: - m_material_states[Face::Front].shininess = param; - m_material_states[Face::Back].shininess = param; + case GL_UNPACK_ALIGNMENT: + RETURN_WITH_ERROR_IF(param != 1 && param != 2 && param != 4 && param != 8, GL_INVALID_VALUE); + m_unpack_alignment = param; break; default: - VERIFY_NOT_REACHED(); + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); } - - m_light_state_is_dirty = true; } -void GLContext::gl_materialfv(GLenum face, GLenum pname, GLfloat const* params) +void GLContext::gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialfv, face, pname, params); - RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION || pname == GL_SHININESS || pname == GL_AMBIENT_AND_DIFFUSE), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF((pname == GL_SHININESS && *params > 128.0f), GL_INVALID_VALUE); - - auto update_material = [](GPU::Material& material, GLenum pname, GLfloat const* params) { - switch (pname) { - case GL_AMBIENT: - material.ambient = { params[0], params[1], params[2], params[3] }; - break; - case GL_DIFFUSE: - material.diffuse = { params[0], params[1], params[2], params[3] }; - break; - case GL_SPECULAR: - material.specular = { params[0], params[1], params[2], params[3] }; - break; - case GL_EMISSION: - material.emissive = { params[0], params[1], params[2], params[3] }; - break; - case GL_SHININESS: - material.shininess = *params; - break; - case GL_AMBIENT_AND_DIFFUSE: - material.ambient = { params[0], params[1], params[2], params[3] }; - material.diffuse = { params[0], params[1], params[2], params[3] }; - break; - } - }; + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scissor, x, y, width, height); + RETURN_WITH_ERROR_IF(width < 0 || height < 0, GL_INVALID_VALUE); - switch (face) { - case GL_FRONT: - update_material(m_material_states[Face::Front], pname, params); - break; - case GL_BACK: - update_material(m_material_states[Face::Back], pname, params); - break; - case GL_FRONT_AND_BACK: - update_material(m_material_states[Face::Front], pname, params); - update_material(m_material_states[Face::Back], pname, params); - break; - } + auto options = m_rasterizer->options(); + options.scissor_box = { x, y, width, height }; + m_rasterizer->set_options(options); +} - m_light_state_is_dirty = true; +void GLContext::gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_raster_pos, x, y, z, w); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + m_rasterizer->set_raster_position({ x, y, z, w }, m_model_view_matrix, m_projection_matrix); } -void GLContext::gl_materialiv(GLenum face, GLenum pname, GLint const* params) +void GLContext::gl_line_width(GLfloat width) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialiv, face, pname, params); - RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION || pname == GL_SHININESS || pname == GL_AMBIENT_AND_DIFFUSE), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF((pname == GL_SHININESS && *params > 128), GL_INVALID_VALUE); - - auto update_material = [](GPU::Material& material, GLenum pname, GLint const* params) { - switch (pname) { - case GL_AMBIENT: - material.ambient = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; - break; - case GL_DIFFUSE: - material.diffuse = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; - break; - case GL_SPECULAR: - material.specular = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; - break; - case GL_EMISSION: - material.emissive = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; - break; - case GL_SHININESS: - material.shininess = static_cast(params[0]); - break; - case GL_AMBIENT_AND_DIFFUSE: - material.ambient = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; - material.diffuse = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; - break; - } - }; + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_line_width, width); - switch (face) { - case GL_FRONT: - update_material(m_material_states[Face::Front], pname, params); - break; - case GL_BACK: - update_material(m_material_states[Face::Back], pname, params); - break; - case GL_FRONT_AND_BACK: - update_material(m_material_states[Face::Front], pname, params); - update_material(m_material_states[Face::Back], pname, params); - break; - } + RETURN_WITH_ERROR_IF(width <= 0, GL_INVALID_VALUE); - m_light_state_is_dirty = true; + m_line_width = width; } -void GLContext::gl_color_material(GLenum face, GLenum mode) +void GLContext::gl_push_attrib(GLbitfield mask) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color_material, face, mode); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_attrib, mask); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(face != GL_FRONT - && face != GL_BACK - && face != GL_FRONT_AND_BACK, - GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(mode != GL_EMISSION - && mode != GL_AMBIENT - && mode != GL_DIFFUSE - && mode != GL_SPECULAR - && mode != GL_AMBIENT_AND_DIFFUSE, - GL_INVALID_ENUM); - - m_color_material_face = face; - m_color_material_mode = mode; - - m_light_state_is_dirty = true; + // FIXME: implement + dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_push_attrib({})", mask); } -void GLContext::gl_get_light(GLenum light, GLenum pname, void* params, GLenum type) +void GLContext::gl_pop_attrib() { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_get_light, light, pname, params, type); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_attrib); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light > GL_LIGHT0 + m_device_info.num_lights, GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_SPOT_DIRECTION || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_CUTOFF || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION), GL_INVALID_ENUM); - if (type == GL_FLOAT) - get_light_param(light, pname, static_cast(params)); - else if (type == GL_INT) - get_light_param(light, pname, static_cast(params)); - else - VERIFY_NOT_REACHED(); + // FIXME: implement + dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_pop_attrib()"); } -template -void GLContext::get_light_param(GLenum light, GLenum pname, T* params) +void GLContext::gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap) { - auto const& light_state = m_light_states[light - GL_LIGHT0]; - switch (pname) { - case GL_AMBIENT: - params[0] = light_state.ambient_intensity.x(); - params[1] = light_state.ambient_intensity.y(); - params[2] = light_state.ambient_intensity.z(); - params[3] = light_state.ambient_intensity.w(); - break; - case GL_DIFFUSE: - params[0] = light_state.diffuse_intensity.x(); - params[1] = light_state.diffuse_intensity.y(); - params[2] = light_state.diffuse_intensity.z(); - params[3] = light_state.diffuse_intensity.w(); - break; - case GL_SPECULAR: - params[0] = light_state.specular_intensity.x(); - params[1] = light_state.specular_intensity.y(); - params[2] = light_state.specular_intensity.z(); - params[3] = light_state.specular_intensity.w(); - break; - case GL_SPOT_DIRECTION: - params[0] = light_state.spotlight_direction.x(); - params[1] = light_state.spotlight_direction.y(); - params[2] = light_state.spotlight_direction.z(); - break; - case GL_SPOT_EXPONENT: - *params = light_state.spotlight_exponent; - break; - case GL_SPOT_CUTOFF: - *params = light_state.spotlight_cutoff_angle; - break; - case GL_CONSTANT_ATTENUATION: - *params = light_state.constant_attenuation; - break; - case GL_LINEAR_ATTENUATION: - *params = light_state.linear_attenuation; - break; - case GL_QUADRATIC_ATTENUATION: - *params = light_state.quadratic_attenuation; - break; + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_bitmap, width, height, xorig, yorig, xmove, ymove, bitmap); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + if (bitmap != nullptr) { + // FIXME: implement + dbgln_if(GL_DEBUG, "gl_bitmap({}, {}, {}, {}, {}, {}, {}): unimplemented", width, height, xorig, yorig, xmove, ymove, bitmap); } + + auto raster_position = m_rasterizer->raster_position(); + raster_position.window_coordinates += { xmove, ymove, 0.f, 0.f }; + m_rasterizer->set_raster_position(raster_position); } -void GLContext::gl_get_material(GLenum face, GLenum pname, void* params, GLenum type) +void GLContext::gl_rect(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_get_material, face, pname, params, type); + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rect, x1, y1, x2, y2); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); - RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK), GL_INVALID_ENUM); - Face material_face = Front; - switch (face) { - case GL_FRONT: - material_face = Front; - break; - case GL_BACK: - material_face = Back; - break; - } + gl_begin(GL_POLYGON); + gl_vertex(x1, y1, 0.0, 1.0); + gl_vertex(x2, y1, 0.0, 1.0); + gl_vertex(x2, y2, 0.0, 1.0); + gl_vertex(x1, y2, 0.0, 1.0); + gl_end(); +} - if (type == GL_FLOAT) - get_material_param(material_face, pname, static_cast(params)); - else if (type == GL_INT) - get_material_param(material_face, pname, static_cast(params)); - else - VERIFY_NOT_REACHED(); +void GLContext::present() +{ + m_rasterizer->blit_color_buffer_to(*m_frontbuffer); } -template -void GLContext::get_material_param(Face face, GLenum pname, T* params) +void GLContext::sync_device_config() { - auto const& material = m_material_states[face]; - switch (pname) { - case GL_AMBIENT: - params[0] = static_cast(material.ambient.x()); - params[1] = static_cast(material.ambient.y()); - params[2] = static_cast(material.ambient.z()); - params[3] = static_cast(material.ambient.w()); - break; - case GL_DIFFUSE: - params[0] = static_cast(material.diffuse.x()); - params[1] = static_cast(material.diffuse.y()); - params[2] = static_cast(material.diffuse.z()); - params[3] = static_cast(material.diffuse.w()); - break; - case GL_SPECULAR: - params[0] = static_cast(material.specular.x()); - params[1] = static_cast(material.specular.y()); - params[2] = static_cast(material.specular.z()); - params[3] = static_cast(material.specular.w()); - break; - case GL_EMISSION: - params[0] = static_cast(material.emissive.x()); - params[1] = static_cast(material.emissive.y()); - params[2] = static_cast(material.emissive.z()); - params[3] = static_cast(material.emissive.w()); - break; - case GL_SHININESS: - *params = material.shininess; - break; - } + sync_device_sampler_config(); + sync_device_texcoord_config(); + sync_light_state(); + sync_stencil_configuration(); } -void GLContext::gl_copy_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height) +void GLContext::build_extension_string() { - APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_sub_image_2d, target, level, xoffset, yoffset, x, y, width, height); - RETURN_WITH_ERROR_IF(!(target == GL_TEXTURE_2D || target == GL_TEXTURE_1D_ARRAY), GL_INVALID_ENUM); - RETURN_WITH_ERROR_IF(level < 0, GL_INVALID_VALUE); - RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + Vector extensions; - // FIXME: implement - dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_copy_tex_sub_image_2d({:#x}, {}, {}, {}, {}, {}, {}, {})", - target, level, xoffset, yoffset, x, y, width, height); + // FIXME: npot texture support became a required core feature starting with OpenGL 2.0 (https://www.khronos.org/opengl/wiki/NPOT_Texture) + // Ideally we would verify if the selected device adheres to the requested OpenGL context version before context creation + // and refuse to create a context if it doesn't. + if (m_device_info.supports_npot_textures) + extensions.append("GL_ARB_texture_non_power_of_two"); + + if (m_device_info.num_texture_units > 1) + extensions.append("GL_ARB_multitexture"); + + m_extensions = String::join(" ", extensions); } NonnullOwnPtr create_context(Gfx::Bitmap& bitmap) diff --git a/Userland/Libraries/LibGL/GLContext.h b/Userland/Libraries/LibGL/GLContext.h index 8404fa9fa1..8ed5b14ee6 100644 --- a/Userland/Libraries/LibGL/GLContext.h +++ b/Userland/Libraries/LibGL/GLContext.h @@ -30,6 +30,41 @@ namespace GL { +#define APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(name, ...) \ + if (should_append_to_listing()) { \ + append_to_listing<&GLContext::name>(__VA_ARGS__); \ + if (!should_execute_after_appending_to_listing()) \ + return; \ + } + +#define APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(name, arg) \ + if (should_append_to_listing()) { \ + auto ptr = store_in_listing(arg); \ + append_to_listing<&GLContext::name>(*ptr); \ + if (!should_execute_after_appending_to_listing()) \ + return; \ + } + +#define RETURN_WITH_ERROR_IF(condition, error) \ + if (condition) { \ + dbgln_if(GL_DEBUG, "{}(): error {:#x}", __func__, error); \ + if (m_error == GL_NO_ERROR) \ + m_error = error; \ + return; \ + } + +#define RETURN_VALUE_WITH_ERROR_IF(condition, error, return_value) \ + if (condition) { \ + dbgln_if(GL_DEBUG, "{}(): error {:#x}", __func__, error); \ + if (m_error == GL_NO_ERROR) \ + m_error = error; \ + return return_value; \ + } + +constexpr size_t MODELVIEW_MATRIX_STACK_LIMIT = 64; +constexpr size_t PROJECTION_MATRIX_STACK_LIMIT = 8; +constexpr size_t TEXTURE_MATRIX_STACK_LIMIT = 8; + struct ContextParameter { GLenum type; bool is_capability { false }; diff --git a/Userland/Libraries/LibGL/Lighting.cpp b/Userland/Libraries/LibGL/Lighting.cpp new file mode 100644 index 0000000000..f79f3e17bf --- /dev/null +++ b/Userland/Libraries/LibGL/Lighting.cpp @@ -0,0 +1,539 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * Copyright (c) 2021, Stephan Unverwerth + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace GL { + +template +void GLContext::get_light_param(GLenum light, GLenum pname, T* params) +{ + auto const& light_state = m_light_states[light - GL_LIGHT0]; + switch (pname) { + case GL_AMBIENT: + params[0] = light_state.ambient_intensity.x(); + params[1] = light_state.ambient_intensity.y(); + params[2] = light_state.ambient_intensity.z(); + params[3] = light_state.ambient_intensity.w(); + break; + case GL_DIFFUSE: + params[0] = light_state.diffuse_intensity.x(); + params[1] = light_state.diffuse_intensity.y(); + params[2] = light_state.diffuse_intensity.z(); + params[3] = light_state.diffuse_intensity.w(); + break; + case GL_SPECULAR: + params[0] = light_state.specular_intensity.x(); + params[1] = light_state.specular_intensity.y(); + params[2] = light_state.specular_intensity.z(); + params[3] = light_state.specular_intensity.w(); + break; + case GL_SPOT_DIRECTION: + params[0] = light_state.spotlight_direction.x(); + params[1] = light_state.spotlight_direction.y(); + params[2] = light_state.spotlight_direction.z(); + break; + case GL_SPOT_EXPONENT: + *params = light_state.spotlight_exponent; + break; + case GL_SPOT_CUTOFF: + *params = light_state.spotlight_cutoff_angle; + break; + case GL_CONSTANT_ATTENUATION: + *params = light_state.constant_attenuation; + break; + case GL_LINEAR_ATTENUATION: + *params = light_state.linear_attenuation; + break; + case GL_QUADRATIC_ATTENUATION: + *params = light_state.quadratic_attenuation; + break; + } +} + +template +void GLContext::get_material_param(Face face, GLenum pname, T* params) +{ + auto const& material = m_material_states[face]; + switch (pname) { + case GL_AMBIENT: + params[0] = static_cast(material.ambient.x()); + params[1] = static_cast(material.ambient.y()); + params[2] = static_cast(material.ambient.z()); + params[3] = static_cast(material.ambient.w()); + break; + case GL_DIFFUSE: + params[0] = static_cast(material.diffuse.x()); + params[1] = static_cast(material.diffuse.y()); + params[2] = static_cast(material.diffuse.z()); + params[3] = static_cast(material.diffuse.w()); + break; + case GL_SPECULAR: + params[0] = static_cast(material.specular.x()); + params[1] = static_cast(material.specular.y()); + params[2] = static_cast(material.specular.z()); + params[3] = static_cast(material.specular.w()); + break; + case GL_EMISSION: + params[0] = static_cast(material.emissive.x()); + params[1] = static_cast(material.emissive.y()); + params[2] = static_cast(material.emissive.z()); + params[3] = static_cast(material.emissive.w()); + break; + case GL_SHININESS: + *params = material.shininess; + break; + } +} + +void GLContext::gl_color_material(GLenum face, GLenum mode) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color_material, face, mode); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + RETURN_WITH_ERROR_IF(face != GL_FRONT + && face != GL_BACK + && face != GL_FRONT_AND_BACK, + GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(mode != GL_EMISSION + && mode != GL_AMBIENT + && mode != GL_DIFFUSE + && mode != GL_SPECULAR + && mode != GL_AMBIENT_AND_DIFFUSE, + GL_INVALID_ENUM); + + m_color_material_face = face; + m_color_material_mode = mode; + + m_light_state_is_dirty = true; +} + +void GLContext::gl_get_light(GLenum light, GLenum pname, void* params, GLenum type) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_get_light, light, pname, params, type); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light > GL_LIGHT0 + m_device_info.num_lights, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_SPOT_DIRECTION || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_CUTOFF || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION), GL_INVALID_ENUM); + + if (type == GL_FLOAT) + get_light_param(light, pname, static_cast(params)); + else if (type == GL_INT) + get_light_param(light, pname, static_cast(params)); + else + VERIFY_NOT_REACHED(); +} + +void GLContext::gl_get_material(GLenum face, GLenum pname, void* params, GLenum type) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_get_material, face, pname, params, type); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK), GL_INVALID_ENUM); + + Face material_face = Front; + switch (face) { + case GL_FRONT: + material_face = Front; + break; + case GL_BACK: + material_face = Back; + break; + } + + if (type == GL_FLOAT) + get_material_param(material_face, pname, static_cast(params)); + else if (type == GL_INT) + get_material_param(material_face, pname, static_cast(params)); + else + VERIFY_NOT_REACHED(); +} + +void GLContext::gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GLfloat w) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_light_model, pname, x, y, z, w); + + RETURN_WITH_ERROR_IF(pname != GL_LIGHT_MODEL_AMBIENT + && pname != GL_LIGHT_MODEL_COLOR_CONTROL + && pname != GL_LIGHT_MODEL_LOCAL_VIEWER + && pname != GL_LIGHT_MODEL_TWO_SIDE, + GL_INVALID_ENUM); + + auto lighting_params = m_rasterizer->light_model(); + + switch (pname) { + case GL_LIGHT_MODEL_AMBIENT: + lighting_params.scene_ambient_color = { x, y, z, w }; + break; + case GL_LIGHT_MODEL_COLOR_CONTROL: { + GLenum color_control = static_cast(x); + RETURN_WITH_ERROR_IF(color_control != GL_SINGLE_COLOR && color_control != GL_SEPARATE_SPECULAR_COLOR, GL_INVALID_ENUM); + lighting_params.color_control = (color_control == GL_SINGLE_COLOR) ? GPU::ColorControl::SingleColor : GPU::ColorControl::SeparateSpecularColor; + break; + } + case GL_LIGHT_MODEL_LOCAL_VIEWER: + // 0 means the viewer is at infinity + // 1 means they're in local (eye) space + lighting_params.viewer_at_infinity = (x != 1.0f); + break; + case GL_LIGHT_MODEL_TWO_SIDE: + VERIFY(y == 0.0f && z == 0.0f && w == 0.0f); + lighting_params.two_sided_lighting = x; + break; + default: + VERIFY_NOT_REACHED(); + } + + m_rasterizer->set_light_model_params(lighting_params); +} + +void GLContext::gl_lightf(GLenum light, GLenum pname, GLfloat param) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightf, light, pname, param); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname != GL_SPOT_EXPONENT || pname != GL_SPOT_CUTOFF), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(param < 0.f, GL_INVALID_VALUE); + + auto& light_state = m_light_states.at(light - GL_LIGHT0); + + switch (pname) { + case GL_CONSTANT_ATTENUATION: + light_state.constant_attenuation = param; + break; + case GL_LINEAR_ATTENUATION: + light_state.linear_attenuation = param; + break; + case GL_QUADRATIC_ATTENUATION: + light_state.quadratic_attenuation = param; + break; + case GL_SPOT_EXPONENT: + RETURN_WITH_ERROR_IF(param > 128.f, GL_INVALID_VALUE); + light_state.spotlight_exponent = param; + break; + case GL_SPOT_CUTOFF: + RETURN_WITH_ERROR_IF(param > 90.f && param != 180.f, GL_INVALID_VALUE); + light_state.spotlight_cutoff_angle = param; + break; + default: + VERIFY_NOT_REACHED(); + } + + m_light_state_is_dirty = true; +} + +void GLContext::gl_lightfv(GLenum light, GLenum pname, GLfloat const* params) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightfv, light, pname, params); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_POSITION || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname == GL_SPOT_CUTOFF || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_DIRECTION), GL_INVALID_ENUM); + + auto& light_state = m_light_states.at(light - GL_LIGHT0); + + switch (pname) { + case GL_AMBIENT: + light_state.ambient_intensity = { params[0], params[1], params[2], params[3] }; + break; + case GL_DIFFUSE: + light_state.diffuse_intensity = { params[0], params[1], params[2], params[3] }; + break; + case GL_SPECULAR: + light_state.specular_intensity = { params[0], params[1], params[2], params[3] }; + break; + case GL_POSITION: + light_state.position = { params[0], params[1], params[2], params[3] }; + light_state.position = m_model_view_matrix * light_state.position; + break; + case GL_CONSTANT_ATTENUATION: + RETURN_WITH_ERROR_IF(params[0] < 0.f, GL_INVALID_VALUE); + light_state.constant_attenuation = params[0]; + break; + case GL_LINEAR_ATTENUATION: + RETURN_WITH_ERROR_IF(params[0] < 0.f, GL_INVALID_VALUE); + light_state.linear_attenuation = params[0]; + break; + case GL_QUADRATIC_ATTENUATION: + RETURN_WITH_ERROR_IF(params[0] < 0.f, GL_INVALID_VALUE); + light_state.quadratic_attenuation = params[0]; + break; + case GL_SPOT_EXPONENT: { + auto exponent = params[0]; + RETURN_WITH_ERROR_IF(exponent < 0.f || exponent > 128.f, GL_INVALID_VALUE); + light_state.spotlight_exponent = exponent; + break; + } + case GL_SPOT_CUTOFF: { + auto cutoff = params[0]; + RETURN_WITH_ERROR_IF((cutoff < 0.f || cutoff > 90.f) && cutoff != 180.f, GL_INVALID_VALUE); + light_state.spotlight_cutoff_angle = cutoff; + break; + } + case GL_SPOT_DIRECTION: { + FloatVector4 direction_vector = { params[0], params[1], params[2], 0.f }; + direction_vector = m_model_view_matrix * direction_vector; + light_state.spotlight_direction = direction_vector.xyz(); + break; + } + default: + VERIFY_NOT_REACHED(); + } + + m_light_state_is_dirty = true; +} + +void GLContext::gl_lightiv(GLenum light, GLenum pname, GLint const* params) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_lightiv, light, pname, params); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(light < GL_LIGHT0 || light >= (GL_LIGHT0 + m_device_info.num_lights), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_POSITION || pname == GL_CONSTANT_ATTENUATION || pname == GL_LINEAR_ATTENUATION || pname == GL_QUADRATIC_ATTENUATION || pname == GL_SPOT_CUTOFF || pname == GL_SPOT_EXPONENT || pname == GL_SPOT_DIRECTION), GL_INVALID_ENUM); + + auto& light_state = m_light_states[light - GL_LIGHT0]; + + auto const to_float_vector = [](GLfloat x, GLfloat y, GLfloat z, GLfloat w) { + return FloatVector4(x, y, z, w); + }; + + switch (pname) { + case GL_AMBIENT: + light_state.ambient_intensity = to_float_vector(params[0], params[1], params[2], params[3]); + break; + case GL_DIFFUSE: + light_state.diffuse_intensity = to_float_vector(params[0], params[1], params[2], params[3]); + break; + case GL_SPECULAR: + light_state.specular_intensity = to_float_vector(params[0], params[1], params[2], params[3]); + break; + case GL_POSITION: + light_state.position = to_float_vector(params[0], params[1], params[2], params[3]); + light_state.position = m_model_view_matrix * light_state.position; + break; + case GL_CONSTANT_ATTENUATION: + RETURN_WITH_ERROR_IF(params[0] < 0, GL_INVALID_VALUE); + light_state.constant_attenuation = static_cast(params[0]); + break; + case GL_LINEAR_ATTENUATION: + RETURN_WITH_ERROR_IF(params[0] < 0, GL_INVALID_VALUE); + light_state.linear_attenuation = static_cast(params[0]); + break; + case GL_QUADRATIC_ATTENUATION: + RETURN_WITH_ERROR_IF(params[0] < 0, GL_INVALID_VALUE); + light_state.quadratic_attenuation = static_cast(params[0]); + break; + case GL_SPOT_EXPONENT: { + auto exponent = static_cast(params[0]); + RETURN_WITH_ERROR_IF(exponent < 0.f || exponent > 128.f, GL_INVALID_VALUE); + light_state.spotlight_exponent = exponent; + break; + } + case GL_SPOT_CUTOFF: { + auto cutoff = static_cast(params[0]); + RETURN_WITH_ERROR_IF((cutoff < 0.f || cutoff > 90.f) && cutoff != 180.f, GL_INVALID_VALUE); + light_state.spotlight_cutoff_angle = cutoff; + break; + } + case GL_SPOT_DIRECTION: { + auto direction_vector = to_float_vector(params[0], params[1], params[2], 0.0f); + direction_vector = m_model_view_matrix * direction_vector; + light_state.spotlight_direction = direction_vector.xyz(); + break; + } + default: + VERIFY_NOT_REACHED(); + } + + m_light_state_is_dirty = true; +} + +void GLContext::gl_materialf(GLenum face, GLenum pname, GLfloat param) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialf, face, pname, param); + RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(pname != GL_SHININESS, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(param > 128.0f, GL_INVALID_VALUE); + + switch (face) { + case GL_FRONT: + m_material_states[Face::Front].shininess = param; + break; + case GL_BACK: + m_material_states[Face::Back].shininess = param; + break; + case GL_FRONT_AND_BACK: + m_material_states[Face::Front].shininess = param; + m_material_states[Face::Back].shininess = param; + break; + default: + VERIFY_NOT_REACHED(); + } + + m_light_state_is_dirty = true; +} + +void GLContext::gl_materialfv(GLenum face, GLenum pname, GLfloat const* params) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialfv, face, pname, params); + RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION || pname == GL_SHININESS || pname == GL_AMBIENT_AND_DIFFUSE), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF((pname == GL_SHININESS && *params > 128.0f), GL_INVALID_VALUE); + + auto update_material = [](GPU::Material& material, GLenum pname, GLfloat const* params) { + switch (pname) { + case GL_AMBIENT: + material.ambient = { params[0], params[1], params[2], params[3] }; + break; + case GL_DIFFUSE: + material.diffuse = { params[0], params[1], params[2], params[3] }; + break; + case GL_SPECULAR: + material.specular = { params[0], params[1], params[2], params[3] }; + break; + case GL_EMISSION: + material.emissive = { params[0], params[1], params[2], params[3] }; + break; + case GL_SHININESS: + material.shininess = *params; + break; + case GL_AMBIENT_AND_DIFFUSE: + material.ambient = { params[0], params[1], params[2], params[3] }; + material.diffuse = { params[0], params[1], params[2], params[3] }; + break; + } + }; + + switch (face) { + case GL_FRONT: + update_material(m_material_states[Face::Front], pname, params); + break; + case GL_BACK: + update_material(m_material_states[Face::Back], pname, params); + break; + case GL_FRONT_AND_BACK: + update_material(m_material_states[Face::Front], pname, params); + update_material(m_material_states[Face::Back], pname, params); + break; + } + + m_light_state_is_dirty = true; +} + +void GLContext::gl_materialiv(GLenum face, GLenum pname, GLint const* params) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_materialiv, face, pname, params); + RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(pname == GL_AMBIENT || pname == GL_DIFFUSE || pname == GL_SPECULAR || pname == GL_EMISSION || pname == GL_SHININESS || pname == GL_AMBIENT_AND_DIFFUSE), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF((pname == GL_SHININESS && *params > 128), GL_INVALID_VALUE); + + auto update_material = [](GPU::Material& material, GLenum pname, GLint const* params) { + switch (pname) { + case GL_AMBIENT: + material.ambient = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; + break; + case GL_DIFFUSE: + material.diffuse = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; + break; + case GL_SPECULAR: + material.specular = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; + break; + case GL_EMISSION: + material.emissive = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; + break; + case GL_SHININESS: + material.shininess = static_cast(params[0]); + break; + case GL_AMBIENT_AND_DIFFUSE: + material.ambient = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; + material.diffuse = { static_cast(params[0]), static_cast(params[1]), static_cast(params[2]), static_cast(params[3]) }; + break; + } + }; + + switch (face) { + case GL_FRONT: + update_material(m_material_states[Face::Front], pname, params); + break; + case GL_BACK: + update_material(m_material_states[Face::Back], pname, params); + break; + case GL_FRONT_AND_BACK: + update_material(m_material_states[Face::Front], pname, params); + update_material(m_material_states[Face::Back], pname, params); + break; + } + + m_light_state_is_dirty = true; +} + +void GLContext::gl_shade_model(GLenum mode) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_shade_model, mode); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(mode != GL_FLAT && mode != GL_SMOOTH, GL_INVALID_ENUM); + + auto options = m_rasterizer->options(); + options.shade_smooth = (mode == GL_SMOOTH); + m_rasterizer->set_options(options); +} + +void GLContext::sync_light_state() +{ + if (!m_light_state_is_dirty) + return; + + m_light_state_is_dirty = false; + + auto options = m_rasterizer->options(); + options.color_material_enabled = m_color_material_enabled; + switch (m_color_material_face) { + case GL_BACK: + options.color_material_face = GPU::ColorMaterialFace::Back; + break; + case GL_FRONT: + options.color_material_face = GPU::ColorMaterialFace::Front; + break; + case GL_FRONT_AND_BACK: + options.color_material_face = GPU::ColorMaterialFace::FrontAndBack; + break; + default: + VERIFY_NOT_REACHED(); + } + switch (m_color_material_mode) { + case GL_AMBIENT: + options.color_material_mode = GPU::ColorMaterialMode::Ambient; + break; + case GL_AMBIENT_AND_DIFFUSE: + options.color_material_mode = GPU::ColorMaterialMode::Ambient; + options.color_material_mode = GPU::ColorMaterialMode::Diffuse; + break; + case GL_DIFFUSE: + options.color_material_mode = GPU::ColorMaterialMode::Diffuse; + break; + case GL_EMISSION: + options.color_material_mode = GPU::ColorMaterialMode::Emissive; + break; + case GL_SPECULAR: + options.color_material_mode = GPU::ColorMaterialMode::Specular; + break; + default: + VERIFY_NOT_REACHED(); + } + m_rasterizer->set_options(options); + + for (auto light_id = 0u; light_id < m_device_info.num_lights; light_id++) { + auto const& current_light_state = m_light_states.at(light_id); + m_rasterizer->set_light_state(light_id, current_light_state); + } + + m_rasterizer->set_material_state(GPU::Face::Front, m_material_states[Face::Front]); + m_rasterizer->set_material_state(GPU::Face::Back, m_material_states[Face::Back]); +} + +} diff --git a/Userland/Libraries/LibGL/Lists.cpp b/Userland/Libraries/LibGL/Lists.cpp new file mode 100644 index 0000000000..c8d9dcb2bc --- /dev/null +++ b/Userland/Libraries/LibGL/Lists.cpp @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * Copyright (c) 2021, Stephan Unverwerth + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace GL { + +void GLContext::gl_call_list(GLuint list) +{ + if (m_gl_call_depth > max_allowed_gl_call_depth) + return; + + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_list, list); + + if (m_listings.size() < list) + return; + + TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 }; + + invoke_list(list); +} + +void GLContext::gl_call_lists(GLsizei n, GLenum type, void const* lists) +{ + if (m_gl_call_depth > max_allowed_gl_call_depth) + return; + + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_call_lists, n, type, lists); + + RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(!(type == GL_BYTE + || type == GL_UNSIGNED_BYTE + || type == GL_SHORT + || type == GL_UNSIGNED_SHORT + || type == GL_INT + || type == GL_UNSIGNED_INT + || type == GL_FLOAT + || type == GL_2_BYTES + || type == GL_3_BYTES + || type == GL_4_BYTES), + GL_INVALID_ENUM); + + TemporaryChange change { m_gl_call_depth, m_gl_call_depth + 1 }; + + auto invoke_all_lists = [&](T const* lists) { + for (int i = 0; i < n; ++i) { + auto list = static_cast(lists[i]); + invoke_list(m_list_base + list); + } + }; + switch (type) { + case GL_BYTE: + invoke_all_lists(static_cast(lists)); + break; + case GL_UNSIGNED_BYTE: + invoke_all_lists(static_cast(lists)); + break; + case GL_SHORT: + invoke_all_lists(static_cast(lists)); + break; + case GL_UNSIGNED_SHORT: + invoke_all_lists(static_cast(lists)); + break; + case GL_INT: + invoke_all_lists(static_cast(lists)); + break; + case GL_UNSIGNED_INT: + invoke_all_lists(static_cast(lists)); + break; + case GL_FLOAT: + invoke_all_lists(static_cast(lists)); + break; + case GL_2_BYTES: + case GL_3_BYTES: + case GL_4_BYTES: + dbgln("GLContext FIXME: unimplemented glCallLists() with type {}", type); + break; + default: + VERIFY_NOT_REACHED(); + } +} + +void GLContext::gl_delete_lists(GLuint list, GLsizei range) +{ + if (m_listings.size() < list || m_listings.size() <= list + range) + return; + + for (auto& entry : m_listings.span().slice(list - 1, range)) + entry.entries.clear_with_capacity(); +} + +void GLContext::gl_end_list() +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(!m_current_listing_index.has_value(), GL_INVALID_OPERATION); + + m_listings[m_current_listing_index->index] = move(m_current_listing_index->listing); + m_current_listing_index.clear(); +} + +GLuint GLContext::gl_gen_lists(GLsizei range) +{ + RETURN_VALUE_WITH_ERROR_IF(range <= 0, GL_INVALID_VALUE, 0); + RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, 0); + + auto initial_entry = m_listings.size(); + m_listings.resize(range + initial_entry); + return initial_entry + 1; +} + +GLboolean GLContext::gl_is_list(GLuint list) +{ + RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE); + + return list < m_listings.size() ? GL_TRUE : GL_FALSE; +} + +void GLContext::gl_list_base(GLuint base) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_list_base, base); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + m_list_base = base; +} + +void GLContext::gl_new_list(GLuint list, GLenum mode) +{ + RETURN_WITH_ERROR_IF(list == 0, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(mode != GL_COMPILE && mode != GL_COMPILE_AND_EXECUTE, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(m_current_listing_index.has_value(), GL_INVALID_OPERATION); + + if (m_listings.size() < list) + return; + + m_current_listing_index = CurrentListing { {}, static_cast(list - 1), mode }; +} + +void GLContext::invoke_list(size_t list_index) +{ + auto& listing = m_listings[list_index - 1]; + for (auto& entry : listing.entries) { + entry.function.visit([&](auto& function) { + entry.arguments.visit([&](auto& arguments) { + auto apply = [&](Args&&... args) { + if constexpr (requires { (this->*function)(forward(args)...); }) + (this->*function)(forward(args)...); + }; + + arguments.apply_as_args(apply); + }); + }); + } +} + +} diff --git a/Userland/Libraries/LibGL/Matrix.cpp b/Userland/Libraries/LibGL/Matrix.cpp new file mode 100644 index 0000000000..186661c777 --- /dev/null +++ b/Userland/Libraries/LibGL/Matrix.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * Copyright (c) 2021, Stephan Unverwerth + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace GL { + +static constexpr size_t matrix_stack_limit(GLenum matrix_mode) +{ + switch (matrix_mode) { + case GL_MODELVIEW: + return MODELVIEW_MATRIX_STACK_LIMIT; + case GL_PROJECTION: + return PROJECTION_MATRIX_STACK_LIMIT; + case GL_TEXTURE: + return TEXTURE_MATRIX_STACK_LIMIT; + } + VERIFY_NOT_REACHED(); +} + +void GLContext::gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_frustum, left, right, bottom, top, near_val, far_val); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(near_val < 0 || far_val < 0, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE); + + // Let's do some math! + auto a = static_cast((right + left) / (right - left)); + auto b = static_cast((top + bottom) / (top - bottom)); + auto c = static_cast(-((far_val + near_val) / (far_val - near_val))); + auto d = static_cast(-((2 * far_val * near_val) / (far_val - near_val))); + + FloatMatrix4x4 frustum { + static_cast(2 * near_val / (right - left)), 0, a, 0, + 0, static_cast(2 * near_val / (top - bottom)), b, 0, + 0, 0, c, d, + 0, 0, -1, 0 + }; + *m_current_matrix = *m_current_matrix * frustum; +} + +void GLContext::gl_load_identity() +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_load_identity); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + *m_current_matrix = FloatMatrix4x4::identity(); +} + +void GLContext::gl_load_matrix(FloatMatrix4x4 const& matrix) +{ + APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_load_matrix, matrix); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + *m_current_matrix = matrix; +} + +void GLContext::gl_matrix_mode(GLenum mode) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_matrix_mode, mode); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(mode < GL_MODELVIEW || mode > GL_TEXTURE, GL_INVALID_ENUM); + + m_current_matrix_mode = mode; + switch (mode) { + case GL_MODELVIEW: + m_current_matrix = &m_model_view_matrix; + m_current_matrix_stack = &m_model_view_matrix_stack; + break; + case GL_PROJECTION: + m_current_matrix = &m_projection_matrix; + m_current_matrix_stack = &m_projection_matrix_stack; + break; + case GL_TEXTURE: + m_current_matrix = &m_texture_matrix; + m_current_matrix_stack = &m_texture_matrix_stack; + break; + default: + VERIFY_NOT_REACHED(); + } +} + +void GLContext::gl_mult_matrix(FloatMatrix4x4 const& matrix) +{ + APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(gl_mult_matrix, matrix); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + *m_current_matrix = *m_current_matrix * matrix; +} + +void GLContext::gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_ortho, left, right, bottom, top, near_val, far_val); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE); + + auto rl = right - left; + auto tb = top - bottom; + auto fn = far_val - near_val; + auto tx = -(right + left) / rl; + auto ty = -(top + bottom) / tb; + auto tz = -(far_val + near_val) / fn; + + FloatMatrix4x4 projection { + static_cast(2 / rl), 0, 0, static_cast(tx), + 0, static_cast(2 / tb), 0, static_cast(ty), + 0, 0, static_cast(-2 / fn), static_cast(tz), + 0, 0, 0, 1 + }; + *m_current_matrix = *m_current_matrix * projection; +} + +void GLContext::gl_pop_matrix() +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_pop_matrix); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF((*m_current_matrix_stack).is_empty(), GL_STACK_UNDERFLOW); + + *m_current_matrix = (*m_current_matrix_stack).take_last(); +} + +void GLContext::gl_push_matrix() +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_push_matrix); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF((*m_current_matrix_stack).size() >= matrix_stack_limit(m_current_matrix_mode), GL_STACK_OVERFLOW); + + (*m_current_matrix_stack).append(*m_current_matrix); +} + +void GLContext::gl_rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_rotate, angle, x, y, z); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + FloatVector3 axis = { x, y, z }; + if (axis.length() > 0.f) + axis.normalize(); + auto rotation_mat = Gfx::rotation_matrix(axis, angle * static_cast(M_PI * 2 / 360)); + *m_current_matrix = *m_current_matrix * rotation_mat; +} + +void GLContext::gl_scale(GLdouble x, GLdouble y, GLdouble z) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_scale, x, y, z); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + auto scale_matrix = Gfx::scale_matrix(FloatVector3 { static_cast(x), static_cast(y), static_cast(z) }); + *m_current_matrix = *m_current_matrix * scale_matrix; +} + +void GLContext::gl_translate(GLdouble x, GLdouble y, GLdouble z) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_translate, x, y, z); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + auto translation_matrix = Gfx::translation_matrix(FloatVector3 { static_cast(x), static_cast(y), static_cast(z) }); + *m_current_matrix = *m_current_matrix * translation_matrix; +} + +} diff --git a/Userland/Libraries/LibGL/Stencil.cpp b/Userland/Libraries/LibGL/Stencil.cpp new file mode 100644 index 0000000000..c2ea1988a4 --- /dev/null +++ b/Userland/Libraries/LibGL/Stencil.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * Copyright (c) 2021, Stephan Unverwerth + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace GL { + +void GLContext::gl_clear_stencil(GLint s) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_clear_stencil, s); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + m_clear_stencil = static_cast(s & ((1 << m_device_info.stencil_bits) - 1)); +} + +void GLContext::gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_func_separate, face, func, ref, mask); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); + + RETURN_WITH_ERROR_IF(!(func == GL_NEVER + || func == GL_LESS + || func == GL_LEQUAL + || func == GL_GREATER + || func == GL_GEQUAL + || func == GL_EQUAL + || func == GL_NOTEQUAL + || func == GL_ALWAYS), + GL_INVALID_ENUM); + + ref = clamp(ref, 0, (1 << m_device_info.stencil_bits) - 1); + + StencilFunctionOptions new_options = { func, ref, mask }; + if (face == GL_FRONT || face == GL_FRONT_AND_BACK) + m_stencil_function[Face::Front] = new_options; + if (face == GL_BACK || face == GL_FRONT_AND_BACK) + m_stencil_function[Face::Back] = new_options; + + m_stencil_configuration_dirty = true; +} + +void GLContext::gl_stencil_mask_separate(GLenum face, GLuint mask) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_mask_separate, face, mask); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + if (face == GL_FRONT || face == GL_FRONT_AND_BACK) + m_stencil_operation[Face::Front].write_mask = mask; + if (face == GL_BACK || face == GL_FRONT_AND_BACK) + m_stencil_operation[Face::Back].write_mask = mask; + + m_stencil_configuration_dirty = true; +} + +void GLContext::gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_stencil_op_separate, face, sfail, dpfail, dppass); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + RETURN_WITH_ERROR_IF(!(face == GL_FRONT || face == GL_BACK || face == GL_FRONT_AND_BACK), GL_INVALID_ENUM); + + auto is_valid_op = [](GLenum op) -> bool { + return op == GL_KEEP || op == GL_ZERO || op == GL_REPLACE || op == GL_INCR || op == GL_INCR_WRAP + || op == GL_DECR || op == GL_DECR_WRAP || op == GL_INVERT; + }; + RETURN_WITH_ERROR_IF(!is_valid_op(sfail), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!is_valid_op(dpfail), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!is_valid_op(dppass), GL_INVALID_ENUM); + + auto update_stencil_operation = [&](Face face, GLenum sfail, GLenum dpfail, GLenum dppass) { + auto& stencil_operation = m_stencil_operation[face]; + stencil_operation.op_fail = sfail; + stencil_operation.op_depth_fail = dpfail; + stencil_operation.op_pass = dppass; + }; + if (face == GL_FRONT || face == GL_FRONT_AND_BACK) + update_stencil_operation(Face::Front, sfail, dpfail, dppass); + if (face == GL_BACK || face == GL_FRONT_AND_BACK) + update_stencil_operation(Face::Back, sfail, dpfail, dppass); + + m_stencil_configuration_dirty = true; +} + +void GLContext::sync_stencil_configuration() +{ + if (!m_stencil_configuration_dirty) + return; + m_stencil_configuration_dirty = false; + + auto set_device_stencil = [&](GPU::Face face, StencilFunctionOptions func, StencilOperationOptions op) { + GPU::StencilConfiguration device_configuration; + + // Stencil test function + auto map_func = [](GLenum func) -> GPU::StencilTestFunction { + switch (func) { + case GL_ALWAYS: + return GPU::StencilTestFunction::Always; + case GL_EQUAL: + return GPU::StencilTestFunction::Equal; + case GL_GEQUAL: + return GPU::StencilTestFunction::GreaterOrEqual; + case GL_GREATER: + return GPU::StencilTestFunction::Greater; + case GL_LESS: + return GPU::StencilTestFunction::Less; + case GL_LEQUAL: + return GPU::StencilTestFunction::LessOrEqual; + case GL_NEVER: + return GPU::StencilTestFunction::Never; + case GL_NOTEQUAL: + return GPU::StencilTestFunction::NotEqual; + } + VERIFY_NOT_REACHED(); + }; + device_configuration.test_function = map_func(func.func); + device_configuration.reference_value = func.reference_value; + device_configuration.test_mask = func.mask; + + // Stencil operation + auto map_operation = [](GLenum operation) -> GPU::StencilOperation { + switch (operation) { + case GL_DECR: + return GPU::StencilOperation::Decrement; + case GL_DECR_WRAP: + return GPU::StencilOperation::DecrementWrap; + case GL_INCR: + return GPU::StencilOperation::Increment; + case GL_INCR_WRAP: + return GPU::StencilOperation::IncrementWrap; + case GL_INVERT: + return GPU::StencilOperation::Invert; + case GL_KEEP: + return GPU::StencilOperation::Keep; + case GL_REPLACE: + return GPU::StencilOperation::Replace; + case GL_ZERO: + return GPU::StencilOperation::Zero; + } + VERIFY_NOT_REACHED(); + }; + device_configuration.on_stencil_test_fail = map_operation(op.op_fail); + device_configuration.on_depth_test_fail = map_operation(op.op_depth_fail); + device_configuration.on_pass = map_operation(op.op_pass); + device_configuration.write_mask = op.write_mask; + + m_rasterizer->set_stencil_configuration(face, device_configuration); + }; + set_device_stencil(GPU::Face::Front, m_stencil_function[Face::Front], m_stencil_operation[Face::Front]); + set_device_stencil(GPU::Face::Back, m_stencil_function[Face::Back], m_stencil_operation[Face::Back]); +} + +} diff --git a/Userland/Libraries/LibGL/Textures.cpp b/Userland/Libraries/LibGL/Textures.cpp new file mode 100644 index 0000000000..dab82c0804 --- /dev/null +++ b/Userland/Libraries/LibGL/Textures.cpp @@ -0,0 +1,666 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * Copyright (c) 2021, Stephan Unverwerth + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace GL { + +void GLContext::gl_active_texture(GLenum texture) +{ + RETURN_WITH_ERROR_IF(texture < GL_TEXTURE0 || texture >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM); + + m_active_texture_unit_index = texture - GL_TEXTURE0; + m_active_texture_unit = &m_texture_units.at(m_active_texture_unit_index); +} + +void GLContext::gl_bind_texture(GLenum target, GLuint texture) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(target != GL_TEXTURE_1D + && target != GL_TEXTURE_2D + && target != GL_TEXTURE_3D + && target != GL_TEXTURE_1D_ARRAY + && target != GL_TEXTURE_2D_ARRAY + && target != GL_TEXTURE_CUBE_MAP, + GL_INVALID_ENUM); + + // FIXME: We only support GL_TEXTURE_2D for now + if (target != GL_TEXTURE_2D) { + dbgln("gl_bind_texture(target = {:#x}): currently only GL_TEXTURE_2D is supported", target); + return; + } + + RefPtr texture_2d; + + if (texture == 0) { + // Texture name 0 refers to the default texture + texture_2d = get_default_texture(target); + } else { + // Find this texture name in our previously allocated textures + auto it = m_allocated_textures.find(texture); + if (it != m_allocated_textures.end()) { + auto texture_object = it->value; + if (!texture_object.is_null()) { + // Texture must have been created with the same target + RETURN_WITH_ERROR_IF(!texture_object->is_texture_2d(), GL_INVALID_OPERATION); + texture_2d = static_cast(texture_object.ptr()); + } + } + + // OpenGL 1.x supports binding texture names that were not previously generated by glGenTextures. + // If there is not an allocated texture, meaning it was not previously generated by glGenTextures, + // we can keep texture_object null to both allocate and bind the texture with the passed in texture name. + // FIXME: Later OpenGL versions such as 4.x enforce that texture names being bound were previously generated + // by glGenTextures. + if (!texture_2d) { + texture_2d = adopt_ref(*new Texture2D()); + m_allocated_textures.set(texture, texture_2d); + } + } + + m_active_texture_unit->set_texture_2d_target_texture(texture_2d); + m_sampler_config_is_dirty = true; +} + +void GLContext::gl_client_active_texture(GLenum target) +{ + RETURN_WITH_ERROR_IF(target < GL_TEXTURE0 || target >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM); + + m_client_active_texture = target - GL_TEXTURE0; +} + +void GLContext::gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_image_2d, target, level, internalformat, x, y, width, height, border); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // FIXME: implement + dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_copy_tex_image_2d({:#x}, {}, {:#x}, {}, {}, {}, {}, {})", + target, level, internalformat, x, y, width, height, border); +} + +void GLContext::gl_copy_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_copy_tex_sub_image_2d, target, level, xoffset, yoffset, x, y, width, height); + RETURN_WITH_ERROR_IF(!(target == GL_TEXTURE_2D || target == GL_TEXTURE_1D_ARRAY), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(level < 0, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // FIXME: implement + dbgln_if(GL_DEBUG, "GLContext FIXME: implement gl_copy_tex_sub_image_2d({:#x}, {}, {}, {}, {}, {}, {}, {})", + target, level, xoffset, yoffset, x, y, width, height); +} + +void GLContext::gl_delete_textures(GLsizei n, GLuint const* textures) +{ + RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + for (auto i = 0; i < n; i++) { + GLuint name = textures[i]; + if (name == 0) + continue; + + m_name_allocator.free(name); + + auto texture_object = m_allocated_textures.find(name); + if (texture_object == m_allocated_textures.end() || texture_object->value.is_null()) + continue; + auto texture = texture_object->value; + + // Check all texture units + for (auto& texture_unit : m_texture_units) { + if (texture->is_texture_2d() && texture_unit.texture_2d_target_texture() == texture) { + // If a texture that is currently bound is deleted, the binding reverts to 0 (the default texture) + texture_unit.set_texture_2d_target_texture(get_default_texture(GL_TEXTURE_2D)); + } + } + + m_allocated_textures.remove(name); + } +} + +void GLContext::gl_gen_textures(GLsizei n, GLuint* textures) +{ + RETURN_WITH_ERROR_IF(n < 0, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + m_name_allocator.allocate(n, textures); + + // Initialize all texture names with a nullptr + for (auto i = 0; i < n; ++i) { + GLuint name = textures[i]; + m_allocated_textures.set(name, nullptr); + } +} + +void GLContext::gl_get_tex_parameter_integerv(GLenum target, GLint level, GLenum pname, GLint* params) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + // FIXME: support targets other than GL_TEXTURE_2D + RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); + // FIXME: support other parameter names + RETURN_WITH_ERROR_IF(pname < GL_TEXTURE_WIDTH || pname > GL_TEXTURE_HEIGHT, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE); + // FIXME: GL_INVALID_VALUE is generated if target is GL_TEXTURE_BUFFER and level is not zero + // FIXME: GL_INVALID_OPERATION is generated if GL_TEXTURE_COMPRESSED_IMAGE_SIZE is queried on texture images with an uncompressed internal format or on proxy targets + + VERIFY(!m_active_texture_unit->texture_2d_target_texture().is_null()); + auto const texture_2d = m_active_texture_unit->texture_2d_target_texture(); + + switch (pname) { + case GL_TEXTURE_HEIGHT: + *params = texture_2d->height_at_lod(level); + break; + case GL_TEXTURE_WIDTH: + *params = texture_2d->width_at_lod(level); + break; + } +} + +GLboolean GLContext::gl_is_texture(GLuint texture) +{ + RETURN_VALUE_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION, GL_FALSE); + + if (texture == 0) + return GL_FALSE; + + auto it = m_allocated_textures.find(texture); + if (it == m_allocated_textures.end()) + return GL_FALSE; + + return it->value.is_null() ? GL_FALSE : GL_TRUE; +} + +void GLContext::gl_multi_tex_coord(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_multi_tex_coord, target, s, t, r, q); + + RETURN_WITH_ERROR_IF(target < GL_TEXTURE0 || target >= GL_TEXTURE0 + m_device_info.num_texture_units, GL_INVALID_ENUM); + + m_current_vertex_tex_coord[target - GL_TEXTURE0] = { s, t, r, q }; +} + +void GLContext::gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_coord, s, t, r, q); + + m_current_vertex_tex_coord[0] = { s, t, r, q }; +} + +void GLContext::gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(!(size == 1 || size == 2 || size == 3 || size == 4), GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE); + + auto& tex_coord_pointer = m_client_tex_coord_pointer[m_client_active_texture]; + tex_coord_pointer = { .size = size, .type = type, .stride = stride, .pointer = pointer }; +} + +void GLContext::gl_tex_env(GLenum target, GLenum pname, GLfloat param) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_env, target, pname, param); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // FIXME: We currently only support a subset of possible target values. Implement the rest! + RETURN_WITH_ERROR_IF(target != GL_TEXTURE_ENV, GL_INVALID_ENUM); + + // FIXME: We currently only support a subset of possible pname values. Implement the rest! + RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_ENV_MODE, GL_INVALID_ENUM); + + auto param_enum = static_cast(param); + + switch (param_enum) { + case GL_MODULATE: + case GL_REPLACE: + case GL_DECAL: + case GL_ADD: + m_active_texture_unit->set_env_mode(param_enum); + m_sampler_config_is_dirty = true; + break; + default: + // FIXME: We currently only support a subset of possible param values. Implement the rest! + dbgln_if(GL_DEBUG, "gl_tex_env({:#x}, {:#x}, {}): param unimplemented", target, pname, param); + RETURN_WITH_ERROR_IF(true, GL_INVALID_ENUM); + } +} + +void GLContext::gl_tex_gen(GLenum coord, GLenum pname, GLint param) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen, coord, pname, param); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR + && param != GL_OBJECT_LINEAR + && param != GL_SPHERE_MAP + && param != GL_NORMAL_MAP + && param != GL_REFLECTION_MAP, + GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM); + + GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S); + texture_coordinate_generation(m_active_texture_unit_index, capability).generation_mode = param; + m_texcoord_generation_dirty = true; +} + +void GLContext::gl_tex_gen_floatv(GLenum coord, GLenum pname, GLfloat const* params) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_gen_floatv, coord, pname, params); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + RETURN_WITH_ERROR_IF(coord < GL_S || coord > GL_Q, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(pname != GL_TEXTURE_GEN_MODE + && pname != GL_OBJECT_PLANE + && pname != GL_EYE_PLANE, + GL_INVALID_ENUM); + + GLenum const capability = GL_TEXTURE_GEN_S + (coord - GL_S); + + switch (pname) { + case GL_TEXTURE_GEN_MODE: { + auto param = static_cast(params[0]); + RETURN_WITH_ERROR_IF(param != GL_EYE_LINEAR + && param != GL_OBJECT_LINEAR + && param != GL_SPHERE_MAP + && param != GL_NORMAL_MAP + && param != GL_REFLECTION_MAP, + GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF((coord == GL_R || coord == GL_Q) && param == GL_SPHERE_MAP, GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(coord == GL_Q && (param == GL_REFLECTION_MAP || param == GL_NORMAL_MAP), GL_INVALID_ENUM); + + texture_coordinate_generation(m_active_texture_unit_index, capability).generation_mode = param; + break; + } + case GL_OBJECT_PLANE: + texture_coordinate_generation(m_active_texture_unit_index, capability).object_plane_coefficients = { params[0], params[1], params[2], params[3] }; + break; + case GL_EYE_PLANE: { + auto const& inverse_model_view = m_model_view_matrix.inverse(); + auto input_coefficients = FloatVector4 { params[0], params[1], params[2], params[3] }; + + // Note: we are allowed to store transformed coefficients here, according to the documentation on + // `glGetTexGen`: + // + // "The returned values are those maintained in eye coordinates. They are not equal to the values + // specified using glTexGen, unless the modelview matrix was identity when glTexGen was called." + + texture_coordinate_generation(m_active_texture_unit_index, capability).eye_plane_coefficients = inverse_model_view * input_coefficients; + break; + } + default: + VERIFY_NOT_REACHED(); + } + + m_texcoord_generation_dirty = true; +} + +void GLContext::gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLvoid const* data) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // We only support GL_TEXTURE_2D for now + RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); + + // Internal format can also be a number between 1 and 4. Symbolic formats were only added with EXT_texture, promoted to core in OpenGL 1.1 + if (internal_format == 1) + internal_format = GL_ALPHA; + else if (internal_format == 2) + internal_format = GL_LUMINANCE_ALPHA; + else if (internal_format == 3) + internal_format = GL_RGB; + else if (internal_format == 4) + internal_format = GL_RGBA; + + // We only support symbolic constants for now + RETURN_WITH_ERROR_IF(!(internal_format == GL_RGB || internal_format == GL_RGBA || internal_format == GL_LUMINANCE8 || internal_format == GL_LUMINANCE8_ALPHA8), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE); + // Check if width and height are a power of 2 + if (!m_device_info.supports_npot_textures) { + RETURN_WITH_ERROR_IF(!is_power_of_two(width), GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(!is_power_of_two(height), GL_INVALID_VALUE); + } + RETURN_WITH_ERROR_IF(border != 0, GL_INVALID_VALUE); + + auto texture_2d = m_active_texture_unit->texture_2d_target_texture(); + VERIFY(!texture_2d.is_null()); + + if (level == 0) { + // FIXME: OpenGL has the concept of texture and mipmap completeness. A texture has to fulfill certain criteria to be considered complete. + // Trying to render while an incomplete texture is bound will result in an error. + // Here we simply create a complete device image when mipmap level 0 is attached to the texture object. This has the unfortunate side effect + // that constructing GL textures in any but the default mipmap order, going from level 0 upwards will cause mip levels to stay uninitialized. + // To be spec compliant we should create the device image once the texture has become complete and is used for rendering the first time. + // All images that were attached before the device image was created need to be stored somewhere to be used to initialize the device image once complete. + texture_2d->set_device_image(m_rasterizer->create_image(GPU::ImageFormat::BGRA8888, width, height, 1, 999, 1)); + m_sampler_config_is_dirty = true; + } + + texture_2d->upload_texture_data(level, internal_format, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment); +} + +void GLContext::gl_tex_parameter(GLenum target, GLenum pname, GLfloat param) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameter, target, pname, param); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter) + RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); + + // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter) + RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_MIN_FILTER + || pname == GL_TEXTURE_MAG_FILTER + || pname == GL_TEXTURE_WRAP_S + || pname == GL_TEXTURE_WRAP_T), + GL_INVALID_ENUM); + + // We assume GL_TEXTURE_2D (see above) + auto texture_2d = m_active_texture_unit->texture_2d_target_texture(); + if (texture_2d.is_null()) + return; + + switch (pname) { + case GL_TEXTURE_MIN_FILTER: + RETURN_WITH_ERROR_IF(!(param == GL_NEAREST + || param == GL_LINEAR + || param == GL_NEAREST_MIPMAP_NEAREST + || param == GL_LINEAR_MIPMAP_NEAREST + || param == GL_NEAREST_MIPMAP_LINEAR + || param == GL_LINEAR_MIPMAP_LINEAR), + GL_INVALID_ENUM); + + texture_2d->sampler().set_min_filter(param); + break; + + case GL_TEXTURE_MAG_FILTER: + RETURN_WITH_ERROR_IF(!(param == GL_NEAREST + || param == GL_LINEAR), + GL_INVALID_ENUM); + + texture_2d->sampler().set_mag_filter(param); + break; + + case GL_TEXTURE_WRAP_S: + RETURN_WITH_ERROR_IF(!(param == GL_CLAMP + || param == GL_CLAMP_TO_BORDER + || param == GL_CLAMP_TO_EDGE + || param == GL_MIRRORED_REPEAT + || param == GL_REPEAT), + GL_INVALID_ENUM); + + texture_2d->sampler().set_wrap_s_mode(param); + break; + + case GL_TEXTURE_WRAP_T: + RETURN_WITH_ERROR_IF(!(param == GL_CLAMP + || param == GL_CLAMP_TO_BORDER + || param == GL_CLAMP_TO_EDGE + || param == GL_MIRRORED_REPEAT + || param == GL_REPEAT), + GL_INVALID_ENUM); + + texture_2d->sampler().set_wrap_t_mode(param); + break; + + default: + VERIFY_NOT_REACHED(); + } + + m_sampler_config_is_dirty = true; +} + +void GLContext::gl_tex_parameterfv(GLenum target, GLenum pname, GLfloat const* params) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_tex_parameterfv, target, pname, params); + + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // FIXME: We currently only support GL_TETXURE_2D targets. 1D, 3D and CUBE should also be supported (https://docs.gl/gl2/glTexParameter) + RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); + + // FIXME: implement the remaining parameters. (https://docs.gl/gl2/glTexParameter) + RETURN_WITH_ERROR_IF(!(pname == GL_TEXTURE_BORDER_COLOR), GL_INVALID_ENUM); + + // We assume GL_TEXTURE_2D (see above) + auto texture_2d = m_active_texture_unit->texture_2d_target_texture(); + RETURN_WITH_ERROR_IF(texture_2d.is_null(), GL_INVALID_OPERATION); + + switch (pname) { + case GL_TEXTURE_BORDER_COLOR: + texture_2d->sampler().set_border_color(params[0], params[1], params[2], params[3]); + break; + default: + VERIFY_NOT_REACHED(); + } + + m_sampler_config_is_dirty = true; +} + +void GLContext::gl_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid const* data) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // We only support GL_TEXTURE_2D for now + RETURN_WITH_ERROR_IF(target != GL_TEXTURE_2D, GL_INVALID_ENUM); + + // We only support symbolic constants for now + RETURN_WITH_ERROR_IF(!(format == GL_RGBA || format == GL_RGB), GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5), GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(level < 0 || level > Texture2D::LOG2_MAX_TEXTURE_SIZE, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(width < 0 || height < 0 || width > (2 + Texture2D::MAX_TEXTURE_SIZE) || height > (2 + Texture2D::MAX_TEXTURE_SIZE), GL_INVALID_VALUE); + + // A 2D texture array must have been defined by a previous glTexImage2D operation + auto texture_2d = m_active_texture_unit->texture_2d_target_texture(); + RETURN_WITH_ERROR_IF(texture_2d.is_null(), GL_INVALID_OPERATION); + + RETURN_WITH_ERROR_IF(xoffset < 0 || yoffset < 0 || xoffset + width > texture_2d->width_at_lod(level) || yoffset + height > texture_2d->height_at_lod(level), GL_INVALID_VALUE); + + texture_2d->replace_sub_texture_data(level, xoffset, yoffset, width, height, format, type, data, m_unpack_row_length, m_unpack_alignment); +} + +void GLContext::sync_device_sampler_config() +{ + if (!m_sampler_config_is_dirty) + return; + + m_sampler_config_is_dirty = false; + + for (unsigned i = 0; i < m_texture_units.size(); ++i) { + auto const& texture_unit = m_texture_units[i]; + + if (!texture_unit.texture_2d_enabled()) + continue; + + GPU::SamplerConfig config; + + auto texture_2d = texture_unit.texture_2d_target_texture(); + if (texture_2d.is_null()) { + config.bound_image = nullptr; + m_rasterizer->set_sampler_config(i, config); + continue; + } + + config.bound_image = texture_2d->device_image(); + + auto const& sampler = texture_2d->sampler(); + + switch (sampler.min_filter()) { + case GL_NEAREST: + config.texture_min_filter = GPU::TextureFilter::Nearest; + config.mipmap_filter = GPU::MipMapFilter::None; + break; + case GL_LINEAR: + config.texture_min_filter = GPU::TextureFilter::Linear; + config.mipmap_filter = GPU::MipMapFilter::None; + break; + case GL_NEAREST_MIPMAP_NEAREST: + config.texture_min_filter = GPU::TextureFilter::Nearest; + config.mipmap_filter = GPU::MipMapFilter::Nearest; + break; + case GL_LINEAR_MIPMAP_NEAREST: + config.texture_min_filter = GPU::TextureFilter::Linear; + config.mipmap_filter = GPU::MipMapFilter::Nearest; + break; + case GL_NEAREST_MIPMAP_LINEAR: + config.texture_min_filter = GPU::TextureFilter::Nearest; + config.mipmap_filter = GPU::MipMapFilter::Linear; + break; + case GL_LINEAR_MIPMAP_LINEAR: + config.texture_min_filter = GPU::TextureFilter::Linear; + config.mipmap_filter = GPU::MipMapFilter::Linear; + break; + default: + VERIFY_NOT_REACHED(); + } + + switch (sampler.mag_filter()) { + case GL_NEAREST: + config.texture_mag_filter = GPU::TextureFilter::Nearest; + break; + case GL_LINEAR: + config.texture_mag_filter = GPU::TextureFilter::Linear; + break; + default: + VERIFY_NOT_REACHED(); + } + + switch (sampler.wrap_s_mode()) { + case GL_CLAMP: + config.texture_wrap_u = GPU::TextureWrapMode::Clamp; + break; + case GL_CLAMP_TO_BORDER: + config.texture_wrap_u = GPU::TextureWrapMode::ClampToBorder; + break; + case GL_CLAMP_TO_EDGE: + config.texture_wrap_u = GPU::TextureWrapMode::ClampToEdge; + break; + case GL_REPEAT: + config.texture_wrap_u = GPU::TextureWrapMode::Repeat; + break; + case GL_MIRRORED_REPEAT: + config.texture_wrap_u = GPU::TextureWrapMode::MirroredRepeat; + break; + default: + VERIFY_NOT_REACHED(); + } + + switch (sampler.wrap_t_mode()) { + case GL_CLAMP: + config.texture_wrap_v = GPU::TextureWrapMode::Clamp; + break; + case GL_CLAMP_TO_BORDER: + config.texture_wrap_v = GPU::TextureWrapMode::ClampToBorder; + break; + case GL_CLAMP_TO_EDGE: + config.texture_wrap_v = GPU::TextureWrapMode::ClampToEdge; + break; + case GL_REPEAT: + config.texture_wrap_v = GPU::TextureWrapMode::Repeat; + break; + case GL_MIRRORED_REPEAT: + config.texture_wrap_v = GPU::TextureWrapMode::MirroredRepeat; + break; + default: + VERIFY_NOT_REACHED(); + } + + switch (texture_unit.env_mode()) { + case GL_MODULATE: + config.fixed_function_texture_env_mode = GPU::TextureEnvMode::Modulate; + break; + case GL_REPLACE: + config.fixed_function_texture_env_mode = GPU::TextureEnvMode::Replace; + break; + case GL_DECAL: + config.fixed_function_texture_env_mode = GPU::TextureEnvMode::Decal; + break; + case GL_ADD: + config.fixed_function_texture_env_mode = GPU::TextureEnvMode::Add; + break; + default: + VERIFY_NOT_REACHED(); + } + + config.border_color = sampler.border_color(); + m_rasterizer->set_sampler_config(i, config); + } +} + +void GLContext::sync_device_texcoord_config() +{ + if (!m_texcoord_generation_dirty) + return; + m_texcoord_generation_dirty = false; + + auto options = m_rasterizer->options(); + + for (size_t i = 0; i < m_device_info.num_texture_units; ++i) { + + u8 enabled_coordinates = GPU::TexCoordGenerationCoordinate::None; + for (GLenum capability = GL_TEXTURE_GEN_S; capability <= GL_TEXTURE_GEN_Q; ++capability) { + auto const context_coordinate_config = texture_coordinate_generation(i, capability); + if (!context_coordinate_config.enabled) + continue; + + GPU::TexCoordGenerationConfig* texcoord_generation_config; + switch (capability) { + case GL_TEXTURE_GEN_S: + enabled_coordinates |= GPU::TexCoordGenerationCoordinate::S; + texcoord_generation_config = &options.texcoord_generation_config[i][0]; + break; + case GL_TEXTURE_GEN_T: + enabled_coordinates |= GPU::TexCoordGenerationCoordinate::T; + texcoord_generation_config = &options.texcoord_generation_config[i][1]; + break; + case GL_TEXTURE_GEN_R: + enabled_coordinates |= GPU::TexCoordGenerationCoordinate::R; + texcoord_generation_config = &options.texcoord_generation_config[i][2]; + break; + case GL_TEXTURE_GEN_Q: + enabled_coordinates |= GPU::TexCoordGenerationCoordinate::Q; + texcoord_generation_config = &options.texcoord_generation_config[i][3]; + break; + default: + VERIFY_NOT_REACHED(); + } + + switch (context_coordinate_config.generation_mode) { + case GL_OBJECT_LINEAR: + texcoord_generation_config->mode = GPU::TexCoordGenerationMode::ObjectLinear; + texcoord_generation_config->coefficients = context_coordinate_config.object_plane_coefficients; + break; + case GL_EYE_LINEAR: + texcoord_generation_config->mode = GPU::TexCoordGenerationMode::EyeLinear; + texcoord_generation_config->coefficients = context_coordinate_config.eye_plane_coefficients; + break; + case GL_SPHERE_MAP: + texcoord_generation_config->mode = GPU::TexCoordGenerationMode::SphereMap; + break; + case GL_REFLECTION_MAP: + texcoord_generation_config->mode = GPU::TexCoordGenerationMode::ReflectionMap; + break; + case GL_NORMAL_MAP: + texcoord_generation_config->mode = GPU::TexCoordGenerationMode::NormalMap; + break; + } + } + options.texcoord_generation_enabled_coordinates[i] = enabled_coordinates; + } + + m_rasterizer->set_options(options); +} + +} diff --git a/Userland/Libraries/LibGL/Vertex.cpp b/Userland/Libraries/LibGL/Vertex.cpp new file mode 100644 index 0000000000..e6bb923b74 --- /dev/null +++ b/Userland/Libraries/LibGL/Vertex.cpp @@ -0,0 +1,336 @@ +/* + * Copyright (c) 2021, Jesse Buhagiar + * Copyright (c) 2021, Stephan Unverwerth + * Copyright (c) 2022, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace GL { + +void GLContext::gl_array_element(GLint i) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_array_element, i); + RETURN_WITH_ERROR_IF(i < 0, GL_INVALID_VALUE); + + // This is effectively the same as `gl_draw_elements`, except we only output a single + // vertex (this is done between a `gl_begin/end` call) that is to be rendered. + if (!m_client_side_vertex_array_enabled) + return; + + if (m_client_side_color_array_enabled) { + float color[4] { 0, 0, 0, 1 }; + read_from_vertex_attribute_pointer(m_client_color_pointer, i, color); + gl_color(color[0], color[1], color[2], color[3]); + } + + for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) { + if (m_client_side_texture_coord_array_enabled[t]) { + float tex_coords[4] { 0, 0, 0, 0 }; + read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords); + gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]); + } + } + + if (m_client_side_normal_array_enabled) { + float normal[3]; + read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal); + gl_normal(normal[0], normal[1], normal[2]); + } + + float vertex[4] { 0, 0, 0, 1 }; + read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex); + gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]); +} + +void GLContext::gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color, r, g, b, a); + + m_current_vertex_color = { + static_cast(r), + static_cast(g), + static_cast(b), + static_cast(a), + }; +} + +void GLContext::gl_color_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(!(size == 3 || size == 4), GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(type != GL_BYTE + && type != GL_UNSIGNED_BYTE + && type != GL_SHORT + && type != GL_UNSIGNED_SHORT + && type != GL_INT + && type != GL_UNSIGNED_INT + && type != GL_FLOAT + && type != GL_DOUBLE, + GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE); + + m_client_color_pointer = { .size = size, .type = type, .stride = stride, .pointer = pointer }; +} + +void GLContext::gl_draw_arrays(GLenum mode, GLint first, GLsizei count) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_arrays, mode, first, count); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES) + RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP + || mode == GL_TRIANGLE_FAN + || mode == GL_TRIANGLES + || mode == GL_QUADS + || mode == GL_QUAD_STRIP + || mode == GL_POLYGON), + GL_INVALID_ENUM); + + RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE); + + // At least the vertex array needs to be enabled + if (!m_client_side_vertex_array_enabled) + return; + + auto last = first + count; + gl_begin(mode); + for (int i = first; i < last; i++) { + if (m_client_side_color_array_enabled) { + float color[4] { 0, 0, 0, 1 }; + read_from_vertex_attribute_pointer(m_client_color_pointer, i, color); + gl_color(color[0], color[1], color[2], color[3]); + } + + for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) { + if (m_client_side_texture_coord_array_enabled[t]) { + float tex_coords[4] { 0, 0, 0, 0 }; + read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords); + gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]); + } + } + + if (m_client_side_normal_array_enabled) { + float normal[3]; + read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal); + gl_normal(normal[0], normal[1], normal[2]); + } + + float vertex[4] { 0, 0, 0, 1 }; + read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex); + gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]); + } + gl_end(); +} + +void GLContext::gl_draw_elements(GLenum mode, GLsizei count, GLenum type, void const* indices) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_elements, mode, count, type, indices); + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + + // FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES) + RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP + || mode == GL_TRIANGLE_FAN + || mode == GL_TRIANGLES + || mode == GL_QUADS + || mode == GL_QUAD_STRIP + || mode == GL_POLYGON), + GL_INVALID_ENUM); + + RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE + || type == GL_UNSIGNED_SHORT + || type == GL_UNSIGNED_INT), + GL_INVALID_ENUM); + + RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE); + + // At least the vertex array needs to be enabled + if (!m_client_side_vertex_array_enabled) + return; + + gl_begin(mode); + for (int index = 0; index < count; index++) { + int i = 0; + switch (type) { + case GL_UNSIGNED_BYTE: + i = reinterpret_cast(indices)[index]; + break; + case GL_UNSIGNED_SHORT: + i = reinterpret_cast(indices)[index]; + break; + case GL_UNSIGNED_INT: + i = reinterpret_cast(indices)[index]; + break; + } + + if (m_client_side_color_array_enabled) { + float color[4] { 0, 0, 0, 1 }; + read_from_vertex_attribute_pointer(m_client_color_pointer, i, color); + gl_color(color[0], color[1], color[2], color[3]); + } + + for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) { + if (m_client_side_texture_coord_array_enabled[t]) { + float tex_coords[4] { 0, 0, 0, 0 }; + read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords); + gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]); + } + } + + if (m_client_side_normal_array_enabled) { + float normal[3]; + read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal); + gl_normal(normal[0], normal[1], normal[2]); + } + + float vertex[4] { 0, 0, 0, 1 }; + read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex); + gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]); + } + gl_end(); +} + +void GLContext::gl_normal(GLfloat nx, GLfloat ny, GLfloat nz) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_normal, nx, ny, nz); + + m_current_vertex_normal = { nx, ny, nz }; +} + +void GLContext::gl_normal_pointer(GLenum type, GLsizei stride, void const* pointer) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(type != GL_BYTE + && type != GL_SHORT + && type != GL_INT + && type != GL_FLOAT + && type != GL_DOUBLE, + GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE); + + m_client_normal_pointer = { .size = 3, .type = type, .stride = stride, .pointer = pointer }; +} + +void GLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) +{ + APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_vertex, x, y, z, w); + + GPU::Vertex vertex; + + vertex.position = { static_cast(x), static_cast(y), static_cast(z), static_cast(w) }; + vertex.color = m_current_vertex_color; + for (size_t i = 0; i < m_device_info.num_texture_units; ++i) + vertex.tex_coords[i] = m_current_vertex_tex_coord[i]; + vertex.normal = m_current_vertex_normal; + + m_vertex_list.append(vertex); +} + +void GLContext::gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer) +{ + RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(!(size == 2 || size == 3 || size == 4), GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM); + RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE); + + m_client_vertex_pointer = { .size = size, .type = type, .stride = stride, .pointer = pointer }; +} + +// General helper function to read arbitrary vertex attribute data into a float array +void GLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements) +{ + auto byte_ptr = reinterpret_cast(attrib.pointer); + auto normalize = attrib.normalize; + size_t stride = attrib.stride; + + switch (attrib.type) { + case GL_BYTE: { + if (stride == 0) + stride = sizeof(GLbyte) * attrib.size; + + for (int i = 0; i < attrib.size; i++) { + elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); + if (normalize) + elements[i] /= 0x80; + } + break; + } + case GL_UNSIGNED_BYTE: { + if (stride == 0) + stride = sizeof(GLubyte) * attrib.size; + + for (int i = 0; i < attrib.size; i++) { + elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); + if (normalize) + elements[i] /= 0xff; + } + break; + } + case GL_SHORT: { + if (stride == 0) + stride = sizeof(GLshort) * attrib.size; + + for (int i = 0; i < attrib.size; i++) { + elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); + if (normalize) + elements[i] /= 0x8000; + } + break; + } + case GL_UNSIGNED_SHORT: { + if (stride == 0) + stride = sizeof(GLushort) * attrib.size; + + for (int i = 0; i < attrib.size; i++) { + elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); + if (normalize) + elements[i] /= 0xffff; + } + break; + } + case GL_INT: { + if (stride == 0) + stride = sizeof(GLint) * attrib.size; + + for (int i = 0; i < attrib.size; i++) { + elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); + if (normalize) + elements[i] /= 0x80000000; + } + break; + } + case GL_UNSIGNED_INT: { + if (stride == 0) + stride = sizeof(GLuint) * attrib.size; + + for (int i = 0; i < attrib.size; i++) { + elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); + if (normalize) + elements[i] /= 0xffffffff; + } + break; + } + case GL_FLOAT: { + if (stride == 0) + stride = sizeof(GLfloat) * attrib.size; + + for (int i = 0; i < attrib.size; i++) + elements[i] = *(reinterpret_cast(byte_ptr + stride * index) + i); + break; + } + case GL_DOUBLE: { + if (stride == 0) + stride = sizeof(GLdouble) * attrib.size; + + for (int i = 0; i < attrib.size; i++) + elements[i] = static_cast(*(reinterpret_cast(byte_ptr + stride * index) + i)); + break; + } + } +} + +} -- cgit v1.2.3