diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index e1c628240e..2d3dc016f2 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -161,7 +161,7 @@ if(WITH_OPENGL) set(GLAD_SOUURCES_DIR "lib/glad") add_subdirectory("${GLAD_SOUURCES_DIR}/cmake" glad_cmake SYSTEM) if(EMSCRIPTEN) - glad_add_library(glad REPRODUCIBLE API gles2=3.0) + glad_add_library(glad REPRODUCIBLE API gles2=3.0 EXTENSIONS GL_EXT_color_buffer_float) else() glad_add_library(glad REPRODUCIBLE API gl:core=3.3) endif() diff --git a/core/include/cubos/core/gl/render_device.hpp b/core/include/cubos/core/gl/render_device.hpp index ebb4f2fe61..719f19842f 100644 --- a/core/include/cubos/core/gl/render_device.hpp +++ b/core/include/cubos/core/gl/render_device.hpp @@ -952,6 +952,11 @@ namespace cubos::core::gl /// @brief Unmaps the pixel buffer. virtual void unmap() = 0; + // @brief Fills the buffer with the given data. + /// @param data Pointer to data. + /// @param size Data size in bytes. + virtual void fill(const void* data, std::size_t size) = 0; + protected: PixelPackBuffer() = default; }; @@ -1081,15 +1086,6 @@ namespace cubos::core::gl public: virtual ~ConstantBuffer() = default; - /// @brief Maps the constant buffer to a region in memory. Must be matched with a call - /// to @ref unmap(). - /// @return Pointer to the memory region. - virtual void* map() = 0; - - /// @brief Unmaps the constant buffer, updating it with data written to the mapped - /// region. - virtual void unmap() = 0; - /// @brief Fills the buffer with the given data. /// @param data Pointer to data. /// @param size Data size in bytes. @@ -1105,13 +1101,10 @@ namespace cubos::core::gl public: virtual ~IndexBuffer() = default; - /// @brief Maps the index buffer to a region in memory. Must be matched with a call - /// to @ref unmap(). - /// @return Pointer to the memory region. - virtual void* map() = 0; - - /// @brief Unmaps the index buffer, updating it with data written to the mapped region. - virtual void unmap() = 0; + /// @brief Fills the buffer with the given data. + /// @param data Pointer to data. + /// @param size Data size in bytes. + virtual void fill(const void* data, std::size_t size) = 0; protected: IndexBuffer() = default; @@ -1123,23 +1116,17 @@ namespace cubos::core::gl public: virtual ~VertexBuffer() = default; - /// @brief Maps the vertex buffer to a region in memory. Must be matched with a call - /// to @ref unmap(). - /// @return Pointer to the memory region. - virtual void* map() = 0; - - /// @brief Maps a region of the vertex buffer to a region in memory. Must be matched with a call to @ref - /// unmap(). - /// @param offset Offset in bytes. - /// @param length Length in bytes. - /// @param synchronized Whether pending operations on the buffer should be synchronized prior to returning - /// from this method. - /// @return Pointer to the memory region. - virtual void* map(std::size_t offset, std::size_t length, bool synchronized = true) = 0; + // @brief Fills the buffer with the given data. + /// @param data Pointer to data. + /// @param size Data size in bytes. + virtual void fill(const void* data, std::size_t size) = 0; - /// @brief Unmaps the vertex buffer, updating it with data written to the mapped - /// region. - virtual void unmap() = 0; + // @brief Fills the buffer with the given data. + /// @param data Pointer to data. + /// @param size Data size in bytes. + /// @param offset Offset in the buffer where the data will be written. + /// @param synchronized TODO: IDK WHAT THIS DOES HELP RICARDO + virtual void fill(const void* data, std::size_t offset, std::size_t size, bool synchronized) = 0; protected: VertexBuffer() = default; diff --git a/core/src/gl/ogl_render_device.cpp b/core/src/gl/ogl_render_device.cpp index fb6e6bc105..41254d750d 100644 --- a/core/src/gl/ogl_render_device.cpp +++ b/core/src/gl/ogl_render_device.cpp @@ -583,6 +583,13 @@ class OGLPixelPackBuffer : public impl::PixelPackBuffer CHECK(glUnmapBuffer(GL_PIXEL_PACK_BUFFER)); } + void fill(const void* data, std::size_t size) override + { + glBindBuffer(GL_PIXEL_PACK_BUFFER, this->id); + glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, static_cast(size), data); + glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); + } + std::shared_ptr destroyed; GLuint id; @@ -630,6 +637,11 @@ class OGLTexture2D : public impl::Texture2D { if (!*destroyed) { + if (this->framebuffer != 0) + { + CHECK(glDeleteFramebuffers(1, &this->framebuffer)); + } + CHECK(glDeleteTextures(1, &this->id)); } } @@ -645,17 +657,29 @@ class OGLTexture2D : public impl::Texture2D void read(void* outputBuffer) override { - CHECK(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0)); CHECK(glBindTexture(GL_TEXTURE_2D, this->id)); CHECK(glReadPixels(0, 0, this->width, this->height, this->format, this->type, outputBuffer)); } void copyTo(PixelPackBuffer buffer) override { + if (this->framebuffer == 0) + { + CHECK(glGenFramebuffers(1, &this->framebuffer)); + CHECK(glBindFramebuffer(GL_FRAMEBUFFER, this->framebuffer)); + CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->id, 0)); + } + else + { + CHECK(glBindFramebuffer(GL_FRAMEBUFFER, this->framebuffer)); + } + + CHECK(glViewport(0, 0, this->width, this->height)); CHECK(glBindBuffer(GL_PIXEL_PACK_BUFFER, std::static_pointer_cast(buffer)->id)); - CHECK(glActiveTexture(GL_TEXTURE0)); - CHECK(glBindTexture(GL_TEXTURE_2D, this->id)); CHECK(glReadPixels(0, 0, this->width, this->height, this->format, this->type, nullptr)); + CHECK(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0)); + + CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0)); } void generateMipmaps() override @@ -672,6 +696,7 @@ class OGLTexture2D : public impl::Texture2D GLenum type; GLsizei width; GLsizei height; + GLuint framebuffer = 0; }; class OGLTexture2DArray : public impl::Texture2DArray @@ -824,17 +849,6 @@ class OGLConstantBuffer : public impl::ConstantBuffer } } - void* map() override - { - glBindBuffer(GL_UNIFORM_BUFFER, this->id); - return glMapBufferRange(GL_UNIFORM_BUFFER, 0, this->size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); - } - - void unmap() override - { - glUnmapBuffer(GL_UNIFORM_BUFFER); - } - void fill(const void* data, std::size_t size) override { CHECK(glBindBuffer(GL_UNIFORM_BUFFER, this->id)); @@ -867,18 +881,10 @@ class OGLIndexBuffer : public impl::IndexBuffer } } - void* map() override + void fill(const void* data, std::size_t size) override { CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id)); - void* ptr; - CHECK_VAL(ptr, glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, this->size, - GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT)); - return ptr; - } - - void unmap() override - { - CHECK(glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER)); + CHECK(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast(size), data)); } std::shared_ptr destroyed; @@ -907,29 +913,33 @@ class OGLVertexBuffer : public impl::VertexBuffer } } - void* map() override + void fill(const void* data, std::size_t size) override { CHECK(glBindBuffer(GL_ARRAY_BUFFER, this->id)); - void* ptr; - CHECK_VAL(ptr, - glMapBufferRange(GL_ARRAY_BUFFER, 0, this->size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT)); - return ptr; + CHECK(glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast(size), data)); } - void* map(std::size_t offset, std::size_t length, bool synchronized) override + void fill(const void* data, std::size_t offset, std::size_t size, bool synchronized = true) override { - GLbitfield flags = GL_MAP_WRITE_BIT; - flags |= synchronized ? 0 : GL_MAP_UNSYNCHRONIZED_BIT; - CHECK(glBindBuffer(GL_ARRAY_BUFFER, this->id)); - void* ptr; - CHECK_VAL(ptr, glMapBufferRange(GL_ARRAY_BUFFER, static_cast(offset), static_cast(length), - flags)); - return ptr; - } +#ifdef __EMSCRIPTEN__ + synchronized = true; +#endif - void unmap() override - { - CHECK(glUnmapBuffer(GL_ARRAY_BUFFER)); + if (synchronized) + { + CHECK(glBindBuffer(GL_ARRAY_BUFFER, this->id)); + CHECK(glBufferSubData(GL_ARRAY_BUFFER, static_cast(offset), static_cast(size), data)); + } + else + { + CHECK(glBindBuffer(GL_ARRAY_BUFFER, this->id)); + void* ptr; + CHECK_VAL(ptr, + glMapBufferRange(GL_ARRAY_BUFFER, static_cast(offset), static_cast(size), + GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT)); + memcpy(ptr, data, size); + CHECK(glUnmapBuffer(GL_ARRAY_BUFFER)); + } } std::shared_ptr destroyed; @@ -1308,6 +1318,11 @@ OGLRenderDevice::OGLRenderDevice() CHECK(glEnable(GL_DEBUG_OUTPUT)); CHECK(glDebugMessageCallback(messageCallback, nullptr)); } +#else + if (!GLAD_GL_EXT_color_buffer_float) + { + CUBOS_WARN("GL_EXT_color_buffer_float is required to use floating point textures as render targets"); + } #endif // Create default states @@ -2174,7 +2189,8 @@ ShaderStage OGLRenderDevice::createShaderStage(Stage stage, const char* src) #define GLSL_HEADER \ "#version 300 es\n" \ "#define ES\n" \ - "precision highp float;\n" + "precision highp float;\n" \ + "precision highp int;\n" #else #define GLSL_HEADER "#version 330 core\n" #endif diff --git a/engine/assets/render/bloom.fs b/engine/assets/render/bloom.fs index 3e121edc23..a5d6e608b8 100644 --- a/engine/assets/render/bloom.fs +++ b/engine/assets/render/bloom.fs @@ -12,7 +12,7 @@ in vec2 fragUv; out vec4 fragColor; -vec3 sample(vec2 uv) +vec3 sampleTex(vec2 uv) { return texture(inputTexture, uv).rgb; } @@ -20,14 +20,14 @@ vec3 sample(vec2 uv) vec4 sampleBox(vec2 texelSize, vec2 uv, float delta) { vec4 offset = texelSize.xyxy * vec4(-delta, -delta, delta, delta); - vec3 color = sample(uv + offset.xy) + sample(uv + offset.zy) + - sample(uv + offset.xw) + sample(uv + offset.zw); + vec3 color = sampleTex(uv + offset.xy) + sampleTex(uv + offset.zy) + + sampleTex(uv + offset.xw) + sampleTex(uv + offset.zw); return vec4(color * 0.25, 1.0); } void main(void) { - vec2 texelSize = 1.0 / textureSize(inputTexture, 0); + vec2 texelSize = 1.0 / vec2(textureSize(inputTexture, 0)); switch(pass) { case DOWNSCALE_PASS: diff --git a/engine/assets/render/deferred_shading.fs b/engine/assets/render/deferred_shading.fs index fb1d2aa478..5936b187e5 100644 --- a/engine/assets/render/deferred_shading.fs +++ b/engine/assets/render/deferred_shading.fs @@ -65,15 +65,15 @@ vec3 spotLightCalc(vec3 fragPos, vec3 fragNormal, SpotLight light) { vec3 toLight = vec3(light.position) - fragPos; float r = length(toLight) / light.range; - if (r < 1) + if (r < 1.0) { vec3 toLightNormalized = normalize(toLight); float a = dot(toLightNormalized, -light.direction.xyz); if (a > light.spotCutoff) { - float angleValue = clamp(remap(a, light.innerSpotCutoff, light.spotCutoff, 1, 0), 0, 1); - float attenuation = clamp(1.0 / (1.0 + 25.0 * r * r) * clamp((1 - r) * 5.0, 0, 1), 0, 1); - float diffuse = max(dot(fragNormal, toLightNormalized), 0); + float angleValue = clamp(remap(a, light.innerSpotCutoff, light.spotCutoff, 1.0, 0.0), 0.0, 1.0); + float attenuation = clamp(1.0 / (1.0 + 25.0 * r * r) * clamp((1.0 - r) * 5.0, 0.0, 1.0), 0.0, 1.0); + float diffuse = max(dot(fragNormal, toLightNormalized), 0.0); return angleValue * attenuation * diffuse * light.intensity * vec3(light.color); } } @@ -82,17 +82,17 @@ vec3 spotLightCalc(vec3 fragPos, vec3 fragNormal, SpotLight light) vec3 directionalLightCalc(vec3 fragNormal, DirectionalLight light) { - return max(dot(fragNormal, -light.direction.xyz), 0) * light.intensity * vec3(light.color); + return max(dot(fragNormal, -light.direction.xyz), 0.0) * light.intensity * vec3(light.color); } vec3 pointLightCalc(vec3 fragPos, vec3 fragNormal, PointLight light) { vec3 toLight = vec3(light.position) - fragPos; float r = length(toLight) / light.range; - if (r < 1) + if (r < 1.0) { - float attenuation = clamp(1.0 / (1.0 + 25.0 * r * r) * clamp((1 - r) * 5.0, 0, 1), 0, 1); - float diffuse = max(dot(fragNormal, vec3(normalize(toLight))), 0); + float attenuation = clamp(1.0 / (1.0 + 25.0 * r * r) * clamp((1.0 - r) * 5.0, 0.0, 1.0), 0.0, 1.0); + float diffuse = max(dot(fragNormal, vec3(normalize(toLight))), 0.0); return attenuation * diffuse * light.intensity * vec3(light.color); } return vec3(0); diff --git a/engine/assets/render/g_buffer_rasterizer.vs b/engine/assets/render/g_buffer_rasterizer.vs index 8bbdfb756c..d961523554 100644 --- a/engine/assets/render/g_buffer_rasterizer.vs +++ b/engine/assets/render/g_buffer_rasterizer.vs @@ -26,17 +26,17 @@ void main(void) fragPosition = vec3(worldPosition); gl_Position = viewProj * worldPosition; - vec3 localNormal = vec3(0, 0, 0); - if (normal == 0u) localNormal.x = +1; - if (normal == 1u) localNormal.x = -1; - if (normal == 2u) localNormal.y = +1; - if (normal == 3u) localNormal.y = -1; - if (normal == 4u) localNormal.z = +1; - if (normal == 5u) localNormal.z = -1; + vec3 localNormal = vec3(0.0, 0.0, 0.0); + if (normal == 0u) localNormal.x = +1.0; + if (normal == 1u) localNormal.x = -1.0; + if (normal == 2u) localNormal.y = +1.0; + if (normal == 3u) localNormal.y = -1.0; + if (normal == 4u) localNormal.z = +1.0; + if (normal == 5u) localNormal.z = -1.0; mat3 N = transpose(inverse(mat3(model))); fragNormal = N * localNormal; - fragAlbedo = texelFetch(palette, ivec2(mod(material, 256u), material / 256u), 0).rgb; + fragAlbedo = texelFetch(palette, ivec2(mod(float(material), 256.0), material / 256u), 0).rgb; fragPicker.r = (picker >> 16U); fragPicker.g = (picker & 65535U); diff --git a/engine/assets/render/ssao_base.fs b/engine/assets/render/ssao_base.fs index 1c2be45442..f459662c29 100644 --- a/engine/assets/render/ssao_base.fs +++ b/engine/assets/render/ssao_base.fs @@ -42,7 +42,7 @@ void main(void) vec3 normal = getViewNormal(fragUv); // Sample a noise vector. - vec2 noiseScale = vec2(textureSize(positionTexture, 0)) / 4; + vec2 noiseScale = vec2(textureSize(positionTexture, 0)) / 4.0; vec3 randomVector = normalize(texture(noiseTexture, fragUv * noiseScale).xyz); // Calculate the TBN matrix from the fragment normal and the random vector. @@ -75,5 +75,5 @@ void main(void) // We substract the occlusion factor from 1.0 to get the ambient occlusion color. // Then in further processing we can simply multiply this value with the color of the fragment. - color = 1.0 - (occlusion / kernelSize); + color = 1.0 - (occlusion / float(kernelSize)); } diff --git a/engine/assets/ui/element.vs b/engine/assets/ui/element.vs index 96fd764468..b8c1a7c7a1 100644 --- a/engine/assets/ui/element.vs +++ b/engine/assets/ui/element.vs @@ -15,6 +15,6 @@ uniform MVP void main() { - gl_Position = mvp * vec4((1 - position.x) * xRange.x + position.x * xRange.y, - (1 - position.y) * yRange.x + position.y * yRange.y, depth, 1); + gl_Position = mvp * vec4((1.0 - position.x) * xRange.x + position.x * xRange.y, + (1.0 - position.y) * yRange.x + position.y * yRange.y, depth, 1.0); } diff --git a/engine/src/gizmos/types/box.hpp b/engine/src/gizmos/types/box.hpp index 9478218434..4e16b32a84 100644 --- a/engine/src/gizmos/types/box.hpp +++ b/engine/src/gizmos/types/box.hpp @@ -38,17 +38,17 @@ namespace cubos::engine void draw(cubos::engine::GizmosRenderer& renderer, DrawPhase phase, const glm::mat<4, 4, float, glm::packed_highp>& mvp) override { - auto* verts = static_cast(renderer.boxPrimitive.vb->map()); - verts[0] = mTransform * glm::vec4{mPointA[0], mPointA[1], mPointA[2], 1.0F}; - verts[1] = mTransform * glm::vec4{mPointB[0], mPointA[1], mPointA[2], 1.0F}; - verts[2] = mTransform * glm::vec4{mPointB[0], mPointA[1], mPointB[2], 1.0F}; - verts[3] = mTransform * glm::vec4{mPointA[0], mPointA[1], mPointB[2], 1.0F}; - verts[4] = mTransform * glm::vec4{mPointA[0], mPointB[1], mPointB[2], 1.0F}; - verts[5] = mTransform * glm::vec4{mPointA[0], mPointB[1], mPointA[2], 1.0F}; - verts[6] = mTransform * glm::vec4{mPointB[0], mPointB[1], mPointA[2], 1.0F}; - verts[7] = mTransform * glm::vec4{mPointB[0], mPointB[1], mPointB[2], 1.0F}; - - renderer.boxPrimitive.vb->unmap(); + glm::vec3 verts[8] = { + mTransform * glm::vec4{mPointA[0], mPointA[1], mPointA[2], 1.0F}, + mTransform * glm::vec4{mPointB[0], mPointA[1], mPointA[2], 1.0F}, + mTransform * glm::vec4{mPointB[0], mPointA[1], mPointB[2], 1.0F}, + mTransform * glm::vec4{mPointA[0], mPointA[1], mPointB[2], 1.0F}, + mTransform * glm::vec4{mPointA[0], mPointB[1], mPointB[2], 1.0F}, + mTransform * glm::vec4{mPointA[0], mPointB[1], mPointA[2], 1.0F}, + mTransform * glm::vec4{mPointB[0], mPointB[1], mPointA[2], 1.0F}, + mTransform * glm::vec4{mPointB[0], mPointB[1], mPointB[2], 1.0F}, + }; + renderer.boxPrimitive.vb->fill(verts, sizeof(verts)); renderer.renderDevice->setVertexArray(renderer.boxPrimitive.va); renderer.renderDevice->setIndexBuffer(renderer.boxPrimitive.ib); @@ -71,5 +71,5 @@ namespace cubos::engine renderer.renderDevice->createRasterState(cubos::core::gl::RasterStateDesc{})); renderer.renderDevice->drawTrianglesIndexed(0, 36); } - }; + }; // namespace cubos::engine } // namespace cubos::engine diff --git a/engine/src/gizmos/types/cut_cone.hpp b/engine/src/gizmos/types/cut_cone.hpp index 37d94eb3d0..83cd2b24f3 100644 --- a/engine/src/gizmos/types/cut_cone.hpp +++ b/engine/src/gizmos/types/cut_cone.hpp @@ -43,8 +43,7 @@ namespace cubos::engine void draw(GizmosRenderer& renderer, DrawPhase phase, const glm::mat<4, 4, float, glm::packed_highp>& mvp) override { - auto* verts = static_cast(renderer.cutConePrimitive.vb->map()); - + glm::vec3 verts[2 * GizmosRenderer::CutConeVertsPerBase]; glm::vec3 n = glm::normalize(mPointB - mPointA); glm::vec3 p; @@ -81,23 +80,21 @@ namespace cubos::engine for (int i = 0; i < GizmosRenderer::CutConeVertsPerBase; i++) { - glm::vec3 vert = + verts[i] = glm::rotate(pA, (float)i * glm::radians(360.0F / (float)GizmosRenderer::CutConeVertsPerBase), n) + mPointA; - verts[i] = {vert[0], vert[1], vert[2]}; } glm::vec3 pB = p * mRadiusB; for (int i = GizmosRenderer::CutConeVertsPerBase; i < 2 * GizmosRenderer::CutConeVertsPerBase; i++) { - glm::vec3 vert = + verts[i] = glm::rotate(pB, (float)i * glm::radians(360.0F / (float)GizmosRenderer::CutConeVertsPerBase), n) + mPointB; - verts[i] = {vert[0], vert[1], vert[2]}; } - renderer.cutConePrimitive.vb->unmap(); + renderer.cutConePrimitive.vb->fill(verts, sizeof(verts)); renderer.renderDevice->setVertexArray(renderer.cutConePrimitive.va); renderer.renderDevice->setIndexBuffer(renderer.cutConePrimitive.ib); diff --git a/engine/src/gizmos/types/line.hpp b/engine/src/gizmos/types/line.hpp index a262a1106c..8a055c06bd 100644 --- a/engine/src/gizmos/types/line.hpp +++ b/engine/src/gizmos/types/line.hpp @@ -35,11 +35,8 @@ namespace cubos::engine void draw(cubos::engine::GizmosRenderer& renderer, DrawPhase phase, const glm::mat<4, 4, float, glm::packed_highp>& mvp) override { - auto* verts = static_cast(renderer.linePrimitive.vb->map()); - verts[0] = {mPointA[0], mPointA[1], mPointA[2]}; - verts[1] = {mPointB[0], mPointB[1], mPointB[2]}; - - renderer.linePrimitive.vb->unmap(); + glm::vec3 verts[2] = {mPointA, mPointB}; + renderer.linePrimitive.vb->fill(verts, sizeof(verts)); renderer.renderDevice->setVertexArray(renderer.linePrimitive.va); diff --git a/engine/src/gizmos/types/ring.hpp b/engine/src/gizmos/types/ring.hpp index c79ff9065c..a3ac7025b7 100644 --- a/engine/src/gizmos/types/ring.hpp +++ b/engine/src/gizmos/types/ring.hpp @@ -43,8 +43,6 @@ namespace cubos::engine void draw(GizmosRenderer& renderer, DrawPhase phase, const glm::mat<4, 4, float, glm::packed_highp>& mvp) override { - auto* verts = static_cast(renderer.ringPrimitive.vb->map()); - glm::vec3 n = glm::normalize(mPointB - mPointA); glm::vec3 p; if (n[0] != 0 && n[1] != 0) @@ -79,29 +77,23 @@ namespace cubos::engine glm::vec3 pA = p * mRadiusOuter; glm::vec3 pB = p * mRadiusInner; + glm::vec3 verts[4 * GizmosRenderer::RingSections]; for (std::size_t i = 0; i < GizmosRenderer::RingSections; i++) { - glm::vec3 vertOuter = + verts[2 * i] = glm::rotate(pA, (float)i * glm::radians(360.0F / (float)GizmosRenderer::RingSections), n) + mPointA; - glm::vec3 vertInner = + verts[(2 * i) + 1] = glm::rotate(pB, (float)i * glm::radians(360.0F / (float)GizmosRenderer::RingSections), n) + mPointA; - verts[2 * i] = {vertOuter[0], vertOuter[1], vertOuter[2]}; - verts[(2 * i) + 1] = {vertInner[0], vertInner[1], vertInner[2]}; } for (std::size_t i = 0; i < GizmosRenderer::RingSections; i++) { - glm::vec3 vertOuter = + verts[(std::size_t)GizmosRenderer::RingSections * 2 + 2 * i] = glm::rotate(pA, (float)i * glm::radians(360.0F / (float)GizmosRenderer::RingSections), n) + mPointB; - glm::vec3 vertInner = + verts[(std::size_t)GizmosRenderer::RingSections * 2 + (2 * i) + 1] = glm::rotate(pB, (float)i * glm::radians(360.0F / (float)GizmosRenderer::RingSections), n) + mPointB; - verts[(std::size_t)GizmosRenderer::RingSections * 2 + 2 * i] = {vertOuter[0], vertOuter[1], - vertOuter[2]}; - verts[(std::size_t)GizmosRenderer::RingSections * 2 + (2 * i) + 1] = {vertInner[0], vertInner[1], - vertInner[2]}; } - - renderer.ringPrimitive.vb->unmap(); + renderer.ringPrimitive.vb->fill(verts, sizeof(verts)); renderer.renderDevice->setVertexArray(renderer.ringPrimitive.va); renderer.renderDevice->setIndexBuffer(renderer.ringPrimitive.ib); diff --git a/engine/src/imgui/imgui.cpp b/engine/src/imgui/imgui.cpp index d44e13d9c2..5e52220af2 100644 --- a/engine/src/imgui/imgui.cpp +++ b/engine/src/imgui/imgui.cpp @@ -419,10 +419,9 @@ void cubos::engine::imguiEndFrame(const gl::Framebuffer& target) auto* drawData = ImGui::GetDrawData(); // Upload projection matrix to constant buffer. - glm::mat4& proj = *(glm::mat4*)bd->cb->map(); - proj = glm::ortho(drawData->DisplayPos.x, drawData->DisplayPos.x + drawData->DisplaySize.x, - drawData->DisplayPos.y + drawData->DisplaySize.y, drawData->DisplayPos.y); - bd->cb->unmap(); + auto proj = glm::ortho(drawData->DisplayPos.x, drawData->DisplayPos.x + drawData->DisplaySize.x, + drawData->DisplayPos.y + drawData->DisplaySize.y, drawData->DisplayPos.y); + bd->cb->fill(&proj, sizeof(proj)); // Set render state. setupRenderState(bd, target); @@ -476,12 +475,8 @@ void cubos::engine::imguiEndFrame(const gl::Framebuffer& target) } // Upload vertex and index data into vertex buffer. - auto* vtxDst = (ImDrawVert*)bd->vb->map(); - auto* idxDst = (ImDrawIdx*)bd->ib->map(); - memcpy(vtxDst, cmdList->VtxBuffer.Data, static_cast(cmdList->VtxBuffer.Size) * sizeof(ImDrawVert)); - memcpy(idxDst, cmdList->IdxBuffer.Data, static_cast(cmdList->IdxBuffer.Size) * sizeof(ImDrawIdx)); - bd->vb->unmap(); - bd->ib->unmap(); + bd->vb->fill(cmdList->VtxBuffer.Data, static_cast(cmdList->VtxBuffer.Size) * sizeof(ImDrawVert)); + bd->ib->fill(cmdList->IdxBuffer.Data, static_cast(cmdList->IdxBuffer.Size) * sizeof(ImDrawIdx)); rd.setVertexArray(bd->va); rd.setIndexBuffer(bd->ib); diff --git a/engine/src/render/g_buffer/plugin.cpp b/engine/src/render/g_buffer/plugin.cpp index 091894a30b..ae83b26f95 100644 --- a/engine/src/render/g_buffer/plugin.cpp +++ b/engine/src/render/g_buffer/plugin.cpp @@ -43,7 +43,7 @@ void cubos::engine::gBufferPlugin(Cubos& cubos) auto& rd = window->renderDevice(); // Create position and normal textures. - desc.format = TextureFormat::RGB32Float; + desc.format = TextureFormat::RGBA32Float; gBuffer.position = rd.createTexture2D(desc); gBuffer.normal = rd.createTexture2D(desc); diff --git a/engine/src/render/mesh/pool.cpp b/engine/src/render/mesh/pool.cpp index efea9c2ff1..3aa1bb54ce 100644 --- a/engine/src/render/mesh/pool.cpp +++ b/engine/src/render/mesh/pool.cpp @@ -92,10 +92,8 @@ auto RenderMeshPool::allocate(const RenderMeshVertex* vertices, std::size_t coun // Notice that we do an unsynchronized access - if any operation on the region is still occurring, the behavior // will be undefined. Since we popped the first bucket in the free queue, it probably hasn't been used in a long // time. Thus, no synchronization here should be safe (and give us a very big performance boost). - auto* buffer = mVertexBuffer->map(bucketId.inner * mBucketSize * sizeof(RenderMeshVertex), - amount * sizeof(RenderMeshVertex), /*synchronized=*/false); - memcpy(buffer, vertices, amount * sizeof(RenderMeshVertex)); - mVertexBuffer->unmap(); + mVertexBuffer->fill(vertices, bucketId.inner * mBucketSize * sizeof(RenderMeshVertex), + amount * sizeof(RenderMeshVertex), /*synchronized=*/false); // Setup bucket information. mBuckets[bucketId.inner].size = amount; diff --git a/engine/src/render/ssao/plugin.cpp b/engine/src/render/ssao/plugin.cpp index 0a7fc3649b..3cbfb56f9e 100644 --- a/engine/src/render/ssao/plugin.cpp +++ b/engine/src/render/ssao/plugin.cpp @@ -104,7 +104,7 @@ namespace Texture2DDesc desc{}; desc.width = desc.height = 4; - desc.format = TextureFormat::RGB32Float; + desc.format = TextureFormat::RGBA32Float; desc.data[0] = ssaoNoise.data(); noiseTexture = renderDevice.createTexture2D(desc); }