2020-03-08 13:38:40 +01:00
|
|
|
#include "GraphicsResources.h"
|
2020-05-05 01:51:13 +02:00
|
|
|
#include "Material/MaterialInstance.h"
|
2020-08-06 00:54:43 +02:00
|
|
|
#include "Material/Material.h"
|
2020-06-02 11:46:18 +02:00
|
|
|
#include "Graphics.h"
|
2020-03-08 13:38:40 +01:00
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
using namespace Seele;
|
2020-03-20 01:27:40 +01:00
|
|
|
using namespace Seele::Gfx;
|
2020-03-08 13:38:40 +01:00
|
|
|
|
2020-08-06 00:54:43 +02:00
|
|
|
std::string getShaderNameFromRenderPassType(Gfx::RenderPassType type)
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case Gfx::RenderPassType::DepthPrepass:
|
|
|
|
|
return "DepthPrepass.slang";
|
|
|
|
|
case Gfx::RenderPassType::BasePass:
|
|
|
|
|
return "ForwardPlus.slang";
|
|
|
|
|
default:
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
PMaterial material,
|
2020-09-19 14:36:50 +02:00
|
|
|
VertexInputType* vertexInput,
|
2020-08-06 00:54:43 +02:00
|
|
|
bool bPositionOnly)
|
|
|
|
|
{
|
|
|
|
|
ShaderCollection& collection = shaders.add();
|
2020-09-19 14:36:50 +02:00
|
|
|
//collection.vertexDeclaration = bPositionOnly ? vertexInput->getPositionDeclaration() : vertexInput->getDeclaration();
|
2020-08-06 00:54:43 +02:00
|
|
|
|
|
|
|
|
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";
|
2020-08-06 00:54:43 +02:00
|
|
|
|
2020-09-19 14:36:50 +02:00
|
|
|
std::ifstream codeStream("./shaders/" + getShaderNameFromRenderPassType(renderPass), std::ios::ate);
|
|
|
|
|
auto fileSize = codeStream.tellg();
|
|
|
|
|
codeStream.seekg(0);
|
|
|
|
|
Array<char> buffer(static_cast<uint32>(fileSize));
|
|
|
|
|
codeStream.read(buffer.data(), fileSize);
|
|
|
|
|
|
|
|
|
|
createInfo.shaderCode.add(std::string(buffer.data()));
|
|
|
|
|
|
2020-08-06 00:54:43 +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;
|
|
|
|
|
std::string materialName = material->getFileName(); //Filename is fine, because it just has to be unique
|
|
|
|
|
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
|
|
|
|
2020-08-06 00:54:43 +02:00
|
|
|
return collection;
|
|
|
|
|
}
|
2020-03-20 01:27:40 +01:00
|
|
|
void DescriptorLayout::addDescriptorBinding(uint32 bindingIndex, SeDescriptorType type, uint32 arrayCount)
|
2020-03-08 13:38:40 +01:00
|
|
|
{
|
|
|
|
|
if (descriptorBindings.size() <= bindingIndex)
|
|
|
|
|
{
|
|
|
|
|
descriptorBindings.resize(bindingIndex + 1);
|
|
|
|
|
}
|
|
|
|
|
DescriptorBinding binding;
|
|
|
|
|
binding.binding = bindingIndex;
|
|
|
|
|
binding.descriptorType = type;
|
|
|
|
|
binding.descriptorCount = arrayCount;
|
|
|
|
|
descriptorBindings[bindingIndex] = binding;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
PDescriptorSet DescriptorLayout::allocatedDescriptorSet()
|
2020-03-08 13:38:40 +01:00
|
|
|
{
|
|
|
|
|
PDescriptorSet result;
|
|
|
|
|
allocator->allocateDescriptorSet(result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
void DescriptorLayout::reset()
|
|
|
|
|
{
|
|
|
|
|
allocator->reset();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void PipelineLayout::addDescriptorLayout(uint32 setIndex, PDescriptorLayout layout)
|
2020-03-08 13:38:40 +01:00
|
|
|
{
|
|
|
|
|
if (descriptorSetLayouts.size() <= setIndex)
|
|
|
|
|
{
|
|
|
|
|
descriptorSetLayouts.resize(setIndex + 1);
|
|
|
|
|
}
|
|
|
|
|
if (descriptorSetLayouts[setIndex] != nullptr)
|
|
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
auto &thisBindings = descriptorSetLayouts[setIndex]->descriptorBindings;
|
|
|
|
|
auto &otherBindings = layout->descriptorBindings;
|
2020-03-08 13:38:40 +01:00
|
|
|
thisBindings.resize(otherBindings.size());
|
|
|
|
|
for (size_t i = 0; i < otherBindings.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if (otherBindings[i].descriptorType != SE_DESCRIPTOR_TYPE_MAX_ENUM)
|
|
|
|
|
{
|
|
|
|
|
thisBindings[i] = otherBindings[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
descriptorSetLayouts[setIndex] = layout;
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
layout->setIndex = setIndex;
|
2020-03-08 13:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
void PipelineLayout::addPushConstants(const SePushConstantRange &pushConstant)
|
2020-03-08 13:38:40 +01:00
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
: mapping(mapping)
|
2020-06-02 11:46:18 +02:00
|
|
|
, currentOwner(startQueueType)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
currentOwner = newOwner;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
, size(resourceData.size)
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2020-10-03 11:00:10 +02:00
|
|
|
if(resourceData.data != nullptr)
|
|
|
|
|
{
|
|
|
|
|
contents = new uint8[size];
|
|
|
|
|
std::memcpy(contents, resourceData.data, size);
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UniformBuffer::~UniformBuffer()
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
|
|
|
|
void UniformBuffer::updateContents(const BulkResourceData& resourceData)
|
|
|
|
|
{
|
|
|
|
|
assert(size == resourceData.size);
|
|
|
|
|
std::memcpy(contents, resourceData.data, size);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 14:36:50 +02:00
|
|
|
StructuredBuffer::StructuredBuffer(QueueFamilyMapping mapping, QueueType startQueueType)
|
|
|
|
|
: Buffer(mapping, startQueueType)
|
2020-06-02 11:46:18 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
StructuredBuffer::~StructuredBuffer()
|
|
|
|
|
{
|
|
|
|
|
}
|
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:
|
|
|
|
|
numIndices = size / 16;
|
|
|
|
|
break;
|
|
|
|
|
case SE_INDEX_TYPE_UINT32:
|
|
|
|
|
numIndices = size / 32;
|
|
|
|
|
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)
|
|
|
|
|
: stride(stride), instanced(instanced), offset(offset)
|
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
|
|
|
{
|
2020-10-03 11:00:10 +02:00
|
|
|
std::scoped_lock lock(vertexDeclarationLock);
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 02:17:49 +02:00
|
|
|
RenderTargetLayout::RenderTargetLayout()
|
2020-04-12 15:47:19 +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)
|
2020-10-03 11:00:10 +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)
|
2020-10-03 11:00:10 +02:00
|
|
|
: inputAttachments(), depthAttachment(depthAttachment), width(depthAttachment->getTexture()->getSizeX()), height(depthAttachment->getTexture()->getSizeY())
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
|
|
|
|
colorAttachments.add(colorAttachment);
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
RenderTargetLayout::RenderTargetLayout(Array<PRenderTargetAttachment> colorAttachments, PRenderTargetAttachment depthAttachmet)
|
2020-10-03 11:00:10 +02:00
|
|
|
: 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)
|
2020-10-03 11:00:10 +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)
|
2020-08-11 21:23:20 +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)
|
|
|
|
|
: sizeX(viewportInfo.sizeX), sizeY(viewportInfo.sizeY), offsetX(viewportInfo.offsetX), offsetY(viewportInfo.offsetY), owner(owner)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Viewport::~Viewport()
|
|
|
|
|
{
|
2020-10-03 11:00:10 +02:00
|
|
|
}
|
|
|
|
|
|