Skip to content

Commit

Permalink
IBL half way to go!
Browse files Browse the repository at this point in the history
  • Loading branch information
Catddly committed Apr 26, 2021
1 parent 38ccd35 commit 8904b46
Show file tree
Hide file tree
Showing 15 changed files with 618 additions and 247 deletions.
44 changes: 22 additions & 22 deletions Resources/Meshes/sphere.gltf

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Resources/Shaders/LightProxy/lightGeo.vert
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ layout(set = 1, binding = 1) uniform LightUbo
} lightUbo;

layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
layout(location = 1) in vec2 TexCoord;
layout(location = 2) in vec3 Normal;

layout(location = 0) out vec4 outLightColor;

Expand Down
36 changes: 36 additions & 0 deletions Resources/Shaders/irradianceCube.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#version 450 core

#define PI 3.1415926535897932384626433832795

layout (set = 0, binding = 0) uniform samplerCube skyboxCubeMap;

layout (location = 0) in vec3 inTexCoord;
layout (location = 1) in float inDeltaPhi;
layout (location = 2) in float inDeltaTheta;

layout(location = 0) out vec4 outColor;

void main()
{
vec3 N = normalize(inTexCoord);
vec3 up = vec3(0.0, 0.0, 1.0);
vec3 right = normalize(cross(up, N));
up = cross(N, right);

const float TWO_PI = PI * 2.0;
const float HALF_PI = PI * 0.5;

vec3 color = vec3(0.0);
uint sampleCount = 0u;
// sample map (semisphere integral)
for (float phi = 0.0; phi < TWO_PI; phi += inDeltaPhi) {
for (float theta = 0.0; theta < HALF_PI; theta += inDeltaTheta) {
vec3 tempVec = cos(phi) * right + sin(phi) * up;
vec3 sampleVector = cos(theta) * N + sin(theta) * tempVec;

color += texture(skyboxCubeMap, sampleVector).rgb * cos(theta) * sin(theta);
sampleCount++;
}
}
outColor = vec4(PI * color / float(sampleCount), 1.0);
}
27 changes: 27 additions & 0 deletions Resources/Shaders/irradianceCube.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 450 core

layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 TexCoord;
layout(location = 2) in vec3 Normal;

layout(std140, push_constant) uniform PushConsts
{
layout (offset = 0) mat4 mvp;
layout (offset = 64) float deltaPhi;
layout (offset = 68) float deltaTheta;
} pushConsts;

layout (location = 0) out vec3 outTexCoord;
layout (location = 1) out float outDeltaPhi;
layout (location = 2) out float outDeltaTheta;

void main()
{
outTexCoord = Position;
outDeltaPhi = pushConsts.deltaPhi;
outDeltaTheta = pushConsts.deltaTheta;

vec3 pos = Position;
pos.y = pos.y * -1;
gl_Position = pushConsts.mvp * vec4(pos, 1.0);
}
46 changes: 45 additions & 1 deletion Resources/Shaders/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ layout(set = 1, binding = 3) uniform MaterialData
float smoothness;
} mat;

layout(std140, push_constant) uniform PushConsts
{
layout (offset = 0) mat4 model;
} pushConsts;

layout (set = 0, binding = 0) uniform samplerCube samplerIrradiance;

layout(location = 0) in vec2 inTexCoord;
layout(location = 1) in vec3 inNormalW;
layout(location = 2) in vec3 inPosW;
Expand All @@ -46,6 +53,11 @@ vec3 F_Schlick(float cosTheta, vec3 F0)
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); // ?? should be dotVH?
}

vec3 F_SchlickR(float cosTheta, vec3 F0, float roughness)
{
return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(1.0 - cosTheta, 5.0);
}

float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness)
{
float r = (roughness + 1.0);
Expand Down Expand Up @@ -87,6 +99,18 @@ vec3 specularBRDF(vec3 L, vec3 V, vec3 N, vec3 F0, float metallic, float roughne
return color;
}

// From http://filmicgames.com/archives/75
vec3 Uncharted2Tonemap(vec3 x)
{
float A = 0.15;
float B = 0.50;
float C = 0.10;
float D = 0.20;
float E = 0.02;
float F = 0.30;
return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
}

void main()
{
vec3 N = normalize(inNormalW);
Expand Down Expand Up @@ -133,9 +157,29 @@ void main()
// light 2 end
}

// IBL
//vec2 brdf = texture(samplerBRDFLUT, vec2(max(dot(N, V), 0.0), roughness)).rg;
//vec3 reflection = prefilteredReflection(R, roughness).rgb;
vec3 samplerN = mat3(pushConsts.model) * N;
vec3 irradiance = texture(samplerIrradiance, samplerN).rgb;
vec3 diffuse = irradiance * mat.color;

vec3 F = F_SchlickR(max(dot(N, V), 0.0), F0, roughness);

// ambient part
vec3 kd = 1.0 - F;
kd *= 1.0 - metallic;
vec3 ambient = (kd * diffuse); // should add specular here

// fixed ambient
vec3 ambient = vec3(0.05, 0.05, 0.05);
//vec3 ambient = vec3(0.05, 0.05, 0.05);
vec3 color = Lo + ambient;

// tone mapping
color = Uncharted2Tonemap(color * 4.5);
color = color * (1.0f / Uncharted2Tonemap(vec3(11.2f)));
// gamma correction
color = pow(color, vec3(1.0f / 2.2));

outColor = vec4(color, 1.0);
}
13 changes: 0 additions & 13 deletions Resources/Shaders/sky.frag

This file was deleted.

20 changes: 20 additions & 0 deletions Resources/Shaders/skybox.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,31 @@
layout (SG_UPDATE_FREQ_NONE, binding = 0) uniform samplerCube skyboxCubeMap; // combind image sampler

