work on moving shader loading/compilation to a separate file

This commit is contained in:
2025-08-17 00:32:43 -05:00
parent 06d99cf87e
commit 47564bb3cf
12 changed files with 199 additions and 72 deletions

8
.clang-format Normal file
View File

@@ -0,0 +1,8 @@
# Format Style Options - Created with Clang Power Tools
---
BasedOnStyle: WebKit
BreakBeforeBraces: Attach
ColumnLimit: 110
SortIncludes: false
SpaceAfterTemplateKeyword: false
...

1
.gitignore vendored
View File

@@ -10,3 +10,4 @@ x64**
**.idb
**.pdb
**.exe
.cache**

View File

@@ -73,6 +73,9 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LibraryPath>$(SolutionDir)ext\glfw\build\src\Debug;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64)</LibraryPath>
<IncludePath>$(SolutionDir)ext\glfw\include;$(SolutionDir)inc;$(IncludePath)</IncludePath>
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)obj\$(Platform)\$(Configuration)\</IntDir>
<SourcePath>$(SolutionDir)src;$(SourcePath)</SourcePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@@ -135,6 +138,8 @@
<ItemGroup>
<ClCompile Include="glad.c" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\shaders.cpp" />
<ClCompile Include="src\util.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@@ -21,5 +21,11 @@
<ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\shaders.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

13
compile_commands.json Normal file
View File

@@ -0,0 +1,13 @@
[
{
"directory": "C:/Users/seth/Documents/repos/LivePlotter/",
"command": "\"C:/Users/seth/AppData/Roaming/ClangPowerTools/LLVM_Lite/Bin/clang++.exe\" -x c \"C:/Users/seth/Documents/repos/LivePlotter/glad.c\" -Wall -fms-compatibility-version=19.10 -Wmicrosoft -Wno-invalid-token-paste -Wno-unknown-pragmas -Wno-unused-value -fsyntax-only \"-DUNICODE\" \"-D_UNICODE\" \"-D_MT\" \"-D_DLL\" \"-D_DEBUG\" \"-D_CONSOLE\" \"-D_DEBUG_FUNCTIONAL_MACHINERY\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/include\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/atlmfc/include\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Auxiliary/VS/include\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/ucrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/um\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/shared\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/winrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/cppwinrt\" -isystem\"C:/Users/seth/Documents/repos/LivePlotter/ext/glfw/include\" -isystem\"C:/Users/seth/Documents/repos/LivePlotter/inc\"",
"file": "C:/Users/seth/Documents/repos/LivePlotter/glad.c"
}
,
{
"directory": "C:/Users/seth/Documents/repos/LivePlotter/",
"command": "\"C:/Users/seth/AppData/Roaming/ClangPowerTools/LLVM_Lite/Bin/clang++.exe\" -x c++ \"C:/Users/seth/Documents/repos/LivePlotter/src/main.cpp\" -std=c++17 -Wall -fms-compatibility-version=19.10 -Wmicrosoft -Wno-invalid-token-paste -Wno-unknown-pragmas -Wno-unused-value -fsyntax-only \"-DUNICODE\" \"-D_UNICODE\" \"-D_MT\" \"-D_DLL\" \"-D_DEBUG\" \"-D_CONSOLE\" \"-D_DEBUG_FUNCTIONAL_MACHINERY\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/include\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/atlmfc/include\" -isystem\"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Auxiliary/VS/include\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/ucrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/um\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/shared\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/winrt\" -isystem\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.26100.0/cppwinrt\" -isystem\"C:/Users/seth/Documents/repos/LivePlotter/ext/glfw/include\" -isystem\"C:/Users/seth/Documents/repos/LivePlotter/inc\"",
"file": "C:/Users/seth/Documents/repos/LivePlotter/src/main.cpp"
}
]

19
inc/shaders.hpp Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <glad/glad.h>
#include <variant>
#include "util.hpp"
namespace shader {
// Reads shader source files, compiles, and links
bool load(uint* out_id, const char *vertex_filepath, const char *fragment_filepath);
// Sets shader as active on the gpu
void use(uint id);
// Set uniform value
typedef std::variant<int, float, bool> uniform_variant;
void set_uniform(uint id, const char *name, uniform_variant value);
} // namespace shaders

14
inc/util.hpp Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <stdio.h>
template<class T> struct Array {
T* _data;
size_t len;
T& operator[](int i);
};
typedef unsigned int uint;
bool read_file(Array<char>* out, const char* filepath);

View File

