Skip to content

Commit

Permalink
Gamma correct rendering + sRGBDemo app
Browse files Browse the repository at this point in the history
- Add sRGB helper class to allow user to input colors in non-linear sRGB (which will be what most people are used to).  Conversion to linear sRGB is done automatic
- Shaders assume linear color inputs
- Textures can be marked as either sRGB (for things that are colors) or not (for things that are "maps", like normal, bump, etc).
   Marking color textures as sRGB encoded means that samplers will decode "automatically" to linear for use in shaders
- Output framebuffers (both the backbuffer and intermediate framebuffers) are marked as sRGB, so that writes from the shaders (linear) will automatcally sRGB encode the outputs
   (i.e. no need to manually gamma correct as the final step of the shader)

- Add "012 - sRGB" demo to show gamma correct alpha blending

- Move "ImGuiEx" into Pikzel "ImGui" library.  Saves duplicating it in clients.
- Add a method to initialise ImGui to a "default" Pikzel ImGui style

Note: ImGui does not play nice with sRGB framebuffers. This is a known issue (eg. ocornut/imgui#578   and also issues 1724, and 2468).
  • Loading branch information
0xworks committed Nov 23, 2020
1 parent ccd05d1 commit 45937c0
Show file tree
Hide file tree
Showing 52 changed files with 1,681 additions and 782 deletions.
10 changes: 6 additions & 4 deletions 001 - Triangle/src/Triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
#include "Pikzel/Core/Utility.h"
#include "Pikzel/Input/Input.h"
#include "Pikzel/Renderer/RenderCore.h"
#include "Pikzel/Renderer/sRGB.h"

#include <glm/gtc/matrix_transform.hpp>

#include <filesystem>


class Triangle final : public Pikzel::Application {
public:
Triangle(int argc, const char* argv[])
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = {0.2f, 0.3f, 0.3f, 1.0f}}}
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = Pikzel::sRGB{0.5f, 0.5f, 0.5f}}}
, m_Input {GetWindow()}
{
CreateVertexBuffer();
Expand Down Expand Up @@ -43,9 +45,9 @@ class Triangle final : public Pikzel::Application {

void CreateVertexBuffer() {
Vertex vertices[] = {
{.Pos{-0.5f, -0.5f, 0.0f}, .Color{1.0f, 0.0f, 0.0f}},
{.Pos{ 0.5f, -0.5f, 0.0f}, .Color{0.0f, 1.0f, 0.0f}},
{.Pos{ 0.0f, 0.5f, 0.0f}, .Color{0.0f, 0.0f, 1.0f}}
{.Pos{-0.5f, -0.5f, 0.0f}, .Color{Pikzel::sRGB{1.0f, 0.0f, 0.0f}}},
{.Pos{ 0.5f, -0.5f, 0.0f}, .Color{Pikzel::sRGB{0.0f, 1.0f, 0.0f}}},
{.Pos{ 0.0f, 0.5f, 0.0f}, .Color{Pikzel::sRGB{0.0f, 0.0f, 1.0f}}}
};

m_VertexBuffer = Pikzel::RenderCore::CreateVertexBuffer(sizeof(vertices), vertices);
Expand Down
22 changes: 6 additions & 16 deletions 002 - Textured/src/Textured.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Pikzel/Core/Utility.h"
#include "Pikzel/Input/Input.h"
#include "Pikzel/Renderer/RenderCore.h"
#include "Pikzel/Renderer/sRGB.h"

#include <glm/gtc/matrix_transform.hpp>

Expand All @@ -11,11 +12,10 @@
class Textured final : public Pikzel::Application {
public:
Textured(int argc, const char* argv[])
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = {0.2f, 0.3f, 0.3f, 1.0f}}}
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = Pikzel::sRGB{0.2f, 0.3f, 0.3f}}}
, m_Input {GetWindow()}
{
CreateVertexBuffer();
CreateIndexBuffer();
CreateTextures();
CreatePipeline();
}
Expand All @@ -35,7 +35,7 @@ class Textured final : public Pikzel::Application {

//Pikzel::GCBinder bindUniformBuffer {gc, *m_UBO, "ubo"_hs}; // Ditto uniform buffer objects
gc.PushConstant("constants.mvp"_hs, glm::identity<glm::mat4>());
gc.DrawIndexed(*m_VertexBuffer, *m_IndexBuffer);
gc.DrawTriangles(*m_VertexBuffer, 3);
}


