Irradiance works

This commit is contained in:
Dynamitos
2025-04-06 09:57:47 +02:00
parent aa1f037cb5
commit f21606379c
28 changed files with 524 additions and 401 deletions
+5 -1
View File
@@ -11,8 +11,12 @@ class EnvironmentMapAsset : public Asset {
virtual ~EnvironmentMapAsset();
virtual void save(ArchiveBuffer& buffer) const override;
virtual void load(ArchiveBuffer& buffer) override;
Gfx::PTextureCube getSkybox() const { return skybox; }
Gfx::PTextureCube getIrradianceMap() const { return irradianceMap; }
Gfx::PTextureCube getSpecularMap() const { return specularMap; }
private:
Gfx::OTextureCube diffuseMap;
Gfx::OTextureCube skybox;
Gfx::OTextureCube irradianceMap;
Gfx::OTextureCube specularMap;
friend class EnvironmentLoader;
};
-1
View File
@@ -45,7 +45,6 @@ void TextureAsset::load(ArchiveBuffer& buffer) {
.width = ktxHandle->baseWidth,
.height = ktxHandle->baseHeight,
.depth = ktxHandle->baseDepth,
.layers = ktxHandle->numFaces,
.elements = ktxHandle->numLayers,
.useMip = true,
.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
+1 -1
View File
@@ -56,7 +56,7 @@ class Graphics {
virtual OViewport createViewport(PWindow owner, const ViewportCreateInfo& createInfo) = 0;
virtual ORenderPass createRenderPass(RenderTargetLayout layout, Array<SubPassDependency> dependencies, URect renderArea,
std::string name = "", Array<uint32> viewMasks = {}, Array<uint32> correlationMasks = {}) = 0;
std::string name = "", Array<uint32> viewMasks = {0}, Array<uint32> correlationMasks = {}) = 0;
virtual void beginRenderPass(PRenderPass renderPass) = 0;
virtual void endRenderPass() = 0;
virtual void waitDeviceIdle() = 0;
-1
View File
@@ -49,7 +49,6 @@ struct TextureCreateInfo {
uint32 width = 1;
uint32 height = 1;
uint32 depth = 1;
uint32 layers = 1;
uint32 elements = 1;
uint32 samples = 1;
bool useMip = false;
+3 -2
View File
@@ -1,6 +1,7 @@
#include "BasePass.h"
#include "Actor/CameraActor.h"
#include "Asset/AssetRegistry.h"
#include "Asset/EnvironmentMapAsset.h"
#include "Component/Camera.h"
#include "Component/Mesh.h"
#include "Component/WaterTile.h"
@@ -78,8 +79,8 @@ BasePass::BasePass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics)
});
}
skybox = Seele::Component::Skybox{
.day = AssetRegistry::findTexture("", "skybox")->getTexture().cast<Gfx::TextureCube>(),
.night = AssetRegistry::findTexture("", "skybox")->getTexture().cast<Gfx::TextureCube>(),
.day = scene->getLightEnvironment()->getEnvironmentMap()->getIrradianceMap(),
.night = scene->getLightEnvironment()->getEnvironmentMap()->getIrradianceMap(),
.fogColor = Vector(0.1, 0.1, 0.8),
.blendFactor = 0,
};
-1
View File
@@ -108,7 +108,6 @@ class RenderPass {
RenderPass(RenderPass&&) = default;
RenderPass& operator=(RenderPass&&) = default;
const RenderTargetLayout& getLayout() const { return layout; }
void updateColorAttachment(uint32 index, Gfx::PTextureCube attachment) { layout.colorAttachments[index].setTexture(attachment); }
protected:
RenderTargetLayout layout;
@@ -16,12 +16,14 @@ Framebuffer::Framebuffer(PGraphics graphics, PRenderPass renderPass, Gfx::Render
Array<VkImageView> attachments;
uint32 width = 0;
uint32 height = 0;
uint32 layers = 0;
for (auto inputAttachment : layout.inputAttachments) {
PTextureBase vkInputAttachment = inputAttachment.getTexture().cast<TextureBase>();
attachments.add(vkInputAttachment->getView());
description.inputAttachments[description.numInputAttachments++] = vkInputAttachment->getView();
width = std::max(width, vkInputAttachment->getWidth());
height = std::max(height, vkInputAttachment->getHeight());
layers = std::max(layers, vkInputAttachment->getNumLayers());
}
for (auto colorAttachment : layout.colorAttachments) {
PTextureBase vkColorAttachment = colorAttachment.getTexture().cast<TextureBase>();
@@ -29,6 +31,7 @@ Framebuffer::Framebuffer(PGraphics graphics, PRenderPass renderPass, Gfx::Render
description.colorAttachments[description.numColorAttachments++] = vkColorAttachment->getView();
width = std::max(width, vkColorAttachment->getWidth());
height = std::max(height, vkColorAttachment->getHeight());
layers = std::max(layers, vkColorAttachment->getNumLayers());
}
for (auto resolveAttachment : layout.resolveAttachments) {
PTextureBase vkResolveAttachment = resolveAttachment.getTexture().cast<TextureBase>();
@@ -36,6 +39,7 @@ Framebuffer::Framebuffer(PGraphics graphics, PRenderPass renderPass, Gfx::Render
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<TextureBase>();
@@ -43,6 +47,7 @@ Framebuffer::Framebuffer(PGraphics graphics, PRenderPass renderPass, Gfx::Render
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<TextureBase>();
@@ -50,6 +55,7 @@ Framebuffer::Framebuffer(PGraphics graphics, PRenderPass renderPass, Gfx::Render
description.depthResolveAttachment = vkDepthAttachment->getView();
width = std::max(width, vkDepthAttachment->getWidth());
height = std::max(height, vkDepthAttachment->getHeight());
layers = std::max(layers, vkDepthAttachment->getNumLayers());
}
VkFramebufferCreateInfo createInfo = {
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
+5 -14
View File
@@ -9,8 +9,8 @@
using namespace Seele;
using namespace Seele::Vulkan;
RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Array<Gfx::SubPassDependency> _dependencies,
URect viewport, std::string name, Array<uint32> viewMasks, Array<uint32> correlationMasks)
RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Array<Gfx::SubPassDependency> _dependencies, URect viewport,
std::string name, Array<uint32> viewMasks, Array<uint32> correlationMasks)
: Gfx::RenderPass(std::move(_layout), std::move(_dependencies)), graphics(graphics) {
renderArea.extent.width = viewport.size.x;
renderArea.extent.height = viewport.size.y;
@@ -166,6 +166,7 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra
.pNext = layout.depthResolveAttachment.getTexture() != nullptr ? &depthResolve : nullptr,
.flags = 0,
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.viewMask = viewMasks[0],
.inputAttachmentCount = (uint32)inputRefs.size(),
.pInputAttachments = inputRefs.data(),
.colorAttachmentCount = (uint32)colorRefs.size(),
@@ -196,19 +197,9 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra
.pSubpasses = &subPassDesc,
.dependencyCount = (uint32)dep.size(),
.pDependencies = dep.data(),
.correlatedViewMaskCount = (uint32)correlationMasks.size(),
.pCorrelatedViewMasks = correlationMasks.data(),
};
VkRenderPassMultiviewCreateInfo multiViewInfo;
if (!viewMasks.empty()) {
multiViewInfo = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO,
.pNext = nullptr,
.subpassCount = 1,
.pViewMasks = viewMasks.data(),
.correlationMaskCount = (uint32)correlationMasks.size(),
.pCorrelationMasks = correlationMasks.data(),
};
info.pNext = &multiViewInfo;
}
VK_CHECK(vkCreateRenderPass2(graphics->getDevice(), &info, nullptr, &renderPass));
VkDebugUtilsObjectNameInfoEXT nameInfo = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
+7 -7
View File
@@ -31,7 +31,7 @@ VkImageAspectFlags getAspectFromFormat(Gfx::SeFormat format) {
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),
arrayCount(createInfo.elements), layerCount(createInfo.layers), mipLevels(1), samples(createInfo.samples), format(createInfo.format),
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) {
if (createInfo.useMip) {
mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(width, height)))) + 1;
@@ -55,7 +55,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const
case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
type = VK_IMAGE_TYPE_2D;
flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
layerCount = 6 * arrayCount;
layerCount = 6 * layerCount;
break;
default:
break;
@@ -76,7 +76,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const
.depth = depth,
},
.mipLevels = mipLevels,
.arrayLayers = arrayCount * layerCount,
.arrayLayers = layerCount,
.samples = (VkSampleCountFlagBits)samples,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = usage,
@@ -129,7 +129,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.mipLevel = 0,
.baseArrayLayer = 0,
.layerCount = arrayCount * layerCount,
.layerCount = layerCount,
},
.imageOffset =
{
@@ -163,7 +163,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const
{
.aspectMask = aspect,
.levelCount = mipLevels,
.layerCount = layerCount * arrayCount,
.layerCount = layerCount,
},
};
@@ -195,7 +195,7 @@ void TextureHandle::pipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlag
.baseMipLevel = 0,
.levelCount = mipLevels,
.baseArrayLayer = 0,
.layerCount = layerCount * arrayCount,
.layerCount = layerCount,
},
};
PCommand command = graphics->getQueueCommands(owner)->getCommands();
@@ -223,7 +223,7 @@ void TextureHandle::transferOwnership(Gfx::QueueType newOwner) {
.baseMipLevel = 0,
.levelCount = mipLevels,
.baseArrayLayer = 0,
.layerCount = arrayCount,
.layerCount = layerCount,
},
};
PCommandPool sourcePool = graphics->getQueueCommands(owner);
+1 -1
View File
@@ -25,7 +25,6 @@ class TextureHandle : public CommandBoundResource {
uint32 width;
uint32 height;
uint32 depth;
uint32 arrayCount;
uint32 layerCount;
uint32 mipLevels;
uint32 samples;
@@ -48,6 +47,7 @@ class TextureBase {
uint32 getWidth() const { return handle->width; }
uint32 getHeight() const { return handle->height; }
uint32 getDepth() const { return handle->depth; }
uint32 getNumLayers() const { return handle->layerCount; }
PTextureHandle getHandle() const { return handle; }
VkImage getImage() const { return handle->image; };
VkImageView getView() const { return handle->imageView; };
-1
View File
@@ -276,7 +276,6 @@ void Window::createSwapChain() {
.width = extent.width,
.height = extent.height,
.depth = 1,
.layers = 1,
.elements = 1,
.samples = 1,
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_TRANSFER_DST_BIT,
+6 -1
View File
@@ -9,7 +9,12 @@ Window::~Window() {}
Viewport::Viewport(PWindow owner, const ViewportCreateInfo& viewportInfo)
: sizeX(viewportInfo.dimensions.size.x), sizeY(viewportInfo.dimensions.size.y), offsetX(viewportInfo.dimensions.offset.x),
offsetY(viewportInfo.dimensions.offset.y), fieldOfView(viewportInfo.fieldOfView), owner(owner) {}
offsetY(viewportInfo.dimensions.offset.y), fieldOfView(viewportInfo.fieldOfView), owner(owner) {
if (owner != nullptr) {
sizeX = std::min(owner->getFramebufferWidth(), sizeX);
sizeY = std::min(owner->getFramebufferHeight(), sizeY);
}
}
Viewport::~Viewport() {}
+1 -1
View File
@@ -158,8 +158,8 @@ void Material::load(ArchiveBuffer& buffer) {
void Material::compile() {
std::ofstream codeStream("./shaders/generated/" + materialName + ".slang");
codeStream << "import BRDF;\n";
codeStream << "import MaterialParameter;\n";
codeStream << "import LightEnv;\n";
codeStream << "import Material;\n";
codeStream << "struct Material{\n";
codeStream << "\ttypedef " << brdf.profile << " BRDF;\n";
+16 -1
View File
@@ -1,9 +1,12 @@
#include "LightEnvironment.h"
#include "Asset/EnvironmentMapAsset.h"
#include "Graphics/Graphics.h"
#include "Asset/AssetRegistry.h"
using namespace Seele;
LightEnvironment::LightEnvironment(Gfx::PGraphics graphics) : graphics(graphics) {
LightEnvironment::LightEnvironment(Gfx::PGraphics graphics)
: graphics(graphics), environment(AssetRegistry::findEnvironmentMap("", "newport_loft")) {
layout = graphics->createDescriptorLayout("pLightEnv");
layout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "directionalLights",
@@ -21,6 +24,10 @@ LightEnvironment::LightEnvironment(Gfx::PGraphics graphics) : graphics(graphics)
.name = "numPointLights",
.uniformLength = sizeof(uint32),
});
layout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "irradianceMap",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
});
layout->create();
directionalLights = graphics->createShaderBuffer(ShaderBufferCreateInfo{
@@ -29,6 +36,13 @@ LightEnvironment::LightEnvironment(Gfx::PGraphics graphics) : graphics(graphics)
pointLights = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.name = "PointLights",
});
environmentSampler = graphics->createSampler({
.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,
.addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
});
}
LightEnvironment::~LightEnvironment() {}
@@ -63,6 +77,7 @@ void LightEnvironment::commit() {
set->updateBuffer("pointLights", 0, pointLights);
set->updateConstants("numDirectionalLights", 0, &numDirectionalLights);
set->updateBuffer("directionalLights", 0, directionalLights);
set->updateTexture("irradianceMap", 0, environment->getIrradianceMap());
set->writeChanges();
}
+4 -1
View File
@@ -4,8 +4,8 @@
#include "Graphics/Buffer.h"
#include "Graphics/Descriptor.h"
namespace Seele {
DECLARE_REF(EnvironmentMapAsset)
class LightEnvironment {
public:
LightEnvironment(Gfx::PGraphics graphics);
@@ -16,12 +16,15 @@ class LightEnvironment {
void commit();
const Gfx::PDescriptorLayout getDescriptorLayout() const;
Gfx::PDescriptorSet getDescriptorSet();
PEnvironmentMapAsset getEnvironmentMap() { return environment; }
private:
Gfx::OShaderBuffer directionalLights;
Gfx::OShaderBuffer pointLights;
Array<Component::DirectionalLight> dirs;
Array<Component::PointLight> points;
PEnvironmentMapAsset environment;
Gfx::OSampler environmentSampler;
Gfx::ODescriptorLayout layout;
Gfx::PDescriptorSet set;
Gfx::PGraphics graphics;
+2 -2
View File
@@ -1,6 +1,5 @@
#include "Scene.h"
#include "Actor/PointLightActor.h"
#include "Asset/AssetRegistry.h"
#include "Asset/MaterialAsset.h"
#include "Asset/TextureAsset.h"
#include "Component/DirectionalLight.h"
@@ -13,7 +12,8 @@
using namespace Seele;
Scene::Scene(Gfx::PGraphics graphics) : graphics(graphics), lightEnv(new LightEnvironment(graphics)), physics(registry) {}
Scene::Scene(Gfx::PGraphics graphics)
: graphics(graphics), lightEnv(new LightEnvironment(graphics)), physics(registry) {}
Scene::~Scene() {}
+1 -1
View File
@@ -1,8 +1,8 @@
#pragma once
#include "MinimalEngine.h"
#include "Component/Skybox.h"
#include "Graphics/Graphics.h"
#include "LightEnvironment.h"
#include "MinimalEngine.h"
#include "Physics/PhysicsSystem.h"
#include <entt/entt.hpp>
#include <iostream>