Adding prefiltered specular reflections
This commit is contained in:
@@ -99,9 +99,6 @@ if(UNIX)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable(Editor "")
|
add_executable(Editor "")
|
||||||
set_target_properties(Editor PROPERTIES
|
|
||||||
INSTALL_RPATH "$ORIGIN"
|
|
||||||
)
|
|
||||||
target_link_libraries(Editor PRIVATE Engine)
|
target_link_libraries(Editor PRIVATE Engine)
|
||||||
target_include_directories(Editor PRIVATE src/Editor)
|
target_include_directories(Editor PRIVATE src/Editor)
|
||||||
target_include_directories(Editor PRIVATE ${Stb_INCLUDE_DIR})
|
target_include_directories(Editor PRIVATE ${Stb_INCLUDE_DIR})
|
||||||
|
|||||||
@@ -193,3 +193,67 @@ float4 computePrefilteredCubemap(float3 localPos : LOCALPOS) : SV_Target
|
|||||||
|
|
||||||
return float4(prefilteredColor, 1.0);
|
return float4(prefilteredColor, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: this is basically duplicate of Cook-Torrance BRDF
|
||||||
|
float GeometrySchlickGGX(float NdotV, float roughness)
|
||||||
|
{
|
||||||
|
float a = roughness;
|
||||||
|
float k = (a * a) / 2.0;
|
||||||
|
|
||||||
|
float nom = NdotV;
|
||||||
|
float denom = NdotV * (1.0 - k) + k;
|
||||||
|
|
||||||
|
return nom / denom;
|
||||||
|
}
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
|
||||||
|
{
|
||||||
|
float NdotV = max(dot(N, V), 0.0);
|
||||||
|
float NdotL = max(dot(N, L), 0.0);
|
||||||
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
||||||
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
||||||
|
|
||||||
|
return ggx1 * ggx2;
|
||||||
|
}
|
||||||
|
|
||||||
|
float2 integrateBRDF(float nDotV, float roughness) {
|
||||||
|
float3 V;
|
||||||
|
V.x = sqrt(1.0 - nDotV * nDotV);
|
||||||
|
V.y = 0.0f;
|
||||||
|
V.z = nDotV;
|
||||||
|
float A = 0.0;
|
||||||
|
float B = 0.0;
|
||||||
|
|
||||||
|
float3 N = float3(0.0, 0.0, 1.0);
|
||||||
|
|
||||||
|
const uint SAMPLE_COUNT = 1024u;
|
||||||
|
for (uint i = 0u; i < SAMPLE_COUNT; ++i)
|
||||||
|
{
|
||||||
|
float2 Xi = Hammersley(i, SAMPLE_COUNT);
|
||||||
|
float3 H = ImportanceSampleGGX(Xi, N, roughness);
|
||||||
|
float3 L = normalize(2.0 * dot(V, H) * H - V);
|
||||||
|
|
||||||
|
float nDotL = max(L.z, 0.0);
|
||||||
|
float nDotH = max(H.z, 0.0);
|
||||||
|
float vDotH = max(dot(V, H), 0.0);
|
||||||
|
|
||||||
|
if (nDotL > 0.0)
|
||||||
|
{
|
||||||
|
float G = GeometrySmith(N, V, L, roughness);
|
||||||
|
float G_Vis = (G * vDotH) / (nDotH * nDotV);
|
||||||
|
float Fc = pow(1.0 - vDotH, 5.0);
|
||||||
|
|
||||||
|
A += (1.0 - Fc) * G_Vis;
|
||||||
|
B += Fc * G_Vis;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
A /= float(SAMPLE_COUNT);
|
||||||
|
B /= float(SAMPLE_COUNT);
|
||||||
|
return float2(A, B);
|
||||||
|
}
|
||||||
|
|
||||||
|
[shader("pixel")]
|
||||||
|
float2 precomputeBRDF(float2 texCoords) : SV_Target
|
||||||
|
{
|
||||||
|
return integrateBRDF(texCoords.x, texCoords.y);
|
||||||
|
}
|
||||||
@@ -70,8 +70,8 @@ struct LightEnv
|
|||||||
TextureCube irradianceMap;
|
TextureCube irradianceMap;
|
||||||
SamplerState irradianceSampler;
|
SamplerState irradianceSampler;
|
||||||
TextureCube prefilteredMap;
|
TextureCube prefilteredMap;
|
||||||
SamplerState prefilteredSampler;
|
|
||||||
Texture2D brdfLUT;
|
Texture2D brdfLUT;
|
||||||
|
SamplerState lutSampler;
|
||||||
};
|
};
|
||||||
layout(set=3)
|
layout(set=3)
|
||||||
ParameterBlock<LightEnv> pLightEnv;
|
ParameterBlock<LightEnv> pLightEnv;
|
||||||
@@ -314,15 +314,15 @@ struct CookTorrance : IBRDF
|
|||||||
float G = Smith(n, viewDir_WS, lightDir_WS);
|
float G = Smith(n, viewDir_WS, lightDir_WS);
|
||||||
float3 F = FresnelSchlickRoughness(max(dot(h, viewDir_WS), 0.0), F0, roughness);
|
float3 F = FresnelSchlickRoughness(max(dot(h, viewDir_WS), 0.0), F0, roughness);
|
||||||
|
|
||||||
float3 num = NDF * G * F;
|
|
||||||
float denom = 4.0 * max(dot(n, viewDir_WS), 0.0) * max(dot(n, lightDir_WS), 0.0) + 0.000001;
|
|
||||||
float3 specular = num / denom;
|
|
||||||
|
|
||||||
float3 k_s = F;
|
float3 k_s = F;
|
||||||
float3 k_d = float3(1.0) - k_s;
|
float3 k_d = float3(1.0) - k_s;
|
||||||
|
|
||||||
k_d *= 1.0 - metallic;
|
k_d *= 1.0 - metallic;
|
||||||
|
|
||||||
|
float3 num = NDF * G * F;
|
||||||
|
float denom = 4.0 * max(dot(n, viewDir_WS), 0.0) * max(dot(n, lightDir_WS), 0.0) + 0.000001;
|
||||||
|
float3 specular = num / denom;
|
||||||
|
|
||||||
float nDotL = max(dot(n, lightDir_WS), 0.0);
|
float nDotL = max(dot(n, lightDir_WS), 0.0);
|
||||||
|
|
||||||
float3 result = (k_d * baseColor / PI + specular) * nDotL * lightColor;
|
float3 result = (k_d * baseColor / PI + specular) * nDotL * lightColor;
|
||||||
@@ -340,12 +340,24 @@ struct CookTorrance : IBRDF
|
|||||||
{
|
{
|
||||||
float3 F0 = float3(0.04);
|
float3 F0 = float3(0.04);
|
||||||
F0 = lerp(F0, baseColor, metallic);
|
F0 = lerp(F0, baseColor, metallic);
|
||||||
float3 k_s = FresnelSchlickRoughness(max(dot(normal, viewDir_WS), 0.0), F0, roughness);
|
float3 F = FresnelSchlickRoughness(max(dot(normal, viewDir_WS), 0.0), F0, roughness);
|
||||||
|
|
||||||
|
float3 k_s = F;
|
||||||
float3 k_d = 1 - k_s;
|
float3 k_d = 1 - k_s;
|
||||||
k_d *= 1 - metallic;
|
k_d *= 1 - metallic;
|
||||||
|
|
||||||
float3 irradiance = pLightEnv.irradianceMap.SampleLevel(pLightEnv.irradianceSampler, normal, 4).rgb;
|
float3 irradiance = pLightEnv.irradianceMap.SampleLevel(pLightEnv.irradianceSampler, normal, 4).rgb;
|
||||||
float3 diffuse = irradiance * baseColor;
|
float3 diffuse = irradiance * baseColor;
|
||||||
return (k_d * diffuse) * ambientOcclusion;
|
|
||||||
|
float3 r = reflect(-viewDir_WS, normal);
|
||||||
|
|
||||||
|
const float MAX_REFLECTION_LOD = 4;
|
||||||
|
float3 prefilteredColor = pLightEnv.prefilteredMap.SampleLevel(pLightEnv.irradianceSampler, r, roughness * MAX_REFLECTION_LOD).xyz;
|
||||||
|
|
||||||
|
float2 envBRDF = pLightEnv.brdfLUT.Sample(pLightEnv.lutSampler, float2(max(dot(normal, viewDir_WS), 0), roughness)).rg;
|
||||||
|
float3 specular = prefilteredColor * (F * envBRDF.x + envBRDF.y);
|
||||||
|
|
||||||
|
return (k_d * diffuse + specular) * ambientOcclusion;
|
||||||
}
|
}
|
||||||
float getAlpha()
|
float getAlpha()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
#include "Asset/AssetRegistry.h"
|
#include "Asset/AssetRegistry.h"
|
||||||
#include "Asset/EnvironmentMapAsset.h"
|
#include "Asset/EnvironmentMapAsset.h"
|
||||||
#include "Graphics/Descriptor.h"
|
#include "Graphics/Descriptor.h"
|
||||||
|
#include "Graphics/Enums.h"
|
||||||
|
#include "Graphics/Graphics.h"
|
||||||
|
#include "Graphics/Initializer.h"
|
||||||
|
#include "Graphics/RenderTarget.h"
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
|
||||||
using namespace Seele;
|
using namespace Seele;
|
||||||
@@ -80,11 +84,16 @@ EnvironmentLoader::EnvironmentLoader(Gfx::PGraphics graphics) : graphics(graphic
|
|||||||
|
|
||||||
graphics->beginShaderCompilation(ShaderCompilationInfo{
|
graphics->beginShaderCompilation(ShaderCompilationInfo{
|
||||||
.name = "CubeRenderPipeline",
|
.name = "CubeRenderPipeline",
|
||||||
.modules = {"EnvironmentMapping"},
|
.modules = {"FullScreenQuad", "EnvironmentMapping"},
|
||||||
.entryPoints = {{"vertMain", "EnvironmentMapping"},
|
.entryPoints =
|
||||||
{"computeCubemap", "EnvironmentMapping"},
|
{
|
||||||
{"convolveCubemap", "EnvironmentMapping"},
|
{"vertMain", "EnvironmentMapping"},
|
||||||
{"computePrefilteredCubemap", "EnvironmentMapping"}},
|
{"computeCubemap", "EnvironmentMapping"},
|
||||||
|
{"convolveCubemap", "EnvironmentMapping"},
|
||||||
|
{"computePrefilteredCubemap", "EnvironmentMapping"},
|
||||||
|
{"quadMain", "FullScreenQuad"},
|
||||||
|
{"precomputeBRDF", "EnvironmentMapping"},
|
||||||
|
},
|
||||||
.rootSignature = cubePipelineLayout,
|
.rootSignature = cubePipelineLayout,
|
||||||
});
|
});
|
||||||
cubePipelineLayout->create();
|
cubePipelineLayout->create();
|
||||||
@@ -93,6 +102,65 @@ EnvironmentLoader::EnvironmentLoader(Gfx::PGraphics graphics) : graphics(graphic
|
|||||||
cubeRenderFrag = graphics->createFragmentShader({1});
|
cubeRenderFrag = graphics->createFragmentShader({1});
|
||||||
convolutionFrag = graphics->createFragmentShader({2});
|
convolutionFrag = graphics->createFragmentShader({2});
|
||||||
prefilterFrag = graphics->createFragmentShader({3});
|
prefilterFrag = graphics->createFragmentShader({3});
|
||||||
|
lutVert = graphics->createVertexShader({4});
|
||||||
|
lutFrag = graphics->createFragmentShader({5});
|
||||||
|
Gfx::OPipelineLayout emptyLayout = graphics->createPipelineLayout("LUTlayout");
|
||||||
|
{
|
||||||
|
graphics->beginDebugRegion("PrecomputeBRDF");
|
||||||
|
lutTexture = graphics->createTexture2D(TextureCreateInfo{
|
||||||
|
.format = Gfx::SE_FORMAT_R16G16_SFLOAT,
|
||||||
|
.width = 512,
|
||||||
|
.height = 512,
|
||||||
|
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
||||||
|
});
|
||||||
|
lutSampler = graphics->createSampler(SamplerCreateInfo{
|
||||||
|
.magFilter = Gfx::SE_FILTER_LINEAR,
|
||||||
|
.minFilter = Gfx::SE_FILTER_LINEAR,
|
||||||
|
.addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
||||||
|
.addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
||||||
|
});
|
||||||
|
lutViewport = graphics->createViewport(nullptr, ViewportCreateInfo{
|
||||||
|
.dimensions =
|
||||||
|
{
|
||||||
|
.size = {512, 512},
|
||||||
|
.offset = {0, 0},
|
||||||
|
},
|
||||||
|
.fieldOfView = glm::radians(90.0f),
|
||||||
|
});
|
||||||
|
Gfx::ORenderPass lutPass = graphics->createRenderPass(
|
||||||
|
Gfx::RenderTargetLayout{
|
||||||
|
.colorAttachments = {Gfx::RenderTargetAttachment(lutTexture->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED,
|
||||||
|
Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||||
|
Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE)},
|
||||||
|
},
|
||||||
|
{Gfx::SubPassDependency{
|
||||||
|
.srcSubpass = 0,
|
||||||
|
.dstSubpass = ~0U,
|
||||||
|
.srcStage = Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
||||||
|
.dstStage = Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
||||||
|
.srcAccess = Gfx::SE_ACCESS_NONE,
|
||||||
|
.dstAccess = Gfx::SE_ACCESS_SHADER_READ_BIT,
|
||||||
|
}},
|
||||||
|
URect{{512, 512}, {0, 0}}, "LUTGeneration");
|
||||||
|
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
|
||||||
|
.vertexShader = lutVert,
|
||||||
|
.fragmentShader = lutFrag,
|
||||||
|
.renderPass = lutPass,
|
||||||
|
.pipelineLayout = emptyLayout,
|
||||||
|
.colorBlend =
|
||||||
|
{
|
||||||
|
.attachmentCount = 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
graphics->beginRenderPass(lutPass);
|
||||||
|
Gfx::ORenderCommand cmd = graphics->createRenderCommand("LUT");
|
||||||
|
cmd->setViewport(lutViewport);
|
||||||
|
cmd->bindPipeline(pipeline);
|
||||||
|
cmd->draw(3, 1, 0, 0);
|
||||||
|
graphics->executeCommands(std::move(cmd));
|
||||||
|
graphics->endRenderPass();
|
||||||
|
graphics->endDebugRegion();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EnvironmentLoader::~EnvironmentLoader() {}
|
EnvironmentLoader::~EnvironmentLoader() {}
|
||||||
@@ -146,56 +214,60 @@ void EnvironmentLoader::import(EnvironmentImportArgs args, PEnvironmentMapAsset
|
|||||||
.height = SOURCE_RESOLUTION,
|
.height = SOURCE_RESOLUTION,
|
||||||
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
||||||
});
|
});
|
||||||
Array<Gfx::OTextureView> cubeViews;
|
{
|
||||||
cubeViews.add(cubeMap->createTextureView(0, 1, 0, 1));
|
graphics->beginDebugRegion("CubemapGeneration");
|
||||||
cubeViews.add(cubeMap->createTextureView(0, 1, 1, 1));
|
Array<Gfx::OTextureView> cubeViews;
|
||||||
cubeViews.add(cubeMap->createTextureView(0, 1, 2, 1));
|
cubeViews.add(cubeMap->createTextureView(0, 1, 0, 1));
|
||||||
cubeViews.add(cubeMap->createTextureView(0, 1, 3, 1));
|
cubeViews.add(cubeMap->createTextureView(0, 1, 1, 1));
|
||||||
cubeViews.add(cubeMap->createTextureView(0, 1, 4, 1));
|
cubeViews.add(cubeMap->createTextureView(0, 1, 2, 1));
|
||||||
cubeViews.add(cubeMap->createTextureView(0, 1, 5, 1));
|
cubeViews.add(cubeMap->createTextureView(0, 1, 3, 1));
|
||||||
for (uint32 i = 0; i < 6; ++i) {
|
cubeViews.add(cubeMap->createTextureView(0, 1, 4, 1));
|
||||||
Gfx::ORenderPass cubeRenderPass = graphics->createRenderPass(
|
cubeViews.add(cubeMap->createTextureView(0, 1, 5, 1));
|
||||||
Gfx::RenderTargetLayout{
|
for (uint32 i = 0; i < 6; ++i) {
|
||||||
.colorAttachments =
|
Gfx::ORenderPass cubeRenderPass = graphics->createRenderPass(
|
||||||
{
|
Gfx::RenderTargetLayout{
|
||||||
Gfx::RenderTargetAttachment(cubeViews[i], Gfx::SE_IMAGE_LAYOUT_UNDEFINED,
|
.colorAttachments =
|
||||||
Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE,
|
{
|
||||||
Gfx::SE_ATTACHMENT_STORE_OP_STORE),
|
Gfx::RenderTargetAttachment(cubeViews[i], Gfx::SE_IMAGE_LAYOUT_UNDEFINED,
|
||||||
},
|
Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE,
|
||||||
},
|
Gfx::SE_ATTACHMENT_STORE_OP_STORE),
|
||||||
{
|
},
|
||||||
Gfx::SubPassDependency{
|
|
||||||
.srcSubpass = 0,
|
|
||||||
.dstSubpass = ~0U,
|
|
||||||
.srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
||||||
.dstStage = Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
|
||||||
.srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
|
||||||
.dstAccess = Gfx::SE_ACCESS_SHADER_READ_BIT,
|
|
||||||
},
|
},
|
||||||
},
|
|
||||||
{
|
|
||||||
.size = {SOURCE_RESOLUTION, SOURCE_RESOLUTION},
|
|
||||||
.offset = {0, 0},
|
|
||||||
},
|
|
||||||
"EnvironmentRenderPass");
|
|
||||||
Gfx::PGraphicsPipeline cubeRenderPipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
|
|
||||||
.vertexShader = cubeRenderVertex,
|
|
||||||
.fragmentShader = cubeRenderFrag,
|
|
||||||
.renderPass = cubeRenderPass,
|
|
||||||
.pipelineLayout = cubePipelineLayout,
|
|
||||||
.colorBlend =
|
|
||||||
{
|
{
|
||||||
.attachmentCount = 1,
|
Gfx::SubPassDependency{
|
||||||
|
.srcSubpass = 0,
|
||||||
|
.dstSubpass = ~0U,
|
||||||
|
.srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||||
|
.dstStage = Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
||||||
|
.srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
||||||
|
.dstAccess = Gfx::SE_ACCESS_SHADER_READ_BIT,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
{
|
||||||
graphics->beginRenderPass(cubeRenderPass);
|
.size = {SOURCE_RESOLUTION, SOURCE_RESOLUTION},
|
||||||
Gfx::ORenderCommand renderCommand = graphics->createRenderCommand("CubeMapRender");
|
.offset = {0, 0},
|
||||||
renderCommand->bindPipeline(cubeRenderPipeline);
|
},
|
||||||
renderCommand->bindDescriptor({set});
|
"EnvironmentRenderPass");
|
||||||
renderCommand->setViewport(cubeRenderViewport);
|
Gfx::PGraphicsPipeline cubeRenderPipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
|
||||||
renderCommand->draw(6, 1, i * 6, 0);
|
.vertexShader = cubeRenderVertex,
|
||||||
graphics->executeCommands(std::move(renderCommand));
|
.fragmentShader = cubeRenderFrag,
|
||||||
graphics->endRenderPass();
|
.renderPass = cubeRenderPass,
|
||||||
|
.pipelineLayout = cubePipelineLayout,
|
||||||
|
.colorBlend =
|
||||||
|
{
|
||||||
|
.attachmentCount = 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
graphics->beginRenderPass(cubeRenderPass);
|
||||||
|
Gfx::ORenderCommand renderCommand = graphics->createRenderCommand("CubeMapRender");
|
||||||
|
renderCommand->bindPipeline(cubeRenderPipeline);
|
||||||
|
renderCommand->bindDescriptor({set});
|
||||||
|
renderCommand->setViewport(cubeRenderViewport);
|
||||||
|
renderCommand->draw(6, 1, i * 6, 0);
|
||||||
|
graphics->executeCommands(std::move(renderCommand));
|
||||||
|
graphics->endRenderPass();
|
||||||
|
}
|
||||||
|
graphics->endDebugRegion();
|
||||||
}
|
}
|
||||||
|
|
||||||
set = cubeRenderLayout->allocateDescriptorSet();
|
set = cubeRenderLayout->allocateDescriptorSet();
|
||||||
@@ -211,45 +283,49 @@ void EnvironmentLoader::import(EnvironmentImportArgs args, PEnvironmentMapAsset
|
|||||||
.height = CONVOLUTED_RESOLUTION,
|
.height = CONVOLUTED_RESOLUTION,
|
||||||
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
||||||
});
|
});
|
||||||
Array<Gfx::OTextureView> convolutedViews;
|
{
|
||||||
convolutedViews.add(convolutedMap->createTextureView(0, 1, 0, 1));
|
graphics->beginDebugRegion("Convolution");
|
||||||
convolutedViews.add(convolutedMap->createTextureView(0, 1, 1, 1));
|
Array<Gfx::OTextureView> convolutedViews;
|
||||||
convolutedViews.add(convolutedMap->createTextureView(0, 1, 2, 1));
|
convolutedViews.add(convolutedMap->createTextureView(0, 1, 0, 1));
|
||||||
convolutedViews.add(convolutedMap->createTextureView(0, 1, 3, 1));
|
convolutedViews.add(convolutedMap->createTextureView(0, 1, 1, 1));
|
||||||
convolutedViews.add(convolutedMap->createTextureView(0, 1, 4, 1));
|
convolutedViews.add(convolutedMap->createTextureView(0, 1, 2, 1));
|
||||||
convolutedViews.add(convolutedMap->createTextureView(0, 1, 5, 1));
|
convolutedViews.add(convolutedMap->createTextureView(0, 1, 3, 1));
|
||||||
for (uint32 i = 0; i < 6; ++i) {
|
convolutedViews.add(convolutedMap->createTextureView(0, 1, 4, 1));
|
||||||
Gfx::ORenderPass convolutionPass = graphics->createRenderPass(
|
convolutedViews.add(convolutedMap->createTextureView(0, 1, 5, 1));
|
||||||
Gfx::RenderTargetLayout{
|
for (uint32 i = 0; i < 6; ++i) {
|
||||||
.colorAttachments = {Gfx::RenderTargetAttachment(convolutedViews[i], Gfx::SE_IMAGE_LAYOUT_UNDEFINED,
|
Gfx::ORenderPass convolutionPass = graphics->createRenderPass(
|
||||||
Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
Gfx::RenderTargetLayout{
|
||||||
Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE)},
|
.colorAttachments = {Gfx::RenderTargetAttachment(
|
||||||
},
|
convolutedViews[i], Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||||
{},
|
Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE)},
|
||||||
{
|
|
||||||
.size = {CONVOLUTED_RESOLUTION, CONVOLUTED_RESOLUTION},
|
|
||||||
.offset = {0, 0},
|
|
||||||
},
|
|
||||||
"EnvironmentRenderPass");
|
|
||||||
Gfx::PGraphicsPipeline convolutionPipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
|
|
||||||
.vertexShader = cubeRenderVertex,
|
|
||||||
.fragmentShader = convolutionFrag,
|
|
||||||
.renderPass = convolutionPass,
|
|
||||||
.pipelineLayout = cubePipelineLayout,
|
|
||||||
.colorBlend =
|
|
||||||
{
|
|
||||||
.attachmentCount = 1,
|
|
||||||
},
|
},
|
||||||
});
|
{},
|
||||||
|
{
|
||||||
|
.size = {CONVOLUTED_RESOLUTION, CONVOLUTED_RESOLUTION},
|
||||||
|
.offset = {0, 0},
|
||||||
|
},
|
||||||
|
"EnvironmentRenderPass");
|
||||||
|
Gfx::PGraphicsPipeline convolutionPipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
|
||||||
|
.vertexShader = cubeRenderVertex,
|
||||||
|
.fragmentShader = convolutionFrag,
|
||||||
|
.renderPass = convolutionPass,
|
||||||
|
.pipelineLayout = cubePipelineLayout,
|
||||||
|
.colorBlend =
|
||||||
|
{
|
||||||
|
.attachmentCount = 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
graphics->beginRenderPass(convolutionPass);
|
graphics->beginRenderPass(convolutionPass);
|
||||||
Gfx::ORenderCommand cmd = graphics->createRenderCommand("ConvolutionPass");
|
Gfx::ORenderCommand cmd = graphics->createRenderCommand("ConvolutionPass");
|
||||||
cmd->bindPipeline(convolutionPipeline);
|
cmd->bindPipeline(convolutionPipeline);
|
||||||
cmd->bindDescriptor({set});
|
cmd->bindDescriptor({set});
|
||||||
cmd->setViewport(convolutionViewport);
|
cmd->setViewport(convolutionViewport);
|
||||||
cmd->draw(6, 1, i * 6, 0);
|
cmd->draw(6, 1, i * 6, 0);
|
||||||
graphics->executeCommands(std::move(cmd));
|
graphics->executeCommands(std::move(cmd));
|
||||||
graphics->endRenderPass();
|
graphics->endRenderPass();
|
||||||
|
}
|
||||||
|
graphics->endDebugRegion();
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::OTextureCube prefilteredCubeMap = graphics->createTextureCube(TextureCreateInfo{
|
Gfx::OTextureCube prefilteredCubeMap = graphics->createTextureCube(TextureCreateInfo{
|
||||||
@@ -259,53 +335,59 @@ void EnvironmentLoader::import(EnvironmentImportArgs args, PEnvironmentMapAsset
|
|||||||
.useMip = true,
|
.useMip = true,
|
||||||
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
||||||
});
|
});
|
||||||
for (uint32 mip = 0; mip < prefilteredCubeMap->getMipLevels(); ++mip) {
|
{
|
||||||
Array<Gfx::OTextureView> views;
|
graphics->beginDebugRegion("Prefiltering");
|
||||||
views.add(prefilteredCubeMap->createTextureView(mip, 1, 0, 1));
|
for (uint32 mip = 0; mip < prefilteredCubeMap->getMipLevels(); ++mip) {
|
||||||
views.add(prefilteredCubeMap->createTextureView(mip, 1, 1, 1));
|
Array<Gfx::OTextureView> views;
|
||||||
views.add(prefilteredCubeMap->createTextureView(mip, 1, 2, 1));
|
views.add(prefilteredCubeMap->createTextureView(mip, 1, 0, 1));
|
||||||
views.add(prefilteredCubeMap->createTextureView(mip, 1, 3, 1));
|
views.add(prefilteredCubeMap->createTextureView(mip, 1, 1, 1));
|
||||||
views.add(prefilteredCubeMap->createTextureView(mip, 1, 4, 1));
|
views.add(prefilteredCubeMap->createTextureView(mip, 1, 2, 1));
|
||||||
views.add(prefilteredCubeMap->createTextureView(mip, 1, 5, 1));
|
views.add(prefilteredCubeMap->createTextureView(mip, 1, 3, 1));
|
||||||
for (uint32 i = 0; i < 6; ++i) {
|
views.add(prefilteredCubeMap->createTextureView(mip, 1, 4, 1));
|
||||||
Gfx::ORenderPass prefilterPass = graphics->createRenderPass(
|
views.add(prefilteredCubeMap->createTextureView(mip, 1, 5, 1));
|
||||||
Gfx::RenderTargetLayout{
|
for (uint32 i = 0; i < 6; ++i) {
|
||||||
.colorAttachments = {Gfx::RenderTargetAttachment(
|
Gfx::ORenderPass prefilterPass = graphics->createRenderPass(
|
||||||
views[i], Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
Gfx::RenderTargetLayout{
|
||||||
Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE)},
|
.colorAttachments = {Gfx::RenderTargetAttachment(
|
||||||
},
|
views[i], Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||||
{},
|
Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE)},
|
||||||
{
|
|
||||||
.size = {prefilterViewports[mip]->getWidth(), prefilterViewports[mip]->getHeight()},
|
|
||||||
.offset = {0, 0},
|
|
||||||
},
|
|
||||||
"PrefilterPass");
|
|
||||||
Gfx::PGraphicsPipeline prefilterPipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
|
|
||||||
.vertexShader = cubeRenderVertex,
|
|
||||||
.fragmentShader = prefilterFrag,
|
|
||||||
.renderPass = prefilterPass,
|
|
||||||
.pipelineLayout = cubePipelineLayout,
|
|
||||||
.colorBlend =
|
|
||||||
{
|
|
||||||
.attachmentCount = 1,
|
|
||||||
},
|
},
|
||||||
});
|
{},
|
||||||
|
{
|
||||||
|
.size = {prefilterViewports[mip]->getWidth(), prefilterViewports[mip]->getHeight()},
|
||||||
|
.offset = {0, 0},
|
||||||
|
},
|
||||||
|
"PrefilterPass");
|
||||||
|
Gfx::PGraphicsPipeline prefilterPipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
|
||||||
|
.vertexShader = cubeRenderVertex,
|
||||||
|
.fragmentShader = prefilterFrag,
|
||||||
|
.renderPass = prefilterPass,
|
||||||
|
.pipelineLayout = cubePipelineLayout,
|
||||||
|
.colorBlend =
|
||||||
|
{
|
||||||
|
.attachmentCount = 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
graphics->beginRenderPass(prefilterPass);
|
graphics->beginRenderPass(prefilterPass);
|
||||||
Gfx::ORenderCommand cmd = graphics->createRenderCommand("PrefilterPass");
|
Gfx::ORenderCommand cmd = graphics->createRenderCommand("PrefilterPass");
|
||||||
cmd->bindPipeline(prefilterPipeline);
|
cmd->bindPipeline(prefilterPipeline);
|
||||||
cmd->bindDescriptor({set});
|
cmd->bindDescriptor({set});
|
||||||
float roughness = (float)mip / (float)(prefilteredCubeMap->getMipLevels() - 1);
|
float roughness = (float)mip / (float)(prefilteredCubeMap->getMipLevels() - 1);
|
||||||
cmd->pushConstants(Gfx::SE_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(float), &roughness);
|
cmd->pushConstants(Gfx::SE_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(float), &roughness);
|
||||||
cmd->setViewport(prefilterViewports[mip]);
|
cmd->setViewport(prefilterViewports[mip]);
|
||||||
cmd->draw(6, 1, i * 6, 0);
|
cmd->draw(6, 1, i * 6, 0);
|
||||||
graphics->executeCommands(std::move(cmd));
|
graphics->executeCommands(std::move(cmd));
|
||||||
graphics->endRenderPass();
|
graphics->endRenderPass();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
graphics->endDebugRegion();
|
||||||
}
|
}
|
||||||
|
|
||||||
asset->skybox = std::move(cubeMap);
|
asset->skybox = std::move(cubeMap);
|
||||||
asset->irradianceMap = std::move(convolutedMap);
|
asset->irradianceMap = std::move(convolutedMap);
|
||||||
asset->prefilteredMap = std::move(prefilteredCubeMap);
|
asset->prefilteredMap = std::move(prefilteredCubeMap);
|
||||||
|
asset->brdfLUT = lutTexture;
|
||||||
|
asset->lutSampler = lutSampler;
|
||||||
graphics->waitDeviceIdle();
|
graphics->waitDeviceIdle();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Graphics/Descriptor.h"
|
||||||
#include "MinimalEngine.h"
|
#include "MinimalEngine.h"
|
||||||
#include "Graphics/Graphics.h"
|
#include "Graphics/Graphics.h"
|
||||||
#include "Graphics/Shader.h"
|
#include "Graphics/Shader.h"
|
||||||
@@ -20,12 +21,22 @@ class EnvironmentLoader {
|
|||||||
private:
|
private:
|
||||||
void import(EnvironmentImportArgs args, PEnvironmentMapAsset asset);
|
void import(EnvironmentImportArgs args, PEnvironmentMapAsset asset);
|
||||||
Gfx::PGraphics graphics;
|
Gfx::PGraphics graphics;
|
||||||
|
|
||||||
|
Gfx::OTexture2D lutTexture;
|
||||||
|
Gfx::OSampler lutSampler;
|
||||||
|
Gfx::OViewport lutViewport;
|
||||||
|
Gfx::OVertexShader lutVert;
|
||||||
|
Gfx::OFragmentShader lutFrag;
|
||||||
|
|
||||||
Gfx::OVertexShader cubeRenderVertex;
|
Gfx::OVertexShader cubeRenderVertex;
|
||||||
Gfx::OFragmentShader cubeRenderFrag;
|
Gfx::OFragmentShader cubeRenderFrag;
|
||||||
Gfx::ODescriptorLayout cubeRenderLayout;
|
Gfx::ODescriptorLayout cubeRenderLayout;
|
||||||
Gfx::OPipelineLayout cubePipelineLayout;
|
Gfx::OPipelineLayout cubePipelineLayout;
|
||||||
|
|
||||||
Gfx::OFragmentShader convolutionFrag;
|
Gfx::OFragmentShader convolutionFrag;
|
||||||
|
|
||||||
Gfx::OFragmentShader prefilterFrag;
|
Gfx::OFragmentShader prefilterFrag;
|
||||||
|
|
||||||
Gfx::OViewport cubeRenderViewport;
|
Gfx::OViewport cubeRenderViewport;
|
||||||
Gfx::OViewport convolutionViewport;
|
Gfx::OViewport convolutionViewport;
|
||||||
// for now we hardcode 128x128 as mip 0, so we have 8 roughness levels
|
// for now we hardcode 128x128 as mip 0, so we have 8 roughness levels
|
||||||
|
|||||||
@@ -14,10 +14,14 @@ class EnvironmentMapAsset : public Asset {
|
|||||||
Gfx::PTextureCube getSkybox() const { return skybox; }
|
Gfx::PTextureCube getSkybox() const { return skybox; }
|
||||||
Gfx::PTextureCube getIrradianceMap() const { return irradianceMap; }
|
Gfx::PTextureCube getIrradianceMap() const { return irradianceMap; }
|
||||||
Gfx::PTextureCube getPrefilteredMap() const { return prefilteredMap; }
|
Gfx::PTextureCube getPrefilteredMap() const { return prefilteredMap; }
|
||||||
|
Gfx::PTexture2D getBrdfLUT() const { return brdfLUT; }
|
||||||
|
Gfx::PSampler getLUTSampler() const { return lutSampler; }
|
||||||
private:
|
private:
|
||||||
Gfx::OTextureCube skybox;
|
Gfx::OTextureCube skybox;
|
||||||
Gfx::OTextureCube irradianceMap;
|
Gfx::OTextureCube irradianceMap;
|
||||||
Gfx::OTextureCube prefilteredMap;
|
Gfx::OTextureCube prefilteredMap;
|
||||||
|
Gfx::PTexture2D brdfLUT;
|
||||||
|
Gfx::PSampler lutSampler;
|
||||||
friend class EnvironmentLoader;
|
friend class EnvironmentLoader;
|
||||||
};
|
};
|
||||||
} // namespace Seele
|
} // namespace Seele
|
||||||
@@ -79,8 +79,8 @@ BasePass::BasePass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
skybox = Seele::Component::Skybox{
|
skybox = Seele::Component::Skybox{
|
||||||
.day = scene->getLightEnvironment()->getEnvironmentMap()->getPrefilteredMap(),
|
.day = scene->getLightEnvironment()->getEnvironmentMap()->getSkybox(),
|
||||||
.night = scene->getLightEnvironment()->getEnvironmentMap()->getPrefilteredMap(),
|
.night = scene->getLightEnvironment()->getEnvironmentMap()->getSkybox(),
|
||||||
.fogColor = Vector(0, 0, 0),
|
.fogColor = Vector(0, 0, 0),
|
||||||
.blendFactor = 0,
|
.blendFactor = 0,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ RayTracingPass::RayTracingPass(Gfx::PGraphics graphics, PScene scene) : RenderPa
|
|||||||
.useMaterial = true,
|
.useMaterial = true,
|
||||||
.rayTracing = true,
|
.rayTracing = true,
|
||||||
});
|
});
|
||||||
skyBox = AssetRegistry::findEnvironmentMap("", "newport_loft")->getIrradianceMap();
|
skyBox = AssetRegistry::findEnvironmentMap("", "newport_loft")->getSkybox();
|
||||||
skyBoxSampler = graphics->createSampler({});
|
skyBoxSampler = graphics->createSampler({});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "LightEnvironment.h"
|
#include "LightEnvironment.h"
|
||||||
#include "Asset/AssetRegistry.h"
|
#include "Asset/AssetRegistry.h"
|
||||||
#include "Asset/EnvironmentMapAsset.h"
|
#include "Asset/EnvironmentMapAsset.h"
|
||||||
|
#include "Graphics/Descriptor.h"
|
||||||
|
#include "Graphics/Enums.h"
|
||||||
#include "Graphics/Graphics.h"
|
#include "Graphics/Graphics.h"
|
||||||
|
|
||||||
using namespace Seele;
|
using namespace Seele;
|
||||||
@@ -40,6 +42,18 @@ LightEnvironment::LightEnvironment(Gfx::PGraphics graphics)
|
|||||||
.name = "irradianceSampler",
|
.name = "irradianceSampler",
|
||||||
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
|
||||||
});
|
});
|
||||||
|
layout->addDescriptorBinding(Gfx::DescriptorBinding{
|
||||||
|
.name = "prefilteredMap",
|
||||||
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||||
|
});
|
||||||
|
layout->addDescriptorBinding(Gfx::DescriptorBinding{
|
||||||
|
.name = "brdfLUT",
|
||||||
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||||
|
});
|
||||||
|
layout->addDescriptorBinding(Gfx::DescriptorBinding{
|
||||||
|
.name = "lutSampler",
|
||||||
|
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
|
||||||
|
});
|
||||||
layout->create();
|
layout->create();
|
||||||
|
|
||||||
directionalLights = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
directionalLights = graphics->createShaderBuffer(ShaderBufferCreateInfo{
|
||||||
@@ -128,6 +142,9 @@ void LightEnvironment::commit() {
|
|||||||
set->updateBuffer("pointLights", 0, pointLights);
|
set->updateBuffer("pointLights", 0, pointLights);
|
||||||
set->updateTexture("irradianceMap", 0, environment->getIrradianceMap()->getDefaultView());
|
set->updateTexture("irradianceMap", 0, environment->getIrradianceMap()->getDefaultView());
|
||||||
set->updateSampler("irradianceSampler", 0, environmentSampler);
|
set->updateSampler("irradianceSampler", 0, environmentSampler);
|
||||||
|
set->updateTexture("prefilteredMap", 0, environment->getPrefilteredMap()->getDefaultView());
|
||||||
|
set->updateTexture("brdfLUT", 0, environment->getBrdfLUT()->getDefaultView());
|
||||||
|
set->updateSampler("lutSampler", 0, environment->getLUTSampler());
|
||||||
set->writeChanges();
|
set->writeChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user