Skip to content

Commit 78c06db

Browse files
committed
Use sRGB textures and backbuffer
1 parent 5a8d80f commit 78c06db

File tree

8 files changed

+30
-52
lines changed

8 files changed

+30
-52
lines changed

src/Cluster.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void Cluster::initialize(int _argc, char* _argv[])
8686
if(config->profile)
8787
bgfx::setDebug(BGFX_DEBUG_PROFILER);
8888

89-
uint32_t resetFlags = BGFX_RESET_MAXANISOTROPY;
89+
uint32_t resetFlags = BGFX_RESET_MAXANISOTROPY | BGFX_RESET_SRGB_BACKBUFFER;
9090
if(config->vsync)
9191
resetFlags |= BGFX_RESET_VSYNC;
9292
reset(resetFlags);

src/Renderer/Shaders/fs_deferred_pointlight.sc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,4 @@ void main()
5252
}
5353

5454
gl_FragColor = vec4(radianceOut, 1.0);
55-
56-
//gl_FragColor = vec4(0.3, 0.0, 0.0, 1.0);
5755
}

src/Renderer/Shaders/fs_tonemap.sc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,5 @@ void main()
9191
result.rgb = tonemap_aces_luminance(result.rgb);
9292
}
9393

94-
// gamma correction
95-
result.rgb = LinearTosRGB(result.rgb);
96-
9794
gl_FragColor = result;
9895
}

src/Renderer/Shaders/pbr.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define PBR_SH_HEADER_GUARD
33

44
#include "samplers.sh"
5-
#include "tonemapping.sh"
65

76
// only define this if you need to retrieve the material parameters
87
// without it you can still use the struct definition or BRDF functions
@@ -55,9 +54,7 @@ vec4 pbrBaseColor(vec2 texcoord)
5554
{
5655
if(u_hasBaseColorTexture)
5756
{
58-
vec4 color = texture2D(s_texBaseColor, texcoord);
59-
// GLTF base color texture is stored as sRGB
60-
return vec4(sRGBToLinear(color.rgb), color.a) * u_baseColorFactor;
57+
return texture2D(s_texBaseColor, texcoord) * u_baseColorFactor;
6158
}
6259
else
6360
{
@@ -111,7 +108,7 @@ vec3 pbrEmissive(vec2 texcoord)
111108
{
112109
if(u_hasEmissiveTexture)
113110
{
114-
return sRGBToLinear(texture2D(s_texEmissive, texcoord).rgb) * u_emissiveFactor;
111+
return texture2D(s_texEmissive, texcoord).rgb * u_emissiveFactor;
115112
}
116113
else
117114
{

src/Renderer/Shaders/tonemapping.sh

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,6 @@
11
#ifndef TONEMAPPING_SH_HEADER_GUARD
22
#define TONEMAPPING_SH_HEADER_GUARD
33

4-
// Taken from
5-
// https://gamedev.stackexchange.com/a/148088/45850
6-
7-
// sRGB gamma encoding
8-
vec3 LinearTosRGB(vec3 linearRGB)
9-
{
10-
#ifdef SRGB_CONVERSION_FAST
11-
return pow(linearRGB, vec3_splat(1.0/2.2));
12-
#else
13-
vec3 cutoff = step(linearRGB, vec3_splat(0.0031308));
14-
vec3 higher = 1.055 * pow(linearRGB, vec3_splat(1.0/2.4)) - 0.055;
15-
vec3 lower = linearRGB * 12.92;
16-
return mix(higher, lower, cutoff);
17-
#endif
18-
}
19-
20-
// sRGB gamma decoding
21-
vec3 sRGBToLinear(vec3 sRGB)
22-
{
23-
#ifdef SRGB_CONVERSION_FAST
24-
return pow(sRGB, vec3_splat(2.2));
25-
#else
26-
vec3 cutoff = step(sRGB, vec3_splat(0.04045));
27-
vec3 higher = pow((sRGB + 0.055) / 1.055, vec3_splat(2.4));
28-
vec3 lower = sRGB / 12.92;
29-
return mix(higher, lower, cutoff);
30-
#endif
31-
}
32-
334
// relative luminance of linear RGB(!)
345
// BT.709 primaries
356
float luminance(vec3 RGB)

src/Scene/Scene.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ bool Scene::load(const char* file)
141141
}
142142
}
143143

144+
center = minBounds + (maxBounds - minBounds) / 2.0f;
145+
glm::vec3 extent = glm::abs(maxBounds - minBounds);
146+
diagonal = glm::sqrt(glm::dot(extent, extent));
147+
144148
char dir[bx::kMaxFilePath] = "";
145149
bx::strCopy(dir, BX_COUNTOF(dir), bx::FilePath(file).getPath());
146150
for(unsigned int i = 0; i < scene->mNumMaterials; i++)
@@ -214,9 +218,6 @@ Mesh Scene::loadMesh(const aiMesh* mesh)
214218

215219
minBounds = glm::min(minBounds, { pos.x, pos.y, pos.z });
216220
maxBounds = glm::max(maxBounds, { pos.x, pos.y, pos.z });
217-
center = minBounds + (maxBounds - minBounds) / 2.0f;
218-
glm::vec3 extent = glm::abs(maxBounds - minBounds);
219-
diagonal = glm::sqrt(glm::dot(extent, extent));
220221

221222
aiVector3D nrm = mesh->mNormals[i];
222223
vertex.nx = nrm.x;
@@ -286,7 +287,7 @@ Material Scene::loadMaterial(const aiMaterial* material, const char* dir)
286287
aiString pathBaseColor;
287288
pathBaseColor.Set(dir);
288289
pathBaseColor.Append(fileBaseColor.C_Str());
289-
out.baseColorTexture = loadTexture(pathBaseColor.C_Str());
290+
out.baseColorTexture = loadTexture(pathBaseColor.C_Str(), true /* sRGB */);
290291
}
291292

292293
aiColor4D baseColorFactor;
@@ -352,7 +353,7 @@ Material Scene::loadMaterial(const aiMaterial* material, const char* dir)
352353
aiString pathEmissive;
353354
pathEmissive.Set(dir);
354355
pathEmissive.Append(fileEmissive.C_Str());
355-
out.emissiveTexture = loadTexture(pathEmissive.C_Str());
356+
out.emissiveTexture = loadTexture(pathEmissive.C_Str(), true /* sRGB */);
356357
}
357358