Expand All @@ -49,9 +49,9 @@ class Textured final : public Pikzel::Application {

void CreateVertexBuffer() {
Vertex vertices[] = {
{.Pos{-0.5f, -0.5f, 0.0f}, .Color{1.0f, 0.0f, 0.0f}, .TexCoord{0.0f, 0.0f}},
{.Pos{ 0.5f, -0.5f, 0.0f}, .Color{0.0f, 1.0f, 0.0f}, .TexCoord{1.0f, 0.0f}},
{.Pos{ 0.0f, 0.5f, 0.0f}, .Color{0.0f, 0.0f, 1.0f}, .TexCoord{0.5f, 1.0f}}
{.Pos{-0.5f, -0.5f, 0.0f}, .Color{Pikzel::sRGB{1.0f, 0.0f, 0.0f}}, .TexCoord{0.0f, 0.0f}},
{.Pos{ 0.5f, -0.5f, 0.0f}, .Color{Pikzel::sRGB{0.0f, 1.0f, 0.0f}}, .TexCoord{1.0f, 0.0f}},
{.Pos{ 0.0f, 0.5f, 0.0f}, .Color{Pikzel::sRGB{0.0f, 0.0f, 1.0f}}, .TexCoord{0.5f, 1.0f}}
};

m_VertexBuffer = Pikzel::RenderCore::CreateVertexBuffer(sizeof(vertices), vertices);
Expand All @@ -63,15 +63,6 @@ class Textured final : public Pikzel::Application {
}


void CreateIndexBuffer() {
uint32_t indices[] = {
0, 1, 2
};

m_IndexBuffer = Pikzel::RenderCore::CreateIndexBuffer(sizeof(indices) / sizeof(uint32_t), indices);
}


void CreateTextures() {
m_Texture = Pikzel::RenderCore::CreateTexture2D("Assets/" APP_NAME "/Textures/Container.jpg");
}
Expand All @@ -93,7 +84,6 @@ class Textured final : public Pikzel::Application {
Pikzel::Input m_Input;
glm::mat4 m_Transform = glm::identity<glm::mat4>();
std::shared_ptr<Pikzel::VertexBuffer> m_VertexBuffer;
std::shared_ptr<Pikzel::IndexBuffer> m_IndexBuffer;
std::unique_ptr<Pikzel::Texture2D> m_Texture;
std::unique_ptr<Pikzel::Pipeline> m_Pipeline;

Expand Down
27 changes: 3 additions & 24 deletions 003 - Cube/src/Cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Pikzel/Core/Utility.h"
#include "Pikzel/Input/Input.h"
#include "Pikzel/Renderer/RenderCore.h"
#include "Pikzel/Renderer/sRGB.h"

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
Expand All @@ -12,11 +13,10 @@
class Cube final : public Pikzel::Application {
public:
Cube(int argc, const char* argv[])
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = {0.2f, 0.3f, 0.3f, 1.0f}}}
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = Pikzel::sRGB{0.2f, 0.3f, 0.3f}}}
, m_Input {GetWindow()}
{
CreateVertexBuffer();
CreateIndexBuffer();
CreateTextures();
CreatePipeline();

Expand Down Expand Up @@ -65,7 +65,7 @@ class Cube final : public Pikzel::Application {
model = glm::rotate(model, glm::radians(20.0f * i), {1.0f, 0.3f, 0.5f});

gc.PushConstant("constants.mvp"_hs, projView * model);
gc.DrawIndexed(*m_VertexBuffer, *m_IndexBuffer);
gc.DrawTriangles(*m_VertexBuffer, 36);
}
}

Expand Down Expand Up @@ -130,26 +130,6 @@ class Cube final : public Pikzel::Application {
}


void CreateIndexBuffer() {
uint32_t indices[] = {
0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17,
18,19,20,
21,22,23,
24,25,26,
27,28,29,
30,31,32,
33,34,35
};

m_IndexBuffer = Pikzel::RenderCore::CreateIndexBuffer(sizeof(indices) / sizeof(uint32_t), indices);
}


void CreateTextures() {
m_Texture = Pikzel::RenderCore::CreateTexture2D("Assets/" APP_NAME "/Textures/Container.jpg");
}
Expand Down Expand Up @@ -193,7 +173,6 @@ class Cube final : public Pikzel::Application {
};

std::shared_ptr<Pikzel::VertexBuffer> m_VertexBuffer;
std::shared_ptr<Pikzel::IndexBuffer> m_IndexBuffer;
std::unique_ptr<Pikzel::Texture2D> m_Texture;
std::unique_ptr<Pikzel::Pipeline> m_Pipeline;

