Uniform arrays working.

This commit is contained in:
2023-12-11 23:54:52 -06:00
parent 81adf91816
commit 0fa08a1c92
18 changed files with 447 additions and 23 deletions

View File

@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME}
Shader.cpp
ShaderStage.cpp
SimpleTexturedShader.cpp
UIShader.cpp
)

View File

@ -22,6 +22,7 @@ namespace Dawn {
std::string name;
size_t offset;
enum ShaderParameterType type;
int32_t count;
GLint location = -1;
@ -33,6 +34,19 @@ namespace Dawn {
this->name = name;
this->offset = (size_t)offset;
this->type = type;
this->count = 1;
}
ShaderOpenGLParameter(
const std::string &name,
const void *offset,
const enum ShaderParameterType type,
const int32_t count
) {
this->name = name;
this->offset = (size_t)offset;
this->type = type;
this->count = count;
}
};
@ -136,6 +150,9 @@ namespace Dawn {
switch(param.type) {
case ShaderParameterType::MAT4: {
glm::mat4 *matrix = (glm::mat4 *)value;
if(param.count != 1) {
assertUnreachable("I haven't implemented multiple mat4s");
}
glUniformMatrix4fv(
param.location, 1, GL_FALSE, glm::value_ptr(*matrix)
);
@ -144,25 +161,21 @@ namespace Dawn {
case ShaderParameterType::COLOR: {
auto color = (Color *)value;
glUniform4f(
glUniform4fv(
param.location,
color->r,
color->g,
color->b,
color->a
param.count,
(GLfloat*)value
);
break;
}
case ShaderParameterType::BOOLEAN: {
auto boolean = (bool_t *)value;
glUniform1i(param.location, *boolean ? 1 : 0);
glUniform1iv(param.location, param.count, (GLint*)value);
break;
}
case ShaderParameterType::TEXTURE: {
textureslot_t texture = *((textureslot_t*)value);
glUniform1i(param.location, texture);
glUniform1iv(param.location, param.count, (GLint*)value);
break;
}

View File

@ -0,0 +1,100 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/shader/UIShader.hpp"
using namespace Dawn;
void UIShader::getStages(
const enum ShaderOpenGLVariant variant,
const struct UIShaderData *rel,
std::vector<std::shared_ptr<ShaderStage>> &stages,
std::vector<struct ShaderOpenGLParameter> &parameters
) {
// Stages
std::shared_ptr<ShaderStage> vertex;
std::shared_ptr<ShaderStage> fragment;
switch(variant) {
case ShaderOpenGLVariant::GLSL_330_CORE:
vertex = std::make_shared<ShaderStage>(
ShaderStageType::VERTEX,
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"uniform mat4 u_Projection;\n"
"uniform mat4 u_View;\n"
"uniform mat4 u_Model;\n"
"out vec2 o_TextCoord;\n"
"void main() {\n"
"gl_Position = u_Projection * u_View * u_Model * vec4(aPos, 1.0);\n"
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"}"
);
fragment = std::make_shared<ShaderStage>(
ShaderStageType::FRAGMENT,
"#version 330 core\n"
"in vec2 o_TextCoord;\n"
"uniform vec4 u_Color[4];\n"
"out vec4 o_Color;\n"
"void main() {\n"
"o_Color = u_Color[2];\n"
"}\n"
);
break;
default:
assertUnreachable("Unsupported ShaderOpenGLVariant");
}
// Add stages
stages.push_back(vertex);
stages.push_back(fragment);
// Parameters
parameters.push_back(ShaderOpenGLParameter(
"u_Projection",
&rel->projection,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderOpenGLParameter(
"u_View",
&rel->view,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderOpenGLParameter(
"u_Model",
&rel->model,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderOpenGLParameter(
"u_Color",
&rel->colors,
ShaderParameterType::COLOR,
4
));
// parameters.push_back(ShaderOpenGLParameter(
// "u_Color",
// &rel->color,
// ShaderParameterType::COLOR
// ));
// parameters.push_back(ShaderOpenGLParameter(
// "u_HasTexture",
// &rel->hasTexture,
// ShaderParameterType::BOOLEAN
// ));
// parameters.push_back(ShaderOpenGLParameter(
// "u_Texture",
// &rel->texture,
// ShaderParameterType::TEXTURE
// ));
}

View File

@ -0,0 +1,26 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/Shader.hpp"
namespace Dawn {
struct UIShaderData {
glm::mat4 projection;
glm::mat4 view;
glm::mat4 model;
struct Color colors[4];
};
class UIShader : public Shader<UIShaderData> {
protected:
void getStages(
const enum ShaderOpenGLVariant variant,
const struct UIShaderData *rel,
std::vector<std::shared_ptr<ShaderStage>> &stages,
std::vector<struct ShaderOpenGLParameter> &parameters
) override;
};
}