Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
luishfonseca committed Jun 14, 2024
1 parent 857eebe commit 31108c1
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 145 deletions.
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
51 changes: 19 additions & 32 deletions core/include/cubos/core/gl/render_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,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;
};
Expand Down Expand Up @@ -1071,15 +1076,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.
Expand All @@ -1095,13 +1091,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;
Expand All @@ -1113,23 +1106,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;
Expand Down
90 changes: 56 additions & 34 deletions core/src/gl/ogl_render_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,13 @@ class OGLPixelPackBuffer : public impl::PixelPackBuffer
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<GLsizeiptr>(size), data);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
}

std::shared_ptr<bool> destroyed;

GLuint id;
Expand Down Expand Up @@ -586,6 +593,11 @@ class OGLTexture2D : public impl::Texture2D
{
if (!*destroyed)
{
if (this->framebuffer != 0)
{
glDeleteFramebuffers(1, &this->framebuffer);
}

glDeleteTextures(1, &this->id);
}
}
Expand All @@ -600,17 +612,29 @@ class OGLTexture2D : public impl::Texture2D

void read(void* outputBuffer) override
{
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, this->id);
glReadPixels(0, 0, this->width, this->height, this->format, this->type, outputBuffer);
}

void copyTo(PixelPackBuffer buffer) override
{
if (this->framebuffer == 0)
{
glGenFramebuffers(1, &this->framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, this->framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->id, 0);
}
else
{
glBindFramebuffer(GL_FRAMEBUFFER, this->framebuffer);
}

glViewport(0, 0, this->width, this->height);
glBindBuffer(GL_PIXEL_PACK_BUFFER, std::static_pointer_cast<OGLPixelPackBuffer>(buffer)->id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->id);
glReadPixels(0, 0, this->width, this->height, this->format, this->type, nullptr);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void generateMipmaps() override
Expand All @@ -627,6 +651,7 @@ class OGLTexture2D : public impl::Texture2D
GLenum type;
GLsizei width;
GLsizei height;
GLuint framebuffer = 0;
};

class OGLTexture2DArray : public impl::Texture2DArray
Expand Down Expand Up @@ -778,17 +803,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
{
glBindBuffer(GL_UNIFORM_BUFFER, this->id);
Expand Down Expand Up @@ -821,16 +835,10 @@ class OGLIndexBuffer : public impl::IndexBuffer
}
}

void* map() override
void fill(const void* data, std::size_t size) override
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id);
return glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, this->size,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
}

void unmap() override
{
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(size), data);
}

std::shared_ptr<bool> destroyed;
Expand Down Expand Up @@ -859,23 +867,31 @@ class OGLVertexBuffer : public impl::VertexBuffer
}
}

void* map() override
void fill(const void* data, std::size_t size) override
{
glBindBuffer(GL_ARRAY_BUFFER, this->id);
return glMapBufferRange(GL_ARRAY_BUFFER, 0, this->size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(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;
glBindBuffer(GL_ARRAY_BUFFER, this->id);
return glMapBufferRange(GL_ARRAY_BUFFER, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(length), flags);
}
#ifdef __EMSCRIPTEN__
synchronized = true;
#endif

void unmap() override
{
glUnmapBuffer(GL_ARRAY_BUFFER);
if (synchronized)
{
glBindBuffer(GL_ARRAY_BUFFER, this->id);
glBufferSubData(GL_ARRAY_BUFFER, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(size), data);
}
else
{
glBindBuffer(GL_ARRAY_BUFFER, this->id);
auto* ptr = glMapBufferRange(GL_ARRAY_BUFFER, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(size),
GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
memcpy(ptr, data, size);
glUnmapBuffer(GL_ARRAY_BUFFER);
}
}

std::shared_ptr<bool> destroyed;
Expand Down Expand Up @@ -1264,6 +1280,11 @@ OGLRenderDevice::OGLRenderDevice()
glEnable(GL_DEBUG_OUTPUT);
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
Expand Down Expand Up @@ -2193,7 +2214,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
Expand Down
8 changes: 4 additions & 4 deletions engine/assets/render/bloom.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ in vec2 fragUv;

out vec4 fragColor;

vec3 sample(vec2 uv)
vec3 sampleTex(vec2 uv)
{
return texture(inputTexture, uv).rgb;
}

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:
Expand Down
16 changes: 8 additions & 8 deletions engine/assets/render/deferred_shading.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions engine/assets/render/g_buffer_rasterizer.vs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions engine/assets/render/ssao_base.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}
4 changes: 2 additions & 2 deletions engine/assets/ui/element.vs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading

0 comments on commit 31108c1

Please sign in to comment.