glClear is causing stack corrpution

This commit is contained in:
2023-11-19 01:16:41 -06:00
parent 26b5ec2c7f
commit 830694ee0a
22 changed files with 503 additions and 37 deletions

View File

@ -12,7 +12,7 @@
#include "component/display/MeshRenderer.hpp"
namespace Dawn {
struct IRenderPass {
class IRenderPass {
public:
std::shared_ptr<Mesh> mesh;
@ -43,14 +43,17 @@ namespace Dawn {
};
template<class S, typename D>
struct RenderPass : public IRenderPass {
class RenderPass : public IRenderPass {
private:
std::shared_ptr<S> shader;
const D data;
const std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
> &textures;
std::shared_ptr<Mesh> mesh;
const enum MeshDrawMode drawMode;
const int32_t indiceStart;
const int32_t indiceCount;
const D data;
public:
/**
@ -66,12 +69,16 @@ namespace Dawn {
RenderPass(
SceneComponent &self,
const D d,
const std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
> textures,
const std::shared_ptr<Mesh> mesh,
const enum MeshDrawMode drawMode,
const int32_t indiceStart,
const int32_t indiceCount
) :
data(d),
textures(textures),
mesh(mesh),
drawMode(drawMode),
indiceStart(indiceStart),
@ -91,6 +98,7 @@ namespace Dawn {
}
void bind() override {
std::cout << "textures: " << textures.size() << "\n";
shader->bind();
}
@ -99,6 +107,10 @@ namespace Dawn {
}
void upload() override {
for(auto &pair : textures) {
assertNotNull(pair.second, "Texture cannot be null!");
pair.second->bind(pair.first);
}
shader->upload();
}
@ -139,17 +151,21 @@ namespace Dawn {
* @return Created render pass.
*/
template<class S, typename D>
std::shared_ptr<struct IRenderPass> createRenderPass(
std::shared_ptr<IRenderPass> createRenderPass(
SceneComponent &self,
const D data,
const std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
> textures = {},
const std::shared_ptr<Mesh> mesh = nullptr,
const enum MeshDrawMode drawMode = MeshDrawMode::TRIANGLES,
int32_t indiceStart = 0,
int32_t indiceCount = -1
) {
return std::make_shared<struct RenderPass<S,D>>(
return std::make_shared<RenderPass<S,D>>(
self,
data,
textures,
mesh,
drawMode,
indiceStart,

View File

@ -11,7 +11,16 @@ struct Color SimpleTexturedMaterial::getColor() {
return this->data.color;
}
void SimpleTexturedMaterial::setColor(struct Color color) {
std::shared_ptr<Texture> SimpleTexturedMaterial::getTexture() {
return this->texture;
}
void SimpleTexturedMaterial::setTexture(std::shared_ptr<Texture> texture) {
this->texture = texture;
}
void SimpleTexturedMaterial::setColor(const struct Color color) {
this->data.color = color;
}
@ -22,11 +31,23 @@ std::vector<std::shared_ptr<IRenderPass>> SimpleTexturedMaterial::getPasses(
this->data.projection = ctx.camera->getProjection();
this->data.view = ctx.camera->getItem()->getWorldTransform();
this->data.color = COLOR_RED;
auto textures = std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
>();
if(this->texture) {
this->data.hasTexture = true;
this->data.texture = 0;
textures[this->data.texture] = this->texture;
} else {
this->data.hasTexture = false;
}
return {
createRenderPass<SimpleTexturedShader, struct SimpleTexturedShaderData>(
*this,
data
data,
textures
)
};
}

View File

@ -6,11 +6,13 @@
#pragma once
#include "component/display/material/Material.hpp"
#include "display/shader/SimpleTexturedShader.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class SimpleTexturedMaterial : public Material {
private:
struct SimpleTexturedShaderData data;
std::shared_ptr<Texture> texture;
public:
/**
@ -18,12 +20,26 @@ namespace Dawn {
*/
struct Color getColor();
/**
* Returns the texture of this material.
*
* @return The texture of this material.
*/
std::shared_ptr<Texture> getTexture();
/**
* Sets the texture of this material.
*
* @param texture The texture to set.
*/
void setTexture(std::shared_ptr<Texture> texture);
/**
* Sets the color of this material.
*
* @param color The color to set.
*/
void setColor(struct Color color);
void setColor(const struct Color color);
std::vector<std::shared_ptr<IRenderPass>> getPasses(
struct RenderPassContext &ctx

View File

@ -0,0 +1,109 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/Color.hpp"
namespace Dawn {
enum TextureFormat {
TEXTURE_FORMAT_R = 1,
TEXTURE_FORMAT_RG = 2,
TEXTURE_FORMAT_RGB = 3,
TEXTURE_FORMAT_RGBA = 4
};
enum TextureWrapMode {
TEXTURE_WRAP_MODE_REPEAT = 0,
TEXTURE_WRAP_MODE_MIRRORED_REPEAT = 1,
TEXTURE_WRAP_MODE_CLAMP_TO_EDGE = 2,
TEXTURE_WRAP_MODE_CLAMP_TO_BORDER = 3
};
enum TextureFilterMode {
TEXTURE_FILTER_MODE_NEAREST = 0,
TEXTURE_FILTER_MODE_LINEAR = 1
};
enum TextureDataFormat {
TEXTURE_DATA_FORMAT_UNSIGNED_BYTE = 0,
TEXTURE_DATA_FORMAT_FLOAT = 1
};
class ITexture {
public:
enum TextureWrapMode wrapModeX = TEXTURE_WRAP_MODE_REPEAT;
enum TextureWrapMode wrapModeY = TEXTURE_WRAP_MODE_REPEAT;
enum TextureFilterMode filterModeMin = TEXTURE_FILTER_MODE_NEAREST;
enum TextureFilterMode filterModeMag = TEXTURE_FILTER_MODE_NEAREST;
enum TextureFilterMode mipMapFilterModeMin = TEXTURE_FILTER_MODE_NEAREST;
enum TextureFilterMode mipMapFilterModeMag = TEXTURE_FILTER_MODE_NEAREST;
/**
* Returns the width of the texture.
*
* @return Width of the texture.
*/
virtual int32_t getWidth() = 0;
/**
* Returns the height of the texture.
*
* @return Height of the texture.
*/
virtual int32_t getHeight() = 0;
/**
* Initializes a texture.
*
* @param width Width of the texture (in pixels).
* @param height Height of the texture (in pixels).
* @param format Data format of the texture to use.
* @param dataFormat Data format of the texture to use.
*/
virtual void setSize(
const int32_t width,
const int32_t height,
const enum TextureFormat format,
const enum TextureDataFormat dataFormat
) = 0;
/**
* Returns true only when the texture has been loaded, sized and put on
* the gpu for rendering.
*
* @return True if ready, otherwise false.
*/
// virtual bool_t isReady() = 0;
/**
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so
* avoid doing this too often.
*
* @param pixels Array of pixels you're trying to buffer.
*/
virtual void buffer(const struct ColorU8 pixels[]) = 0;
/**
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so
* avoid doing this too often.
*
* @param pixels Array of pixels you're trying to buffer.
*/
virtual void buffer(const uint8_t pixels[]) = 0;
/**
* Binds the texture to the given slot (for use by the shaders).
*
* @param slot Slot to bind to.
*/
virtual void bind(const uint8_t slot) = 0;
/**
* Disposes of the texture.
*/
virtual ~ITexture() {
}
};
}

View File

@ -68,6 +68,7 @@ void RenderPipeline::renderSceneCamera(
assertNotNull(game, "Game cannot be null");
assertNotNull(scene, "Scene cannot be null");
assertNotNull(camera, "Camera cannot be null");
assertNotNull(renderTarget, "RenderTarget cannot be null");
struct RenderPassContext ctx = {
game,
@ -77,14 +78,15 @@ void RenderPipeline::renderSceneCamera(
};
// Get list of renderables
std::vector<std::shared_ptr<struct IRenderPass>> renderPasses;
std::vector<std::shared_ptr<IRenderPass>> renderPasses;
auto renderables = scene->findComponents<IRenderableComponent>();
for(auto renderable : renderables) {
auto rp = renderable->getPasses(ctx);
renderPasses.insert(renderPasses.end(), rp.begin(), rp.end());
}
// Sort the render passes
auto rp = renderPasses[0];
rp->bind();
// TODO: Make clearing the buffers editable!
renderTarget->bind();
@ -92,17 +94,15 @@ void RenderPipeline::renderSceneCamera(
RENDER_TARGET_CLEAR_COLOR |
RENDER_TARGET_CLEAR_DEPTH
);
rp->bind();
std::for_each(
renderPasses.begin(),
renderPasses.end(),
[&](std::shared_ptr<struct IRenderPass> pass) {
pass->bind();
pass->setData();
pass->upload();
pass->draw();
}
);
for(auto renderPass : renderPasses) {
renderPass->bind();
renderPass->setData();
renderPass->upload();
renderPass->draw();
}
}
RenderPipeline::~RenderPipeline() {

View File

@ -6,8 +6,8 @@
#pragma once
#include "event/Event.hpp"
#define RENDER_TARGET_CLEAR_COLOR 1 << 0
#define RENDER_TARGET_CLEAR_DEPTH 1 << 1
#define RENDER_TARGET_CLEAR_COLOR (1 << 0)
#define RENDER_TARGET_CLEAR_DEPTH (1 << 1)
namespace Dawn {
class RenderTarget {

View File

@ -123,6 +123,12 @@ glm::quat SceneItemTransform::getLocalRotation() {
return this->localRotation;
}
void SceneItemTransform::setLocalTransform(const glm::mat4 transform) {
this->transformLocal = transform;
this->updateLocalValuesFromLocalTransform();
this->updateWorldTransformFromLocalTransform();
}
void SceneItemTransform::setWorldTransform(const glm::mat4 transform) {
this->transformWorld = transform;
this->updateLocalTransformFromWorldTransform();

View File

@ -106,6 +106,13 @@ namespace Dawn {
* @return Local rotation of this item.
*/
glm::quat getLocalRotation();
/**
* Sets the transform of this item within local space (relative to parent)
*
* @param transform Transform of this item in local space.
*/
void setLocalTransform(const glm::mat4 transform);
/**
* Sets the transform of this item within world space (relative to scene