Skip to content

Commit

Permalink
Implemented sRGB gamma demo
Browse files Browse the repository at this point in the history
  • Loading branch information
vcoda committed Aug 28, 2020
1 parent cb2df15 commit 994e9d7
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 1 deletion.
15 changes: 15 additions & 0 deletions aggregated-graphics-samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "edge-detection", "edge-dete
{67B01ECD-2613-4FA0-84F7-87F5BCF40EB1} = {67B01ECD-2613-4FA0-84F7-87F5BCF40EB1}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "srgb", "srgb\srgb.vcxproj", "{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}"
ProjectSection(ProjectDependencies) = postProject
{8D9D4A3E-439A-4210-8879-259B20D992CA} = {8D9D4A3E-439A-4210-8879-259B20D992CA}
{51CC36B3-921E-4853-ACD5-CB6CBC27FBA1} = {51CC36B3-921E-4853-ACD5-CB6CBC27FBA1}
{67B01ECD-2613-4FA0-84F7-87F5BCF40EB1} = {67B01ECD-2613-4FA0-84F7-87F5BCF40EB1}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -286,6 +293,14 @@ Global
{591F9775-8088-457A-B237-A7EA1DEF699D}.Release|x64.Build.0 = Release|x64
{591F9775-8088-457A-B237-A7EA1DEF699D}.Release|x86.ActiveCfg = Release|Win32
{591F9775-8088-457A-B237-A7EA1DEF699D}.Release|x86.Build.0 = Release|Win32
{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}.Debug|x64.ActiveCfg = Debug|x64
{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}.Debug|x64.Build.0 = Debug|x64
{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}.Debug|x86.ActiveCfg = Debug|Win32
{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}.Debug|x86.Build.0 = Debug|Win32
{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}.Release|x64.ActiveCfg = Release|x64
{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}.Release|x64.Build.0 = Release|x64
{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}.Release|x86.ActiveCfg = Release|Win32
{E8FAC55E-ACDF-497D-B8F9-A85599BBEEA6}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 4 additions & 1 deletion framework/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ struct alignas(16) ViewProjTransforms

