Implemented loading shaders from file.

This commit is contained in:
Patrick 2024-09-13 11:12:59 +02:00
parent ba49a1e47a
commit d6745226a1
6 changed files with 84 additions and 24 deletions

View File

@ -0,0 +1,9 @@
#version 460
layout(location = 0)
out vec4 o_color;
void main()
{
o_color = vec4(0.0, 1.0, 0.0, 1.0);
}

View File

@ -0,0 +1,12 @@
#version 460
const vec2 vertices[3] = vec2[3](
vec2(-0.5, 0.5),
vec2( 0.5, 0.5),
vec2( 0.0, -0.5)
);
void main()
{
gl_Position = vec4(vertices[gl_VertexIndex], 0.0, 1.0);
}

View File

@ -0,0 +1,12 @@
struct PS_OUTPUT
{
float4 color : COLOR0;
};
PS_OUTPUT main() : SV_TARGET
{
PS_OUTPUT output;
output.color = float4(0.0, 1.0, 0.0, 1.0);
return output;
}

View File

@ -0,0 +1,19 @@
struct VS_OUTPUT
{
float4 vPosition : POSITION;
};
static const float2 vertices[3] =
{
float2(-0.5, 0.5),
float2( 0.5, 0.5),
float2( 0.0, -0.5)
};
VS_OUTPUT main(uint vertexID : SV_VertexID)
{
VS_OUTPUT output;
output.vPosition = float4(vertices[vertexID], 0.0, 1.0);
return output;
}

View File

@ -18,7 +18,11 @@ prog_app = env.UnityProgram(
} }
}, },
'spdlog': {}, 'spdlog': {},
'glslang': {} 'glslang': {
'options': {
'enable_hlsl': True
}
}
} }
) )
env.Default(prog_app) env.Default(prog_app)

View File

@ -1,6 +1,9 @@
#include <mijin/util/scope_guard.hpp> #include <mijin/util/scope_guard.hpp>
#include <mijin/util/variant.hpp> #include <mijin/util/variant.hpp>
#include <mijin/virtual_filesystem/filesystem.hpp>
#include <mijin/virtual_filesystem/relative.hpp>
#include <mijin/virtual_filesystem/stacked.hpp>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <mijin/util/winundef.hpp> #include <mijin/util/winundef.hpp>
@ -11,38 +14,36 @@
namespace namespace
{ {
const char* const VERTEX_SOURCE = R"( mijin::StackedFileSystemAdapter gFileSystem;
#version 460
const vec2 vertices[3] = vec2[3]( void initFileSystem(const fs::path& executablePath) noexcept
vec2(-0.5, 0.5),
vec2( 0.5, 0.5),
vec2( 0.0, -0.5)
);
void main()
{ {
gl_Position = vec4(vertices[gl_VertexIndex], 0.0, 1.0); gFileSystem.emplaceAdapter<mijin::RelativeFileSystemAdapter<mijin::OSFileSystemAdapter>>(executablePath.parent_path() / "assets");
} }
)";
const char* const FRAGMENT_SOURCE = R"( [[nodiscard]]
#version 460 std::string getFileContents(const fs::path& path)
layout(location = 0)
out vec4 o_color;
void main()
{ {
o_color = vec4(0.0, 1.0, 0.0, 1.0); std::unique_ptr<mijin::Stream> stream;
mijin::throwOnError(gFileSystem.open(path, mijin::FileOpenMode::READ, stream),
"Error opening file for reading.");
std::string content;
mijin::throwOnError(stream->readAsString(content), "Error reading file contents.");
return content;
} }
)";
} }
int main(int, char**) int main(int argc, char* argv[])
{ {
using namespace sdl_gpu_test; using namespace sdl_gpu_test;
if (argc < 1)
{
return 1;
}
initFileSystem(fs::absolute(fs::path(argv[0])).parent_path());
// init SDL // init SDL
if (SDL_Init(0) != SDL_TRUE) if (SDL_Init(0) != SDL_TRUE)
{ {
@ -61,6 +62,7 @@ int main(int, char**)
sdlpp::Window window; sdlpp::Window window;
window.create({ window.create({
.title = "SDL_gpu Test",
.flags = {.vulkan = true} .flags = {.vulkan = true}
}); });
@ -73,7 +75,8 @@ int main(int, char**)
gpuDevice.claimWindow(window); gpuDevice.claimWindow(window);
// create vertex shader // create vertex shader
std::vector<std::uint32_t> vertexSpv = compileGLSL(VERTEX_SOURCE, {.stage = ShaderStage::VERTEX}); const std::string vertexSource = getFileContents("shaders/glsl/triangle.vert");
std::vector<std::uint32_t> vertexSpv = compileGLSL(vertexSource, {.stage = ShaderStage::VERTEX});
sdlpp::GPUShader vertexShader; sdlpp::GPUShader vertexShader;
vertexShader.create(gpuDevice, { vertexShader.create(gpuDevice, {
.code = {reinterpret_cast<const Uint8*>(vertexSpv.data()), vertexSpv.size() * sizeof(std::uint32_t)}, .code = {reinterpret_cast<const Uint8*>(vertexSpv.data()), vertexSpv.size() * sizeof(std::uint32_t)},
@ -82,7 +85,8 @@ int main(int, char**)
}); });
// create fragment shader // create fragment shader
std::vector<std::uint32_t> fragmentSpv = compileGLSL(FRAGMENT_SOURCE, {.stage = ShaderStage::FRAGMENT}); const std::string fragmentSource = getFileContents("shaders/glsl/green.frag");
std::vector<std::uint32_t> fragmentSpv = compileGLSL(fragmentSource, {.stage = ShaderStage::FRAGMENT});
sdlpp::GPUShader fragmentShader; sdlpp::GPUShader fragmentShader;
fragmentShader.create(gpuDevice, { fragmentShader.create(gpuDevice, {
.code = {reinterpret_cast<const Uint8*>(fragmentSpv.data()), fragmentSpv.size() * sizeof(std::uint32_t)}, .code = {reinterpret_cast<const Uint8*>(fragmentSpv.data()), fragmentSpv.size() * sizeof(std::uint32_t)},