mirror of
https://gitea.windcorp.ru/Wind-Corporation/Progressia.git
synced 2025-04-21 12:20:46 +03:00
TMP / Fixed vulkan_adapter.cpp
This commit is contained in:
parent
2c45f519fb
commit
2dc60d589c
@ -50,7 +50,7 @@ auto getVertexFieldProperties() {
|
||||
|
||||
namespace {
|
||||
std::vector<char> tmp_readFile(const std::string &path) {
|
||||
auto resource = __embedded_resources::getEmbeddedResource(path.c_str());
|
||||
auto resource = __embedded_resources::getEmbeddedResource(path);
|
||||
|
||||
if (resource.data == nullptr) {
|
||||
// REPORT_ERROR
|
||||
@ -59,7 +59,7 @@ std::vector<char> tmp_readFile(const std::string &path) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return std::vector<char>(resource.data, resource.data + resource.length);
|
||||
return {resource.data, resource.data + resource.length};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@ -82,25 +82,26 @@ Adapter::Adapter(Vulkan &vulkan)
|
||||
VK_ATTACHMENT_LOAD_OP_CLEAR,
|
||||
VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
||||
|
||||
{1.0f, 0},
|
||||
{1.0F, 0},
|
||||
|
||||
nullptr});
|
||||
}
|
||||
|
||||
Adapter::~Adapter() {
|
||||
// Do nothing
|
||||
}
|
||||
Adapter::~Adapter() = default;
|
||||
|
||||
std::vector<Attachment> &Adapter::getAttachments() { return attachments; }
|
||||
|
||||
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing
|
||||
std::vector<char> Adapter::loadVertexShader() {
|
||||
return tmp_readFile("shader.vert.spv");
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing
|
||||
std::vector<char> Adapter::loadFragmentShader() {
|
||||
return tmp_readFile("shader.frag.spv");
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing
|
||||
VkVertexInputBindingDescription Adapter::getVertexInputBindingDescription() {
|
||||
VkVertexInputBindingDescription bindingDescription{};
|
||||
bindingDescription.binding = 0;
|
||||
@ -111,6 +112,7 @@ VkVertexInputBindingDescription Adapter::getVertexInputBindingDescription() {
|
||||
}
|
||||
|
||||
std::vector<VkVertexInputAttributeDescription>
|
||||
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing
|
||||
Adapter::getVertexInputAttributeDescriptions() {
|
||||
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
||||
|
||||
@ -163,122 +165,125 @@ struct DrawRequest {
|
||||
glm::mat4 modelTransform;
|
||||
};
|
||||
|
||||
// NOLINTNEXTLINE: TODO
|
||||
std::vector<DrawRequest> pendingDrawCommands;
|
||||
constexpr std::size_t PENDING_DRAW_COMMANDS_MAX_SIZE = 100000;
|
||||
|
||||
// NOLINTNEXTLINE: TODO
|
||||
glm::mat4 currentModelTransform;
|
||||
|
||||
} // namespace
|
||||
|
||||
progressia::main::Texture::Texture(Backend backend) : backend(backend) {}
|
||||
struct progressia::main::Texture::Backend {
|
||||
progressia::desktop::Texture texture;
|
||||
};
|
||||
|
||||
progressia::main::Texture::~Texture() {
|
||||
delete static_cast<progressia::desktop::Texture *>(this->backend);
|
||||
}
|
||||
progressia::main::Texture::Texture(std::unique_ptr<Backend> backend)
|
||||
: backend(std::move(backend)) {}
|
||||
|
||||
namespace {
|
||||
struct PrimitiveBackend {
|
||||
progressia::main::Texture::~Texture() = default;
|
||||
|
||||
struct Primitive::Backend {
|
||||
IndexedBuffer<Vertex> buf;
|
||||
progressia::main::Texture *tex;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Primitive::Primitive(Backend backend) : backend(backend) {}
|
||||
Primitive::Primitive(std::unique_ptr<Backend> backend)
|
||||
: backend(std::move(backend)) {}
|
||||
|
||||
Primitive::~Primitive() {
|
||||
delete static_cast<PrimitiveBackend *>(this->backend);
|
||||
}
|
||||
Primitive::~Primitive() = default;
|
||||
|
||||
void Primitive::draw() {
|
||||
auto backend = static_cast<PrimitiveBackend *>(this->backend);
|
||||
|
||||
if (pendingDrawCommands.size() > 100000) {
|
||||
if (pendingDrawCommands.size() > PENDING_DRAW_COMMANDS_MAX_SIZE) {
|
||||
backend->buf.getVulkan().getGint().flush();
|
||||
}
|
||||
|
||||
pendingDrawCommands.push_back(
|
||||
{static_cast<progressia::desktop::Texture *>(backend->tex->backend),
|
||||
&backend->buf, currentModelTransform});
|
||||
pendingDrawCommands.push_back({&backend->tex->backend->texture,
|
||||
&backend->buf, currentModelTransform});
|
||||
}
|
||||
|
||||
const progressia::main::Texture *Primitive::getTexture() const {
|
||||
return static_cast<PrimitiveBackend *>(this->backend)->tex;
|
||||
return backend->tex;
|
||||
}
|
||||
|
||||
View::View(Backend backend) : backend(backend) {}
|
||||
struct View::Backend {
|
||||
Adapter::ViewUniform::State state;
|
||||
};
|
||||
|
||||
View::~View() {
|
||||
delete static_cast<Adapter::ViewUniform::State *>(this->backend);
|
||||
}
|
||||
View::View(std::unique_ptr<Backend> backend) : backend(std::move(backend)) {}
|
||||
|
||||
View::~View() = default;
|
||||
|
||||
void View::configure(const glm::mat4 &proj, const glm::mat4 &view) {
|
||||
|
||||
static_cast<Adapter::ViewUniform::State *>(this->backend)
|
||||
->update(proj, view);
|
||||
backend->state.update(proj, view);
|
||||
}
|
||||
|
||||
void View::use() {
|
||||
auto backend = static_cast<Adapter::ViewUniform::State *>(this->backend);
|
||||
backend->uniform->getVulkan().getGint().flush();
|
||||
backend->bind();
|
||||
backend->state.uniform->getVulkan().getGint().flush();
|
||||
backend->state.bind();
|
||||
}
|
||||
|
||||
Light::Light(Backend backend) : backend(backend) {}
|
||||
struct Light::Backend {
|
||||
Adapter::LightUniform::State state;
|
||||
};
|
||||
|
||||
Light::~Light() {
|
||||
delete static_cast<Adapter::LightUniform::State *>(this->backend);
|
||||
}
|
||||
Light::Light(std::unique_ptr<Backend> backend) : backend(std::move(backend)) {}
|
||||
Light::~Light() = default;
|
||||
|
||||
void Light::configure(const glm::vec3 &color, const glm::vec3 &from,
|
||||
float contrast, float softness) {
|
||||
|
||||
static_cast<Adapter::LightUniform::State *>(this->backend)
|
||||
->update(Adapter::Light{glm::vec4(color, 1.0f),
|
||||
glm::vec4(glm::normalize(from), 1.0f), contrast,
|
||||
softness});
|
||||
backend->state.update(Adapter::Light{glm::vec4(color, 1.0F),
|
||||
glm::vec4(glm::normalize(from), 1.0F),
|
||||
contrast, softness});
|
||||
}
|
||||
|
||||
void Light::use() {
|
||||
auto backend = static_cast<Adapter::LightUniform::State *>(this->backend);
|
||||
backend->uniform->getVulkan().getGint().flush();
|
||||
backend->bind();
|
||||
backend->state.uniform->getVulkan().getGint().flush();
|
||||
backend->state.bind();
|
||||
}
|
||||
|
||||
GraphicsInterface::GraphicsInterface(Backend backend) : backend(backend) {}
|
||||
|
||||
GraphicsInterface::~GraphicsInterface() {
|
||||
// Do nothing
|
||||
}
|
||||
GraphicsInterface::~GraphicsInterface() = default;
|
||||
|
||||
progressia::main::Texture *
|
||||
std::unique_ptr<progressia::main::Texture>
|
||||
GraphicsInterface::newTexture(const progressia::main::Image &src) {
|
||||
auto backend = new progressia::desktop::Texture(
|
||||
src, *static_cast<Vulkan *>(this->backend));
|
||||
using Backend = progressia::main::Texture::Backend;
|
||||
|
||||
return new Texture(backend);
|
||||
return std::make_unique<progressia::main::Texture>(
|
||||
std::unique_ptr<Backend>(new Backend{progressia::desktop::Texture(
|
||||
src, *static_cast<Vulkan *>(this->backend))}));
|
||||
}
|
||||
|
||||
Primitive *
|
||||
std::unique_ptr<Primitive>
|
||||
GraphicsInterface::newPrimitive(const std::vector<Vertex> &vertices,
|
||||
const std::vector<Vertex::Index> &indices,
|
||||
progressia::main::Texture *texture) {
|
||||
|
||||
auto backend = new PrimitiveBackend{
|
||||
IndexedBuffer<Vertex>(vertices.size(), indices.size(),
|
||||
*static_cast<Vulkan *>(this->backend)),
|
||||
texture};
|
||||
auto primitive = std::make_unique<Primitive>(
|
||||
std::unique_ptr<Primitive::Backend>(new Primitive::Backend{
|
||||
IndexedBuffer<Vertex>(vertices.size(), indices.size(),
|
||||
*static_cast<Vulkan *>(this->backend)),
|
||||
texture}));
|
||||
|
||||
backend->buf.load(vertices.data(), indices.data());
|
||||
primitive->backend->buf.load(vertices.data(), indices.data());
|
||||
|
||||
return new Primitive(backend);
|
||||
return primitive;
|
||||
}
|
||||
|
||||
View *GraphicsInterface::newView() {
|
||||
return new View(new Adapter::ViewUniform::State(
|
||||
static_cast<Vulkan *>(this->backend)->getAdapter().createView()));
|
||||
std::unique_ptr<View> GraphicsInterface::newView() {
|
||||
return std::make_unique<View>(std::unique_ptr<View::Backend>(
|
||||
new View::Backend{Adapter::ViewUniform::State(
|
||||
static_cast<Vulkan *>(this->backend)->getAdapter().createView())}));
|
||||
}
|
||||
|
||||
Light *GraphicsInterface::newLight() {
|
||||
return new Light(new Adapter::LightUniform::State(
|
||||
static_cast<Vulkan *>(this->backend)->getAdapter().createLight()));
|
||||
std::unique_ptr<Light> GraphicsInterface::newLight() {
|
||||
return std::make_unique<Light>(
|
||||
std::unique_ptr<Light::Backend>(new Light::Backend{
|
||||
Adapter::LightUniform::State(static_cast<Vulkan *>(this->backend)
|
||||
->getAdapter()
|
||||
.createLight())}));
|
||||
}
|
||||
|
||||
glm::vec2 GraphicsInterface::getViewport() const {
|
||||
@ -287,16 +292,17 @@ glm::vec2 GraphicsInterface::getViewport() const {
|
||||
return {extent.width, extent.height};
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing
|
||||
void GraphicsInterface::setModelTransform(const glm::mat4 &m) {
|
||||
currentModelTransform = m;
|
||||
}
|
||||
|
||||
void GraphicsInterface::flush() {
|
||||
|
||||
auto commandBuffer = static_cast<Vulkan *>(this->backend)
|
||||
->getCurrentFrame()
|
||||
->getCommandBuffer();
|
||||
auto pipelineLayout =
|
||||
auto *commandBuffer = static_cast<Vulkan *>(this->backend)
|
||||
->getCurrentFrame()
|
||||
->getCommandBuffer();
|
||||
auto *pipelineLayout =
|
||||
static_cast<Vulkan *>(this->backend)->getPipeline().getLayout();
|
||||
|
||||
progressia::desktop::Texture *lastTexture = nullptr;
|
||||
@ -328,6 +334,7 @@ void GraphicsInterface::flush() {
|
||||
pendingDrawCommands.clear();
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE: TODO
|
||||
float GraphicsInterface::tmp_getTime() { return glfwGetTime(); }
|
||||
|
||||
uint64_t GraphicsInterface::getLastStartedFrame() {
|
||||
|
@ -62,10 +62,10 @@ void initialize(GraphicsInterface &gintp) {
|
||||
debug("game init begin");
|
||||
gint = &gintp;
|
||||
|
||||
texture1.reset(
|
||||
gint->newTexture(progressia::main::loadImage("assets/texture.png")));
|
||||
texture2.reset(
|
||||
gint->newTexture(progressia::main::loadImage("assets/texture2.png")));
|
||||
texture1 =
|
||||
gint->newTexture(progressia::main::loadImage("assets/texture.png"));
|
||||
texture2 =
|
||||
gint->newTexture(progressia::main::loadImage("assets/texture2.png"));
|
||||
|
||||
// Cube 1
|
||||
{
|
||||
@ -91,7 +91,7 @@ void initialize(GraphicsInterface &gintp) {
|
||||
c.normal = normal;
|
||||
}
|
||||
|
||||
cube1.reset(gint->newPrimitive(vertices, indices, &*texture1));
|
||||
cube1 = gint->newPrimitive(vertices, indices, &*texture1);
|
||||
}
|
||||
|
||||
// Cube 2
|
||||
@ -118,11 +118,11 @@ void initialize(GraphicsInterface &gintp) {
|
||||
c.normal = normal;
|
||||
}
|
||||
|
||||
cube2.reset(gint->newPrimitive(vertices, indices, &*texture2));
|
||||
cube2 = gint->newPrimitive(vertices, indices, &*texture2);
|
||||
}
|
||||
|
||||
perspective.reset(gint->newView());
|
||||
light.reset(gint->newLight());
|
||||
perspective = gint->newView();
|
||||
light = gint->newLight();
|
||||
|
||||
debug("game init complete");
|
||||
}
|
||||
|
@ -25,30 +25,26 @@ struct Vertex {
|
||||
};
|
||||
|
||||
class Texture : private progressia::main::NonCopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
struct Backend;
|
||||
std::unique_ptr<Backend> backend;
|
||||
friend class GraphicsInterface;
|
||||
|
||||
friend class Primitive;
|
||||
|
||||
public:
|
||||
Texture(Backend);
|
||||
Texture(std::unique_ptr<Backend>);
|
||||
~Texture();
|
||||
};
|
||||
|
||||
class Primitive : private progressia::main::NonCopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
|
||||
struct Backend;
|
||||
std::unique_ptr<Backend> backend;
|
||||
friend class GraphicsInterface;
|
||||
|
||||
public:
|
||||
Primitive(Backend);
|
||||
Primitive(std::unique_ptr<Backend>);
|
||||
~Primitive();
|
||||
|
||||
void draw();
|
||||
@ -57,14 +53,13 @@ class Primitive : private progressia::main::NonCopyable {
|
||||
};
|
||||
|
||||
class View : private progressia::main::NonCopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
struct Backend;
|
||||
std::unique_ptr<Backend> backend;
|
||||
friend class GraphicsInterface;
|
||||
|
||||
public:
|
||||
View(Backend);
|
||||
View(std::unique_ptr<Backend>);
|
||||
~View();
|
||||
|
||||
void configure(const glm::mat4 &proj, const glm::mat4 &view);
|
||||
@ -72,14 +67,13 @@ class View : private progressia::main::NonCopyable {
|
||||
};
|
||||
|
||||
class Light : private progressia::main::NonCopyable {
|
||||
public:
|
||||
using Backend = void *;
|
||||
|
||||
private:
|
||||
Backend backend;
|
||||
struct Backend;
|
||||
std::unique_ptr<Backend> backend;
|
||||
friend class GraphicsInterface;
|
||||
|
||||
public:
|
||||
Light(Backend);
|
||||
Light(std::unique_ptr<Backend>);
|
||||
~Light();
|
||||
|
||||
void configure(const glm::vec3 &color, const glm::vec3 &from,
|
||||
@ -98,18 +92,18 @@ class GraphicsInterface : private progressia::main::NonCopyable {
|
||||
GraphicsInterface(Backend);
|
||||
~GraphicsInterface();
|
||||
|
||||
Texture *newTexture(const Image &);
|
||||
std::unique_ptr<Texture> newTexture(const Image &);
|
||||
|
||||
Primitive *newPrimitive(const std::vector<Vertex> &,
|
||||
const std::vector<Vertex::Index> &,
|
||||
Texture *texture);
|
||||
std::unique_ptr<Primitive> newPrimitive(const std::vector<Vertex> &,
|
||||
const std::vector<Vertex::Index> &,
|
||||
Texture *texture);
|
||||
|
||||
glm::vec2 getViewport() const;
|
||||
|
||||
void setModelTransform(const glm::mat4 &);
|
||||
|
||||
View *newView();
|
||||
Light *newLight();
|
||||
std::unique_ptr<View> newView();
|
||||
std::unique_ptr<Light> newLight();
|
||||
|
||||
void flush();
|
||||
void startNextLayer();
|
||||
|
@ -14,7 +14,9 @@ Checks: "-*,\
|
||||
-readability-else-after-return,\
|
||||
-readability-named-parameter,\
|
||||
-readability-use-anyofallof,\
|
||||
-cppcoreguidelines-pro-bounds-pointer-arithmetic"
|
||||
-cppcoreguidelines-pro-bounds-pointer-arithmetic,\
|
||||
-performance-trivially-destructible,\
|
||||
-modernize-make-unique"
|
||||
|
||||
# modernize-use-trailing-return-type
|
||||
# ignore reason: reduces readability
|
||||
@ -47,3 +49,13 @@ Checks: "-*,\
|
||||
|
||||
# cppcoreguidelines-pro-bounds-pointer-arithmetic
|
||||
# ignore reason: infeasible to avoid in C++17
|
||||
|
||||
# performance-trivially-destructible
|
||||
# ignore reason: breaks incapsulation too often
|
||||
|
||||
# modernize-make-unique
|
||||
# ignore reason: impossible with brace-init lists:
|
||||
# struct S { int a; int b; };
|
||||
# std::unique_ptr<S>(new S { 1, 2 }) // works
|
||||
# std::make_unique<S>(1, 2) // error
|
||||
# std::make_unique<S>({1, 2}) // error
|
||||
|
Loading…
x
Reference in New Issue
Block a user