Skip to content

Commit

Permalink
Updated to match the book text
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Dec 11, 2024
1 parent 9228379 commit d8b7630
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 132 deletions.
2 changes: 1 addition & 1 deletion Chapter08/03_LargeScene/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ int main()
const ImGuiViewport* v = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(10, 200));
ImGui::SetNextWindowSize(ImVec2(v->WorkSize.x / 6, v->WorkSize.y - 210));
ImGui::Begin("Scene graph", nullptr, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize);
ImGui::Begin("Scene graph", nullptr, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoResize);
ImGui::Checkbox("Draw wireframe", &drawWireframe);
ImGui::Checkbox("Draw bounding boxes", &drawBoundingBoxes);
ImGui::Separator();
Expand Down
16 changes: 11 additions & 5 deletions Chapter08/SceneUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#include "shared/UtilsGLTF.h"
#include "Chapter08/VKMesh08.h"

// these macros can be redefined externally
#if !defined(DEMO_TEXTURE_MAX_SIZE) && !defined(DEMO_TEXTURE_CACHE_FOLDER)
#define DEMO_TEXTURE_MAX_SIZE 512
#define DEMO_TEXTURE_CACHE_FOLDER ".cache/out_textures/"
#endif

// find a file in directory which "almost" coincides with the origFile (their lowercase versions coincide)
std::string findSubstitute(const std::string& origFile)
{
Expand Down Expand Up @@ -46,15 +52,15 @@ std::string convertTexture(
const std::string& file, const std::string& basePath, std::unordered_map<std::string, uint32_t>& opacityMapIndices,
const std::vector<std::string>& opacityMaps)
{
const int maxNewWidth = 512;
const int maxNewHeight = 512;
const int maxNewWidth = DEMO_TEXTURE_MAX_SIZE;
const int maxNewHeight = DEMO_TEXTURE_MAX_SIZE;

if (!std::filesystem::exists(".cache/out_textures/")) {
std::filesystem::create_directories(".cache/out_textures/");
if (!std::filesystem::exists(DEMO_TEXTURE_CACHE_FOLDER)) {
std::filesystem::create_directories(DEMO_TEXTURE_CACHE_FOLDER);
}

const std::string srcFile = replaceAll(basePath + file, "\\", "/");
const std::string newFile = std::string(".cache/out_textures/") +
const std::string newFile = std::string(DEMO_TEXTURE_CACHE_FOLDER) +
lowercaseString(replaceAll(replaceAll(srcFile, "..", "__"), "/", "__") + std::string("__rescaled")) +
std::string(".ktx");

Expand Down
2 changes: 1 addition & 1 deletion Chapter10/03_MSAA/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int main()
const ImGuiViewport* v = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(10, 200));
ImGui::Begin(
"MSAA", nullptr, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize);
"MSAA", nullptr, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Checkbox("Enable MSAA", &enableMSAA);
ImGui::Checkbox("Draw wireframe", &drawWireframe);
ImGui::Checkbox("Draw bounding boxes", &drawBoundingBoxes);
Expand Down
2 changes: 1 addition & 1 deletion Chapter10/04_SSAO/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ int main()
const float windowWidth = v->WorkSize.x / 5;
ImGui::SetNextWindowPos(ImVec2(10, 200));
ImGui::Begin(
"SSAO", nullptr, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize);
"SSAO", nullptr, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Checkbox("Draw wireframe", &drawWireframe);
ImGui::Checkbox("Enable blur", &enableBlur);
ImGui::BeginDisabled(!enableBlur);
Expand Down
64 changes: 3 additions & 61 deletions Chapter10/05_HDR/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,11 @@
#include "shared/VulkanApp.h"
#include "shared/Tonemap.h"

#include "Chapter10/Bistro.h"
#include "Chapter10/Skybox.h"

#include <implot/implot.h>

// Uchimura 2017, "HDR theory and practice"
// Math: https://www.desmos.com/calculator/gslcdxvipg
// Source: https://www.slideshare.net/nikuque/hdr-theory-and-practicce-jp
float uchimura(float x, float P, float a, float m, float l, float c, float b)
{
float l0 = ((P - m) * l) / a;
float L0 = m - m / a;
float L1 = m + (1.0f - m) / a;
float S0 = m + l0;
float S1 = m + a * l0;
float C2 = (a * P) / (P - S1);
float CP = -C2 / P;

float w0 = float(1.0f - glm::smoothstep(0.0f, m, x));
float w2 = float(glm::step(m + l0, x));
float w1 = float(1.0f - w0 - w2);

float T = float(m * pow(x / m, float(c)) + b);
float S = float(P - (P - S1) * exp(CP * (x - S0)));
float L = float(m + a * (x - m));

return T * w0 + L * w1 + S * w2;
}

float reinhard2(float v, float maxWhite)
{
return v * (1.0f + (v / (maxWhite * maxWhite))) / (1.0f + v);
}

// Khronos PBR Neutral Tone Mapper
// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/README.md#pbr-neutral-specification
// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/pbrNeutral.glsl
float PBRNeutralToneMapping(float color, float startCompression, float desaturation)
{
startCompression -= 0.04f;

float x = color;
float offset = x < 0.08f ? x - 6.25f * x * x : 0.04f;
color -= offset;

float peak = color;
if (peak < startCompression)
return color;

const float d = 1. - startCompression;
float newPeak = 1. - d * d / (peak + d - startCompression);
color *= newPeak / peak;

float g = 1.0f - 1.0f / (desaturation * (peak - newPeak) + 1.0f);
return glm::mix(color, newPeak, g);
}

int main()
{
MeshData meshData;
Expand Down Expand Up @@ -184,16 +133,9 @@ int main()
bool drawWireframe = false;
bool drawCurves = true;
bool enableBloom = true;
float bloomStrength = 0.1f;
float bloomStrength = 0.05f;
int numBloomPasses = 2;

enum ToneMappingMode {
ToneMapping_None = 0,
ToneMapping_Reinhard = 1,
ToneMapping_Uchimura = 2,
ToneMapping_KhronosPBR = 3,
};

ImPlotContext* implotCtx = ImPlot::CreateContext();

struct {
Expand Down Expand Up @@ -341,7 +283,7 @@ int main()
const float windowWidth = v->WorkSize.x / 5;
ImGui::SetNextWindowPos(ImVec2(10, 200));
ImGui::SetNextWindowSize(ImVec2(0, v->WorkSize.y - 210));
ImGui::Begin("HDR", nullptr, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse);
ImGui::Begin("HDR", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
ImGui::Checkbox("Draw wireframe", &drawWireframe);
ImGui::Checkbox("Draw tone mapping curves", &drawCurves);
ImGui::Separator();
Expand Down
62 changes: 2 additions & 60 deletions Chapter10/06_HDR_Adaptation/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,11 @@
#include "shared/VulkanApp.h"
#include "shared/Tonemap.h"

#include "Chapter10/Bistro.h"
#include "Chapter10/Skybox.h"

#include <implot/implot.h>

// Uchimura 2017, "HDR theory and practice"
// Math: https://www.desmos.com/calculator/gslcdxvipg
// Source: https://www.slideshare.net/nikuque/hdr-theory-and-practicce-jp
float uchimura(float x, float P, float a, float m, float l, float c, float b)
{
float l0 = ((P - m) * l) / a;
float L0 = m - m / a;
float L1 = m + (1.0f - m) / a;
float S0 = m + l0;
float S1 = m + a * l0;
float C2 = (a * P) / (P - S1);
float CP = -C2 / P;

float w0 = float(1.0f - glm::smoothstep(0.0f, m, x));
float w2 = float(glm::step(m + l0, x));
float w1 = float(1.0f - w0 - w2);

float T = float(m * pow(x / m, float(c)) + b);
float S = float(P - (P - S1) * exp(CP * (x - S0)));
float L = float(m + a * (x - m));

return T * w0 + L * w1 + S * w2;
}

float reinhard2(float v, float maxWhite)
{
return v * (1.0f + (v / (maxWhite * maxWhite))) / (1.0f + v);
}

// Khronos PBR Neutral Tone Mapper
// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/README.md#pbr-neutral-specification
// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/pbrNeutral.glsl
float PBRNeutralToneMapping(float color, float startCompression, float desaturation)
{
startCompression -= 0.04f;

float x = color;
float offset = x < 0.08f ? x - 6.25f * x * x : 0.04f;
color -= offset;

float peak = color;
if (peak < startCompression)
return color;

const float d = 1. - startCompression;
float newPeak = 1. - d * d / (peak + d - startCompression);
color *= newPeak / peak;

float g = 1.0f - 1.0f / (desaturation * (peak - newPeak) + 1.0f);
return glm::mix(color, newPeak, g);
}

int main()
{
MeshData meshData;
Expand Down Expand Up @@ -207,13 +156,6 @@ int main()
int numBloomPasses = 2;
float adaptationSpeed = 1.0f;

enum ToneMappingMode {
ToneMapping_None = 0,
ToneMapping_Reinhard = 1,
ToneMapping_Uchimura = 2,
ToneMapping_KhronosPBR = 3,
};

ImPlotContext* implotCtx = ImPlot::CreateContext();

struct {
Expand Down Expand Up @@ -385,7 +327,7 @@ int main()
const float windowWidth = v->WorkSize.x / 5;
ImGui::SetNextWindowPos(ImVec2(10, 200));
ImGui::SetNextWindowSize(ImVec2(0, v->WorkSize.y - 210));
ImGui::Begin("HDR", nullptr, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse);
ImGui::Begin("HDR", nullptr, ImGuiWindowFlags_NoFocusOnAppearing);
ImGui::Checkbox("Draw wireframe", &drawWireframe);
ImGui::Checkbox("Draw tone mapping curves", &drawCurves);
ImGui::Separator();
Expand Down
9 changes: 6 additions & 3 deletions Chapter10/Bistro.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

#include "Chapter08/SceneUtils.h"

const char* fileNameCachedMeshes = ".cache/ch08_bistro.meshes";
const char* fileNameCachedMaterials = ".cache/ch08_bistro.materials";
const char* fileNameCachedHierarchy = ".cache/ch08_bistro.scene";
#if !defined(fileNameCachedMeshes) || !defined(fileNameCachedMaterials) || !defined(fileNameCachedHierarchy)
// by default, share the precached Bistro with Chapter08/03_LargeScene
#define fileNameCachedMeshes ".cache/ch08_bistro.meshes"
#define fileNameCachedMaterials ".cache/ch08_bistro.materials"
#define fileNameCachedHierarchy ".cache/ch08_bistro.scene"
#endif

void loadBistro(MeshData& meshData, Scene& scene) {
if (!isMeshDataValid(fileNameCachedMeshes) || !isMeshHierarchyValid(fileNameCachedHierarchy) ||
Expand Down
63 changes: 63 additions & 0 deletions shared/Tonemap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <glm/ext.hpp>
#include <glm/glm.hpp>

enum ToneMappingMode {
ToneMapping_None = 0,
ToneMapping_Reinhard = 1,
ToneMapping_Uchimura = 2,
ToneMapping_KhronosPBR = 3,
};

// Uchimura 2017, "HDR theory and practice"
// Math: https://www.desmos.com/calculator/gslcdxvipg
// Source: https://www.slideshare.net/nikuque/hdr-theory-and-practicce-jp
float uchimura(float x, float P, float a, float m, float l, float c, float b)
{
float l0 = ((P - m) * l) / a;
float L0 = m - m / a;
float L1 = m + (1.0f - m) / a;
float S0 = m + l0;
float S1 = m + a * l0;
float C2 = (a * P) / (P - S1);
float CP = -C2 / P;

float w0 = float(1.0f - glm::smoothstep(0.0f, m, x));
float w2 = float(glm::step(m + l0, x));
float w1 = float(1.0f - w0 - w2);

float T = float(m * pow(x / m, float(c)) + b);
float S = float(P - (P - S1) * exp(CP * (x - S0)));
float L = float(m + a * (x - m));

return T * w0 + L * w1 + S * w2;
}

float reinhard2(float v, float maxWhite)
{
return v * (1.0f + (v / (maxWhite * maxWhite))) / (1.0f + v);
}

// Khronos PBR Neutral Tone Mapper
// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/README.md#pbr-neutral-specification
// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/pbrNeutral.glsl
float PBRNeutralToneMapping(float color, float startCompression, float desaturation)
{
startCompression -= 0.04f;

float x = color;
float offset = x < 0.08f ? x - 6.25f * x * x : 0.04f;
color -= offset;

float peak = color;
if (peak < startCompression)
return color;

const float d = 1. - startCompression;
float newPeak = 1. - d * d / (peak + d - startCompression);
color *= newPeak / peak;

float g = 1.0f - 1.0f / (desaturation * (peak - newPeak) + 1.0f);
return glm::mix(color, newPeak, g);
}
1 change: 1 addition & 0 deletions shared/VulkanApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <functional>

using glm::mat3;
using glm::mat4;
using glm::vec2;
using glm::vec3;
Expand Down

0 comments on commit d8b7630

Please sign in to comment.