struct alignas(16) LightSource
{
rapid::vector viewPosition;
union {
rapid::vector viewPosition;
rapid::vector viewDirection;
};
LinearColor ambient;
LinearColor diffuse;
LinearColor specular;
Expand Down
Binary file added screenshots/sRGB.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions srgb/shaders/phong.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
#include "common/transforms.h"
#include "common/sRGB.h"
#include "brdf/phong.h"

layout(binding = 2) uniform Light {
vec3 viewDir;
} light;

layout(constant_id = 0) const bool c_sRGB = false;

layout(location = 0) in vec3 viewPos;
layout(location = 1) in vec3 viewNormal;

layout(location = 0) out vec3 oColor;

vec3 gamma(vec3 color)
{
return c_sRGB ? linear(color) : color;
}

void main()
{
const float ambient = 0.2;
const vec3 alice_blue = vec3(240, 248, 255)/255.;
const vec3 white = vec3(1.);
const float shininess = 128.;

vec3 n = normalize(viewNormal);
vec3 l = normalize(light.viewDir);
vec3 v = normalize(-viewPos);

oColor = phong(n, l, v,
gamma(alice_blue * ambient), gamma(white * ambient),
gamma(alice_blue), gamma(white),
gamma(alice_blue), gamma(white),
shininess, 1.);

if (c_sRGB)
oColor = sRGB(oColor);
}
20 changes: 20 additions & 0 deletions srgb/shaders/transform.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
#include "common/transforms.h"

layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texCoord;

layout(location = 0) out vec3 oViewPos;
layout(location = 1) out vec3 oViewNormal;
out gl_PerVertex {
vec4 gl_Position;
};

void main()
{
oViewPos = (worldView * position).xyz;
oViewNormal = mat3(normalMatrix) * normal;
gl_Position = worldViewProj * position;
}
148 changes: 148 additions & 0 deletions srgb/srgb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include "graphicsApp.h"
#include "colorTable.h"
#include "quadric/include/sphere.h"

class GammaCorrection : public GraphicsApp
{
std::unique_ptr<quadric::Sphere> sphere;
std::shared_ptr<magma::GraphicsPipeline> rgbPipeline;
std::shared_ptr<magma::GraphicsPipeline> srgbPipeline;
DescriptorSet descriptor;

public:
explicit GammaCorrection(const AppEntry& entry):
GraphicsApp(entry, TEXT("Gamma correction"), 1280, 720, false)
{
createTransformBuffer(2);
setupViewProjection();
createMesh();
setupDescriptorSets();
setupGraphicsPipelines();

renderScene(drawCmdBuffer);
blit(msaaFramebuffer->getColorView(), FrontBuffer);
blit(msaaFramebuffer->getColorView(), BackBuffer);
}

virtual void render(uint32_t bufferIndex) override
{
updateTransforms();
submitCommandBuffers(bufferIndex);
sleep(2); // Cap fps
}

virtual void updateLightSource() override
{
magma::helpers::mapScoped(lightSource,
[this](auto *light)
{
const rapid::matrix normalMatrix = viewProj->calculateNormal(rapid::identity());
const rapid::vector3 lightViewDir = normalMatrix * lightViewProj->getPosition();
light->viewDirection = lightViewDir.normalized();
});
}

void updateTransforms()
{
std::vector<rapid::matrix, core::aligned_allocator<rapid::matrix>> transforms = {
rapid::translation(-1.5f, 0.f),
rapid::translation(1.5f, 0.f),
};
updateObjectTransforms(transforms);
}

void setupViewProjection()
{
viewProj = std::make_unique<LeftHandedViewProjection>();
viewProj->setPosition(0.f, 0.f, -100.f);
viewProj->setFocus(0.f, 0.f, 0.f);
viewProj->setFieldOfView(2.f);
viewProj->setNearZ(90.f);
viewProj->setFarZ(110.f);
viewProj->setAspectRatio(width/(float)height);
viewProj->updateView();
viewProj->updateProjection();

lightViewProj = std::make_unique<LeftHandedViewProjection>();
lightViewProj->setPosition(1.f, 1.f, -0.4f);
lightViewProj->setFocus(0.f, 0.f, 0.f);
lightViewProj->setFieldOfView(70.f);
lightViewProj->setNearZ(5.f);
lightViewProj->setFarZ(30.f);
lightViewProj->setAspectRatio(1.f);
lightViewProj->updateView();
lightViewProj->updateProjection();

updateViewProjTransforms();
}

void createMesh()
{
sphere = std::make_unique<quadric::Sphere>(1.f, 64, 64, false, cmdCopyBuf);
}

void setupDescriptorSets()
{
using namespace magma::bindings;
using namespace magma::descriptors;
descriptor.layout = std::shared_ptr<magma::DescriptorSetLayout>(new magma::DescriptorSetLayout(device,
{
VertexStageBinding(0, DynamicUniformBuffer(1)),
FragmentStageBinding(1, UniformBuffer(1)),
FragmentStageBinding(2, UniformBuffer(1))
}));
descriptor.set = descriptorPool->allocateDescriptorSet(descriptor.layout);
descriptor.set->update(0, transforms);
descriptor.set->update(1, viewProjTransforms);
descriptor.set->update(2, lightSource);
}

void setupGraphicsPipelines()
{
struct Constants
{
VkBool32 sRGB;
} constants;
constants.sRGB = false;
auto rgb(std::make_shared<magma::Specialization>(constants,
magma::SpecializationEntry(0, &Constants::sRGB)));
rgbPipeline = createCommonSpecializedPipeline(
"transform.o", "phong.o", std::move(rgb),
sphere->getVertexInput(),
descriptor.layout);
constants.sRGB = true;
auto sRGB(std::make_shared<magma::Specialization>(constants,
magma::SpecializationEntry(0, &Constants::sRGB)));
srgbPipeline = createCommonSpecializedPipeline(
"transform.o", "phong.o", std::move(sRGB),
sphere->getVertexInput(),
descriptor.layout);
}

void renderScene(std::shared_ptr<magma::CommandBuffer> cmdBuffer)
{
cmdBuffer->begin();
{
cmdBuffer->beginRenderPass(msaaFramebuffer->getRenderPass(), msaaFramebuffer->getFramebuffer(),
{
magma::ClearColor(0.35f, 0.53f, 0.7f, 1.f),
magma::clears::depthOne
});
{
cmdBuffer->bindPipeline(rgbPipeline);
cmdBuffer->bindDescriptorSet(rgbPipeline, descriptor.set, transforms->getDynamicOffset(0));
sphere->draw(cmdBuffer);
cmdBuffer->bindPipeline(srgbPipeline);
cmdBuffer->bindDescriptorSet(srgbPipeline, descriptor.set, transforms->getDynamicOffset(1));
sphere->draw(cmdBuffer);
}
cmdBuffer->endRenderPass();
}
cmdBuffer->end();
}
};

std::unique_ptr<IApplication> appFactory(const AppEntry& entry)
{
return std::unique_ptr<GammaCorrection>(new GammaCorrection(entry));
}
Loading

0 comments on commit 994e9d7

Please sign in to comment.