This commit is contained in:
2024-12-25 00:34:24 -06:00
parent afa6a1a036
commit ba305de596
10 changed files with 227 additions and 49 deletions

View File

@ -8,4 +8,5 @@ target_sources(${DAWN_TARGET_NAME}
PRIVATE
ShaderProgram.cpp
ShaderStage.cpp
ShaderData.cpp
)

View File

@ -0,0 +1,63 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "ShaderData.hpp"
#include "assert/assert.hpp"
#include "assert/assertgl.hpp"
using namespace Dawn;
void ShaderData::beginStruct() {
// Do nothing
}
void ShaderData::writeMat4(const glm::mat4 &mat) {
char_t name[1024];
GLsizei length;
GLint size;
GLenum type;
glGetActiveUniform(getShader()->id, i, sizeof(name), &length, &size, &type, name);
assertNoGLError();
std::cout << "Uniform: " << name << std::endl;
glUniformMatrix4fv(this->i, 1, GL_FALSE, glm::value_ptr(mat));
assertNoGLError();
this->i++;
}
void ShaderData::writeVec3(const glm::vec3 &vec) {
assertUnreachable("Not implemented");
}
void ShaderData::writeVec4(const glm::vec4 &vec) {
assertUnreachable("Not implemented");
}
void ShaderData::writeColor(const struct Color &color) {
glUniform4fv(this->i, 1, &color.r);
assertNoGLError();
this->i++;
}
void ShaderData::writeBoolean(const bool &value) {
glUniform1i(this->i, value ? 1 : 0);
assertNoGLError();
this->i++;
}
void ShaderData::writeTexture(std::shared_ptr<Texture> &texture) {
if(texture == nullptr) {
this->i++;
return;
}
texture->bind(0);
glUniform1i(this->i, 0);
assertNoGLError();
this->i++;
}
void ShaderData::endStruct() {
// Do nothing
}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/IShaderData.hpp"
namespace Dawn {
class ShaderData : public IShaderData {
private:
int32_t i = 0;
protected:
void beginStruct() override;
void writeMat4(const glm::mat4 &mat) override;
void writeVec3(const glm::vec3 &vec) override;
void writeVec4(const glm::vec4 &vec) override;
void writeColor(const struct Color &color) override;
void writeBoolean(const bool &value) override;
void writeTexture(std::shared_ptr<Texture> &texture) override;
void endStruct() override;
};
}

View File

@ -7,6 +7,7 @@
#include "assert/assert.hpp"
#include "assert/assertgl.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp"
using namespace Dawn;
@ -50,31 +51,43 @@ void ShaderProgram::init(
// So the uniforms that are in slang are kinda odd when compiled.
//DEBUGGING
GLint numUniforms = 0;
glGetProgramiv(this->id, GL_ACTIVE_UNIFORMS, &numUniforms);
// GLint numUniforms = 0;
// glGetProgramiv(this->id, GL_ACTIVE_UNIFORMS, &numUniforms);
// assertNoGLError();
// for(GLint i = 0; i < numUniforms; ++i) {
// char name[1024];
// GLsizei length;
// GLint size;
// GLenum type;
// glGetActiveUniform(this->id, i, sizeof(name), &length, &size, &type, name);
// assertNoGLError();
// std::cout << "Uniform: " << i << ": " << name << " has size " << size << " and length " << length << std::endl;
// }
// GLint numUniformBlocks = 0;
// glGetProgramiv(this->id, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
// for(GLint i = 0; i < numUniformBlocks; ++i) {
// GLint size;
// glGetActiveUniformBlockiv(this->id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &size);
// assertNoGLError();
// std::cout << "Uniform Block: " << i << " has size " << size << std::endl;
// }
glUseProgram(this->id);
assertNoGLError();
for(GLint i = 0; i < numUniforms; ++i) {
char name[1024];
GLsizei length;
GLint size;
GLenum type;
glGetActiveUniform(this->id, i, sizeof(name), &length, &size, &type, name);
assertNoGLError();
std::cout << "Uniform: " << i << ": " << name << " has size " << size << " and length " << length << std::endl;
}
GLint numUniformBlocks = 0;
glGetProgramiv(this->id, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
for(GLint i = 0; i < numUniformBlocks; ++i) {
GLint size;
glGetActiveUniformBlockiv(this->id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &size);
assertNoGLError();
std::cout << "Uniform Block: " << i << " has size " << size << std::endl;
}
auto data = std::make_shared<SimpleTexturedMaterialShaderData>();
data->color = COLOR_WHITE;
data->model = glm::mat4(1.0f);
data->projection = glm::perspective(
glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f
);
data->view = glm::lookAt(
glm::vec3(4,3,3), glm::vec3(0,0,0), glm::vec3(0,1,0)
);
data->write(shared_from_this());
}
ShaderProgram::~ShaderProgram() {

View File

@ -8,7 +8,12 @@
#include "dawnopengl.hpp"
namespace Dawn {
class ShaderProgram : public IShaderProgram {
class ShaderData;
class ShaderProgram :
public IShaderProgram,
public std::enable_shared_from_this<ShaderProgram>
{
private:
GLuint id = -1;
@ -17,5 +22,7 @@ namespace Dawn {
const std::vector<std::shared_ptr<ShaderStage>> &stages
) override;
~ShaderProgram();
friend class ShaderData;
};
}