Initial environment loading (not working)

This commit is contained in:
Dynamitos
2025-04-04 14:44:53 +02:00
parent 4a34220cd7
commit aa1f037cb5
41 changed files with 553 additions and 208 deletions
+10 -1
View File
@@ -4,7 +4,7 @@
#include "MaterialLoader.h"
#include "MeshLoader.h"
#include "TextureLoader.h"
#include "EnvironmentLoader.h"
using namespace Seele;
@@ -40,12 +40,21 @@ void AssetImporter::importMaterial(MaterialImportArgs args) {
get().materialLoader->importAsset(args);
}
void Seele::AssetImporter::importEnvironmentMap(EnvironmentImportArgs args) {
if (get().registry->getOrCreateFolder(args.importPath)->materials.contains(args.filePath.stem().string())) {
// skip importing duplicates
return;
}
get().environmentLoader->importAsset(args);
}
void AssetImporter::init(Gfx::PGraphics graphics) {
get().registry = AssetRegistry::getInstance();
get().meshLoader = new MeshLoader(graphics);
get().textureLoader = new TextureLoader(graphics);
get().materialLoader = new MaterialLoader(graphics);
get().fontLoader = new FontLoader(graphics);
get().environmentLoader = new EnvironmentLoader(graphics);
}
AssetImporter& AssetImporter::get() {
+4 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include "Asset/AssetRegistry.h"
#include "MinimalEngine.h"
#include "Asset/AssetRegistry.h"
namespace Seele {
@@ -8,12 +8,14 @@ DECLARE_REF(TextureLoader)
DECLARE_REF(FontLoader)
DECLARE_REF(MeshLoader)
DECLARE_REF(MaterialLoader)
DECLARE_REF(EnvironmentLoader)
class AssetImporter {
public:
static void importMesh(struct MeshImportArgs args);
static void importTexture(struct TextureImportArgs args);
static void importFont(struct FontImportArgs args);
static void importMaterial(struct MaterialImportArgs args);
static void importEnvironmentMap(struct EnvironmentImportArgs args);
static void init(Gfx::PGraphics graphics);
private:
@@ -22,6 +24,7 @@ class AssetImporter {
UPFontLoader fontLoader;
UPMeshLoader meshLoader;
UPMaterialLoader materialLoader;
UPEnvironmentLoader environmentLoader;
AssetRegistry* registry;
};
} // namespace Seele
+2
View File
@@ -2,6 +2,8 @@ target_sources(Editor
PRIVATE
AssetImporter.h
AssetImporter.cpp
EnvironmentLoader.h
EnvironmentLoader.cpp
FontLoader.h
FontLoader.cpp
MaterialLoader.h
+147
View File
@@ -0,0 +1,147 @@
#include "EnvironmentLoader.h"
#include "Asset/AssetRegistry.h"
#include "Asset/EnvironmentMapAsset.h"
#include "Graphics/Descriptor.h"
#include "stb_image.h"
using namespace Seele;
EnvironmentLoader::EnvironmentLoader(Gfx::PGraphics graphics) : graphics(graphics) {
cubeRenderViewport = graphics->createViewport(nullptr, ViewportCreateInfo{
.dimensions =
{
.size = {1024, 1024},
.offset = {0, 0},
},
});
cubeRenderLayout = graphics->createDescriptorLayout("pViewParams");
cubeRenderLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "view",
.uniformLength = sizeof(Matrix4) * 6,
});
cubeRenderLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "projection",
.uniformLength = sizeof(Matrix4),
});
cubeRenderLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "equirectangularMap",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
});
cubeRenderLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "sampler",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
});
cubeRenderLayout->create();
cubePipelineLayout = graphics->createPipelineLayout("CubeRenderLayout");
cubePipelineLayout->addDescriptorLayout(cubeRenderLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "CubeRenderPipeline",
.modules = {"EnvironmentMapping"},
.entryPoints =
{
{"vertMain", "EnvironmentMapping"},
{"fragMain", "EnvironmentMapping"},
},
.rootSignature = cubePipelineLayout,
});
cubePipelineLayout->create();
cubeRenderVertex = graphics->createVertexShader({0});
cubeRenderFrag = graphics->createFragmentShader({1});
cubeSampler = 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,
});
}
EnvironmentLoader::~EnvironmentLoader() {}
void EnvironmentLoader::importAsset(EnvironmentImportArgs args) {
std::filesystem::path assetPath = args.filePath.filename();
assetPath.replace_extension("asset");
OEnvironmentMapAsset asset = new EnvironmentMapAsset(args.importPath, assetPath.stem().string());
asset->setStatus(Asset::Status::Loading);
// the registry takes ownership, but we need to edit the reference
PEnvironmentMapAsset ref = asset;
AssetRegistry::get().registerEnvironmentMap(std::move(asset));
import(args, ref);
}
void EnvironmentLoader::import(EnvironmentImportArgs args, PEnvironmentMapAsset asset) {
stbi_set_flip_vertically_on_load(true);
int width, height, nrComponents;
float* data = stbi_loadf(args.filePath.string().c_str(), &width, &height, &nrComponents, 4);
stbi_set_flip_vertically_on_load(false);
assert(data);
Gfx::OTexture2D hdrTexture = graphics->createTexture2D(TextureCreateInfo{
.sourceData =
{
.size = width * height * 4 * sizeof(float),
.data = (uint8*)data,
},
.format = Gfx::SE_FORMAT_R32G32B32A32_SFLOAT,
.width = (uint32)width,
.height = (uint32)height,
.name = "HDRRaw",
});
Matrix4 captureProjection = glm::perspective(glm::radians(90.0f), 1.0f, 0.1f, 10.0f);
Matrix4 captureViews[] = {
glm::lookAt(Vector(0.0f, 0.0f, 0.0f), Vector(0.0f, 0.0f, 1.0f), Vector(0.0f, 1.0f, 0.0f)),
glm::lookAt(Vector(0.0f, 0.0f, 0.0f), Vector(0.0f, 0.0f, -1.0f), Vector(0.0f, 1.0f, 0.0f)),
glm::lookAt(Vector(0.0f, 0.0f, 0.0f), Vector(0.0f, -1.0f, 0.0f), Vector(1.0f, 0.0f, 0.0f)),
glm::lookAt(Vector(0.0f, 0.0f, 0.0f), Vector(0.0f, 1.0f, 0.0f), Vector(1.0f, 0.0f, 0.0f)),
glm::lookAt(Vector(0.0f, 0.0f, 0.0f), Vector(-1.0f, 0.0f, 0.0f), Vector(0.0f, 1.0f, 0.0f)),
glm::lookAt(Vector(0.0f, 0.0f, 0.0f), Vector(1.0f, 0.0f, 0.0f), Vector(0.0f, 1.0f, 0.0f)),
};
Gfx::PDescriptorSet set = cubeRenderLayout->allocateDescriptorSet();
set->updateConstants("view", 0, captureViews);
set->updateConstants("projection", 0, &captureProjection);
set->updateTexture("equirectangularMap", 0, Gfx::PTexture2D(hdrTexture));
set->updateSampler("sampler", 0, cubeSampler);
set->writeChanges();
Gfx::OTextureCube cubeMap = graphics->createTextureCube(TextureCreateInfo{
.format = Gfx::SE_FORMAT_R32G32B32A32_SFLOAT,
.width = 1024,
.height = 1024,
.usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
});
cubeRenderPass = graphics->createRenderPass(
Gfx::RenderTargetLayout{
.colorAttachments = {Gfx::RenderTargetAttachment(cubeMap, 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)},
},
{},
URect{
.size = {1024, 1024},
.offset = {0, 0},
},
"EnvironmentRenderPass", {0b111111}, {0b111111});
cubeRenderPipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
.vertexShader = cubeRenderVertex,
.fragmentShader = cubeRenderFrag,
.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, 0, 0);
graphics->executeCommands(std::move(renderCommand));
graphics->endRenderPass();
graphics->waitDeviceIdle();
asset->diffuseMap = std::move(cubeMap);
}
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "MinimalEngine.h"
#include "Graphics/Graphics.h"
#include "Graphics/Shader.h"
#include <filesystem>
namespace Seele
{
DECLARE_REF(EnvironmentMapAsset)
struct EnvironmentImportArgs {
std::filesystem::path filePath;
std::string importPath;
};
class EnvironmentLoader {
public:
EnvironmentLoader(Gfx::PGraphics graphic);
~EnvironmentLoader();
void importAsset(EnvironmentImportArgs args);
private:
void import(EnvironmentImportArgs args, PEnvironmentMapAsset asset);
Gfx::PGraphics graphics;
Gfx::OVertexShader cubeRenderVertex;
Gfx::OFragmentShader cubeRenderFrag;
Gfx::ODescriptorLayout cubeRenderLayout;
Gfx::OPipelineLayout cubePipelineLayout;
Gfx::PGraphicsPipeline cubeRenderPipeline;
Gfx::ORenderPass cubeRenderPass;
Gfx::OViewport cubeRenderViewport;
Gfx::OSampler cubeSampler;
};
}