358359
// assimp doesn't define this
@@ -385,7 +386,7 @@ Camera Scene::loadCamera(const aiCamera* camera)
385386
return cam;
386387
}
387388

388-
bgfx::TextureHandle Scene::loadTexture(const char* file)
389+
bgfx::TextureHandle Scene::loadTexture(const char* file, bool sRGB)
389390
{
390391
void* data = nullptr;
391392
uint32_t size = 0;
@@ -417,16 +418,19 @@ bgfx::TextureHandle Scene::loadTexture(const char* file)
417418
image);
418419
BX_FREE(&allocator, data);
419420

420-
if(bgfx::isTextureValid(
421-
0, false, image->m_numLayers, (bgfx::TextureFormat::Enum)image->m_format, BGFX_TEXTURE_NONE))
421+
uint64_t textureFlags = BGFX_TEXTURE_NONE | BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC;
422+
if(sRGB)
423+
textureFlags |= BGFX_TEXTURE_SRGB;
424+
425+
if(bgfx::isTextureValid(0, false, image->m_numLayers, (bgfx::TextureFormat::Enum)image->m_format, textureFlags))
422426
{
423427
bgfx::TextureHandle tex =
424428
bgfx::createTexture2D((uint16_t)image->m_width,
425429
(uint16_t)image->m_height,
426430
image->m_numMips > 1,
427431
image->m_numLayers,
428432
(bgfx::TextureFormat::Enum)image->m_format,
429-
BGFX_TEXTURE_NONE | BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC,
433+
textureFlags,
430434
mem);
431435
//bgfx::setName(tex, file); // causes debug errors with DirectX SetPrivateProperty duplicate
432436
return tex;

src/Scene/Scene.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ class Scene
4747
static Material loadMaterial(const aiMaterial* material, const char* dir);
4848
static Camera loadCamera(const aiCamera* camera);
4949

50-
static bgfx::TextureHandle loadTexture(const char* file);
50+
static bgfx::TextureHandle loadTexture(const char* file, bool sRGB = false);
5151
};

src/UI.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
#include "Renderer/Renderer.h"
77
#include "Log/UISink.h"
88
#include "Log/Log.h"
9+
#include <glm/gtc/type_ptr.hpp>
10+
#include <glm/gtc/color_space.hpp>
911
#include <bx/string.h>
10-
#include <IconsForkAwesome.h>
12+
#include <IconsForkAwesome_c.h>
1113
#include <functional>
1214
#include <cctype>
1315

@@ -29,11 +31,20 @@ void ClusterUI::initialize()
2931

3032
ImGuiIO& io = ImGui::GetIO();
3133
//io.IniFilename = nullptr; // don't save window positions etc. to ini
32-
io.MouseDrawCursor = true; // let imgui draw cursors
34+
//io.MouseDrawCursor = true; // let imgui draw cursors
3335

3436
ImGuiStyle& style = ImGui::GetStyle();
3537
ImGui::StyleColorsDark(&style);
3638

39+
// convert imgui style colors to linear RGB
40+
// since we have an sRGB backbuffer
41+
for(size_t i = 0; i < ImGuiCol_COUNT; i++)
42+
{
43+
glm::vec3 sRGB = glm::make_vec3(&style.Colors[i].x);
44+
glm::vec3 linear = glm::convertSRGBToLinear(sRGB);
45+
style.Colors[i] = ImVec4(linear.x, linear.y, linear.z, style.Colors[i].w);
46+
}
47+
3748
// no round corners
3849
style.WindowRounding = 0.0f;
3950
style.ChildRounding = 0.0f;

0 commit comments

Comments
 (0)