diff --git a/res/shaders/EnvironmentMapping.slang b/res/shaders/EnvironmentMapping.slang index 438fbf2..fcd325c 100644 --- a/res/shaders/EnvironmentMapping.slang +++ b/res/shaders/EnvironmentMapping.slang @@ -163,6 +163,9 @@ float3 ImportanceSampleGGX(float2 Xi, float3 N, float roughness) return normalize(sampleVec); } +layout(push_constant) +ConstantBuffer pRoughness; + [shader("pixel")] float4 computePrefilteredCubemap(float3 localPos : LOCALPOS) : SV_Target { @@ -176,7 +179,7 @@ float4 computePrefilteredCubemap(float3 localPos : LOCALPOS) : SV_Target for(uint i = 0u; i < SAMPLE_COUNT; ++i) { float2 Xi = Hammersley(i, SAMPLE_COUNT); - float3 H = ImportanceSampleGGX(Xi, N, roughness); + float3 H = ImportanceSampleGGX(Xi, N, pRoughness); float3 L = normalize(2.0 * dot(V, H) * H - V); float NdotL = max(dot(N, L), 0.0); diff --git a/src/Editor/Asset/EnvironmentLoader.cpp b/src/Editor/Asset/EnvironmentLoader.cpp index 26b644c..66567e0 100644 --- a/src/Editor/Asset/EnvironmentLoader.cpp +++ b/src/Editor/Asset/EnvironmentLoader.cpp @@ -122,7 +122,7 @@ void EnvironmentLoader::import(EnvironmentImportArgs args, PEnvironmentMapAsset Gfx::PDescriptorSet set = cubeRenderLayout->allocateDescriptorSet(); set->updateConstants("view", 0, captureViews); set->updateConstants("projection", 0, &captureProjection); - set->updateTexture("equirectangularMap", 0, Gfx::PTexture2D(hdrTexture)); + set->updateTexture("equirectangularMap", 0, hdrTexture->getDefaultView()); set->updateSampler("sampler", 0, cubeSampler); set->writeChanges(); Gfx::OTextureCube cubeMap = graphics->createTextureCube(TextureCreateInfo{ @@ -135,7 +135,7 @@ void EnvironmentLoader::import(EnvironmentImportArgs args, PEnvironmentMapAsset Gfx::RenderTargetLayout{ .colorAttachments = { - Gfx::RenderTargetAttachment(cubeMap, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + Gfx::RenderTargetAttachment(cubeMap->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), }, }, @@ -181,7 +181,7 @@ void EnvironmentLoader::import(EnvironmentImportArgs args, PEnvironmentMapAsset }); Gfx::ORenderPass convolutionPass = graphics->createRenderPass( Gfx::RenderTargetLayout{ - .colorAttachments = {Gfx::RenderTargetAttachment(convolutedMap, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, + .colorAttachments = {Gfx::RenderTargetAttachment(convolutedMap->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)}, }, @@ -206,7 +206,7 @@ void EnvironmentLoader::import(EnvironmentImportArgs args, PEnvironmentMapAsset set->updateConstants("view", 0, captureViews); set->updateConstants("projection", 0, &captureProjection); set->updateSampler("sampler", 0, cubeSampler); - set->updateTexture("cubeMap", 0, Gfx::PTextureCube(cubeMap)); + set->updateTexture("cubeMap", 0, cubeMap->getDefaultView()); set->writeChanges(); graphics->beginRenderPass(convolutionPass); diff --git a/src/Engine/Graphics/Descriptor.h b/src/Engine/Graphics/Descriptor.h index b454d9b..2f85897 100644 --- a/src/Engine/Graphics/Descriptor.h +++ b/src/Engine/Graphics/Descriptor.h @@ -52,12 +52,9 @@ class DescriptorPool { DEFINE_REF(DescriptorPool) DECLARE_REF(UniformBuffer) DECLARE_REF(ShaderBuffer) -DECLARE_REF(Texture) -DECLARE_REF(Texture2D) -DECLARE_REF(Texture3D) -DECLARE_REF(TextureCube) DECLARE_REF(Sampler) DECLARE_REF(TopLevelAS) +DECLARE_REF(TextureView) class DescriptorSet { public: DescriptorSet(PDescriptorLayout layout); @@ -68,7 +65,7 @@ class DescriptorSet { virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PVertexBuffer indexBuffer) = 0; virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PIndexBuffer indexBuffer) = 0; virtual void updateSampler(const std::string& name, uint32 index, Gfx::PSampler samplerState) = 0; - virtual void updateTexture(const std::string& name, uint32 index, Gfx::PTexture texture) = 0; + virtual void updateTexture(const std::string& name, uint32 index, Gfx::PTextureView texture) = 0; virtual void updateAccelerationStructure(const std::string& name, uint32 index, Gfx::PTopLevelAS as) = 0; bool operator<(PDescriptorSet other); diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 23643df..63e3358 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -128,8 +128,8 @@ void BasePass::beginFrame(const Component::Camera& cam, const Component::Transfo skyboxDataSet->updateConstants("fogBlend", 0, &skyboxData.fogColor); skyboxDataSet->writeChanges(); textureSet = textureLayout->allocateDescriptorSet(); - textureSet->updateTexture(SKYBOXDAY_NAME, 0, skybox.day); - textureSet->updateTexture(SKYBOXNIGHT_NAME, 0, skybox.night); + textureSet->updateTexture(SKYBOXDAY_NAME, 0, skybox.day->getDefaultView()); + textureSet->updateTexture(SKYBOXNIGHT_NAME, 0, skybox.night->getDefaultView()); textureSet->updateSampler(SKYBOXSAMPLER_NAME, 0, skyboxSampler); textureSet->writeChanges(); } @@ -138,9 +138,9 @@ void BasePass::beginFrame(const Component::Camera& cam, const Component::Transfo void BasePass::render() { graphics->beginDebugRegion("BasePass"); opaqueCulling->updateBuffer(LIGHTINDEX_NAME, 0, oLightIndexList); - opaqueCulling->updateTexture(LIGHTGRID_NAME, 0, oLightGrid); + opaqueCulling->updateTexture(LIGHTGRID_NAME, 0, oLightGrid->getDefaultView()); transparentCulling->updateBuffer(LIGHTINDEX_NAME, 0, tLightIndexList); - transparentCulling->updateTexture(LIGHTGRID_NAME, 0, tLightGrid); + transparentCulling->updateTexture(LIGHTGRID_NAME, 0, tLightGrid->getDefaultView()); opaqueCulling->writeChanges(); transparentCulling->writeChanges(); @@ -421,21 +421,21 @@ void BasePass::publishOutputs() { .usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, }); - depthAttachment = Gfx::RenderTargetAttachment(Gfx::PTexture2D(basePassDepth), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, + depthAttachment = Gfx::RenderTargetAttachment(basePassDepth->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE); msDepthAttachment = - Gfx::RenderTargetAttachment(msBasePassDepth, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + Gfx::RenderTargetAttachment(msBasePassDepth->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_DONT_CARE); msDepthAttachment.clear.depthStencil.depth = 0.0f; colorAttachment = - Gfx::RenderTargetAttachment(basePassColor, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + Gfx::RenderTargetAttachment(basePassColor->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE); msColorAttachment = - Gfx::RenderTargetAttachment(msBasePassColor, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + Gfx::RenderTargetAttachment(msBasePassColor->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_DONT_CARE); msColorAttachment.clear.color.float32[0] = 0; msColorAttachment.clear.color.float32[1] = 1; diff --git a/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp b/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp index e1dbfc6..5b8245c 100644 --- a/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp +++ b/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp @@ -156,7 +156,7 @@ void CachedDepthPass::publishOutputs() { }; depthBuffer = graphics->createTexture2D(depthBufferInfo); depthAttachment = - Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + Gfx::RenderTargetAttachment(depthBuffer->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); resources->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment); @@ -168,7 +168,7 @@ void CachedDepthPass::publishOutputs() { }; visibilityBuffer = graphics->createTexture2D(visibilityInfo); visibilityAttachment = - Gfx::RenderTargetAttachment(visibilityBuffer, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + Gfx::RenderTargetAttachment(visibilityBuffer->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); resources->registerRenderPassOutput("VISIBILITY", visibilityAttachment); query = graphics->createPipelineStatisticsQuery("CachedPipelineStatistics"); diff --git a/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp b/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp index 4c4ce38..43f0b63 100644 --- a/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp @@ -75,12 +75,12 @@ void DepthCullingPass::render() { { graphics->beginDebugRegion("MipGeneration"); query->beginQuery(); - depthAttachment.getTexture()->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, + depthAttachment.getTextureView()->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT); depthMipBuffer->rotateBuffer(depthMipBuffer->getNumElements() * sizeof(uint32)); - set->updateTexture(DEPTHTEXTURE_NAME, 0, depthAttachment.getTexture()); + set->updateTexture(DEPTHTEXTURE_NAME, 0, depthAttachment.getTextureView()); set->updateBuffer(DEPTHMIP_NAME, 0, depthMipBuffer); set->writeChanges(); @@ -115,7 +115,7 @@ void DepthCullingPass::render() { depthMipBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT); - depthAttachment.getTexture()->changeLayout( + depthAttachment.getTextureView()->changeLayout( Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT); @@ -216,11 +216,11 @@ void DepthCullingPass::render() { timestamps->write(Gfx::SE_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, "CullingEnd"); query->endQuery(); // Sync depth read/write with compute read - depthAttachment.getTexture()->pipelineBarrier( + depthAttachment.getTextureView()->pipelineBarrier( Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT); // Sync visibility write with compute read - visibilityAttachment.getTexture()->pipelineBarrier(Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + visibilityAttachment.getTextureView()->pipelineBarrier(Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT); graphics->endDebugRegion(); diff --git a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp index 68cbccd..8aab9ec 100644 --- a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp @@ -50,8 +50,8 @@ void LightCullingPass::render() { cullingDescriptorSet->updateBuffer(TLIGHTINDEXCOUNTER_NAME, 0, tLightIndexCounter); cullingDescriptorSet->updateBuffer(OLIGHTINDEXLIST_NAME, 0, oLightIndexList); cullingDescriptorSet->updateBuffer(TLIGHTINDEXLIST_NAME, 0, tLightIndexList); - cullingDescriptorSet->updateTexture(OLIGHTGRID_NAME, 0, Gfx::PTexture2D(oLightGrid)); - cullingDescriptorSet->updateTexture(TLIGHTGRID_NAME, 0, Gfx::PTexture2D(tLightGrid)); + cullingDescriptorSet->updateTexture(OLIGHTGRID_NAME, 0, oLightGrid->getDefaultView()); + cullingDescriptorSet->updateTexture(TLIGHTGRID_NAME, 0, tLightGrid->getDefaultView()); cullingDescriptorSet->writeChanges(); Gfx::OComputeCommand computeCommand = graphics->createComputeCommand("CullingCommand"); if (getGlobals().useLightCulling) { @@ -221,7 +221,7 @@ void LightCullingPass::publishOutputs() { void LightCullingPass::createRenderPass() { timestamps = resources->requestTimestampQuery("TIMESTAMPS"); - depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH").getTexture(); + depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH").getTextureView(); } void LightCullingPass::setupFrustums() { diff --git a/src/Engine/Graphics/RenderPass/LightCullingPass.h b/src/Engine/Graphics/RenderPass/LightCullingPass.h index 0967e58..db1dfae 100644 --- a/src/Engine/Graphics/RenderPass/LightCullingPass.h +++ b/src/Engine/Graphics/RenderPass/LightCullingPass.h @@ -36,7 +36,7 @@ class LightCullingPass : public RenderPass { Gfx::PDescriptorSet viewParamsSet; PLightEnvironment lightEnv; - Gfx::PTexture2D depthAttachment; + Gfx::PTextureView depthAttachment; constexpr static const char* DEPTHATTACHMENT_NAME = "depth"; Gfx::OShaderBuffer oLightIndexCounter; constexpr static const char* OLIGHTINDEXCOUNTER_NAME = "oLightIndexCounter"; diff --git a/src/Engine/Graphics/RenderPass/RayTracingPass.cpp b/src/Engine/Graphics/RenderPass/RayTracingPass.cpp index 737beac..2c46811 100644 --- a/src/Engine/Graphics/RenderPass/RayTracingPass.cpp +++ b/src/Engine/Graphics/RenderPass/RayTracingPass.cpp @@ -153,10 +153,10 @@ void RayTracingPass::render() { }); Gfx::PDescriptorSet desc = paramsLayout->allocateDescriptorSet(); desc->updateAccelerationStructure(TLAS_NAME, 0, tlas); - desc->updateTexture(ACCUMULATOR_NAME, 0, Gfx::PTexture2D(radianceAccumulator)); - desc->updateTexture(TEXTURE_NAME, 0, Gfx::PTexture2D(texture)); + desc->updateTexture(ACCUMULATOR_NAME, 0, radianceAccumulator->getDefaultView()); + desc->updateTexture(TEXTURE_NAME, 0, texture->getDefaultView()); desc->updateBuffer(INDEXBUFFER_NAME, 0, StaticMeshVertexData::getInstance()->getIndexBuffer()); - desc->updateTexture(SKYBOX_NAME, 0, skyBox); + desc->updateTexture(SKYBOX_NAME, 0, skyBox->getDefaultView()); desc->updateSampler(SKYSAMPLER_NAME, 0, skyBoxSampler); desc->writeChanges(); @@ -210,7 +210,7 @@ void RayTracingPass::publishOutputs() { texture->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_NONE, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT, Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR); - resources->registerRenderPassOutput("BASEPASS_COLOR", Gfx::RenderTargetAttachment(texture, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, + resources->registerRenderPassOutput("BASEPASS_COLOR", Gfx::RenderTargetAttachment(texture->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)); ShaderCompilationInfo compileInfo = { .name = "RayGenMiss", diff --git a/src/Engine/Graphics/RenderPass/ShadowPass.cpp b/src/Engine/Graphics/RenderPass/ShadowPass.cpp index ca4d541..509e9d2 100644 --- a/src/Engine/Graphics/RenderPass/ShadowPass.cpp +++ b/src/Engine/Graphics/RenderPass/ShadowPass.cpp @@ -51,7 +51,7 @@ void ShadowPass::render() { Array commands; renderPass = graphics->createRenderPass( Gfx::RenderTargetLayout{ - .depthAttachment = Gfx::RenderTargetAttachment(shadowMaps[shadowIndex], Gfx::SE_IMAGE_LAYOUT_UNDEFINED, + .depthAttachment = Gfx::RenderTargetAttachment(shadowMaps[shadowIndex]->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE), diff --git a/src/Engine/Graphics/RenderPass/ToneMappingPass.cpp b/src/Engine/Graphics/RenderPass/ToneMappingPass.cpp index 9095f31..c1a74aa 100644 --- a/src/Engine/Graphics/RenderPass/ToneMappingPass.cpp +++ b/src/Engine/Graphics/RenderPass/ToneMappingPass.cpp @@ -129,7 +129,7 @@ void ToneMappingPass::beginFrame(const Component::Camera& cam, const Component:: void ToneMappingPass::render() { graphics->beginDebugRegion("ToneMapping"); - hdrInputTexture.getTexture()->changeLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + hdrInputTexture.getTextureView()->changeLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT); @@ -139,7 +139,7 @@ void ToneMappingPass::render() { histogramSet->updateConstants("inverseLogLumRange", 0, &inverseLogLumRange); histogramSet->updateConstants("timeCoeff", 0, &timeCoeff); histogramSet->updateConstants("numPixels", 0, &numPixels); - histogramSet->updateTexture("hdrImage", 0, hdrInputTexture.getTexture()); + histogramSet->updateTexture("hdrImage", 0, hdrInputTexture.getTextureView()); histogramSet->updateBuffer("histogram", 0, histogramBuffer); histogramSet->updateBuffer("averageLuminance", 0, luminanceBuffer); histogramSet->writeChanges(); @@ -172,7 +172,7 @@ void ToneMappingPass::render() { tonemappingSet->updateConstants("slope", 0, &slope); tonemappingSet->updateConstants("power", 0, &power); tonemappingSet->updateConstants("sat", 0, &sat); - tonemappingSet->updateTexture("hdrInputTexture", 0, hdrInputTexture.getTexture()); + tonemappingSet->updateTexture("hdrInputTexture", 0, hdrInputTexture.getTextureView()); tonemappingSet->updateSampler("hdrSampler", 0, sampler); tonemappingSet->updateBuffer("averageLuminance", 0, luminanceBuffer); tonemappingSet->writeChanges(); diff --git a/src/Engine/Graphics/RenderPass/UIPass.cpp b/src/Engine/Graphics/RenderPass/UIPass.cpp index 084fae7..2f34c5c 100644 --- a/src/Engine/Graphics/RenderPass/UIPass.cpp +++ b/src/Engine/Graphics/RenderPass/UIPass.cpp @@ -87,7 +87,7 @@ void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform textDescriptorSet->updateBuffer(GLYPHINSTANCE_NAME, 0, glyphInstanceBuffer); textDescriptorSet->updateSampler(GLYPHSAMPLER_NAME, 0, glyphSampler); for (uint32 i = 0; i < usedTextures.size(); ++i) { - textDescriptorSet->updateTexture(TEXTURES_NAME, i, usedTextures[i]); + textDescriptorSet->updateTexture(TEXTURES_NAME, i, usedTextures[i]->getDefaultView()); } textDescriptorSet->writeChanges(); @@ -100,7 +100,7 @@ void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform uiDescriptorSet->updateBuffer(ELEMENT_NAME, 0, elementBuffer); uiDescriptorSet->updateSampler(GLYPHSAMPLER_NAME, 0, glyphSampler); for (uint32 i = 0; i < usedTextures.size(); ++i) { - uiDescriptorSet->updateTexture(TEXTURES_NAME, i, usedTextures[i]); + uiDescriptorSet->updateTexture(TEXTURES_NAME, i, usedTextures[i]->getDefaultView()); } uiDescriptorSet->writeChanges(); } @@ -132,7 +132,7 @@ void UIPass::createRenderPass() { depthBuffer = graphics->createTexture2D(depthBufferInfo); depthAttachment = - Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + Gfx::RenderTargetAttachment(depthBuffer->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); depthAttachment.clear.depthStencil.depth = 0; diff --git a/src/Engine/Graphics/RenderPass/VisibilityPass.cpp b/src/Engine/Graphics/RenderPass/VisibilityPass.cpp index d2f48a3..4c3c032 100644 --- a/src/Engine/Graphics/RenderPass/VisibilityPass.cpp +++ b/src/Engine/Graphics/RenderPass/VisibilityPass.cpp @@ -23,7 +23,7 @@ void VisibilityPass::render() { Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT); visibilityDescriptor->reset(); visibilitySet = visibilityDescriptor->allocateDescriptorSet(); - visibilitySet->updateTexture(VISIBILITY_NAME, 0, visibilityAttachment.getTexture()); + visibilitySet->updateTexture(VISIBILITY_NAME, 0, visibilityAttachment.getTextureView()); visibilitySet->updateBuffer(CULLINGBUFFER_NAME, 0, cullingBuffer); visibilitySet->writeChanges(); diff --git a/src/Engine/Graphics/RenderTarget.cpp b/src/Engine/Graphics/RenderTarget.cpp index 48174f4..9b9fa22 100644 --- a/src/Engine/Graphics/RenderTarget.cpp +++ b/src/Engine/Graphics/RenderTarget.cpp @@ -3,19 +3,13 @@ using namespace Seele; using namespace Seele::Gfx; -RenderTargetAttachment::RenderTargetAttachment(PTexture2D texture, SeImageLayout initialLayout, SeImageLayout finalLayout, +RenderTargetAttachment::RenderTargetAttachment(PTextureView texture, SeImageLayout initialLayout, SeImageLayout finalLayout, SeAttachmentLoadOp loadOp, SeAttachmentStoreOp storeOp, SeAttachmentLoadOp stencilLoadOp, SeAttachmentStoreOp stencilStoreOp) : componentFlags(0), texture(texture), initialLayout(initialLayout), finalLayout(finalLayout), loadOp(loadOp), storeOp(storeOp), stencilLoadOp(stencilLoadOp), stencilStoreOp(stencilStoreOp) {} -RenderTargetAttachment::RenderTargetAttachment(PTextureCube texture, SeImageLayout initialLayout, SeImageLayout finalLayout, - SeAttachmentLoadOp loadOp, SeAttachmentStoreOp storeOp, SeAttachmentLoadOp stencilLoadOp, - SeAttachmentStoreOp stencilStoreOp) - : componentFlags(0), texture(texture), initialLayout(initialLayout), finalLayout(finalLayout), loadOp(loadOp), - storeOp(storeOp), stencilLoadOp(stencilLoadOp), stencilStoreOp(stencilStoreOp) {} - RenderTargetAttachment::RenderTargetAttachment(PViewport viewport, SeImageLayout initialLayout, SeImageLayout finalLayout, SeAttachmentLoadOp loadOp, SeAttachmentStoreOp storeOp, SeAttachmentLoadOp stencilLoadOp, SeAttachmentStoreOp stencilStoreOp) diff --git a/src/Engine/Graphics/RenderTarget.h b/src/Engine/Graphics/RenderTarget.h index 376e87b..8ff0383 100644 --- a/src/Engine/Graphics/RenderTarget.h +++ b/src/Engine/Graphics/RenderTarget.h @@ -9,12 +9,7 @@ namespace Gfx { class RenderTargetAttachment { public: RenderTargetAttachment() {} - RenderTargetAttachment(PTexture2D texture, SeImageLayout initialLayout, SeImageLayout finalLayout, - SeAttachmentLoadOp loadOp = SE_ATTACHMENT_LOAD_OP_LOAD, - SeAttachmentStoreOp storeOp = SE_ATTACHMENT_STORE_OP_STORE, - SeAttachmentLoadOp stencilLoadOp = SE_ATTACHMENT_LOAD_OP_DONT_CARE, - SeAttachmentStoreOp stencilStoreOp = SE_ATTACHMENT_STORE_OP_DONT_CARE); - RenderTargetAttachment(PTextureCube texture, SeImageLayout initialLayout, SeImageLayout finalLayout, + RenderTargetAttachment(PTextureView texture, SeImageLayout initialLayout, SeImageLayout finalLayout, SeAttachmentLoadOp loadOp = SE_ATTACHMENT_LOAD_OP_LOAD, SeAttachmentStoreOp storeOp = SE_ATTACHMENT_STORE_OP_STORE, SeAttachmentLoadOp stencilLoadOp = SE_ATTACHMENT_LOAD_OP_DONT_CARE, @@ -25,10 +20,10 @@ class RenderTargetAttachment { SeAttachmentLoadOp stencilLoadOp = SE_ATTACHMENT_LOAD_OP_DONT_CARE, SeAttachmentStoreOp stencilStoreOp = SE_ATTACHMENT_STORE_OP_DONT_CARE); ~RenderTargetAttachment(); - void setTexture(PTexture _texture) { texture = _texture; } - PTexture getTexture() const { + void setTextureView(PTextureView _texture) { texture = _texture; } + PTextureView getTextureView() const { if (viewport != nullptr) { - return PTexture(viewport->getOwner()->getBackBuffer()); + return PTextureView(viewport->getOwner()->getBackBuffer()->getDefaultView()); } return texture; } @@ -73,7 +68,7 @@ class RenderTargetAttachment { SeColorComponentFlags componentFlags = 0; protected: - PTexture texture = nullptr; + PTextureView texture = nullptr; PViewport viewport = nullptr; SeImageLayout initialLayout = SE_IMAGE_LAYOUT_UNDEFINED; SeImageLayout finalLayout = SE_IMAGE_LAYOUT_UNDEFINED; diff --git a/src/Engine/Graphics/Texture.cpp b/src/Engine/Graphics/Texture.cpp index 80ba950..824a9b0 100644 --- a/src/Engine/Graphics/Texture.cpp +++ b/src/Engine/Graphics/Texture.cpp @@ -4,6 +4,10 @@ using namespace Seele; using namespace Seele::Gfx; +TextureView::TextureView() {} + +TextureView::~TextureView() {} + Texture::Texture(QueueFamilyMapping mapping) : QueueOwnedResource(mapping) {} Texture::~Texture() {} diff --git a/src/Engine/Graphics/Texture.h b/src/Engine/Graphics/Texture.h index 346e9be..0d0c9f3 100644 --- a/src/Engine/Graphics/Texture.h +++ b/src/Engine/Graphics/Texture.h @@ -3,6 +3,25 @@ namespace Seele { namespace Gfx { + +class TextureView { +public: + TextureView(); + virtual ~TextureView(); + virtual SeFormat getFormat() const = 0; + virtual uint32 getWidth() const = 0; + virtual uint32 getHeight() const = 0; + virtual uint32 getDepth() const = 0; + virtual uint32 getNumLayers() const = 0; + virtual SeSampleCountFlags getNumSamples() const = 0; + virtual uint32 getMipLevels() const = 0; + virtual void changeLayout(SeImageLayout newLayout, SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, + SePipelineStageFlags dstStage) = 0; + virtual void pipelineBarrier(SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; +private: +}; +DEFINE_REF(TextureView) + class Texture : public QueueOwnedResource { public: Texture(QueueFamilyMapping mapping); @@ -12,12 +31,16 @@ class Texture : public QueueOwnedResource { virtual uint32 getWidth() const = 0; virtual uint32 getHeight() const = 0; virtual uint32 getDepth() const = 0; + virtual uint32 getNumLayers() const = 0; virtual SeSampleCountFlags getNumSamples() const = 0; virtual uint32 getMipLevels() const = 0; virtual void changeLayout(SeImageLayout newLayout, SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; virtual void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array& buffer) = 0; virtual void generateMipmaps() = 0; + + virtual PTextureView getDefaultView() const = 0; + virtual OTextureView createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) = 0; protected: // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(QueueType newOwner) = 0; @@ -41,7 +64,9 @@ class Texture2D : public Texture { virtual void changeLayout(SeImageLayout newLayout, SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; virtual void generateMipmaps() = 0; - + virtual PTextureView getDefaultView() const = 0; + + virtual OTextureView createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) = 0; protected: // Inherited via QueueOwnedResource virtual void executeOwnershipBarrier(QueueType newOwner) = 0; @@ -65,6 +90,8 @@ class Texture3D : public Texture { virtual void changeLayout(SeImageLayout newLayout, SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; virtual void generateMipmaps() = 0; + virtual PTextureView getDefaultView() const = 0; + virtual OTextureView createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) = 0; protected: // Inherited via QueueOwnedResource @@ -89,6 +116,8 @@ class TextureCube : public Texture { virtual void changeLayout(SeImageLayout newLayout, SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, SePipelineStageFlags dstStage) = 0; virtual void generateMipmaps() = 0; + virtual PTextureView getDefaultView() const = 0; + virtual OTextureView createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) = 0; protected: // Inherited via QueueOwnedResource diff --git a/src/Engine/Graphics/Vulkan/Descriptor.cpp b/src/Engine/Graphics/Vulkan/Descriptor.cpp index 4048369..b819057 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.cpp +++ b/src/Engine/Graphics/Vulkan/Descriptor.cpp @@ -325,11 +325,11 @@ void DescriptorSet::updateSampler(const std::string& mappingName, uint32 index, boundResources[binding][index] = vulkanSampler->getHandle(); } -void DescriptorSet::updateTexture(const std::string& mappingName, uint32 index, Gfx::PTexture texture) { - TextureBase* vulkanTexture = texture.cast().getHandle(); +void DescriptorSet::updateTexture(const std::string& mappingName, uint32 index, Gfx::PTextureView texture) { + PTextureView vulkanTexture = texture.cast(); const auto& map = owner->getLayout()->mappings[mappingName]; uint32 binding = map.binding; - if (boundResources[binding][index] == vulkanTexture->getHandle()) { + if (boundResources[binding][index] == vulkanTexture->getSource()) { return; } @@ -350,7 +350,7 @@ void DescriptorSet::updateTexture(const std::string& mappingName, uint32 index, .pImageInfo = &imageInfos.back(), }); - boundResources[binding][index] = vulkanTexture->getHandle(); + boundResources[binding][index] = vulkanTexture->getSource(); } void DescriptorSet::updateAccelerationStructure(const std::string& mappingName, uint32 index, Gfx::PTopLevelAS as) { diff --git a/src/Engine/Graphics/Vulkan/Descriptor.h b/src/Engine/Graphics/Vulkan/Descriptor.h index 23cf191..4a5ca04 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.h +++ b/src/Engine/Graphics/Vulkan/Descriptor.h @@ -64,7 +64,7 @@ class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource { virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PVertexBuffer indexBuffer) override; virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PIndexBuffer indexBuffer) override; virtual void updateSampler(const std::string& name, uint32 index, Gfx::PSampler samplerState) override; - virtual void updateTexture(const std::string& name, uint32 index, Gfx::PTexture texture) override; + virtual void updateTexture(const std::string& name, uint32 index, Gfx::PTextureView texture) override; virtual void updateAccelerationStructure(const std::string& name, uint32 index, Gfx::PTopLevelAS as) override; constexpr VkDescriptorSet getHandle() const { return setHandle; } diff --git a/src/Engine/Graphics/Vulkan/Framebuffer.cpp b/src/Engine/Graphics/Vulkan/Framebuffer.cpp index f7b20d6..670c653 100644 --- a/src/Engine/Graphics/Vulkan/Framebuffer.cpp +++ b/src/Engine/Graphics/Vulkan/Framebuffer.cpp @@ -17,7 +17,7 @@ Framebuffer::Framebuffer(PGraphics graphics, PRenderPass renderPass, Gfx::Render uint32 height = 0; uint32 layers = 0; for (auto inputAttachment : layout.inputAttachments) { - PTextureBase vkInputAttachment = inputAttachment.getTexture().cast(); + PTextureView vkInputAttachment = inputAttachment.getTextureView(); attachments.add(vkInputAttachment->getView()); description.inputAttachments[description.numInputAttachments++] = vkInputAttachment->getView(); width = std::max(width, vkInputAttachment->getWidth()); @@ -25,7 +25,7 @@ Framebuffer::Framebuffer(PGraphics graphics, PRenderPass renderPass, Gfx::Render layers = std::max(layers, vkInputAttachment->getNumLayers()); } for (auto colorAttachment : layout.colorAttachments) { - PTextureBase vkColorAttachment = colorAttachment.getTexture().cast(); + PTextureView vkColorAttachment = colorAttachment.getTextureView(); attachments.add(vkColorAttachment->getView()); description.colorAttachments[description.numColorAttachments++] = vkColorAttachment->getView(); width = std::max(width, vkColorAttachment->getWidth()); @@ -33,23 +33,23 @@ Framebuffer::Framebuffer(PGraphics graphics, PRenderPass renderPass, Gfx::Render layers = std::max(layers, vkColorAttachment->getNumLayers()); } for (auto resolveAttachment : layout.resolveAttachments) { - PTextureBase vkResolveAttachment = resolveAttachment.getTexture().cast(); + PTextureView vkResolveAttachment = resolveAttachment.getTextureView(); attachments.add(vkResolveAttachment->getView()); description.resolveAttachments[description.numResolveAttachments++] = vkResolveAttachment->getView(); width = std::max(width, vkResolveAttachment->getWidth()); height = std::max(height, vkResolveAttachment->getHeight()); layers = std::max(layers, vkResolveAttachment->getNumLayers()); } - if (layout.depthAttachment.getTexture() != nullptr) { - PTextureBase vkDepthAttachment = layout.depthAttachment.getTexture().cast(); + if (layout.depthAttachment.getTextureView() != nullptr) { + PTextureView vkDepthAttachment = layout.depthAttachment.getTextureView(); attachments.add(vkDepthAttachment->getView()); description.depthAttachment = vkDepthAttachment->getView(); width = std::max(width, vkDepthAttachment->getWidth()); height = std::max(height, vkDepthAttachment->getHeight()); layers = std::max(layers, vkDepthAttachment->getNumLayers()); } - if (layout.depthResolveAttachment.getTexture() != nullptr) { - PTextureBase vkDepthAttachment = layout.depthResolveAttachment.getTexture().cast(); + if (layout.depthResolveAttachment.getTextureView() != nullptr) { + PTextureView vkDepthAttachment = layout.depthResolveAttachment.getTextureView(); attachments.add(vkDepthAttachment->getView()); description.depthResolveAttachment = vkDepthAttachment->getView(); width = std::max(width, vkDepthAttachment->getWidth()); diff --git a/src/Engine/Graphics/Vulkan/RenderPass.cpp b/src/Engine/Graphics/Vulkan/RenderPass.cpp index 4fe0062..5d64728 100644 --- a/src/Engine/Graphics/Vulkan/RenderPass.cpp +++ b/src/Engine/Graphics/Vulkan/RenderPass.cpp @@ -22,7 +22,7 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra uint32 attachmentCounter = 0; for (auto& inputAttachment : layout.inputAttachments) { - PTexture2D image = inputAttachment.getTexture().cast(); + PTextureView image = inputAttachment.getTextureView(); VkAttachmentDescription2& desc = attachments.add() = { .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2, .pNext = nullptr, @@ -74,7 +74,7 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra .pNext = nullptr, .flags = 0, .format = cast(resolveAttachment.getFormat()), - .samples = (VkSampleCountFlagBits)resolveAttachment.getTexture()->getNumSamples(), + .samples = (VkSampleCountFlagBits)resolveAttachment.getTextureView()->getNumSamples(), .loadOp = cast(resolveAttachment.getLoadOp()), .storeOp = cast(resolveAttachment.getStoreOp()), .stencilLoadOp = cast(resolveAttachment.getStencilLoadOp()), @@ -95,8 +95,8 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, }; } - if (layout.depthAttachment.getTexture() != nullptr) { - PTexture2D image = layout.depthAttachment.getTexture().cast(); + if (layout.depthAttachment.getTextureView() != nullptr) { + PTextureView image = layout.depthAttachment.getTextureView(); attachments.add() = { .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2, .pNext = nullptr, @@ -123,8 +123,8 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, }; } - if (layout.depthResolveAttachment.getTexture() != nullptr) { - PTexture2D image = layout.depthResolveAttachment.getTexture().cast(); + if (layout.depthResolveAttachment.getTextureView() != nullptr) { + PTextureView image = layout.depthResolveAttachment.getTextureView(); attachments.add() = { .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2, .pNext = nullptr, @@ -159,7 +159,7 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra }; VkSubpassDescription2 subPassDesc = { .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2, - .pNext = layout.depthResolveAttachment.getTexture() != nullptr ? &depthResolve : nullptr, + .pNext = layout.depthResolveAttachment.getTextureView() != nullptr ? &depthResolve : nullptr, .flags = 0, .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, .viewMask = viewMasks[0], @@ -168,7 +168,7 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra .colorAttachmentCount = (uint32)colorRefs.size(), .pColorAttachments = colorRefs.data(), .pResolveAttachments = resolveRefs.size() > 0 ? resolveRefs.data() : nullptr, - .pDepthStencilAttachment = layout.depthAttachment.getTexture() != nullptr ? &depthRef : nullptr, + .pDepthStencilAttachment = layout.depthAttachment.getTextureView() != nullptr ? &depthRef : nullptr, }; Array dep; for (const auto& d : dependencies) { @@ -217,23 +217,23 @@ RenderPass::~RenderPass() { uint32 RenderPass::getFramebufferHash() { FramebufferDescription description; for (auto& inputAttachment : layout.inputAttachments) { - PTextureBase tex = inputAttachment.getTexture().cast(); + PTextureView tex = inputAttachment.getTextureView(); description.inputAttachments[description.numInputAttachments++] = tex->getView(); } for (auto& colorAttachment : layout.colorAttachments) { - PTextureBase tex = colorAttachment.getTexture().cast(); + PTextureView tex = colorAttachment.getTextureView(); description.colorAttachments[description.numColorAttachments++] = tex->getView(); } for (auto& resolveAttachment : layout.resolveAttachments) { - PTextureBase tex = resolveAttachment.getTexture().cast(); + PTextureView tex = resolveAttachment.getTextureView(); description.resolveAttachments[description.numResolveAttachments++] = tex->getView(); } - if (layout.depthAttachment.getTexture() != nullptr) { - PTextureBase tex = layout.depthAttachment.getTexture().cast(); + if (layout.depthAttachment.getTextureView() != nullptr) { + PTextureView tex = layout.depthAttachment.getTextureView(); description.depthAttachment = tex->getView(); } - if (layout.depthResolveAttachment.getTexture() != nullptr) { - PTextureBase tex = layout.depthResolveAttachment.getTexture().cast(); + if (layout.depthResolveAttachment.getTextureView() != nullptr) { + PTextureView tex = layout.depthResolveAttachment.getTextureView(); description.depthResolveAttachment = tex->getView(); } return CRC::Calculate(&description, sizeof(FramebufferDescription), CRC::CRC_32()); @@ -241,23 +241,23 @@ uint32 RenderPass::getFramebufferHash() { void RenderPass::endRenderPass() { for (auto& inputAttachment : layout.inputAttachments) { - PTextureBase tex = inputAttachment.getTexture().cast(); + PTextureView tex = inputAttachment.getTextureView(); tex->setLayout(inputAttachment.getFinalLayout()); } for (auto& colorAttachment : layout.colorAttachments) { - PTextureBase tex = colorAttachment.getTexture().cast(); + PTextureView tex = colorAttachment.getTextureView(); tex->setLayout(colorAttachment.getFinalLayout()); } for (auto& resolveAttachment : layout.resolveAttachments) { - PTextureBase tex = resolveAttachment.getTexture().cast(); + PTextureView tex = resolveAttachment.getTextureView(); tex->setLayout(resolveAttachment.getFinalLayout()); } - if (layout.depthAttachment.getTexture() != nullptr) { - PTextureBase tex = layout.depthAttachment.getTexture().cast(); + if (layout.depthAttachment.getTextureView() != nullptr) { + PTextureView tex = layout.depthAttachment.getTextureView(); tex->setLayout(layout.depthAttachment.getFinalLayout()); } - if (layout.depthResolveAttachment.getTexture() != nullptr) { - PTextureBase tex = layout.depthResolveAttachment.getTexture().cast(); + if (layout.depthResolveAttachment.getTextureView() != nullptr) { + PTextureView tex = layout.depthResolveAttachment.getTextureView(); tex->setLayout(layout.depthResolveAttachment.getFinalLayout()); } } diff --git a/src/Engine/Graphics/Vulkan/Texture.cpp b/src/Engine/Graphics/Vulkan/Texture.cpp index d70f52a..c615c5b 100644 --- a/src/Engine/Graphics/Vulkan/Texture.cpp +++ b/src/Engine/Graphics/Vulkan/Texture.cpp @@ -28,11 +28,65 @@ VkImageAspectFlags getAspectFromFormat(Gfx::SeFormat format) { } } +TextureView::TextureView(PTextureHandle source, VkImageView view) : source(source), view(view) {} + +TextureView::~TextureView() {} + +Gfx::SeFormat TextureView::getFormat() const { return source->format; } + +uint32 TextureView::getWidth() const { return source->width; } + +uint32 TextureView::getHeight() const { return source->height; } + +uint32 TextureView::getDepth() const { return source->depth; } + +uint32 TextureView::getNumLayers() const { return source->layerCount; } + +Gfx::SeSampleCountFlags TextureView::getNumSamples() const { return source->samples; } + +uint32 TextureView::getMipLevels() const { return source->mipLevels; } + +Gfx::SeImageLayout TextureView::getLayout() const { return source->layout; } + +void TextureView::pipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess, + VkPipelineStageFlags dstStage) { + return source->pipelineBarrier(srcAccess, srcStage, dstAccess, dstStage); +} + +void TextureView::changeLayout(Gfx::SeImageLayout newLayout, VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, + VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) { + return source->changeLayout(newLayout, srcAccess, srcStage, dstAccess, dstStage); +} + +void TextureView::setLayout(Gfx::SeImageLayout layout) { source->layout = layout; } + +Gfx::OTextureView TextureHandle::createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) { + VkImageView view; + VkImageViewCreateInfo createInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .image = image, + .viewType = viewType, + .format = cast(format), + .subresourceRange = + { + .aspectMask = aspect, + .baseMipLevel = baseMipLevel, + .levelCount = levelCount, + .baseArrayLayer = baseArrayLayer, + .layerCount = layerCount, + }, + }; + vkCreateImageView(graphics->getDevice(), &createInfo, nullptr, &view); + return new TextureView(this, view); +} + TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const TextureCreateInfo& createInfo, VkImage existingImage) : CommandBoundResource(graphics, createInfo.name), image(existingImage), imageView(VK_NULL_HANDLE), allocation(nullptr), owner(createInfo.sourceData.owner), width(createInfo.width), height(createInfo.height), depth(createInfo.depth), - layerCount(createInfo.elements), mipLevels(1), samples(createInfo.samples), format(createInfo.format), - usage(createInfo.usage), layout(Gfx::SE_IMAGE_LAYOUT_UNDEFINED), aspect(getAspectFromFormat(createInfo.format)), ownsImage(false) { + layerCount(createInfo.elements), mipLevels(1), samples(createInfo.samples), format(createInfo.format), usage(createInfo.usage), + layout(Gfx::SE_IMAGE_LAYOUT_UNDEFINED), aspect(getAspectFromFormat(createInfo.format)), viewType(viewType), ownsImage(false) { if (createInfo.useMip) { mipLevels = static_cast(std::floor(std::log2(std::max(width, height)))) + 1; } @@ -151,7 +205,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const graphics->getDestructionManager()->queueResourceForDestruction(std::move(stagingAlloc)); } } - + VkImageView view; VkImageViewCreateInfo viewInfo = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .pNext = nullptr, @@ -167,11 +221,11 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const }, }; - VK_CHECK(vkCreateImageView(graphics->getDevice(), &viewInfo, nullptr, &imageView)); + VK_CHECK(vkCreateImageView(graphics->getDevice(), &viewInfo, nullptr, &view)); + imageView = new TextureView(this, view); } TextureHandle::~TextureHandle() { - vkDestroyImageView(graphics->getDevice(), imageView, nullptr); if (ownsImage) { vmaDestroyImage(graphics->getAllocator(), image, allocation); } @@ -422,6 +476,12 @@ void Texture2D::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array< void Texture2D::generateMipmaps() { TextureBase::generateMipmaps(); } +Gfx::PTextureView Texture2D::getDefaultView() const { return PTextureView(handle->imageView); } + +Gfx::OTextureView Texture2D::createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) { + return handle->createTextureView(baseMipLevel, levelCount, baseArrayLayer, layerCount); +} + void Texture2D::executeOwnershipBarrier(Gfx::QueueType newOwner) { TextureBase::transferOwnership(newOwner); } void Texture2D::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess, @@ -445,6 +505,12 @@ void Texture3D::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array< void Texture3D::generateMipmaps() { TextureBase::generateMipmaps(); } +Gfx::PTextureView Texture3D::getDefaultView() const { return PTextureView(handle->imageView); } + +Gfx::OTextureView Texture3D::createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) { + return handle->createTextureView(baseMipLevel, levelCount, baseArrayLayer, layerCount); +} + void Texture3D::executeOwnershipBarrier(Gfx::QueueType newOwner) { TextureBase::transferOwnership(newOwner); } void Texture3D::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess, @@ -469,6 +535,12 @@ void TextureCube::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Arra void TextureCube::generateMipmaps() { TextureBase::generateMipmaps(); } +Gfx::PTextureView TextureCube::getDefaultView() const { return PTextureView(handle->imageView); } + +Gfx::OTextureView TextureCube::createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) { + return handle->createTextureView(baseMipLevel, levelCount, baseArrayLayer, layerCount); +} + void TextureCube::executeOwnershipBarrier(Gfx::QueueType newOwner) { TextureBase::transferOwnership(newOwner); } void TextureCube::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess, diff --git a/src/Engine/Graphics/Vulkan/Texture.h b/src/Engine/Graphics/Vulkan/Texture.h index 46a933a..47dc303 100644 --- a/src/Engine/Graphics/Vulkan/Texture.h +++ b/src/Engine/Graphics/Vulkan/Texture.h @@ -8,6 +8,32 @@ namespace Seele { namespace Vulkan { +DECLARE_REF(TextureHandle) +class TextureView : public Gfx::TextureView { +public: + TextureView(PTextureHandle source, VkImageView view); + virtual ~TextureView(); + virtual Gfx::SeFormat getFormat() const override; + virtual uint32 getWidth() const override; + virtual uint32 getHeight() const override; + virtual uint32 getDepth() const override; + virtual uint32 getNumLayers() const override; + virtual Gfx::SeSampleCountFlags getNumSamples() const override; + virtual uint32 getMipLevels() const override; + virtual void pipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) override; + virtual void changeLayout(Gfx::SeImageLayout newLayout, VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess, + VkPipelineStageFlags dstStage) override; + VkImageView getView() const { return view; } + Gfx::SeImageLayout getLayout() const; + PTextureHandle getSource() const { return source; } + void setLayout(Gfx::SeImageLayout layout); +private: + PTextureHandle source; + VkImageView view; + friend class TextureBase; +}; +DEFINE_REF(TextureView) + class TextureHandle : public CommandBoundResource { public: TextureHandle(PGraphics graphics, VkImageViewType viewType, const TextureCreateInfo& createInfo, VkImage existingImage); @@ -18,8 +44,10 @@ class TextureHandle : public CommandBoundResource { VkPipelineStageFlags dstStage); void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array& buffer); void generateMipmaps(); + Gfx::OTextureView createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount); + VkImage image; - VkImageView imageView; + OTextureView imageView; VmaAllocation allocation; Gfx::QueueType owner; uint32 width; @@ -32,6 +60,7 @@ class TextureHandle : public CommandBoundResource { Gfx::SeImageUsageFlags usage; Gfx::SeImageLayout layout; VkImageAspectFlags aspect; + VkImageViewType viewType; uint8 ownsImage; }; DEFINE_REF(TextureHandle) @@ -50,7 +79,7 @@ class TextureBase { uint32 getNumLayers() const { return handle->layerCount; } PTextureHandle getHandle() const { return handle; } VkImage getImage() const { return handle->image; }; - VkImageView getView() const { return handle->imageView; }; + VkImageView getView() const { return handle->imageView->view; }; constexpr Gfx::SeImageLayout getLayout() const { return handle->layout; } void setLayout(Gfx::SeImageLayout val) { handle->layout = val; } constexpr VkImageAspectFlags getAspect() const { return handle->aspect; } @@ -91,6 +120,8 @@ class Texture2D : public Gfx::Texture2D, public TextureBase { Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) override; virtual void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array& buffer) override; virtual void generateMipmaps() override; + virtual Gfx::PTextureView getDefaultView() const override; + virtual Gfx::OTextureView createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) override; protected: // Inherited via QueueOwnedResource @@ -115,6 +146,8 @@ class Texture3D : public Gfx::Texture3D, public TextureBase { Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) override; virtual void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array& buffer) override; virtual void generateMipmaps() override; + virtual Gfx::PTextureView getDefaultView() const override; + virtual Gfx::OTextureView createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) override; protected: // Inherited via QueueOwnedResource @@ -139,6 +172,8 @@ class TextureCube : public Gfx::TextureCube, public TextureBase { Gfx::SeAccessFlags dstAccess, Gfx::SePipelineStageFlags dstStage) override; virtual void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array& buffer) override; virtual void generateMipmaps() override; + virtual Gfx::PTextureView getDefaultView() const override; + virtual Gfx::OTextureView createTextureView(uint32 baseMipLevel, uint32 levelCount, uint32 baseArrayLayer, uint32 layerCount) override; protected: // Inherited via QueueOwnedResource diff --git a/src/Engine/Material/Material.cpp b/src/Engine/Material/Material.cpp index 40f771d..1626787 100644 --- a/src/Engine/Material/Material.cpp +++ b/src/Engine/Material/Material.cpp @@ -78,7 +78,7 @@ void Material::updateDescriptor() { set = layout->allocateDescriptorSet(); for (uint32 i = 0; i < textures.size(); ++i) { if (textures[i] != nullptr) { - set->updateTexture("textures", i, textures[i]); + set->updateTexture("textures", i, textures[i]->getDefaultView()); } } for (uint32 i = 0; i < samplers.size(); ++i) { diff --git a/src/Engine/Scene/LightEnvironment.cpp b/src/Engine/Scene/LightEnvironment.cpp index 0691e9b..8364922 100644 --- a/src/Engine/Scene/LightEnvironment.cpp +++ b/src/Engine/Scene/LightEnvironment.cpp @@ -122,11 +122,11 @@ void LightEnvironment::commit() { uint32 numDirectionalLights = (uint32)dirs.size(); set->updateConstants("numDirectionalLights", 0, &numDirectionalLights); set->updateBuffer("directionalLights", 0, directionalLights); - set->updateTexture("shadowMap", 0, Gfx::PTexture2D(shadowMaps[0])); + set->updateTexture("shadowMap", 0, shadowMaps[0]->getDefaultView()); set->updateSampler("shadowSampler", 0, Gfx::PSampler(shadowSamplers[0])); set->updateConstants("numPointLights", 0, &numPointLights); set->updateBuffer("pointLights", 0, pointLights); - set->updateTexture("irradianceMap", 0, environment->getIrradianceMap()); + set->updateTexture("irradianceMap", 0, environment->getIrradianceMap()->getDefaultView()); set->updateSampler("irradianceSampler", 0, environmentSampler); set->writeChanges(); } diff --git a/src/Engine/Scene/LightEnvironment.h b/src/Engine/Scene/LightEnvironment.h index 9e2ae3f..31de620 100644 --- a/src/Engine/Scene/LightEnvironment.h +++ b/src/Engine/Scene/LightEnvironment.h @@ -4,6 +4,7 @@ #include "Component/Transform.h" #include "Graphics/Buffer.h" #include "Graphics/Descriptor.h" +#include "Graphics/Texture.h" namespace Seele { DECLARE_REF(EnvironmentMapAsset)