Expand Down
35 changes: 7 additions & 28 deletions 004 - Lighting/src/Lighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Pikzel/Core/Utility.h"
#include "Pikzel/Input/Input.h"
#include "Pikzel/Renderer/RenderCore.h"
#include "Pikzel/Renderer/sRGB.h"

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
Expand All @@ -12,11 +13,10 @@
class Lighting final : public Pikzel::Application {
public:
Lighting(int argc, const char* argv[])
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = {0.1f, 0.1f, 0.1f, 1.0f}}}
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = Pikzel::sRGB{0.1f, 0.1f, 0.1f}}}
, m_Input {GetWindow()}
{
CreateVertexBuffer();
CreateIndexBuffer();
CreatePipelines();

m_Projection = glm::perspective(glm::radians(45.0f), static_cast<float>(GetWindow().GetWidth()) / static_cast<float>(GetWindow().GetHeight()), 0.1f, 100.0f);
Expand Down Expand Up @@ -54,9 +54,9 @@ class Lighting final : public Pikzel::Application {


virtual void Render() override {
constexpr glm::vec3 lightColor = {1.0f, 1.0f, 1.0f};
constexpr glm::vec3 lightPos = {1.2f, 1.0f, 2.0f};
constexpr glm::vec3 objectColor = {1.0f, 0.5f, 0.31f};
static glm::vec3 lightColor = Pikzel::sRGB{1.0f, 1.0f, 1.0f};
static glm::vec3 lightPos = {1.2f, 1.0f, 2.0f};
static glm::vec3 objectColor = Pikzel::sRGB{1.0f, 0.5f, 0.31f};

glm::mat4 projView = m_Projection * glm::lookAt(m_CameraPos, m_CameraPos + m_CameraDirection, m_CameraUp);

Expand All @@ -66,7 +66,7 @@ class Lighting final : public Pikzel::Application {
glm::mat4 model = glm::scale(glm::translate(glm::identity<glm::mat4>(), glm::vec3 {lightPos}), {0.2f, 0.2f, 0.2f});
gc.PushConstant("constants.mvp"_hs, projView * model);
gc.PushConstant("constants.lightColor"_hs, lightColor);
gc.DrawIndexed(*m_VertexBuffer, *m_IndexBuffer);
gc.DrawTriangles(*m_VertexBuffer, 36);
}
{
Pikzel::GCBinder bindPipeline {gc, *m_PipelineLighting};
Expand All @@ -79,7 +79,7 @@ class Lighting final : public Pikzel::Application {
gc.PushConstant("constants.lightPos"_hs, lightPos);
gc.PushConstant("constants.objectColor"_hs, objectColor);
gc.PushConstant("constants.viewPos"_hs, m_CameraPos);
gc.DrawIndexed(*m_VertexBuffer, *m_IndexBuffer);
gc.DrawTriangles(*m_VertexBuffer, 36);
}
}

Expand Down Expand Up @@ -144,26 +144,6 @@ class Lighting final : public Pikzel::Application {
}


void CreateIndexBuffer() {
uint32_t indices[] = {
0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17,
18,19,20,
21,22,23,
24,25,26,
27,28,29,
30,31,32,
33,34,35
};

m_IndexBuffer = Pikzel::RenderCore::CreateIndexBuffer(sizeof(indices) / sizeof(uint32_t), indices);
}


void CreatePipelines() {
m_PipelineLight = GetWindow().GetGraphicsContext().CreatePipeline({
m_VertexBuffer->GetLayout(),
Expand Down Expand Up @@ -195,7 +175,6 @@ class Lighting final : public Pikzel::Application {
glm::mat4 m_Projection = glm::identity<glm::mat4>();

std::shared_ptr<Pikzel::VertexBuffer> m_VertexBuffer;
std::shared_ptr<Pikzel::IndexBuffer> m_IndexBuffer;
std::unique_ptr<Pikzel::Pipeline> m_PipelineLight;
std::unique_ptr<Pikzel::Pipeline> m_PipelineLighting;

Expand Down
38 changes: 8 additions & 30 deletions 005 - Materials/src/Materials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Pikzel/Core/Utility.h"
#include "Pikzel/Input/Input.h"
#include "Pikzel/Renderer/RenderCore.h"
#include "Pikzel/Renderer/sRGB.h"

#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
Expand All @@ -12,11 +13,10 @@
class Materials final : public Pikzel::Application {
public:
Materials(int argc, const char* argv[])
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = {0.1f, 0.1f, 0.1f, 1.0f}}}
: Pikzel::Application {argc, argv, {.Title = APP_DESCRIPTION, .ClearColor = Pikzel::sRGB{0.1f, 0.1f, 0.1f}}}
, m_Input {GetWindow()}
{
CreateVertexBuffer();
CreateIndexBuffer();
CreateUniformBuffers();
CreatePipelines();

Expand Down Expand Up @@ -66,7 +66,7 @@ class Materials final : public Pikzel::Application {
glm::mat4 model = glm::scale(glm::translate(glm::identity<glm::mat4>(), glm::vec3 {m_LightPos}), {0.2f, 0.2f, 0.2f});
gc.PushConstant("constants.mvp"_hs, projView * model);
gc.PushConstant("constants.lightColor"_hs, m_LightColor);
gc.DrawIndexed(*m_VertexBuffer, *m_IndexBuffer);
gc.DrawTriangles(*m_VertexBuffer, 36);
}
{
Pikzel::GCBinder bindPipeline {gc, *m_PipelineLighting};
Expand All @@ -79,8 +79,7 @@ class Materials final : public Pikzel::Application {
gc.PushConstant("constants.model"_hs, model);
gc.PushConstant("constants.modelInvTrans"_hs, modelInvTrans);
gc.PushConstant("constants.viewPos"_hs, m_CameraPos);

gc.DrawIndexed(*m_VertexBuffer, *m_IndexBuffer);
gc.DrawTriangles(*m_VertexBuffer, 36);
}
}

Expand Down Expand Up @@ -145,26 +144,6 @@ class Materials final : public Pikzel::Application {
}


void CreateIndexBuffer() {
uint32_t indices[] = {
0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17,
18,19,20,
21,22,23,
24,25,26,
27,28,29,
30,31,32,
33,34,35
};

m_IndexBuffer = Pikzel::RenderCore::CreateIndexBuffer(sizeof(indices) / sizeof(uint32_t), indices);
}


struct Material {
alignas(16) glm::vec3 Ambient;
alignas(16) glm::vec3 Diffuse;
Expand All @@ -180,17 +159,17 @@ class Materials final : public Pikzel::Application {
alignas(16) glm::vec3 Specular;
};

constexpr static glm::vec3 m_LightPos = {1.2f, 1.0f, 2.0f};
constexpr static glm::vec3 m_LightColor = {1.0f, 1.0f, 1.0f};
inline static glm::vec3 m_LightPos = {1.2f, 1.0f, 2.0f};
inline static glm::vec3 m_LightColor = Pikzel::sRGB{1.0f, 1.0f, 1.0f};

void CreateUniformBuffers() {
Material materials[] = {
{.Ambient{1.0f, 0.5f, 0.31f}, .Diffuse{1.0f, 0.5f, 0.31f}, .Specular{0.5f, 0.5f, 0.5f}, .Shininess{32.0f}}
{.Ambient{Pikzel::sRGB{1.0f, 0.5f, 0.31f}}, .Diffuse{Pikzel::sRGB{1.0f, 0.5f, 0.31f}}, .Specular{0.5f, 0.5f, 0.5f}, .Shininess{32.0f}}
};
m_MaterialBuffer = Pikzel::RenderCore::CreateUniformBuffer(sizeof(materials), materials);

Light lights[] = {
{.Position{m_LightPos}, .Ambient{0.1f, 0.1f, 0.1f}, .Diffuse{m_LightColor}, .Specular{m_LightColor}}
{.Position{m_LightPos}, .Ambient{Pikzel::sRGB{0.1f, 0.1f, 0.1f}}, .Diffuse{m_LightColor}, .Specular{m_LightColor}}
};
m_LightBuffer = Pikzel::RenderCore::CreateUniformBuffer(sizeof(lights), lights);
}
Expand Down Expand Up @@ -227,7 +206,6 @@ class Materials final : public Pikzel::Application {
glm::mat4 m_Projection = glm::identity<glm::mat4>();

std::unique_ptr<Pikzel::VertexBuffer> m_VertexBuffer;
std::unique_ptr<Pikzel::IndexBuffer> m_IndexBuffer;
std::unique_ptr<Pikzel::UniformBuffer> m_MaterialBuffer;
std::unique_ptr<Pikzel::UniformBuffer> m_LightBuffer;
std::unique_ptr<Pikzel::Pipeline> m_PipelineLight;
Expand Down
Loading

0 comments on commit 45937c0

Please sign in to comment.