Files
Seele/src/Engine/Graphics/GraphicsResources.cpp
T

395 lines
9.8 KiB
C++
Raw Normal View History

#include "GraphicsResources.h"
2020-06-02 11:46:18 +02:00
#include "Graphics.h"
2021-05-06 17:02:10 +02:00
#include "RenderPass/DepthPrepass.h"
#include "RenderPass/BasePass.h"
2020-04-12 15:47:19 +02:00
using namespace Seele;
using namespace Seele::Gfx;
std::string getShaderNameFromRenderPassType(Gfx::RenderPassType type)
{
switch (type)
{
case Gfx::RenderPassType::DepthPrepass:
return "DepthPrepass.slang";
case Gfx::RenderPassType::BasePass:
return "ForwardPlus.slang";
default:
return "";
}
}
2021-05-06 17:02:10 +02:00
void modifyRenderPassMacros(Gfx::RenderPassType type, Map<const char*, const char*>& defines)
{
switch (type)
{
case Gfx::RenderPassType::DepthPrepass:
DepthPrepass::modifyRenderPassMacros(defines);
2021-10-15 23:12:29 +02:00
break;
2021-05-06 17:02:10 +02:00
case Gfx::RenderPassType::BasePass:
BasePass::modifyRenderPassMacros(defines);
2021-10-15 23:12:29 +02:00
break;
2021-05-06 17:02:10 +02:00
}
}
ShaderMap::ShaderMap()
{
}
ShaderMap::~ShaderMap()
{
}
const ShaderCollection* ShaderMap::findShaders(PermutationId&& id) const
{
for(uint32 i = 0; i < shaders.size(); ++i)
{
if(shaders[i].id == id)
{
return &(shaders[i]);
}
}
return nullptr;
}
ShaderCollection& ShaderMap::createShaders(
PGraphics graphics,
RenderPassType renderPass,
2021-10-19 23:04:38 +02:00
PMaterialAsset material,
2020-09-19 14:36:50 +02:00
VertexInputType* vertexInput,
2021-04-01 16:40:14 +02:00
bool /*bPositionOnly*/)
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lock(shadersLock);
ShaderCollection& collection = shaders.add();
2020-09-19 14:36:50 +02:00
//collection.vertexDeclaration = bPositionOnly ? vertexInput->getPositionDeclaration() : vertexInput->getDeclaration();
ShaderCreateInfo createInfo;
createInfo.entryPoint = "vertexMain";
2020-09-19 14:36:50 +02:00
createInfo.typeParameter = {material->getName().c_str()};
createInfo.defines["VERTEX_INPUT_IMPORT"] = vertexInput->getShaderFilename();
createInfo.defines["MATERIAL_IMPORT"] = material->getName().c_str();
createInfo.defines["NUM_MATERIAL_TEXCOORDS"] = "1";
createInfo.defines["USE_INSTANCING"] = "0";
2021-05-06 17:02:10 +02:00
modifyRenderPassMacros(renderPass, createInfo.defines);
createInfo.name = getShaderNameFromRenderPassType(renderPass) + " Material " + material->getName();
2021-10-23 00:22:35 +02:00
std::ifstream codeStream("./shaders/" + getShaderNameFromRenderPassType(renderPass));
createInfo.shaderCode.add(std::string(std::istreambuf_iterator<char>{codeStream}, {}));
2020-09-19 14:36:50 +02:00
collection.vertexShader = graphics->createVertexShader(createInfo);
2020-09-19 14:36:50 +02:00
if(renderPass != RenderPassType::DepthPrepass)
{
createInfo.entryPoint = "fragmentMain";
collection.fragmentShader = graphics->createFragmentShader(createInfo);
}
2020-10-03 11:00:10 +02:00
ShaderPermutation permutation;
2021-04-13 23:09:16 +02:00
std::string materialName = material->getName();
2020-10-03 11:00:10 +02:00
std::string vertexInputName = vertexInput->getName();
permutation.passType = renderPass;
std::memcpy(permutation.materialName, materialName.c_str(), sizeof(permutation.materialName));
std::memcpy(permutation.vertexInputName, vertexInputName.c_str(), sizeof(permutation.vertexInputName));
collection.id = PermutationId(permutation);
2020-09-19 14:36:50 +02:00
return collection;
}
void DescriptorLayout::addDescriptorBinding(uint32 bindingIndex, SeDescriptorType type, uint32 arrayCount)
{
if (descriptorBindings.size() <= bindingIndex)
{
descriptorBindings.resize(bindingIndex + 1);
}
DescriptorBinding binding;
binding.binding = bindingIndex;
binding.descriptorType = type;
binding.descriptorCount = arrayCount;
descriptorBindings[bindingIndex] = binding;
}
2021-05-10 23:57:55 +02:00
PDescriptorSet DescriptorLayout::allocateDescriptorSet()
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lock(allocatorLock);
PDescriptorSet result;
allocator->allocateDescriptorSet(result);
return result;
}
2020-10-03 11:00:10 +02:00
void DescriptorLayout::reset()
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lock(allocatorLock);
2020-10-03 11:00:10 +02:00
allocator->reset();
}
void PipelineLayout::addDescriptorLayout(uint32 setIndex, PDescriptorLayout layout)
{
if (descriptorSetLayouts.size() <= setIndex)
{
descriptorSetLayouts.resize(setIndex + 1);
}
2021-10-16 12:59:11 +02:00
descriptorSetLayouts[setIndex] = layout;
2020-10-03 11:00:10 +02:00
layout->setIndex = setIndex;
}
2020-04-12 15:47:19 +02:00
void PipelineLayout::addPushConstants(const SePushConstantRange &pushConstant)
{
pushConstants.add(pushConstant);
}
2020-04-01 02:17:49 +02:00
2020-09-19 14:36:50 +02:00
QueueOwnedResource::QueueOwnedResource(QueueFamilyMapping mapping, QueueType startQueueType)
2021-04-01 16:40:14 +02:00
: currentOwner(startQueueType)
, mapping(mapping)
2020-06-02 11:46:18 +02:00
{
}
QueueOwnedResource::~QueueOwnedResource()
{
}
void QueueOwnedResource::transferOwnership(QueueType newOwner)
{
2020-09-19 14:36:50 +02:00
if(mapping.needsTransfer(currentOwner, newOwner))
2020-06-02 11:46:18 +02:00
{
executeOwnershipBarrier(newOwner);
}
2021-05-10 23:57:55 +02:00
currentOwner = newOwner;
}
void QueueOwnedResource::pipelineBarrier(SeAccessFlags srcAccess, SePipelineStageFlags srcStage, SeAccessFlags dstAccess, SePipelineStageFlags dstStage)
{
// maybe add some checks
executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
2020-06-02 11:46:18 +02:00
}
2020-09-19 14:36:50 +02:00
Buffer::Buffer(QueueFamilyMapping mapping, QueueType startQueue)
: QueueOwnedResource(mapping, startQueue)
2020-06-02 11:46:18 +02:00
{
}
Buffer::~Buffer()
{
}
2020-10-03 11:00:10 +02:00
UniformBuffer::UniformBuffer(QueueFamilyMapping mapping, const BulkResourceData& resourceData)
: Buffer(mapping, resourceData.owner)
2021-04-25 23:43:40 +02:00
, contents(resourceData.size)
2020-04-12 15:47:19 +02:00
{
2020-10-03 11:00:10 +02:00
if(resourceData.data != nullptr)
{
2021-04-25 23:43:40 +02:00
std::memcpy(contents.data(), resourceData.data, contents.size());
2020-10-03 11:00:10 +02:00
}
2020-04-12 15:47:19 +02:00
}
UniformBuffer::~UniformBuffer()
{
}
2020-10-03 11:00:10 +02:00
2021-04-25 23:43:40 +02:00
bool UniformBuffer::updateContents(const BulkResourceData& resourceData)
2020-10-03 11:00:10 +02:00
{
2021-04-25 23:43:40 +02:00
assert(contents.size() == resourceData.size);
if(std::memcmp(contents.data(), resourceData.data, contents.size()) == 0)
{
return false;
}
std::memcpy(contents.data(), resourceData.data, contents.size());
return true;
2020-10-03 11:00:10 +02:00
}
StructuredBuffer::StructuredBuffer(QueueFamilyMapping mapping, const BulkResourceData& resourceData)
: Buffer(mapping, resourceData.owner)
, contents(resourceData.size)
2020-06-02 11:46:18 +02:00
{
}
StructuredBuffer::~StructuredBuffer()
{
}
bool StructuredBuffer::updateContents(const BulkResourceData& resourceData)
{
assert(contents.size() == resourceData.size);
if(std::memcmp(contents.data(), resourceData.data, contents.size()) == 0)
{
return false;
}
std::memcpy(contents.data(), resourceData.data, contents.size());
return true;
}
2020-09-19 14:36:50 +02:00
VertexBuffer::VertexBuffer(QueueFamilyMapping mapping, uint32 numVertices, uint32 vertexSize, QueueType startQueueType)
: Buffer(mapping, startQueueType)
, numVertices(numVertices)
, vertexSize(vertexSize)
2020-05-05 01:51:13 +02:00
{
}
VertexBuffer::~VertexBuffer()
{
}
2020-09-19 14:36:50 +02:00
IndexBuffer::IndexBuffer(QueueFamilyMapping mapping, uint32 size, Gfx::SeIndexType indexType, QueueType startQueueType)
: Buffer(mapping, startQueueType)
2020-06-02 11:46:18 +02:00
, indexType(indexType)
2020-05-05 01:51:13 +02:00
{
switch (indexType)
{
case SE_INDEX_TYPE_UINT16:
2020-10-23 01:47:56 +02:00
numIndices = size / sizeof(uint16);
2020-05-05 01:51:13 +02:00
break;
case SE_INDEX_TYPE_UINT32:
2020-10-23 01:47:56 +02:00
numIndices = size / sizeof(uint32);
2020-05-05 01:51:13 +02:00
default:
break;
}
}
IndexBuffer::~IndexBuffer()
{
}
2020-06-08 01:44:47 +02:00
VertexStream::VertexStream()
{}
2020-09-19 14:36:50 +02:00
VertexStream::VertexStream(uint32 stride, uint32 offset, uint8 instanced)
2021-04-01 16:40:14 +02:00
: stride(stride)
, offset(offset)
, instanced(instanced)
2020-05-05 01:51:13 +02:00
{
}
VertexStream::~VertexStream()
{
}
void VertexStream::addVertexElement(VertexElement element)
{
vertexDescription.add(element);
}
const Array<VertexElement> VertexStream::getVertexDescriptions() const
{
return vertexDescription;
}
VertexDeclaration::VertexDeclaration()
{
}
VertexDeclaration::~VertexDeclaration()
{
}
2020-10-03 11:00:10 +02:00
static std::mutex vertexDeclarationLock;
static Map<uint32, PVertexDeclaration> vertexDeclarationCache;
PVertexDeclaration VertexDeclaration::createDeclaration(PGraphics graphics, const Array<VertexElement>& elementList)
2020-05-05 01:51:13 +02:00
{
2022-01-12 14:40:26 +01:00
std::scoped_lock lock(vertexDeclarationLock);
2020-10-03 11:00:10 +02:00
boost::crc_32_type result;
result.process_bytes(&elementList, sizeof(VertexElement) * elementList.size());
uint32 key = result.checksum();
auto found = vertexDeclarationCache[key];
if(found == nullptr)
{
return found;
}
PVertexDeclaration newDeclaration = graphics->createVertexDeclaration(elementList);
vertexDeclarationCache[key] = newDeclaration;
return newDeclaration;
2020-05-05 01:51:13 +02:00
}
2020-04-12 15:47:19 +02:00
2020-09-19 14:36:50 +02:00
Texture::Texture(QueueFamilyMapping mapping, Gfx::QueueType startQueueType)
: QueueOwnedResource(mapping, startQueueType)
2020-06-02 11:46:18 +02:00
{
}
Texture::~Texture()
{
}
2020-09-19 14:36:50 +02:00
Texture2D::Texture2D(QueueFamilyMapping mapping, Gfx::QueueType startQueueType)
: Texture(mapping, startQueueType)
2020-06-02 11:46:18 +02:00
{
}
Texture2D::~Texture2D()
{
}
RenderCommand::RenderCommand()
{
}
RenderCommand::~RenderCommand()
{
}
2021-05-10 23:57:55 +02:00
ComputeCommand::ComputeCommand()
{
}
ComputeCommand::~ComputeCommand()
{
}
2020-04-01 02:17:49 +02:00
RenderTargetLayout::RenderTargetLayout()
2021-04-01 16:40:14 +02:00
: inputAttachments()
, colorAttachments()
, depthAttachment()
2020-04-01 02:17:49 +02:00
{
}
2020-04-12 15:47:19 +02:00
RenderTargetLayout::RenderTargetLayout(PRenderTargetAttachment depthAttachment)
2021-04-01 16:40:14 +02:00
: inputAttachments()
, colorAttachments()
, depthAttachment(depthAttachment)
, width(depthAttachment->getTexture()->getSizeX())
, height(depthAttachment->getTexture()->getSizeY())
2020-04-01 02:17:49 +02:00
{
}
2020-04-12 15:47:19 +02:00
RenderTargetLayout::RenderTargetLayout(PRenderTargetAttachment colorAttachment, PRenderTargetAttachment depthAttachment)
2021-04-01 16:40:14 +02:00
: inputAttachments()
, depthAttachment(depthAttachment)
, width(depthAttachment->getTexture()->getSizeX())
, height(depthAttachment->getTexture()->getSizeY())
2020-04-01 02:17:49 +02:00
{
colorAttachments.add(colorAttachment);
}
2021-04-01 16:40:14 +02:00
RenderTargetLayout::RenderTargetLayout(Array<PRenderTargetAttachment> colorAttachments, PRenderTargetAttachment depthAttachment)
: inputAttachments()
, colorAttachments(colorAttachments)
, depthAttachment(depthAttachment)
, width(depthAttachment->getTexture()->getSizeX())
, height(depthAttachment->getTexture()->getSizeY())
2020-04-01 02:17:49 +02:00
{
}
2020-04-12 15:47:19 +02:00
RenderTargetLayout::RenderTargetLayout(Array<PRenderTargetAttachment> inputAttachments, Array<PRenderTargetAttachment> colorAttachments, PRenderTargetAttachment depthAttachment)
2021-04-01 16:40:14 +02:00
: inputAttachments(inputAttachments)
, colorAttachments(colorAttachments)
, depthAttachment(depthAttachment)
, width(depthAttachment->getTexture()->getSizeX())
, height(depthAttachment->getTexture()->getSizeY())
2020-04-01 02:17:49 +02:00
{
}
2020-04-12 15:47:19 +02:00
Window::Window(const WindowCreateInfo &createInfo)
2021-04-01 16:40:14 +02:00
: sizeX(createInfo.width)
, sizeY(createInfo.height)
, bFullscreen(createInfo.bFullscreen)
, title(createInfo.title)
, pixelFormat(createInfo.pixelFormat)
, samples(createInfo.numSamples)
2020-04-12 15:47:19 +02:00
{
}
Window::~Window()
{
}
Viewport::Viewport(PWindow owner, const ViewportCreateInfo &viewportInfo)
2021-04-01 16:40:14 +02:00
: sizeX(viewportInfo.sizeX)
, sizeY(viewportInfo.sizeY)
, offsetX(viewportInfo.offsetX)
, offsetY(viewportInfo.offsetY)
, owner(owner)
2020-04-12 15:47:19 +02:00
{
}
Viewport::~Viewport()
{
2020-10-03 11:00:10 +02:00
}