Adding basic depth prepass

This commit is contained in:
Dynamitos
2021-05-06 17:02:10 +02:00
parent 4a078bd24c
commit 0cf13bcff5
52 changed files with 880 additions and 155 deletions
-2
View File
@@ -1,5 +1,3 @@
import LightEnv;
import Common; import Common;
struct ComputeShaderInput struct ComputeShaderInput
-4
View File
@@ -4,10 +4,6 @@ import VERTEX_INPUT_IMPORT;
import MATERIAL_IMPORT; import MATERIAL_IMPORT;
import PrimitiveSceneData; import PrimitiveSceneData;
struct ModelParameter
{
float4x4 modelMatrix;
}
struct VertexStageOutput struct VertexStageOutput
{ {
+2 -1
View File
@@ -38,6 +38,7 @@ VertexStageOutput vertexMain(
clipSpacePosition = mul(gViewParams.projectionMatrix, viewSpacePosition); clipSpacePosition = mul(gViewParams.projectionMatrix, viewSpacePosition);
output.position = clipSpacePosition; output.position = clipSpacePosition;
output.shaderAttributeInterpolation = input.getInterpolants(cache, vertexParams); output.shaderAttributeInterpolation = input.getInterpolants(cache, vertexParams);
output.cache = cache;
return output; return output;
} }
@@ -72,5 +73,5 @@ float4 fragmentMain(
result += pointLight.illuminate(materialParams, brdf, viewDir); result += pointLight.illuminate(materialParams, brdf, viewDir);
} }
return float4(result, 1); return float4(result, 0);
} }
-2
View File
@@ -19,8 +19,6 @@ cbuffer DispatchParams
layout(binding = 2) layout(binding = 2)
RWTexture2D depthTextureVS; RWTexture2D depthTextureVS;
layout(binding = 3)
ConstantBuffer<Lights> lights;
layout(binding = 4) layout(binding = 4)
StructuredBuffer<Frustum> frustums; StructuredBuffer<Frustum> frustums;
+2 -2
View File
@@ -8,7 +8,7 @@ struct ViewParameter
float4x4 projectionMatrix; float4x4 projectionMatrix;
float4 cameraPos_WS; float4 cameraPos_WS;
} }
layout(set = 1, binding = 0, std430) layout(set = INDEX_VIEW_PARAMS, binding = 0, std430)
ConstantBuffer<ViewParameter> gViewParams; ConstantBuffer<ViewParameter> gViewParams;
struct ScreenToViewParams struct ScreenToViewParams
@@ -16,7 +16,7 @@ struct ScreenToViewParams
float4x4 inverseProjection; float4x4 inverseProjection;
float2 screenDimensions; float2 screenDimensions;
} }
layout(set = 1, binding = 1, std430) layout(set = INDEX_VIEW_PARAMS, binding = 1, std430)
ConstantBuffer<ScreenToViewParams> gScreenToViewParams; ConstantBuffer<ScreenToViewParams> gScreenToViewParams;
// Convert clip space coordinates to view space // Convert clip space coordinates to view space
+2 -2
View File
@@ -15,7 +15,7 @@ struct DirectionalLight : ILightEnv
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf, float3 wo) float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf, float3 wo)
{ {
return intensity.xyz * brdf.evaluate(wo, direction.xyz, input.worldNormal, input.worldTangent, input.worldBiTangent, color.xyz); return intensity.xyz * brdf.evaluate(wo, normalize(direction.xyz), input.worldNormal, input.worldTangent, input.worldBiTangent, color.xyz);
} }
}; };
@@ -68,5 +68,5 @@ struct Lights
uint numPointLights; uint numPointLights;
}; };
layout(set = 0, binding = 0, std430) layout(set = INDEX_LIGHT_ENV, binding = 0, std430)
ConstantBuffer<Lights> gLightEnv; ConstantBuffer<Lights> gLightEnv;
+1 -1
View File
@@ -18,5 +18,5 @@ interface IMaterial
}; };
type_param TMaterial : IMaterial; type_param TMaterial : IMaterial;
layout(set = 2, binding = 0, std430) layout(set = INDEX_MATERIAL, binding = 0, std430)
ParameterBlock<TMaterial> gMaterial; ParameterBlock<TMaterial> gMaterial;
+1 -1
View File
@@ -5,5 +5,5 @@ struct PrimitiveSceneData
float4 actorLocation; float4 actorLocation;
}; };
layout(set = 3, binding = 0, std430) layout(set = INDEX_SCENE_DATA, binding = 0, std430)
ConstantBuffer<PrimitiveSceneData> gSceneData; ConstantBuffer<PrimitiveSceneData> gSceneData;
+25 -29
View File
@@ -12,7 +12,6 @@ struct VertexValueCache
//leading to a compiler error //leading to a compiler error
float3 tangentToLocal[3]; float3 tangentToLocal[3];
float3 tangentToWorld[3]; float3 tangentToWorld[3];
float tangentToWorldSign;
float4 color; float4 color;
float3x3 getTangentToLocal() float3x3 getTangentToLocal()
@@ -46,8 +45,9 @@ struct VertexValueCache
struct ShaderAttributeInterpolation struct ShaderAttributeInterpolation
{ {
float3 worldPosition; float3 worldPosition;
float3 tangentX; float3 normal;
float4 tangentZ; float3 tangent;
float3 biTangent;
float4 color; float4 color;
#if NUM_MATERIAL_TEXCOORDS #if NUM_MATERIAL_TEXCOORDS
float4 packedTexCoords[(NUM_MATERIAL_TEXCOORDS+1) / 2]; float4 packedTexCoords[(NUM_MATERIAL_TEXCOORDS+1) / 2];
@@ -72,16 +72,15 @@ struct ShaderAttributeInterpolation
} }
} }
#endif #endif
float3 tangentY = cross(tangentZ.xyz, tangentX) * tangentZ.w; result.tangentToWorld[0] = tangent;
result.tangentToWorld[0] = cross(tangentY, tangentZ.xyz) * tangentZ.w; result.tangentToWorld[1] = biTangent;
result.tangentToWorld[1] = tangentY; result.tangentToWorld[2] = normal;
result.tangentToWorld[2] = tangentZ.xyz;
result.clipPosition = clipSpacePosition; result.clipPosition = clipSpacePosition;
result.worldPosition = worldPosition; result.worldPosition = worldPosition;
result.viewDir = gViewParams.cameraPos_WS.xyz - worldPosition; result.viewDir = gViewParams.cameraPos_WS.xyz - worldPosition;
result.worldNormal = tangentZ.xyz; result.worldNormal = normal;
result.worldTangent = tangentX; result.worldTangent = tangent;
result.worldBiTangent = tangentY; result.worldBiTangent = biTangent;
result.vertexColor = color; result.vertexColor = color;
return result; return result;
} }
@@ -90,8 +89,9 @@ struct ShaderAttributeInterpolation
struct VertexShaderInput struct VertexShaderInput
{ {
float4 position; float4 position;
float3 tangentX; float3 normal;
float4 tangentZ; float3 tangent;
float3 biTangent;
float4 color; float4 color;
#if NUM_MATERIAL_TEXCOORDS #if NUM_MATERIAL_TEXCOORDS
#if NUM_MATERIAL_TEXCOORDS > 1 #if NUM_MATERIAL_TEXCOORDS > 1
@@ -118,15 +118,12 @@ struct VertexShaderInput
float3 instanceTransform3; float3 instanceTransform3;
#endif // USE_INSTANCING #endif // USE_INSTANCING
float3x3 calcTangentToLocal(out float tangentSign) float3x3 calcTangentToLocal()
{ {
float3x3 result; float3x3 result;
tangentSign = tangentZ.w; result[0] = tangent;
result[1] = biTangent;
float3 tangentY = cross(tangentZ.xyz, tangentX) * tangentZ.w; result[2] = normal;
result[0] = cross(tangentY, tangentZ.xyz) * tangentZ.w;
result[1] = tangentY;
result[2] = tangentZ.xyz;
return result; return result;
} }
@@ -149,17 +146,14 @@ struct VertexShaderInput
cache.instanceOrigin = instanceOrigin; cache.instanceOrigin = instanceOrigin;
#endif #endif
float tangentSign; float3x3 tangentToLocal = calcTangentToLocal();
float3x3 tangentToLocal = calcTangentToLocal(tangentSign);
cache.tangentToLocal[0] = tangentToLocal[0]; cache.tangentToLocal[0] = tangentToLocal[0];
cache.tangentToLocal[1] = tangentToLocal[1]; cache.tangentToLocal[1] = tangentToLocal[1];
cache.tangentToLocal[2] = tangentToLocal[2]; cache.tangentToLocal[2] = tangentToLocal[2];
float3x3 localToWorld = float3x3(gSceneData.localToWorld[0].xyz, gSceneData.localToWorld[1].xyz, gSceneData.localToWorld[2].xyz);
float3x3 tangentToWorld = mul(tangentToLocal, localToWorld); cache.tangentToWorld[0] = mul(gSceneData.localToWorld, float4(tangentToLocal[0], 0.0f)).xyz;
cache.tangentToWorld[0] = tangentToWorld[0]; cache.tangentToWorld[1] = mul(gSceneData.localToWorld, float4(tangentToLocal[1], 0.0f)).xyz;
cache.tangentToWorld[1] = tangentToWorld[1]; cache.tangentToWorld[2] = mul(gSceneData.localToWorld, float4(tangentToLocal[2], 0.0f)).xyz;
cache.tangentToWorld[2] = tangentToWorld[2];
cache.tangentToWorldSign = tangentSign;
return cache; return cache;
} }
@@ -186,8 +180,10 @@ struct VertexShaderInput
ShaderAttributeInterpolation getInterpolants(VertexValueCache cache, MaterialVertexParameter vertexParams) ShaderAttributeInterpolation getInterpolants(VertexValueCache cache, MaterialVertexParameter vertexParams)
{ {
ShaderAttributeInterpolation result = (ShaderAttributeInterpolation)0; ShaderAttributeInterpolation result = (ShaderAttributeInterpolation)0;
result.tangentX = tangentX; float3x3 tangentToWorld = cache.getTangentToWorld();
result.tangentZ = tangentZ; result.normal = mul(gSceneData.localToWorld, float4(normal, 0.0f)).xyz;
result.tangent = mul(gSceneData.localToWorld, float4(tangent, 0.0f)).xyz;
result.biTangent = mul(gSceneData.localToWorld, float4(biTangent, 0.0f)).xyz;
result.color = color; result.color = color;
result.worldPosition = vertexParams.worldPosition; result.worldPosition = vertexParams.worldPosition;
for(int i = 0; i < NUM_MATERIAL_TEXCOORDS; ++i) for(int i = 0; i < NUM_MATERIAL_TEXCOORDS; ++i)
+1
View File
@@ -164,6 +164,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMesh
{ {
//TODO: use bitangent to calculate sign for 4th coordinate of tangentstream //TODO: use bitangent to calculate sign for 4th coordinate of tangentstream
data.tangentBasisComponents[1] = createVertexStream(mesh->mNumVertices, mesh->mTangents, graphics); data.tangentBasisComponents[1] = createVertexStream(mesh->mNumVertices, mesh->mTangents, graphics);
data.tangentBasisComponents[2] = createVertexStream(mesh->mNumVertices, mesh->mBitangents, graphics);
} }
if(mesh->HasVertexColors(0)) if(mesh->HasVertexColors(0))
{ {
+1 -1
View File
@@ -334,7 +334,7 @@ namespace Seele
} }
void resize(size_t newSize) void resize(size_t newSize)
{ {
if (newSize < allocated) if (newSize <= allocated)
{ {
arraySize = newSize; arraySize = newSize;
} }
+4
View File
@@ -37,6 +37,7 @@ public:
, endIt(std::move(other.endIt)) , endIt(std::move(other.endIt))
, _size(std::move(other._size)) , _size(std::move(other._size))
{ {
other._size = 0;
} }
~List() ~List()
{ {
@@ -75,6 +76,7 @@ public:
beginIt = other.beginIt; beginIt = other.beginIt;
endIt = other.endIt; endIt = other.endIt;
_size = other._size; _size = other._size;
other._size = 0;
} }
return *this; return *this;
} }
@@ -252,10 +254,12 @@ public:
} }
void popBack() void popBack()
{ {
assert(_size > 0);
remove(Iterator(tail->prev)); remove(Iterator(tail->prev));
} }
void popFront() void popFront()
{ {
assert(_size > 0);
remove(Iterator(root)); remove(Iterator(root));
} }
Iterator insert(Iterator pos, const T &value) Iterator insert(Iterator pos, const T &value)
+2 -1
View File
@@ -48,9 +48,10 @@ public:
virtual PGeometryShader createGeometryShader(const ShaderCreateInfo& createInfo) = 0; virtual PGeometryShader createGeometryShader(const ShaderCreateInfo& createInfo) = 0;
virtual PFragmentShader createFragmentShader(const ShaderCreateInfo& createInfo) = 0; virtual PFragmentShader createFragmentShader(const ShaderCreateInfo& createInfo) = 0;
virtual PGraphicsPipeline createGraphicsPipeline(const GraphicsPipelineCreateInfo& createInfo) = 0; virtual PGraphicsPipeline createGraphicsPipeline(const GraphicsPipelineCreateInfo& createInfo) = 0;
virtual PComputePipeline createComputePipeline(const ComputePipelineCreateInfo& createInfo) = 0;
virtual PSamplerState createSamplerState(const SamplerCreateInfo& createInfo) = 0; virtual PSamplerState createSamplerState(const SamplerCreateInfo& createInfo) = 0;
virtual PDescriptorLayout createDescriptorLayout() = 0; virtual PDescriptorLayout createDescriptorLayout(const std::string& name = "") = 0;
virtual PPipelineLayout createPipelineLayout() = 0; virtual PPipelineLayout createPipelineLayout() = 0;
PVertexBuffer getNullVertexBuffer(); PVertexBuffer getNullVertexBuffer();
+11 -1
View File
@@ -120,6 +120,7 @@ struct ShaderCreateInfo
{ {
//It's possible to input multiple source files for materials or vertexFactories //It's possible to input multiple source files for materials or vertexFactories
Array<std::string> shaderCode; Array<std::string> shaderCode;
std::string name; // Debug info
std::string entryPoint; std::string entryPoint;
Array<const char*> typeParameter; Array<const char*> typeParameter;
Map<const char*, const char*> defines; Map<const char*, const char*> defines;
@@ -231,7 +232,7 @@ struct GraphicsPipelineCreateInfo
rasterizationState.cullMode = Gfx::SE_CULL_MODE_BACK_BIT; rasterizationState.cullMode = Gfx::SE_CULL_MODE_BACK_BIT;
rasterizationState.polygonMode = Gfx::SE_POLYGON_MODE_FILL; rasterizationState.polygonMode = Gfx::SE_POLYGON_MODE_FILL;
rasterizationState.frontFace = Gfx::SE_FRONT_FACE_COUNTER_CLOCKWISE; rasterizationState.frontFace = Gfx::SE_FRONT_FACE_COUNTER_CLOCKWISE;
depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_LESS; depthStencilState.depthCompareOp = Gfx::SE_COMPARE_OP_LESS_OR_EQUAL;
depthStencilState.minDepthBounds = 0.0f; depthStencilState.minDepthBounds = 0.0f;
depthStencilState.maxDepthBounds = 1.0f; depthStencilState.maxDepthBounds = 1.0f;
depthStencilState.stencilTestEnable = false; depthStencilState.stencilTestEnable = false;
@@ -246,4 +247,13 @@ struct GraphicsPipelineCreateInfo
colorBlend.blendConstants[3] = 1.0f; colorBlend.blendConstants[3] = 1.0f;
} }
}; };
struct ComputePipelineCreateInfo
{
Gfx::PComputeShader computeShader;
Gfx::PPipelineLayout pipelineLayout;
ComputePipelineCreateInfo()
{
std::memset((void*)this, 0, sizeof(*this));
}
};
} // namespace Seele } // namespace Seele
+17 -2
View File
@@ -2,6 +2,8 @@
#include "Material/MaterialInstance.h" #include "Material/MaterialInstance.h"
#include "Material/Material.h" #include "Material/Material.h"
#include "Graphics.h" #include "Graphics.h"
#include "RenderPass/DepthPrepass.h"
#include "RenderPass/BasePass.h"
using namespace Seele; using namespace Seele;
using namespace Seele::Gfx; using namespace Seele::Gfx;
@@ -18,6 +20,16 @@ std::string getShaderNameFromRenderPassType(Gfx::RenderPassType type)
return ""; return "";
} }
} }
void modifyRenderPassMacros(Gfx::RenderPassType type, Map<const char*, const char*>& defines)
{
switch (type)
{
case Gfx::RenderPassType::DepthPrepass:
DepthPrepass::modifyRenderPassMacros(defines);
case Gfx::RenderPassType::BasePass:
BasePass::modifyRenderPassMacros(defines);
}
}
ShaderMap::ShaderMap() ShaderMap::ShaderMap()
{ {
@@ -55,6 +67,8 @@ ShaderCollection& ShaderMap::createShaders(
createInfo.defines["MATERIAL_IMPORT"] = material->getName().c_str(); createInfo.defines["MATERIAL_IMPORT"] = material->getName().c_str();
createInfo.defines["NUM_MATERIAL_TEXCOORDS"] = "1"; createInfo.defines["NUM_MATERIAL_TEXCOORDS"] = "1";
createInfo.defines["USE_INSTANCING"] = "0"; createInfo.defines["USE_INSTANCING"] = "0";
modifyRenderPassMacros(renderPass, createInfo.defines);
createInfo.name = getShaderNameFromRenderPassType(renderPass) + " Material " + material->getName();
std::ifstream codeStream("./shaders/" + getShaderNameFromRenderPassType(renderPass), std::ios::ate); std::ifstream codeStream("./shaders/" + getShaderNameFromRenderPassType(renderPass), std::ios::ate);
auto fileSize = codeStream.tellg(); auto fileSize = codeStream.tellg();
@@ -113,7 +127,8 @@ void PipelineLayout::addDescriptorLayout(uint32 setIndex, PDescriptorLayout layo
{ {
descriptorSetLayouts.resize(setIndex + 1); descriptorSetLayouts.resize(setIndex + 1);
} }
if (descriptorSetLayouts[setIndex] != nullptr) // After a second thought, merging descriptor layout bindings is not a good idea
/*if (descriptorSetLayouts[setIndex] != nullptr)
{ {
auto &thisBindings = descriptorSetLayouts[setIndex]->descriptorBindings; auto &thisBindings = descriptorSetLayouts[setIndex]->descriptorBindings;
auto &otherBindings = layout->descriptorBindings; auto &otherBindings = layout->descriptorBindings;
@@ -126,7 +141,7 @@ void PipelineLayout::addDescriptorLayout(uint32 setIndex, PDescriptorLayout layo
} }
} }
} }
else else*/
{ {
descriptorSetLayouts[setIndex] = layout; descriptorSetLayouts[setIndex] = layout;
} }
+28 -5
View File
@@ -75,6 +75,14 @@ public:
}; };
DEFINE_REF(FragmentShader) DEFINE_REF(FragmentShader)
class ComputeShader
{
public:
ComputeShader() {}
virtual ~ComputeShader() {}
};
DEFINE_REF(ComputeShader)
//Uniquely identifies a permutation of shaders //Uniquely identifies a permutation of shaders
//using the type parameters used to generate it //using the type parameters used to generate it
struct ShaderPermutation struct ShaderPermutation
@@ -204,8 +212,9 @@ DEFINE_REF(DescriptorSet)
class DescriptorLayout class DescriptorLayout
{ {
public: public:
DescriptorLayout() DescriptorLayout(const std::string& name)
: setIndex(0) : setIndex(0)
, name(name)
{ {
} }
virtual ~DescriptorLayout() {} virtual ~DescriptorLayout() {}
@@ -232,6 +241,7 @@ protected:
Array<DescriptorBinding> descriptorBindings; Array<DescriptorBinding> descriptorBindings;
PDescriptorAllocator allocator; PDescriptorAllocator allocator;
uint32 setIndex; uint32 setIndex;
std::string name;
friend class PipelineLayout; friend class PipelineLayout;
friend class DescriptorAllocator; friend class DescriptorAllocator;
}; };
@@ -441,6 +451,19 @@ protected:
}; };
DEFINE_REF(GraphicsPipeline) DEFINE_REF(GraphicsPipeline)
class ComputePipeline
{
public:
ComputePipeline(const ComputePipelineCreateInfo& createInfo, PPipelineLayout layout) : createInfo(createInfo), layout(layout) {}
virtual ~ComputePipeline(){}
const ComputePipelineCreateInfo& getCreateInfo() const { return createInfo; }
PPipelineLayout getPipelineLayout() const { return layout; }
protected:
ComputePipelineCreateInfo createInfo;
PPipelineLayout layout;
};
DEFINE_REF(ComputePipeline)
// IMPORTANT!! // IMPORTANT!!
// WHEN DERIVING FROM ANY Gfx:: BASE CLASSES WITH MULTIPLE INHERITANCE // WHEN DERIVING FROM ANY Gfx:: BASE CLASSES WITH MULTIPLE INHERITANCE
// ALWAYS PUT THE Gfx:: BASE CLASS FIRST // ALWAYS PUT THE Gfx:: BASE CLASS FIRST
@@ -505,7 +528,7 @@ public:
virtual void beginFrame() = 0; virtual void beginFrame() = 0;
virtual void endFrame() = 0; virtual void endFrame() = 0;
virtual void onWindowCloseEvent() = 0; virtual void onWindowCloseEvent() = 0;
virtual PTexture2D getBackBuffer() = 0; virtual PTexture2D getBackBuffer() const = 0;
virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) = 0; virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) = 0;
virtual void setMouseMoveCallback(std::function<void(double, double)> callback) = 0; virtual void setMouseMoveCallback(std::function<void(double, double)> callback) = 0;
virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) = 0; virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) = 0;
@@ -559,7 +582,7 @@ public:
SeAttachmentStoreOp storeOp = SE_ATTACHMENT_STORE_OP_STORE, SeAttachmentStoreOp storeOp = SE_ATTACHMENT_STORE_OP_STORE,
SeAttachmentLoadOp stencilLoadOp = SE_ATTACHMENT_LOAD_OP_DONT_CARE, SeAttachmentLoadOp stencilLoadOp = SE_ATTACHMENT_LOAD_OP_DONT_CARE,
SeAttachmentStoreOp stencilStoreOp = SE_ATTACHMENT_STORE_OP_DONT_CARE) SeAttachmentStoreOp stencilStoreOp = SE_ATTACHMENT_STORE_OP_DONT_CARE)
: texture(texture), loadOp(loadOp), storeOp(storeOp), stencilLoadOp(stencilLoadOp), stencilStoreOp(stencilStoreOp) : loadOp(loadOp), storeOp(storeOp), stencilLoadOp(stencilLoadOp), stencilStoreOp(stencilStoreOp), texture(texture)
{ {
} }
virtual ~RenderTargetAttachment() virtual ~RenderTargetAttachment()
@@ -583,12 +606,12 @@ public:
inline SeAttachmentStoreOp getStencilStoreOp() const { return stencilStoreOp; } inline SeAttachmentStoreOp getStencilStoreOp() const { return stencilStoreOp; }
SeClearValue clear; SeClearValue clear;
SeColorComponentFlags componentFlags; SeColorComponentFlags componentFlags;
protected:
PTexture2D texture;
SeAttachmentLoadOp loadOp; SeAttachmentLoadOp loadOp;
SeAttachmentStoreOp storeOp; SeAttachmentStoreOp storeOp;
SeAttachmentLoadOp stencilLoadOp; SeAttachmentLoadOp stencilLoadOp;
SeAttachmentStoreOp stencilStoreOp; SeAttachmentStoreOp stencilStoreOp;
protected:
PTexture2D texture;
}; };
DEFINE_REF(RenderTargetAttachment) DEFINE_REF(RenderTargetAttachment)
+42 -32
View File
@@ -4,6 +4,7 @@
#include "Scene/Components/CameraComponent.h" #include "Scene/Components/CameraComponent.h"
#include "Scene/Actor/CameraActor.h" #include "Scene/Actor/CameraActor.h"
#include "Math/Vector.h" #include "Math/Vector.h"
#include "RenderGraph.h"
using namespace Seele; using namespace Seele;
@@ -46,10 +47,10 @@ void BasePassMeshProcessor::addMeshBatch(
renderCommand->setViewport(target); renderCommand->setViewport(target);
for(uint32 i = 0; i < batch.elements.size(); ++i) for(uint32 i = 0; i < batch.elements.size(); ++i)
{ {
pipelineLayout->addDescriptorLayout(2, material->getRenderMaterial()->getDescriptorLayout()); pipelineLayout->addDescriptorLayout(BasePass::INDEX_MATERIAL, material->getRenderMaterial()->getDescriptorLayout());
pipelineLayout->create(); pipelineLayout->create();
descriptorSets[2] = material->getDescriptor(); descriptorSets[BasePass::INDEX_MATERIAL] = material->getDescriptor();
descriptorSets[3] = cachedPrimitiveSets[cachedPrimitiveIndex++]; descriptorSets[BasePass::INDEX_SCENE_DATA] = cachedPrimitiveSets[cachedPrimitiveIndex++];
buildMeshDrawCommand(batch, buildMeshDrawCommand(batch,
// primitiveComponent, // primitiveComponent,
renderPass, renderPass,
@@ -78,42 +79,29 @@ void BasePassMeshProcessor::clearCommands()
cachedPrimitiveIndex = 0; cachedPrimitiveIndex = 0;
} }
BasePass::BasePass(const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source) BasePass::BasePass(PRenderGraph renderGraph, const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source)
: processor(new BasePassMeshProcessor(scene, viewport, graphics, false)) : RenderPass(renderGraph)
, processor(new BasePassMeshProcessor(scene, viewport, graphics, false))
, scene(scene) , scene(scene)
, graphics(graphics) , graphics(graphics)
, viewport(viewport) , viewport(viewport)
, descriptorSets(4) , descriptorSets(4)
, source(source->getCameraComponent()) , source(source->getCameraComponent())
{ {
Gfx::PRenderTargetAttachment colorAttachment = new Gfx::SwapchainAttachment(viewport->getOwner());
TextureCreateInfo depthBufferInfo;
depthBufferInfo.width = viewport->getSizeX();
depthBufferInfo.height = viewport->getSizeY();
depthBufferInfo.format = Gfx::SE_FORMAT_D32_SFLOAT;
depthBufferInfo.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
depthBuffer = graphics->createTexture2D(depthBufferInfo);
Gfx::PRenderTargetAttachment depthAttachment =
new Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
depthAttachment->clear.depthStencil.depth = 1.0f;
Gfx::PRenderTargetLayout layout = new Gfx::RenderTargetLayout(colorAttachment, depthAttachment);
renderPass = graphics->createRenderPass(layout);
UniformBufferCreateInfo uniformInitializer; UniformBufferCreateInfo uniformInitializer;
basePassLayout = graphics->createPipelineLayout(); basePassLayout = graphics->createPipelineLayout();
lightLayout = graphics->createDescriptorLayout(); lightLayout = graphics->createDescriptorLayout("LightLayout");
lightLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER); lightLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uniformInitializer.resourceData.size = sizeof(LightEnv); uniformInitializer.resourceData.size = sizeof(LightEnv);
uniformInitializer.resourceData.data = nullptr; uniformInitializer.resourceData.data = nullptr;
uniformInitializer.bDynamic = true; uniformInitializer.bDynamic = true;
lightUniform = graphics->createUniformBuffer(uniformInitializer); lightUniform = graphics->createUniformBuffer(uniformInitializer);
lightLayout->create(); lightLayout->create();
basePassLayout->addDescriptorLayout(0, lightLayout); basePassLayout->addDescriptorLayout(INDEX_LIGHT_ENV, lightLayout);
descriptorSets[0] = lightLayout->allocatedDescriptorSet(); descriptorSets[INDEX_LIGHT_ENV] = lightLayout->allocatedDescriptorSet();
viewLayout = graphics->createDescriptorLayout(); viewLayout = graphics->createDescriptorLayout("ViewLayout");
viewLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER); viewLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uniformInitializer.resourceData.size = sizeof(ViewParameter); uniformInitializer.resourceData.size = sizeof(ViewParameter);
uniformInitializer.resourceData.data = (uint8*)&viewParams; uniformInitializer.resourceData.data = (uint8*)&viewParams;
@@ -125,13 +113,13 @@ BasePass::BasePass(const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport v
uniformInitializer.bDynamic = true; uniformInitializer.bDynamic = true;
screenToViewParamBuffer = graphics->createUniformBuffer(uniformInitializer); screenToViewParamBuffer = graphics->createUniformBuffer(uniformInitializer);
viewLayout->create(); viewLayout->create();
basePassLayout->addDescriptorLayout(1, viewLayout); basePassLayout->addDescriptorLayout(INDEX_VIEW_PARAMS, viewLayout);
descriptorSets[1] = viewLayout->allocatedDescriptorSet(); descriptorSets[INDEX_VIEW_PARAMS] = viewLayout->allocatedDescriptorSet();
primitiveLayout = graphics->createDescriptorLayout(); primitiveLayout = graphics->createDescriptorLayout("PrimitiveLayout");
primitiveLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER); primitiveLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
primitiveLayout->create(); primitiveLayout->create();
basePassLayout->addDescriptorLayout(3, primitiveLayout); basePassLayout->addDescriptorLayout(INDEX_SCENE_DATA, primitiveLayout);
} }
BasePass::~BasePass() BasePass::~BasePass()
@@ -146,8 +134,8 @@ void BasePass::beginFrame()
uniformUpdate.size = sizeof(LightEnv); uniformUpdate.size = sizeof(LightEnv);
uniformUpdate.data = (uint8*)&scene->getLightEnvironment(); uniformUpdate.data = (uint8*)&scene->getLightEnvironment();
lightUniform->updateContents(uniformUpdate); lightUniform->updateContents(uniformUpdate);
descriptorSets[0]->updateBuffer(0, lightUniform); descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(0, lightUniform);
descriptorSets[0]->writeChanges(); descriptorSets[INDEX_LIGHT_ENV]->writeChanges();
viewParams.viewMatrix = source->getViewMatrix(); viewParams.viewMatrix = source->getViewMatrix();
viewParams.projectionMatrix = source->getProjectionMatrix(); viewParams.projectionMatrix = source->getProjectionMatrix();
@@ -160,9 +148,9 @@ void BasePass::beginFrame()
uniformUpdate.size = sizeof(ScreenToViewParameter); uniformUpdate.size = sizeof(ScreenToViewParameter);
uniformUpdate.data = (uint8*)&screenToViewParams; uniformUpdate.data = (uint8*)&screenToViewParams;
screenToViewParamBuffer->updateContents(uniformUpdate); screenToViewParamBuffer->updateContents(uniformUpdate);
descriptorSets[1]->updateBuffer(0, viewParamBuffer); descriptorSets[INDEX_VIEW_PARAMS]->updateBuffer(0, viewParamBuffer);
descriptorSets[1]->updateBuffer(1, screenToViewParamBuffer); descriptorSets[INDEX_VIEW_PARAMS]->updateBuffer(1, screenToViewParamBuffer);
descriptorSets[1]->writeChanges(); descriptorSets[INDEX_VIEW_PARAMS]->writeChanges();
for(auto &&meshBatch : scene->getStaticMeshes()) for(auto &&meshBatch : scene->getStaticMeshes())
{ {
meshBatch.material->updateDescriptorData(); meshBatch.material->updateDescriptorData();
@@ -183,3 +171,25 @@ void BasePass::render()
void BasePass::endFrame() void BasePass::endFrame()
{ {
} }
void BasePass::publishOutputs()
{
colorAttachment = new Gfx::SwapchainAttachment(viewport->getOwner());
renderGraph->registerRenderPassOutput("BASEPASS_COLOR", colorAttachment);
}
void BasePass::createRenderPass()
{
Gfx::PRenderTargetAttachment depthAttachment = renderGraph->requestRenderTarget("DEPTHPREPASS_DEPTH");
depthAttachment->loadOp = Gfx::SE_ATTACHMENT_LOAD_OP_LOAD;
Gfx::PRenderTargetLayout layout = new Gfx::RenderTargetLayout(colorAttachment, depthAttachment);
renderPass = graphics->createRenderPass(layout);
}
void BasePass::modifyRenderPassMacros(Map<const char*, const char*>& defines)
{
defines["INDEX_LIGHT_ENV"] = "0";
defines["INDEX_VIEW_PARAMS"] = "1";
defines["INDEX_MATERIAL"] = "2";
defines["INDEX_SCENE_DATA"] = "3";
}
+16 -19
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "MinimalEngine.h" #include "MinimalEngine.h"
#include "MeshProcessor.h" #include "MeshProcessor.h"
#include "RenderPass.h"
namespace Seele namespace Seele
{ {
@@ -30,28 +31,19 @@ private:
DEFINE_REF(BasePassMeshProcessor) DEFINE_REF(BasePassMeshProcessor)
DECLARE_REF(CameraActor) DECLARE_REF(CameraActor)
DECLARE_REF(CameraComponent) DECLARE_REF(CameraComponent)
class BasePass class BasePass : public RenderPass
{ {
public: public:
BasePass(const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source); BasePass(PRenderGraph renderGraph, const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source);
~BasePass(); virtual ~BasePass();
void beginFrame(); virtual void beginFrame() override;
void render(); virtual void render() override;
void endFrame(); virtual void endFrame() override;
virtual void publishOutputs() override;
virtual void createRenderPass() override;
static void modifyRenderPassMacros(Map<const char*, const char*>& defines);
private: private:
struct ViewParameter Gfx::PRenderTargetAttachment colorAttachment;
{
Matrix4 viewMatrix;
Matrix4 projectionMatrix;
Vector4 cameraPosition;
} viewParams;
struct ScreenToViewParameter
{
Matrix4 inverseProjectionMatrix;
Vector2 screenDimensions;
} screenToViewParams;
Gfx::PRenderPass renderPass;
Gfx::PTexture2D depthBuffer; Gfx::PTexture2D depthBuffer;
UPBasePassMeshProcessor processor; UPBasePassMeshProcessor processor;
const PScene scene; const PScene scene;
@@ -61,16 +53,21 @@ private:
PCameraComponent source; PCameraComponent source;
Gfx::PPipelineLayout basePassLayout; Gfx::PPipelineLayout basePassLayout;
// Set 0: Light environment // Set 0: Light environment
static constexpr uint32 INDEX_LIGHT_ENV = 0;
Gfx::PDescriptorLayout lightLayout; Gfx::PDescriptorLayout lightLayout;
Gfx::PUniformBuffer lightUniform; Gfx::PUniformBuffer lightUniform;
// Set 1: viewParameter // Set 1: viewParameter
static constexpr uint32 INDEX_VIEW_PARAMS = 1;
Gfx::PDescriptorLayout viewLayout; Gfx::PDescriptorLayout viewLayout;
Gfx::PUniformBuffer viewParamBuffer; Gfx::PUniformBuffer viewParamBuffer;
Gfx::PUniformBuffer screenToViewParamBuffer; Gfx::PUniformBuffer screenToViewParamBuffer;
// Set 2: materials, generated // Set 2: materials, generated
static constexpr uint32 INDEX_MATERIAL = 2;
// Set 3: primitive scene data // Set 3: primitive scene data
static constexpr uint32 INDEX_SCENE_DATA = 3;
Gfx::PDescriptorLayout primitiveLayout; Gfx::PDescriptorLayout primitiveLayout;
Gfx::PUniformBuffer primitiveUniformBuffer; Gfx::PUniformBuffer primitiveUniformBuffer;
friend class BasePassMeshProcessor;
}; };
DEFINE_REF(BasePass) DEFINE_REF(BasePass)
} // namespace Seele } // namespace Seele
@@ -4,5 +4,11 @@ target_sources(SeeleEngine
BasePass.cpp BasePass.cpp
DepthPrepass.h DepthPrepass.h
DepthPrepass.cpp DepthPrepass.cpp
LightCullingPass.h
LightCullingPass.cpp
MeshProcessor.h MeshProcessor.h
MeshProcessor.cpp) MeshProcessor.cpp
RenderGraph.h
RenderGraph.cpp
RenderPass.h
RenderPass.cpp)
@@ -0,0 +1,185 @@
#include "DepthPrepass.h"
#include "Graphics/Graphics.h"
#include "Window/Window.h"
#include "Scene/Components/CameraComponent.h"
#include "Scene/Actor/CameraActor.h"
#include "Math/Vector.h"
#include "RenderGraph.h"
using namespace Seele;
DepthPrepassMeshProcessor::DepthPrepassMeshProcessor(const PScene scene, Gfx::PViewport viewport, Gfx::PGraphics graphics)
: MeshProcessor(scene, graphics)
, target(viewport)
, cachedPrimitiveIndex(0)
{
}
DepthPrepassMeshProcessor::~DepthPrepassMeshProcessor()
{
}
void DepthPrepassMeshProcessor::addMeshBatch(
const MeshBatch& batch,
// const PPrimitiveComponent primitiveComponent,
const Gfx::PRenderPass renderPass,
Gfx::PPipelineLayout pipelineLayout,
Gfx::PDescriptorLayout primitiveLayout,
Array<Gfx::PDescriptorSet>& descriptorSets,
int32 /*staticMeshId*/)
{
const PMaterialAsset material = batch.material;
//const Gfx::MaterialShadingModel shadingModel = material->getShadingModel();
const PVertexShaderInput vertexInput = batch.vertexInput;
const Gfx::ShaderCollection* collection = material->getRenderMaterial()->getShaders(Gfx::RenderPassType::DepthPrepass, vertexInput->getType());
assert(collection != nullptr);
for(uint32 i = 0; i < batch.elements.size(); ++i)
{
Gfx::PDescriptorSet descriptorSet = primitiveLayout->allocatedDescriptorSet();
descriptorSet->updateBuffer(0, batch.elements[i].uniformBuffer);
descriptorSet->writeChanges();
cachedPrimitiveSets.add(descriptorSet);
}
Gfx::PRenderCommand renderCommand = graphics->createRenderCommand();
renderCommand->setViewport(target);
for(uint32 i = 0; i < batch.elements.size(); ++i)
{
pipelineLayout->addDescriptorLayout(DepthPrepass::INDEX_MATERIAL, material->getRenderMaterial()->getDescriptorLayout());
pipelineLayout->create();
descriptorSets[DepthPrepass::INDEX_MATERIAL] = material->getDescriptor();
descriptorSets[DepthPrepass::INDEX_SCENE_DATA] = cachedPrimitiveSets[cachedPrimitiveIndex++];
buildMeshDrawCommand(batch,
// primitiveComponent,
renderPass,
pipelineLayout,
renderCommand,
descriptorSets,
collection->vertexShader,
collection->controlShader,
collection->evalutionShader,
collection->geometryShader,
collection->fragmentShader,
true);
}
renderCommands.add(renderCommand);
}
Array<Gfx::PRenderCommand> DepthPrepassMeshProcessor::getRenderCommands()
{
return renderCommands;
}
void DepthPrepassMeshProcessor::clearCommands()
{
renderCommands.clear();
cachedPrimitiveSets.clear();
cachedPrimitiveIndex = 0;
}
DepthPrepass::DepthPrepass(PRenderGraph renderGraph, const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source)
: RenderPass(renderGraph)
, processor(new DepthPrepassMeshProcessor(scene, viewport, graphics))
, scene(scene)
, graphics(graphics)
, viewport(viewport)
, descriptorSets(3)
, source(source->getCameraComponent())
{
UniformBufferCreateInfo uniformInitializer;
depthPrepassLayout = graphics->createPipelineLayout();
viewLayout = graphics->createDescriptorLayout("ViewLayout");
viewLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uniformInitializer.resourceData.size = sizeof(ViewParameter);
uniformInitializer.resourceData.data = (uint8*)&viewParams;
uniformInitializer.bDynamic = true;
viewParamBuffer = graphics->createUniformBuffer(uniformInitializer);
viewLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uniformInitializer.resourceData.size = sizeof(ScreenToViewParameter);
uniformInitializer.resourceData.data = (uint8*)&screenToViewParams;
uniformInitializer.bDynamic = true;
screenToViewParamBuffer = graphics->createUniformBuffer(uniformInitializer);
viewLayout->create();
depthPrepassLayout->addDescriptorLayout(INDEX_VIEW_PARAMS, viewLayout);
descriptorSets[INDEX_VIEW_PARAMS] = viewLayout->allocatedDescriptorSet();
primitiveLayout = graphics->createDescriptorLayout("PrimitiveLayout");
primitiveLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
primitiveLayout->create();
depthPrepassLayout->addDescriptorLayout(INDEX_SCENE_DATA, primitiveLayout);
}
DepthPrepass::~DepthPrepass()
{
}
void DepthPrepass::beginFrame()
{
processor->clearCommands();
primitiveLayout->reset();
BulkResourceData uniformUpdate;
viewParams.viewMatrix = source->getViewMatrix();
viewParams.projectionMatrix = source->getProjectionMatrix();
viewParams.cameraPosition = Vector4(source->getCameraPosition(), 0);
screenToViewParams.inverseProjectionMatrix = glm::inverse(viewParams.projectionMatrix);
screenToViewParams.screenDimensions = Vector2(static_cast<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
uniformUpdate.size = sizeof(ViewParameter);
uniformUpdate.data = (uint8*)&viewParams;
viewParamBuffer->updateContents(uniformUpdate);
uniformUpdate.size = sizeof(ScreenToViewParameter);
uniformUpdate.data = (uint8*)&screenToViewParams;
screenToViewParamBuffer->updateContents(uniformUpdate);
descriptorSets[INDEX_VIEW_PARAMS]->updateBuffer(0, viewParamBuffer);
descriptorSets[INDEX_VIEW_PARAMS]->updateBuffer(1, screenToViewParamBuffer);
descriptorSets[INDEX_VIEW_PARAMS]->writeChanges();
for(auto &&meshBatch : scene->getStaticMeshes())
{
meshBatch.material->updateDescriptorData();
}
}
void DepthPrepass::render()
{
graphics->beginRenderPass(renderPass);
for (auto &&meshBatch : scene->getStaticMeshes())
{
processor->addMeshBatch(meshBatch, renderPass, depthPrepassLayout, primitiveLayout, descriptorSets);
}
graphics->executeCommands(processor->getRenderCommands());
graphics->endRenderPass();
}
void DepthPrepass::endFrame()
{
}
void DepthPrepass::publishOutputs()
{
TextureCreateInfo depthBufferInfo;
depthBufferInfo.width = viewport->getSizeX();
depthBufferInfo.height = viewport->getSizeY();
depthBufferInfo.format = Gfx::SE_FORMAT_D32_SFLOAT;
depthBufferInfo.usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
depthBuffer = graphics->createTexture2D(depthBufferInfo);
depthAttachment =
new Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE);
depthAttachment->clear.depthStencil.depth = 1.0f;
renderGraph->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment);
}
void DepthPrepass::createRenderPass()
{
Gfx::PRenderTargetLayout layout = new Gfx::RenderTargetLayout(depthAttachment);
renderPass = graphics->createRenderPass(layout);
}
void DepthPrepass::modifyRenderPassMacros(Map<const char*, const char*>& defines)
{
defines["INDEX_VIEW_PARAMS"] = "0";
defines["INDEX_MATERIAL"] = "1";
defines["INDEX_SCENE_DATA"] = "2";
}
@@ -0,0 +1,68 @@
#pragma once
#include "MinimalEngine.h"
#include "MeshProcessor.h"
#include "RenderPass.h"
namespace Seele
{
class DepthPrepassMeshProcessor : public MeshProcessor
{
public:
DepthPrepassMeshProcessor(const PScene scene, Gfx::PViewport viewport, Gfx::PGraphics graphics);
virtual ~DepthPrepassMeshProcessor();
virtual void addMeshBatch(
const MeshBatch& batch,
const Gfx::PRenderPass renderPass,
Gfx::PPipelineLayout pipelineLayout,
Gfx::PDescriptorLayout primitiveLayout,
Array<Gfx::PDescriptorSet>& descriptorSets,
int32 staticMeshId = -1) override;
Array<Gfx::PRenderCommand> getRenderCommands();
void clearCommands();
private:
Array<Gfx::PRenderCommand> renderCommands;
Array<Gfx::PDescriptorSet> cachedPrimitiveSets;
Gfx::PViewport target;
uint32 cachedPrimitiveIndex;
};
DEFINE_REF(DepthPrepassMeshProcessor)
DECLARE_REF(CameraActor)
DECLARE_REF(CameraComponent)
class DepthPrepass : public RenderPass
{
public:
DepthPrepass(PRenderGraph renderGraph, const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport viewport, PCameraActor source);
~DepthPrepass();
virtual void beginFrame() override;
virtual void render() override;
virtual void endFrame() override;
virtual void publishOutputs() override;
virtual void createRenderPass() override;
static void modifyRenderPassMacros(Map<const char*, const char*>& defines);
private:
Gfx::PRenderTargetAttachment depthAttachment;
Gfx::PTexture2D depthBuffer;
UPDepthPrepassMeshProcessor processor;
const PScene scene;
Gfx::PGraphics graphics;
Gfx::PViewport viewport;
Array<Gfx::PDescriptorSet> descriptorSets;
PCameraComponent source;
Gfx::PPipelineLayout depthPrepassLayout;
// Set 0: viewParameter
static constexpr uint32 INDEX_VIEW_PARAMS = 0;
Gfx::PDescriptorLayout viewLayout;
Gfx::PUniformBuffer viewParamBuffer;
Gfx::PUniformBuffer screenToViewParamBuffer;
// Set 1: materials, generated
static constexpr uint32 INDEX_MATERIAL = 1;
// Set 2: primitive scene data
static constexpr uint32 INDEX_SCENE_DATA = 2;
Gfx::PDescriptorLayout primitiveLayout;
Gfx::PUniformBuffer primitiveUniformBuffer;
friend class DepthPrepassMeshProcessor;
};
DEFINE_REF(DepthPrepass)
} // namespace Seele
@@ -0,0 +1,83 @@
#include "LightCullingPass.h"
#include "Graphics/Graphics.h"
#include "Scene/Actor/CameraActor.h"
#include "Scene/Components/CameraComponent.h"
using namespace Seele;
LightCullingPass::LightCullingPass(PRenderGraph renderGraph, Gfx::PViewport viewport, Gfx::PGraphics graphics, PCameraActor camera)
: RenderPass(renderGraph)
, viewport(viewport)
, graphics(graphics)
, source(camera->getCameraComponent())
{
}
LightCullingPass::~LightCullingPass()
{
}
void LightCullingPass::beginFrame()
{
uint32_t viewportWidth = viewport->getSizeX();
uint32_t viewportHeight = viewport->getSizeY();
glm::uvec3 numThreads = glm::ceil(glm::vec3(viewportWidth / (float)BLOCK_SIZE, viewportHeight / (float)BLOCK_SIZE, 1));
glm::uvec3 numThreadGroups = glm::ceil(glm::vec3(numThreads.x / (float)BLOCK_SIZE, numThreads.y / (float)BLOCK_SIZE, 1));
dispatchParams.numThreads = numThreads;
dispatchParams.numThreadGroups = numThreadGroups;
ScreenToView screenToView;
screenToView.inverseProjection = glm::inverse(source->getProjectionMatrix());
screenToView.screenDimensions = glm::vec2(viewportWidth, viewportHeight);
frustumShader = renderDevice->createComputeShader(loadPlaintext("./_Game/shaders/ComputeFrustums.slang"), "computeFrustums");
frustumDescriptorLayout = renderDevice->createDescriptorLayout();
frustumDescriptorLayout->addDescriptorBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
frustumDescriptorLayout->addDescriptorBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
frustumDescriptorLayout->addDescriptorBinding(2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
frustumLayout = renderDevice->createPipelineLayout();
frustumLayout->addPushConstants(init::PushConstantRange(VK_SHADER_STAGE_COMPUTE_BIT, sizeof(DispatchParams), 0));
frustumLayout->addDescriptorLayout(0, frustumDescriptorLayout);
frustumLayout->create();
frustumShader->setPipelineLayout(frustumLayout);
RHIResourceCreateInfo frustumInfo;
frustumBuffer = renderDevice->createStructuredBuffer(sizeof(Frustum), sizeof(Frustum) * numThreads.x * numThreads.y * numThreads.z, BufferUsageFlags::BUF_UnorderedAccess, frustumInfo);
dispatchParamsBuffer = renderDevice->createUniformBuffer(&dispatchParams, sizeof(DispatchParams), UniformBuffer_MultiFrame);
screenToViewParams = renderDevice->createUniformBuffer(&screenToView, sizeof(ScreenToView), UniformBuffer_MultiFrame);
frustumDescriptorSet = frustumDescriptorLayout->allocateDescriptorSet();
frustumDescriptorSet->updateBuffer(0, dispatchParamsBuffer);
frustumDescriptorSet->updateBuffer(1, screenToViewParams);
frustumDescriptorSet->updateBuffer(2, frustumBuffer);
frustumDescriptorSet->writeChanges();
renderDevice->setComputeShader(frustumShader);
renderDevice->bindComputeDescriptors(frustumLayout, frustumDescriptorSet);
renderDevice->dispatchComputeShader(numThreadGroups.x, numThreadGroups.y, numThreadGroups.z);
renderDevice->pipelineBarrier(frustumBuffer, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
}
void LightCullingPass::render()
{
}
void LightCullingPass::endFrame()
{
}
void LightCullingPass::publishOutputs()
{
}
void LightCullingPass::createRenderPass()
{
}
void LightCullingPass::modifyRenderPassMacros(Map<const char*, const char*>& defines)
{
}
@@ -0,0 +1,51 @@
#pragma once
#include "RenderPass.h"
namespace Seele
{
DECLARE_REF(CameraActor)
DECLARE_REF(CameraComponent)
DECLARE_NAME_REF(Gfx, Viewport)
DECLARE_NAME_REF(Gfx, Graphics)
class LightCullingPass : public RenderPass
{
public:
LightCullingPass(PRenderGraph renderGraph, Gfx::PViewport viewport, Gfx::PGraphics graphics, PCameraActor camera);
virtual ~LightCullingPass();
virtual void beginFrame() override;
virtual void render() override;
virtual void endFrame() override;
virtual void publishOutputs() override;
virtual void createRenderPass() override;
static void modifyRenderPassMacros(Map<const char*, const char*>& defines);
private:
static constexpr uint32 BLOCK_SIZE = 8;
_declspec(align(16)) struct DispatchParams
{
glm::uvec3 numThreadGroups;
uint32_t pad0;
glm::uvec3 numThreads;
uint32_t pad1;
} dispatchParams;
__declspec(align(16)) struct Plane
{
Vector n;
float d;
Vector p0;
Vector p1;
Vector p2;
};
__declspec(align(16)) struct Frustum
{
Plane planes[4];
};
struct ScreenToView
{
Matrix4 inverseProjection;
Vector2 screenDimensions;
};
Gfx::PViewport viewport;
Gfx::PGraphics graphics;
PCameraComponent source;
};
} // namespace Seele
@@ -0,0 +1,39 @@
#include "RenderGraph.h"
using namespace Seele;
RenderGraph::RenderGraph()
{
}
RenderGraph::~RenderGraph()
{
}
void RenderGraph::setup()
{
for(auto pass : renderPasses)
{
pass->publishOutputs();
}
for(auto pass : renderPasses)
{
pass->createRenderPass();
}
}
Gfx::PRenderTargetAttachment RenderGraph::requestRenderTarget(const std::string& outputName)
{
if(registeredAttachments.find(outputName) == registeredAttachments.end())
{
std::cout << "Attachment " << outputName << " not found" << std::endl;
return nullptr;
}
return registeredAttachments[outputName];
}
void RenderGraph::registerRenderPassOutput(const std::string& outputName, Gfx::PRenderTargetAttachment attachment)
{
registeredAttachments[outputName] = attachment;
}
@@ -0,0 +1,21 @@
#pragma once
#include "MinimalEngine.h"
#include "Graphics/GraphicsResources.h"
#include "RenderPass.h"
namespace Seele
{
class RenderGraph
{
public:
RenderGraph();
~RenderGraph();
void setup();
Gfx::PRenderTargetAttachment requestRenderTarget(const std::string& outputName);
void registerRenderPassOutput(const std::string& ouputName, Gfx::PRenderTargetAttachment attachment);
private:
Map<std::string, Gfx::PRenderTargetAttachment> registeredAttachments;
List<PRenderPass> renderPasses;
};
DEFINE_REF(RenderGraph)
} // namespace Seele
@@ -0,0 +1,13 @@
#include "RenderPass.h"
using namespace Seele;
RenderPass::RenderPass(PRenderGraph renderGraph)
: renderGraph(renderGraph)
{
}
RenderPass::~RenderPass()
{
}
@@ -0,0 +1,35 @@
#pragma once
#include "MinimalEngine.h"
#include "Math/Math.h"
namespace Seele
{
DECLARE_NAME_REF(Gfx, RenderPass)
DECLARE_REF(RenderGraph)
class RenderPass
{
public:
RenderPass(PRenderGraph rendergraph);
virtual ~RenderPass();
virtual void beginFrame() = 0;
virtual void render() = 0;
virtual void endFrame() = 0;
virtual void publishOutputs() = 0;
virtual void createRenderPass() = 0;
protected:
struct ViewParameter
{
Matrix4 viewMatrix;
Matrix4 projectionMatrix;
Vector4 cameraPosition;
} viewParams;
struct ScreenToViewParameter
{
Matrix4 inverseProjectionMatrix;
Vector2 screenDimensions;
} screenToViewParams;
Gfx::PRenderPass renderPass;
PRenderGraph renderGraph;
};
DEFINE_REF(RenderPass)
} // namespace Seele
@@ -30,8 +30,8 @@ void StaticMeshVertexInput::init(Gfx::PGraphics graphics)
elements.add(accessStreamComponent(data.positionStream, 0)); elements.add(accessStreamComponent(data.positionStream, 0));
} }
uint8 tangentBasisAttributes[2] = {1, 2}; uint8 tangentBasisAttributes[3] = {1, 2, 3};
for(int32 axisIndex = 0; axisIndex < 2; axisIndex++) for(int32 axisIndex = 0; axisIndex < 3; axisIndex++)
{ {
if(data.tangentBasisComponents[axisIndex].vertexBuffer != nullptr) if(data.tangentBasisComponents[axisIndex].vertexBuffer != nullptr)
{ {
@@ -41,15 +41,15 @@ void StaticMeshVertexInput::init(Gfx::PGraphics graphics)
if(data.colorComponent.vertexBuffer != nullptr) if(data.colorComponent.vertexBuffer != nullptr)
{ {
elements.add(accessStreamComponent(data.colorComponent, 3)); elements.add(accessStreamComponent(data.colorComponent, 4));
} }
else else
{ {
elements.add(accessStreamComponent(VertexStreamComponent(graphics->getNullVertexBuffer(), 0, 0, Gfx::SE_FORMAT_R32G32B32A32_SFLOAT), 3)); elements.add(accessStreamComponent(VertexStreamComponent(graphics->getNullVertexBuffer(), 0, 0, Gfx::SE_FORMAT_R32G32B32A32_SFLOAT), 4));
} }
if(data.textureCoordinates.size()) if(data.textureCoordinates.size())
{ {
const int32 baseTexCoordAttribute = 4; const int32 baseTexCoordAttribute = 5;
for(uint32 coordinateIndex = 0; coordinateIndex < data.textureCoordinates.size(); ++coordinateIndex) for(uint32 coordinateIndex = 0; coordinateIndex < data.textureCoordinates.size(); ++coordinateIndex)
{ {
elements.add(accessStreamComponent( elements.add(accessStreamComponent(
+1 -1
View File
@@ -7,7 +7,7 @@ enum { MAX_TEXCOORDS = 4 };
struct StaticMeshDataType struct StaticMeshDataType
{ {
VertexStreamComponent positionStream; VertexStreamComponent positionStream;
VertexStreamComponent tangentBasisComponents[2]; VertexStreamComponent tangentBasisComponents[3];
//Dont forget these are 4 component vectors //Dont forget these are 4 component vectors
Array<VertexStreamComponent> textureCoordinates; Array<VertexStreamComponent> textureCoordinates;
+1 -1
View File
@@ -101,7 +101,7 @@ Gfx::VertexElement VertexShaderInput::accessPositionStreamComponent(const Vertex
vertexStream.stride = component.stride; vertexStream.stride = component.stride;
vertexStream.offset = component.offset; vertexStream.offset = component.offset;
return Gfx::VertexElement((uint8)streams.indexOf(streams.addUnique(vertexStream)), component.offset, component.type, attributeIndex, vertexStream.stride); return Gfx::VertexElement((uint8)positionStreams.indexOf(positionStreams.addUnique(vertexStream)), component.offset, component.type, attributeIndex, vertexStream.stride);
} }
void VertexShaderInput::initDeclaration(Gfx::PGraphics graphics, Array<Gfx::VertexElement>& elements) void VertexShaderInput::initDeclaration(Gfx::PGraphics graphics, Array<Gfx::VertexElement>& elements)
@@ -8,8 +8,9 @@
using namespace Seele; using namespace Seele;
using namespace Seele::Vulkan; using namespace Seele::Vulkan;
DescriptorLayout::DescriptorLayout(PGraphics graphics) DescriptorLayout::DescriptorLayout(PGraphics graphics, const std::string& name)
: graphics(graphics) : Gfx::DescriptorLayout(name)
, graphics(graphics)
, layoutHandle(VK_NULL_HANDLE) , layoutHandle(VK_NULL_HANDLE)
{ {
} }
@@ -67,6 +68,7 @@ void PipelineLayout::create()
// There could be unused descriptor set indices // There could be unused descriptor set indices
if(descriptorSetLayouts[i] == nullptr) if(descriptorSetLayouts[i] == nullptr)
{ {
vulkanDescriptorLayouts[i] = VK_NULL_HANDLE;
continue; continue;
} }
PDescriptorLayout layout = descriptorSetLayouts[i].cast<DescriptorLayout>(); PDescriptorLayout layout = descriptorSetLayouts[i].cast<DescriptorLayout>();
@@ -81,7 +83,7 @@ void PipelineLayout::create()
{ {
vkPushConstants[i].offset = pushConstants[i].offset; vkPushConstants[i].offset = pushConstants[i].offset;
vkPushConstants[i].size = pushConstants[i].size; vkPushConstants[i].size = pushConstants[i].size;
vkPushConstants[i].stageFlags = cast((VkShaderStageFlagBits)pushConstants[i].stageFlags); vkPushConstants[i].stageFlags = (VkShaderStageFlagBits)pushConstants[i].stageFlags;
} }
createInfo.pushConstantRangeCount = (uint32)vkPushConstants.size(); createInfo.pushConstantRangeCount = (uint32)vkPushConstants.size();
createInfo.pPushConstantRanges = vkPushConstants.data(); createInfo.pPushConstantRanges = vkPushConstants.data();
@@ -9,7 +9,7 @@ DECLARE_REF(Graphics)
class DescriptorLayout : public Gfx::DescriptorLayout class DescriptorLayout : public Gfx::DescriptorLayout
{ {
public: public:
DescriptorLayout(PGraphics graphics); DescriptorLayout(PGraphics graphics, const std::string& name);
virtual ~DescriptorLayout(); virtual ~DescriptorLayout();
virtual void create(); virtual void create();
inline VkDescriptorSetLayout getHandle() const inline VkDescriptorSetLayout getHandle() const
@@ -22,6 +22,7 @@ private:
PGraphics graphics; PGraphics graphics;
Array<VkDescriptorSetLayoutBinding> bindings; Array<VkDescriptorSetLayoutBinding> bindings;
VkDescriptorSetLayout layoutHandle; VkDescriptorSetLayout layoutHandle;
std::string name;
friend class DescriptorAllocator; friend class DescriptorAllocator;
}; };
DEFINE_REF(DescriptorLayout) DEFINE_REF(DescriptorLayout)
@@ -164,6 +164,12 @@ Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(const GraphicsPipelineCr
return pipeline; return pipeline;
} }
Gfx::PComputePipeline Graphics::createComputePipeline(const ComputePipelineCreateInfo& createInfo)
{
PComputePipeline pipeline = pipelineCache->createPipeline(createInfo);
return pipeline;
}
Gfx::PSamplerState Graphics::createSamplerState(const SamplerCreateInfo&) Gfx::PSamplerState Graphics::createSamplerState(const SamplerCreateInfo&)
{ {
PSamplerState sampler = new SamplerState(); // TODO: proper sampler creation PSamplerState sampler = new SamplerState(); // TODO: proper sampler creation
@@ -172,9 +178,9 @@ Gfx::PSamplerState Graphics::createSamplerState(const SamplerCreateInfo&)
VK_CHECK(vkCreateSampler(handle, &vkInfo, nullptr, &sampler->sampler)); VK_CHECK(vkCreateSampler(handle, &vkInfo, nullptr, &sampler->sampler));
return sampler; return sampler;
} }
Gfx::PDescriptorLayout Graphics::createDescriptorLayout() Gfx::PDescriptorLayout Graphics::createDescriptorLayout(const std::string& name)
{ {
PDescriptorLayout layout = new DescriptorLayout(this); PDescriptorLayout layout = new DescriptorLayout(this, name);
return layout; return layout;
} }
Gfx::PPipelineLayout Graphics::createPipelineLayout() Gfx::PPipelineLayout Graphics::createPipelineLayout()
+2 -1
View File
@@ -60,9 +60,10 @@ public:
virtual Gfx::PGeometryShader createGeometryShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::PGeometryShader createGeometryShader(const ShaderCreateInfo& createInfo) override;
virtual Gfx::PFragmentShader createFragmentShader(const ShaderCreateInfo& createInfo) override; virtual Gfx::PFragmentShader createFragmentShader(const ShaderCreateInfo& createInfo) override;
virtual Gfx::PGraphicsPipeline createGraphicsPipeline(const GraphicsPipelineCreateInfo& createInfo) override; virtual Gfx::PGraphicsPipeline createGraphicsPipeline(const GraphicsPipelineCreateInfo& createInfo) override;
virtual Gfx::PComputePipeline createComputePipeline(const ComputePipelineCreateInfo& createInfo) override;
virtual Gfx::PSamplerState createSamplerState(const SamplerCreateInfo& createInfo) override; virtual Gfx::PSamplerState createSamplerState(const SamplerCreateInfo& createInfo) override;
virtual Gfx::PDescriptorLayout createDescriptorLayout() override; virtual Gfx::PDescriptorLayout createDescriptorLayout(const std::string& name = "") override;
virtual Gfx::PPipelineLayout createPipelineLayout() override; virtual Gfx::PPipelineLayout createPipelineLayout() override;
protected: protected:
@@ -319,7 +319,7 @@ public:
virtual ~Window(); virtual ~Window();
virtual void beginFrame() override; virtual void beginFrame() override;
virtual void endFrame() override; virtual void endFrame() override;
virtual Gfx::PTexture2D getBackBuffer() override; virtual Gfx::PTexture2D getBackBuffer() const override;
virtual void onWindowCloseEvent() override; virtual void onWindowCloseEvent() override;
virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) override; virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) override;
virtual void setMouseMoveCallback(std::function<void(double, double)> callback) override; virtual void setMouseMoveCallback(std::function<void(double, double)> callback) override;
@@ -24,4 +24,26 @@ void GraphicsPipeline::bind(VkCommandBuffer handle)
VkPipelineLayout GraphicsPipeline::getLayout() const VkPipelineLayout GraphicsPipeline::getLayout() const
{ {
return layout.cast<PipelineLayout>()->getHandle(); return layout.cast<PipelineLayout>()->getHandle();
}
ComputePipeline::ComputePipeline(PGraphics graphics, VkPipeline handle, PPipelineLayout pipelineLayout, const ComputePipelineCreateInfo& createInfo)
: Gfx::ComputePipeline(createInfo, pipelineLayout)
, graphics(graphics)
, pipeline(handle)
{
}
ComputePipeline::~ComputePipeline()
{
}
void ComputePipeline::bind(VkCommandBuffer handle)
{
vkCmdBindPipeline(handle, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
}
VkPipelineLayout ComputePipeline::getLayout() const
{
return layout.cast<PipelineLayout>()->getHandle();
} }
@@ -19,5 +19,17 @@ private:
VkPipeline pipeline; VkPipeline pipeline;
}; };
DEFINE_REF(GraphicsPipeline) DEFINE_REF(GraphicsPipeline)
class ComputePipeline : public Gfx::ComputePipeline
{
public:
ComputePipeline(PGraphics graphics, VkPipeline handle, PPipelineLayout pipelineLayout, const ComputePipelineCreateInfo& createInfo);
virtual ~ComputePipeline();
void bind(VkCommandBuffer handle);
VkPipelineLayout getLayout() const;
private:
PGraphics graphics;
VkPipeline pipeline;
};
DEFINE_REF(ComputePipeline)
} // namespace Vulkan } // namespace Vulkan
} // namespace Seele } // namespace Seele
@@ -352,4 +352,25 @@ PGraphicsPipeline PipelineCache::createPipeline(const GraphicsPipelineCreateInfo
PGraphicsPipeline result = new GraphicsPipeline(graphics, pipelineHandle, layout, gfxInfo); PGraphicsPipeline result = new GraphicsPipeline(graphics, pipelineHandle, layout, gfxInfo);
return result; return result;
}
PComputePipeline PipelineCache::createPipeline(const ComputePipelineCreateInfo& computeInfo)
{
VkComputePipelineCreateInfo createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
createInfo.pNext = 0;
createInfo.flags = 0;
createInfo.basePipelineIndex = 0;
createInfo.basePipelineHandle = VK_NULL_HANDLE;
auto layout = computeInfo.pipelineLayout.cast<PipelineLayout>();
createInfo.layout = layout->getHandle();
auto computeStage = computeInfo.computeShader.cast<ComputeShader>();
createInfo.stage = init::PipelineShaderStageCreateInfo(
VK_SHADER_STAGE_COMPUTE_BIT,
computeStage->getModuleHandle(),
computeStage->getEntryPointName());
VkPipeline pipelineHandle;
VK_CHECK(vkCreateComputePipelines(graphics->getDevice(), cache, 1, &createInfo, nullptr, &pipelineHandle));
PComputePipeline result = new ComputePipeline(graphics, pipelineHandle, layout, computeInfo);
return result;
} }
@@ -11,6 +11,7 @@ public:
PipelineCache(PGraphics graphics, const std::string& cacheFilePath); PipelineCache(PGraphics graphics, const std::string& cacheFilePath);
~PipelineCache(); ~PipelineCache();
PGraphicsPipeline createPipeline(const GraphicsPipelineCreateInfo& createInfo); PGraphicsPipeline createPipeline(const GraphicsPipelineCreateInfo& createInfo);
PComputePipeline createPipeline(const ComputePipelineCreateInfo& createInfo);
private: private:
VkPipelineCache cache; VkPipelineCache cache;
PGraphics graphics; PGraphics graphics;
+1 -1
View File
@@ -118,9 +118,9 @@ void Shader::create(const ShaderCreateInfo& createInfo)
if(spCompile(request)) if(spCompile(request))
{ {
char const* diagnostics = spGetDiagnosticOutput(request); char const* diagnostics = spGetDiagnosticOutput(request);
std::cout << "Compile error for shader " << createInfo.name << std::endl;
std::cout << diagnostics << std::endl; std::cout << diagnostics << std::endl;
} }
size_t dataSize = 0; size_t dataSize = 0;
const uint32* data = reinterpret_cast<const uint32*>(spGetEntryPointCode(request, entryPointIndex, &dataSize)); const uint32* data = reinterpret_cast<const uint32*>(spGetEntryPointCode(request, entryPointIndex, &dataSize));
@@ -104,7 +104,7 @@ void Window::onWindowCloseEvent()
{ {
} }
Gfx::PTexture2D Window::getBackBuffer() Gfx::PTexture2D Window::getBackBuffer() const
{ {
return backBufferImages[currentImageIndex]; return backBufferImages[currentImageIndex];
} }
+2 -2
View File
@@ -66,7 +66,7 @@ void BlinnPhong::generateMaterialCode(std::ofstream& codeStream, json codeJson)
}; };
accessorStream << "float3 getBaseColor(MaterialFragmentParameter input) {\n"; accessorStream << "float3 getBaseColor(MaterialFragmentParameter input) {\n";
generateAccessor(accessorStream, "baseColor", "float3(0, 0, 0)"); generateAccessor(accessorStream, "baseColor", "float3(0.5f, 0.5f, 0.5f)");
accessorStream << "}"; accessorStream << "}";
accessorStream << "float getMetallic(MaterialFragmentParameter input) {\n"; accessorStream << "float getMetallic(MaterialFragmentParameter input) {\n";
@@ -121,7 +121,7 @@ void BlinnPhong::generateMaterialCode(std::ofstream& codeStream, json codeJson)
codeStream << name << " result;" << std::endl; codeStream << name << " result;" << std::endl;
codeStream << "result.baseColor = getBaseColor(geometry);" << std::endl; codeStream << "result.baseColor = getBaseColor(geometry);" << std::endl;
codeStream << "result.metallic = getMetallic(geometry);" << std::endl; codeStream << "result.metallic = getMetallic(geometry);" << std::endl;
codeStream << "result.normal = geometry.transformNormalTexture(getNormal(geometry));" << std::endl; codeStream << "result.normal = getNormal(geometry);" << std::endl;
codeStream << "result.specular = getSpecular(geometry);" << std::endl; codeStream << "result.specular = getSpecular(geometry);" << std::endl;
codeStream << "result.roughness = getRoughness(geometry);" << std::endl; codeStream << "result.roughness = getRoughness(geometry);" << std::endl;
codeStream << "result.sheen = getSheen(geometry);" << std::endl; codeStream << "result.sheen = getSheen(geometry);" << std::endl;
+1 -1
View File
@@ -43,11 +43,11 @@ void Material::load()
void Material::compile() void Material::compile()
{ {
setStatus(Status::Loading); setStatus(Status::Loading);
layout = WindowManager::getGraphics()->createDescriptorLayout();
auto& stream = getReadStream(); auto& stream = getReadStream();
json j; json j;
stream >> j; stream >> j;
materialName = j["name"].get<std::string>(); materialName = j["name"].get<std::string>();
layout = WindowManager::getGraphics()->createDescriptorLayout(materialName + "Layout");
//Shader file needs to conform to the slang standard, which prohibits _ //Shader file needs to conform to the slang standard, which prohibits _
materialName.erase(std::remove(materialName.begin(), materialName.end(), '_'), materialName.end()); materialName.erase(std::remove(materialName.begin(), materialName.end(), '_'), materialName.end());
std::ofstream codeStream("./shaders/generated/"+materialName+".slang"); std::ofstream codeStream("./shaders/generated/"+materialName+".slang");
+6 -5
View File
@@ -14,16 +14,17 @@ Actor::~Actor()
} }
void Actor::tick(float deltaTime) void Actor::tick(float deltaTime)
{ {
rootComponent->tick(deltaTime); addWorldRotation(glm::vec3(0, 1 * deltaTime, 0));
for(auto child : children)
{
child->tick(deltaTime);
}
} }
void Actor::notifySceneAttach(PScene scene) void Actor::notifySceneAttach(PScene scene)
{ {
owningScene = scene; owningScene = scene;
scene->getSceneUpdater()->registerActorUpdate(this);
rootComponent->notifySceneAttach(scene); rootComponent->notifySceneAttach(scene);
for(auto child : children)
{
child->notifySceneAttach(scene);
}
} }
PScene Actor::getScene() PScene Actor::getScene()
+1 -4
View File
@@ -15,10 +15,6 @@ Component::~Component()
} }
void Component::tick(float deltaTime) void Component::tick(float deltaTime)
{ {
for (auto child : children)
{
child->tick(deltaTime);
}
} }
PComponent Component::getParent() PComponent Component::getParent()
{ {
@@ -40,6 +36,7 @@ PActor Component::getOwner()
void Component::notifySceneAttach(PScene scene) void Component::notifySceneAttach(PScene scene)
{ {
owningScene = scene; owningScene = scene;
scene->getSceneUpdater()->registerComponentUpdate(this);
for (auto child : children) for (auto child : children)
{ {
child->notifySceneAttach(scene); child->notifySceneAttach(scene);
+15 -11
View File
@@ -7,15 +7,26 @@
using namespace Seele; using namespace Seele;
inline float frand()
{
return (float)rand()/RAND_MAX;
}
Scene::Scene(Gfx::PGraphics graphics) Scene::Scene(Gfx::PGraphics graphics)
: graphics(graphics) : graphics(graphics)
, updater(new SceneUpdater())
{ {
lightEnv.directionalLights[0].color = Vector4(1, 0, 0, 1); lightEnv.directionalLights[0].color = Vector4(1, 1, 1, 1);
lightEnv.directionalLights[0].direction = Vector4(0, 0, 0, 1); lightEnv.directionalLights[0].direction = Vector4(1, 0, 0, 1);
lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1); lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1);
lightEnv.numDirectionalLights = 1; lightEnv.numDirectionalLights = 1;
lightEnv.numPointLights = 0;
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
for(uint32 i = 0; i < MAX_POINT_LIGHTS; ++i)
{
lightEnv.pointLights[i].colorRange = Vector4(frand(), frand(), frand(), frand() * 30);
lightEnv.pointLights[i].positionWS = Vector4(frand() * 200-100, frand(), frand() * 200-100, 1);
}
lightEnv.numPointLights = MAX_POINT_LIGHTS;
} }
Scene::~Scene() Scene::~Scene()
@@ -24,14 +35,7 @@ Scene::~Scene()
void Scene::tick(double deltaTime) void Scene::tick(double deltaTime)
{ {
lightEnv.directionalLights[0].direction.x += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime; updater->runUpdates(static_cast<float>(deltaTime));
lightEnv.directionalLights[0].direction.y += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime;
lightEnv.directionalLights[0].direction.z += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime;
std::cout << lightEnv.directionalLights[0].direction << std::endl;
for (auto actor : rootActors)
{
actor->tick(static_cast<float>(deltaTime));
}
} }
void Scene::addActor(PActor actor) void Scene::addActor(PActor actor)
+3
View File
@@ -5,6 +5,7 @@
#include "Components/PrimitiveComponent.h" #include "Components/PrimitiveComponent.h"
#include "Graphics/MeshBatch.h" #include "Graphics/MeshBatch.h"
#include "Material/Material.h" #include "Material/Material.h"
#include "SceneUpdater.h"
namespace Seele namespace Seele
{ {
@@ -46,11 +47,13 @@ public:
const Array<PPrimitiveComponent>& getPrimitives() const { return primitives; } const Array<PPrimitiveComponent>& getPrimitives() const { return primitives; }
const Array<MeshBatch>& getStaticMeshes() const { return staticMeshes; } const Array<MeshBatch>& getStaticMeshes() const { return staticMeshes; }
const LightEnv& getLightEnvironment() const { return lightEnv; } const LightEnv& getLightEnvironment() const { return lightEnv; }
UPSceneUpdater& getSceneUpdater() { return updater; }
private: private:
Array<MeshBatch> staticMeshes; Array<MeshBatch> staticMeshes;
Array<PActor> rootActors; Array<PActor> rootActors;
Array<PPrimitiveComponent> primitives; Array<PPrimitiveComponent> primitives;
LightEnv lightEnv; LightEnv lightEnv;
Gfx::PGraphics graphics; Gfx::PGraphics graphics;
UPSceneUpdater updater;
}; };
} // namespace Seele } // namespace Seele
+75
View File
@@ -0,0 +1,75 @@
#include "SceneUpdater.h"
#include "Components/Component.h"
#include "Actor/Actor.h"
using namespace Seele;
SceneUpdater::SceneUpdater()
: pendingUpdatesSem(0)
{
running.store(true);
workers.resize(std::thread::hardware_concurrency());
for (size_t i = 0; i < workers.size(); i++)
{
workers[i] = std::thread(&SceneUpdater::work, this);
}
}
SceneUpdater::~SceneUpdater()
{
running.store(false);
for (size_t i = 0; i < workers.size(); i++)
{
workers[i].join();
}
}
void SceneUpdater::registerComponentUpdate(PComponent component)
{
std::unique_lock lck(pendingUpdatesLock);
updatesRan.add([component](float delta) mutable { component->tick(delta); });
}
void SceneUpdater::registerActorUpdate(PActor actor)
{
std::unique_lock lck(pendingUpdatesLock);
updatesRan.add([actor](float delta) mutable { actor->tick(delta); });
}
void SceneUpdater::runUpdates(float delta)
{
std::unique_lock pendingLock(pendingUpdatesLock);
frameDelta = delta;
pendingUpdates = std::move(updatesRan);
updatesRan = List<std::function<void(float)>>();
std::unique_lock lck(frameFinishedLock);
pendingLock.unlock();
pendingUpdatesSem.release(pendingUpdates.size());
frameFinishedCV.wait(lck);
}
void SceneUpdater::work()
{
while(running.load())
{
pendingUpdatesSem.acquire();
std::function<void(float)> function;
{
std::unique_lock lck(pendingUpdatesLock);
function = std::move(pendingUpdates.front());
pendingUpdates.popFront();
if(pendingUpdates.empty())
{
std::unique_lock lck(frameFinishedLock);
frameFinishedCV.notify_all();
}
}
function(frameDelta);
{
std::unique_lock lck(pendingUpdatesLock);
updatesRan.add(std::move(function));
}
}
}
+16 -2
View File
@@ -1,16 +1,30 @@
#pragma once #pragma once
#include "Containers/Array.h" #include "Containers/List.h"
#include <semaphore>
namespace Seele namespace Seele
{ {
DECLARE_REF(Component);
DECLARE_REF(Actor);
class SceneUpdater class SceneUpdater
{ {
public: public:
SceneUpdater(); SceneUpdater();
~SceneUpdater(); ~SceneUpdater();
void registerComponentUpdate(PComponent component);
void registerActorUpdate(PActor actor);
void runUpdates(float delta);
private: private:
Array<std::thread> workers; Array<std::thread> workers;
List<std::function<void(float)> pendingUpdates; std::mutex pendingUpdatesLock;
std::counting_semaphore<> pendingUpdatesSem;
List<std::function<void(float)>> pendingUpdates;
List<std::function<void(float)>> updatesRan;
void work(); void work();
float frameDelta;
std::atomic_bool running;
std::mutex frameFinishedLock;
std::condition_variable frameFinishedCV;
}; };
DEFINE_REF(SceneUpdater)
} // namespace Seele } // namespace Seele
+8 -4
View File
@@ -2,6 +2,7 @@
#include "Scene/Scene.h" #include "Scene/Scene.h"
#include "Material/Material.h" #include "Material/Material.h"
#include "Asset/AssetRegistry.h" #include "Asset/AssetRegistry.h"
#include "Graphics/RenderPass/RenderGraph.h"
using namespace Seele; using namespace Seele;
@@ -9,7 +10,10 @@ SceneRenderPath::SceneRenderPath(PScene scene, Gfx::PGraphics graphics, Gfx::PVi
: RenderPath(graphics, target) : RenderPath(graphics, target)
, scene(scene) , scene(scene)
{ {
basePass = new BasePass(scene, graphics, target, source); renderGraph = new RenderGraph();
depthPrepass = new DepthPrepass(renderGraph, scene, graphics, target, source);
basePass = new BasePass(renderGraph, scene, graphics, target, source);
renderGraph->setup();
} }
SceneRenderPath::~SceneRenderPath() SceneRenderPath::~SceneRenderPath()
@@ -28,15 +32,15 @@ void SceneRenderPath::init()
void SceneRenderPath::beginFrame() void SceneRenderPath::beginFrame()
{ {
basePass->beginFrame(); depthPrepass->beginFrame();
} }
void SceneRenderPath::render() void SceneRenderPath::render()
{ {
basePass->render(); depthPrepass->render();
} }
void SceneRenderPath::endFrame() void SceneRenderPath::endFrame()
{ {
basePass->endFrame(); depthPrepass->endFrame();
} }
+5 -1
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "RenderPath.h" #include "RenderPath.h"
#include "Graphics/RenderPass/BasePass.h" #include "Graphics/RenderPass/BasePass.h"
#include "Graphics/RenderPass/DepthPrepass.h"
namespace Seele namespace Seele
{ {
@@ -19,6 +20,9 @@ public:
protected: protected:
PScene scene; PScene scene;
UPBasePass basePass; PRenderGraph renderGraph;
PDepthPrepass depthPrepass;
PBasePass basePass;
}; };
} // namespace Seele } // namespace Seele
+8 -2
View File
@@ -27,10 +27,16 @@ int main()
window->addView(sceneView); window->addView(sceneView);
sceneView->setFocused(); sceneView->setFocused();
AssetRegistry::init("D:\\Private\\Programming\\C++\\TestSeeleProject\\"); AssetRegistry::init("D:\\Private\\Programming\\C++\\TestSeeleProject\\");
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Arissa\\Arissa.fbx"); AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Ely\\Ely.fbx");
PPrimitiveComponent arissa = new PrimitiveComponent(AssetRegistry::findMesh("Arissa")); AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Cube\\cube.obj");
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.obj");
PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane"));
plane->setWorldScale(Vector(100, 100, 100));
plane->addWorldTranslation(Vector(0, -10, 0));
PPrimitiveComponent arissa = new PrimitiveComponent(AssetRegistry::findMesh("Ely"));
arissa->addWorldTranslation(Vector(0, 0, 100)); arissa->addWorldTranslation(Vector(0, 0, 100));
arissa->setWorldScale(Vector(0.1f, 0.1f, 0.1f)); arissa->setWorldScale(Vector(0.1f, 0.1f, 0.1f));
sceneView->getScene()->addPrimitiveComponent(plane);
sceneView->getScene()->addPrimitiveComponent(arissa); sceneView->getScene()->addPrimitiveComponent(arissa);
core.renderLoop(); core.renderLoop();
core.shutdown(); core.shutdown();