layout(location = 0) in vec3 inTexCoord;
layout(location = 1) in vec3 inNormal;

layout(location = 0) out vec4 outColor;

// From http://filmicgames.com/archives/75
vec3 Uncharted2Tonemap(vec3 x)
{
float A = 0.15;
float B = 0.50;
float C = 0.10;
float D = 0.20;
float E = 0.02;
float F = 0.30;
return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
}

void main()
{
vec3 color = texture(skyboxCubeMap, inTexCoord).rgb;

// tone mapping
color = Uncharted2Tonemap(color * 4.5);
color = color * (1.0f / Uncharted2Tonemap(vec3(11.2f)));
// gamma correction
color = pow(color, vec3(1.0f / 2.2));

outColor = vec4(color, 1.0);
}
12 changes: 8 additions & 4 deletions Resources/Shaders/skybox.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ layout(set = 1, binding = 0) uniform CameraUbo
vec3 positionW;
} camera;

layout(std140, push_constant) uniform PushConsts
{
layout (offset = 0) mat4 model;
} pushConsts;

layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 TexCoord;
layout(location = 2) in vec3 Normal;

layout(location = 0) out vec3 outTexCoord;
layout(location = 1) out vec3 outNormal;

void main()
{
mat3 v = mat3(camera.view);
mat4 view = mat4(v);
vec3 pos = Position.xzy; // we use right-hand z-up coordinate
//pos.z = pos.z * -1;
gl_Position = camera.proj * view * vec4(pos, 1.0);
gl_Position = camera.proj * view * pushConsts.model * vec4(Position, 1.0);
outTexCoord = Position;
//outNormalW = Normal;
outNormal = Normal;
}
Binary file added Resources/Textures/pisa_cube.ktx
Binary file not shown.
13 changes: 13 additions & 0 deletions Seagull-Core/Renderer/IRenderer/Include/IRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ namespace SG
SG_RESOURCE_BUFFER_ALIGNMENT = 4U,
} DefaultResourceAlignment;

typedef struct CopyImageDesc
{
uint32_t arrayLayer;
uint32_t layerCount;
uint32_t mipLevel;
uint32_t offset;
uint32_t width;
uint32_t height;
uint32_t depth;
} CopyImageDesc;

#pragma region (Blending)

static const int BLEND_RED = 0x1;
Expand Down Expand Up @@ -2279,6 +2290,8 @@ namespace SG
SG_RENDER_API void SG_CALLCONV cmd_draw_indexed_instanced(Cmd* pCmd, uint32_t indexCount, uint32_t firstIndex, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance);
SG_RENDER_API void SG_CALLCONV cmd_dispatch(Cmd* pCmd, uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ);

SG_RENDER_API void SG_CALLCONV cmd_image_blit(Cmd* cmd, Texture* pSrcTex, Texture* pDstTex, const CopyImageDesc* pSubresourceDesc);

// barrier transition command
SG_RENDER_API void SG_CALLCONV cmd_resource_barrier(Cmd* pCmd, uint32_t bufferBarrierCount, BufferBarrier* pBufferBarriers, uint32_t textureBarrierCount, TextureBarrier* pTextureBarriers, uint32_t rtBarrierCount, RenderTargetBarrier* pRtBarriers);

Expand Down
32 changes: 32 additions & 0 deletions Seagull-Core/Renderer/Vulkan/Source/RendererVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4420,6 +4420,38 @@ void cmd_update_subresource(Cmd* pCmd, Texture* pTexture, Buffer* pSrcBuffer, co
}
}

// copy image to image
void cmd_image_blit(Cmd* cmd, Texture* pSrcTex, Texture* pDstTex, const CopyImageDesc* pSubresourceDesc)
{
// Copy region for transfer from framebuffer to cube face
VkImageCopy copyRegion = {};

copyRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copyRegion.srcSubresource.baseArrayLayer = 0;
copyRegion.srcSubresource.mipLevel = 0;
copyRegion.srcSubresource.layerCount = 1;
copyRegion.srcOffset = { 0, 0, 0 };

copyRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copyRegion.dstSubresource.baseArrayLayer = pSubresourceDesc->arrayLayer;
copyRegion.dstSubresource.mipLevel = pSubresourceDesc->mipLevel;
copyRegion.dstSubresource.layerCount = pSubresourceDesc->layerCount;
copyRegion.dstOffset = { 0, 0, 0 };

copyRegion.extent.width = pSubresourceDesc->width;
copyRegion.extent.height = pSubresourceDesc->height;
copyRegion.extent.depth = pSubresourceDesc->depth;

vkCmdCopyImage(
cmd->pVkCmdBuf,
pSrcTex->pVkImage,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
pDstTex->pVkImage,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
&copyRegion);
}

void cmd_bind_descriptor_set(Cmd* pCmd, uint32_t index, DescriptorSet* pDescriptorSet)
{
ASSERT(pCmd);
Expand Down
1 change: 0 additions & 1 deletion Seagull-Core/Renderer/Vulkan/Source/ResourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ namespace SG
extern void cmd_update_buffer(Cmd* pCmd, Buffer* pBuffer, uint64_t dstOffset, Buffer* pSrcBuffer, uint64_t srcOffset, uint64_t size);
// copy read-in buffer to image
extern void cmd_update_subresource(Cmd* pCmd, Texture* pTexture, Buffer* pSrcBuffer, const SubresourceDataDesc* pSubresourceDesc);

}

// Xbox, Orbis, Prospero, iOS have unified memory
Expand Down
Loading

0 comments on commit 8904b46

Please sign in to comment.