@@ -1,26 +1,13 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <optional>
#include "shaders.hpp"
using namespace std;
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);"
"} ";
static GLFWwindow* window;
template<class T>
@@ -62,7 +49,7 @@ enum class ParserState {
FACE,
FACE_SKIP,
};
optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_iconsphere() {
optional<pair<SimpleArray<float>, SimpleArray<unsigned int>>> load_icosphere() {
std::FILE* f = NULL;
if (fopen_s(&f, "Icosphere.obj", "r")) {
@@ -133,7 +120,7 @@ int main()
{
if (!glfw_window_setup()) return -1;
auto data = load_iconsphere();
auto data = load_icosphere();
if (!data) {
std::cout << "ERROR loading the icosphere obj file failed" << std::endl;
return -1;
@@ -156,12 +143,6 @@ int main()
glfwSwapBuffers(window); // front buffer is now back
glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer again (former front buf)
/*float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};*/
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
@@ -176,53 +157,9 @@ int main()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size * sizeof(unsigned int), indices.data, GL_STATIC_DRAW);
// Vertex shader
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// Verify vertex shader compilation
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Fragment shader
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// Verify fragment shader compilation
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Link shader programs together
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glUseProgram(shaderProgram); // Set shaderProgram as active shader
// Delete shader program parts (like deleting obj files).
// We want this memory back
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
uint program_id;
if (!shader::load(&program_id, "src/shaders/vertex.vs", "src/shaders/fragment.fs")) return -1;
shader::use(program_id);
// Tell opengl which attribute our data is for (i.e. location)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

88
src/shaders.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include <typeinfo>
#include <stdio.h>
#include "util.hpp"
#include "shaders.hpp"
#define MAX_ERR_MSG_LEN 256
// Visit/Variant overload pattern
// https://www.modernescpp.com/index.php/visiting-a-std-variant-with-the-overload-pattern/
template<typename ... Ts>
struct overload : Ts ... {
using Ts::operator() ...;
};
template<class... Ts> overload(Ts...) -> overload<Ts...>;
namespace shader {
bool _compile_shader(uint *out_id, const char* filepath, int shader_type);
bool load(uint* out_id, const char* vertex_filepath, const char* fragment_filepath) {
uint vertex_id;
if (!_compile_shader(&vertex_id, vertex_filepath, GL_VERTEX_SHADER))
return false;
uint fragment_id;
if (!_compile_shader(&fragment_id, fragment_filepath, GL_FRAGMENT_SHADER))
return false;
uint program_id = glCreateProgram();
glAttachShader(program_id, vertex_id);
glAttachShader(program_id, fragment_id);
glLinkProgram(program_id);
int status;
glGetProgramiv(program_id, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
char err_msg[MAX_ERR_MSG_LEN];
glGetProgramInfoLog(program_id, MAX_ERR_MSG_LEN, NULL, err_msg);
printf("Error linking shaders %s and %s\n", vertex_filepath, fragment_filepath);
printf("Error msg: %s\n", err_msg);
return false;
}
glDeleteShader(vertex_id);
glDeleteShader(fragment_id);
*out_id = program_id;
return true;
}
void use(uint id) { glUseProgram(id); }
void set_uniform(uint id, const char* name, uniform_variant value) {
const auto visitor = overload {
[id, name](int i) { glUniform1i(glGetUniformLocation(id, name), i); },
[id, name](float f) { glUniform1f(glGetUniformLocation(id, name), f); },
[id, name](bool b) { glUniform1i(glGetUniformLocation(id, name), b); },
};
visit(visitor, value);
}
// Privates
bool _compile_shader(uint *out_id, const char* filepath, int shader_type) {
Array<char> source;
if (!read_file(&source, filepath))
return false;
uint id = glCreateShader(shader_type);
glShaderSource(id, 1, &source._data, (int*)&source.len);
glCompileShader(id);
int status;
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
char err_msg[MAX_ERR_MSG_LEN];
glGetShaderInfoLog(id, MAX_ERR_MSG_LEN, NULL, err_msg);
printf("Error compiling shader %s\n", filepath);
printf("%.*s\n", (int)source.len, source._data);
printf("Error msg: %s\n", err_msg);
}
*out_id = id;
return status == GL_TRUE;
}
} // namespace shader

7
src/shaders/fragment.fs Normal file
View File

@@ -0,0 +1,7 @@
#version 330 core
out vec4 frag_color;
void main() {
frag_color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

6
src/shaders/vertex.vs Normal file
View File

@@ -0,0 +1,6 @@
#version 330 core
layout (location = 0) in vec3 aPos;
void main() {
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

23
src/util.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include <cstdio>
#include <stdlib.h>
#include "util.hpp"
bool read_file(Array<char>* out, const char* filepath) {
FILE* fp = NULL;
if (fopen_s(&fp, filepath, "r") != 0) {
printf("ERROR Failed to open file %s\n", filepath);
return false;
}
fseek(fp, 0L, SEEK_END);
size_t sz = ftell(fp);
fseek(fp, 0L, 0L);
char *data = (char*)malloc(sizeof(char)*sz);
fread(data, sizeof(char), sz, fp);
fclose(fp);
out->_data = data;
out->len = sz;
return true;
}