Slang actually worked.

This commit is contained in:
2024-12-09 20:00:09 -06:00
parent ccd5b36965
commit b3c2e0114f
20 changed files with 2422 additions and 3067 deletions

View File

@ -14,6 +14,8 @@ void Texture::bind(const uint8_t slot) {
glActiveTexture(GL_TEXTURE0 + slot);
assertNoGLError();
glBindTexture(GL_TEXTURE_2D, this->id);
// assertNoGLError();
// glBindSampler(slot, this->samplerId);
assertNoGLError();
this->updateTextureProperties();
}
@ -52,10 +54,16 @@ void Texture::setSize(
this->format = format;
this->dataFormat = dataFormat;
// Gen texture
glGenTextures(1, &this->id);
assertNoGLError();
if(this->id <= 0) assertUnreachable("Texture generation failed!");
// Gen sampler
// glGenSamplers(1, &this->samplerId);
// assertNoGLError();
// if(this->samplerId <= 0) assertUnreachable("Sampler generation failed!");
// Initialize the texture to blank
glActiveTexture(GL_TEXTURE0);
assertNoGLError();

View File

@ -17,6 +17,7 @@ namespace Dawn {
int32_t width = -1;
int32_t height = -1;
GLuint id = -1;
GLuint samplerId = -1;
TextureFormat format;
TextureDataFormat dataFormat;

View File

@ -42,6 +42,11 @@ ShaderStage::ShaderStage(
assertNoGLError();
glCompileShader(this->id);
assertNoGLError();
// glShaderBinary(1, &this->id, GL_SHADER_BINARY_FORMAT_SPIR_V, source.data(), source.size());
// assertNoGLError();
// glSpecializeShader(this->id, "main", 0, NULL, NULL);
// assertNoGLError();
// Validate
GLint status;

View File

@ -39,6 +39,7 @@ namespace Dawn {
* @param getParameters A callback that, when invoked, will populate the
* parameters vector with the parameters for this
* structure.
* @param count The number of structures to create.
*/
ShaderStructure(
const std::string &structureName,

View File

@ -5,6 +5,11 @@
#include "display/shader/SimpleTexturedShader.hpp"
#include <fstream>
#include "slang.h"
#include "slang-gfx.h"
using namespace slang;
using namespace Dawn;
void SimpleTexturedShader::getStages(
@ -18,86 +23,237 @@ void SimpleTexturedShader::getStages(
std::shared_ptr<ShaderStage> vertex;
std::shared_ptr<ShaderStage> fragment;
switch(variant) {
case ShaderOpenGLVariant::GLSL_330_CORE:
vertex = std::make_shared<ShaderStage>(
ShaderStageType::VERTEX,R"(
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
uniform mat4 u_Projection;
uniform mat4 u_View;
uniform mat4 u_Model;
out vec2 o_TextCoord;
void main() {
gl_Position = u_Projection * u_View * u_Model * vec4(aPos, 1.0);
o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);
}
)"
);
fragment = std::make_shared<ShaderStage>(
ShaderStageType::FRAGMENT,R"(
#version 330 core
in vec2 o_TextCoord;
out vec4 o_Color;
uniform vec4 u_Color;
uniform bool u_HasTexture;
uniform sampler2D u_Texture;
void main() {
if(u_HasTexture) {
o_Color = texture(u_Texture, o_TextCoord);
} else {
o_Color = u_Color;
}
std::string shader = R"(
cbuffer Uniforms {
float4x4 u_Projection;
float4x4 u_View;
float4x4 u_Model;
float4 u_Color;
bool u_HasTexture;
};
if(o_Color.a == 0) discard;
}
)"
);
break;
layout(binding = 1)
uniform Sampler2D u_Texture;
default:
assertUnreachable("Unsupported ShaderOpenGLVariant");
struct AssembledVertex {
float3 position : POSITION;
float2 texcoord : TEXCOORD;
};
struct CoarseVertex {
};
struct Fragment {
float4 color;
};
struct VertexStageOutput {
float2 uv : UV;
float4 sv_position : SV_Position;
};
[shader("vertex")]
VertexStageOutput vertexMain(
AssembledVertex assembledVertex
) {
VertexStageOutput output;
float3 position = assembledVertex.position;
output.uv = assembledVertex.texcoord;
output.sv_position = mul(
float4(position, 1.0),
mul(u_Model, mul(u_View, u_Projection))
);
return output;
}
[shader("fragment")]
Fragment fragmentMain(
float2 uv: UV
) : SV_Target {
Fragment output;
if(u_HasTexture) {
output.color = u_Texture.Sample(uv) * u_Color;
} else {
output.color = u_Color;
}
return output;
}
)";
Slang::ComPtr<IGlobalSession> globalSession;
createGlobalSession(globalSession.writeRef());
SessionDesc sessionDesc;
TargetDesc targetDesc;
targetDesc.format = SLANG_GLSL;
targetDesc.profile = globalSession->findProfile("glsl_330");
sessionDesc.targets = &targetDesc;
sessionDesc.targetCount = 1;
Slang::ComPtr<IBlob> diagnostics;
const char* searchPaths[] = { "/home/yourwishes/htdocs/Dawn/assets/shaders/" };
sessionDesc.searchPaths = searchPaths;
sessionDesc.searchPathCount = 1;
Slang::ComPtr<ISession> session;
globalSession->createSession(sessionDesc, session.writeRef());
auto module = session->loadModuleFromSourceString(
"hello-world.slang",
"hello-world.slang",
shader.c_str(),
diagnostics.writeRef()
);
if(diagnostics) {
assertUnreachable("Failed to load module %s", (const char*) diagnostics->getBufferPointer());
return;
}
// Add stages
Slang::ComPtr<IEntryPoint> vertexEntryPoint;
Slang::ComPtr<IEntryPoint> fragEntryPoint;
module->findEntryPointByName("vertexMain", vertexEntryPoint.writeRef());
module->findEntryPointByName("fragmentMain", fragEntryPoint.writeRef());
IComponentType* components[] = { module, vertexEntryPoint, fragEntryPoint };
Slang::ComPtr<IComponentType> program;
session->createCompositeComponentType(
components,
sizeof(components) / sizeof(components[0]),
program.writeRef()
);
slang::ProgramLayout* layout = program->getLayout();
Slang::ComPtr<IComponentType> linkedProgram;
auto result = program->link(linkedProgram.writeRef(), diagnostics.writeRef());
std::cout << "Result: " << result << std::endl;
if(diagnostics) {
assertUnreachable("%s\n", (const char*) diagnostics->getBufferPointer());
return;
}
int entryPointIndex = 0;
int targetIndex = 0; // only one target
Slang::ComPtr<IBlob> vertexBlob;
result = linkedProgram->getEntryPointCode(
entryPointIndex,
targetIndex,
vertexBlob.writeRef(),
diagnostics.writeRef()
);
if(diagnostics) {
assertUnreachable("%s\n", (const char*) diagnostics->getBufferPointer());
return;
}
std::string vertexString = (const char*)vertexBlob->getBufferPointer();
std::ofstream out("/home/yourwishes/htdocs/Dawn/vertex.glsl");
out << vertexString;
out.close();
entryPointIndex = 1;
Slang::ComPtr<IBlob> fragmentBlob;
result = linkedProgram->getEntryPointCode(
entryPointIndex,
targetIndex,
fragmentBlob.writeRef(),
diagnostics.writeRef()
);
if(diagnostics) {
assertUnreachable("%s\n", (const char*) diagnostics->getBufferPointer());
return;
}
std::string fragmentString = (const char*)fragmentBlob->getBufferPointer();
out.open("/home/yourwishes/htdocs/Dawn/fragment.glsl");
out << fragmentString;
out.close();
vertex = std::make_shared<ShaderStage>(
ShaderStageType::VERTEX, vertexString
);
stages.push_back(vertex);
fragment = std::make_shared<ShaderStage>(
ShaderStageType::FRAGMENT, fragmentString
);
stages.push_back(fragment);
structures.push_back(ShaderStructure<struct SimpleTexturedShaderDataSub>(
"block_SLANG_ParameterGroup_Uniforms_std140_0",
&rel->data,
ShaderOpenGLStructureType::STD140,
[](const SimpleTexturedShaderDataSub &data, std::vector<struct ShaderParameter> &parameters) {
parameters.push_back(ShaderParameter(
"u_Projection_0",
&data.projection,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_View_0",
&data.view,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_Model_0",
&data.model,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_Color_0",
&data.color,
ShaderParameterType::COLOR
));
parameters.push_back(ShaderParameter(
"u_HasTexture_0",
&data.hasTexture,
ShaderParameterType::BOOLEAN
));
}
));
// Parameters
parameters.push_back(ShaderParameter(
"u_Projection",
&rel->projection,
ShaderParameterType::MAT4
));
// parameters.push_back(ShaderParameter(
// "u_Projection",
// &rel->projection,
// ShaderParameterType::MAT4
// ));
// parameters.push_back(ShaderParameter(
// "u_View",
// &rel->view,
// ShaderParameterType::MAT4
// ));
// parameters.push_back(ShaderParameter(
// "u_Model",
// &rel->model,
// ShaderParameterType::MAT4
// ));
// parameters.push_back(ShaderParameter(
// "u_Color",
// &rel->color,
// ShaderParameterType::COLOR
// ));
// parameters.push_back(ShaderParameter(
// "u_HasTexture",
// &rel->hasTexture,
// ShaderParameterType::BOOLEAN
// ));
parameters.push_back(ShaderParameter(
"u_View",
&rel->view,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_Model",
&rel->model,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderParameter(
"u_Color",
&rel->color,
ShaderParameterType::COLOR
));
parameters.push_back(ShaderParameter(
"u_HasTexture",
&rel->hasTexture,
ShaderParameterType::BOOLEAN
));
parameters.push_back(ShaderParameter(
"u_Texture",
"u_Texture_0",
&rel->texture,
ShaderParameterType::TEXTURE
));

View File

@ -7,12 +7,17 @@
#include "display/shader/Shader.hpp"
namespace Dawn {
struct SimpleTexturedShaderData {
struct SimpleTexturedShaderDataSub {
glm::mat4 projection;
glm::mat4 view;
glm::mat4 model;
struct Color color = COLOR_WHITE;
bool hasTexture = false;
};
struct SimpleTexturedShaderData {
struct SimpleTexturedShaderDataSub data;
shadertexturebinding_t texture = 0;
};