Files
OpenVulkano/openVulkanoCpp/Scene/Shader/Shader.hpp

157 lines
3.9 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Base/ICloseable.hpp"
#include "Base/Utils.hpp"
#include "VertexInputDescription.hpp"
#include "ShaderProgramType.hpp"
#include "DescriptorInputDescription.hpp"
#include <string>
#include <stdexcept>
namespace openVulkanoCpp::Scene
{
enum class CullMode : uint32_t
{
NONE = 0,
FRONT,
BACK,
FRONT_AND_BACK
};
enum class Topology : uint32_t
{
POINT_LIST = 0,
LINE_LIST,
LINE_STRIPE,
TRIANGLE_LIST,
TRIANGLE_STRIP,
TRIANGLE_FAN,
LINE_LIST_WITH_ADJACENCY,
LINE_STRIP_WITH_ADJACENCY,
TRIANGLE_LIST_WITH_ADJACENCY,
TRIANGLE_STRIP_WITH_ADJACENCY,
PATCH_LIST
};
struct ShaderProgram
{
ShaderProgramType type;
std::string name;
ShaderProgram(ShaderProgramType type, const std::string& name) : type(type), name(name) {}
ShaderProgram(const ShaderProgram& program) : type(program.type), name(program.name) {}
ShaderProgram(ShaderProgram&& program) : type(program.type), name(std::move(program.name)) {}
[[nodiscard]] std::string GetShaderNameOpenGL() const
{
std::string oglName;
std::string_view ext = type.GetExtensionOpenGL();
oglName.reserve(name.size() + ext.size());
return oglName.append(name).append(ext);
}
[[nodiscard]] std::string GetShaderNameVulkan() const
{
return GetShaderNameOpenGL() + ".spv";
}
};
class Shader final : public ICloseable
{
public:
std::vector<ShaderProgram> shaderPrograms{};
std::vector<VertexInputDescription> vertexInputDescriptions{};
std::vector<DescriptorSetLayoutBinding> descriptorSetLayoutBindings{};
Topology topology = Topology::TRIANGLE_LIST;
CullMode cullMode = CullMode::BACK;
ICloseable* renderShader = nullptr;
bool alphaBlend = false; // TODO allow fine control over blending
Shader() = default;
~Shader() override { if (renderShader) Shader::Close(); }
Shader& AddShaderProgram(const ShaderProgram& shaderProgram)
{
CheckShaderInitState();
shaderPrograms.push_back(shaderProgram);
return *this;
}
Shader& AddShaderProgram(ShaderProgram&& shaderProgram)
{
CheckShaderInitState();
shaderPrograms.push_back(std::move(shaderProgram));
return *this;
}
template<typename ...ARGS>
Shader& AddShaderProgram(ARGS&&... args)
{
CheckShaderInitState();
shaderPrograms.emplace_back(std::forward<ARGS>(args)...);
return *this;
}
Shader& AddVertexInputDescription(const VertexInputDescription& inputDescription, int bindingId = -1)
{
CheckShaderInitState();
if (bindingId < 0) bindingId = static_cast<int>(vertexInputDescriptions.size());
while (bindingId > vertexInputDescriptions.size())
{
vertexInputDescriptions.emplace_back(0, 0);
}
if (bindingId == vertexInputDescriptions.size())
{
vertexInputDescriptions.emplace_back(bindingId, inputDescription);
}
else
{
vertexInputDescriptions[bindingId] = inputDescription;
}
return *this;
}
Shader& AddDescriptorSetLayoutBinding(const DescriptorSetLayoutBinding& binding, int bindingId = -1)
{
CheckShaderInitState();
if (bindingId < 0) bindingId = static_cast<int>(descriptorSetLayoutBindings.size());
while (bindingId > descriptorSetLayoutBindings.size())
{
descriptorSetLayoutBindings.emplace_back().bindingId = descriptorSetLayoutBindings.size() - 1;
}
if (bindingId == vertexInputDescriptions.size())
{
descriptorSetLayoutBindings.emplace_back(bindingId, binding);
}
else
{
descriptorSetLayoutBindings[bindingId] = binding;
descriptorSetLayoutBindings[bindingId].bindingId = bindingId;
}
return *this;
}
void Close() override
{
renderShader->Close();
renderShader = nullptr;
}
private:
void CheckShaderInitState() const
{
if (renderShader) throw std::runtime_error("Shader already initialized!");
}
};
}