Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
toloudis committed Dec 30, 2024
1 parent 2b43edd commit 105aa70
Show file tree
Hide file tree
Showing 5 changed files with 1,234 additions and 5 deletions.
6 changes: 5 additions & 1 deletion renderlib/graphics/glsl/GLBasicVolumeShader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "GLBasicVolumeShader.h"
#include "glad/glad.h"
#include "shaders.h"

#include "Logging.h"

Expand Down Expand Up @@ -38,7 +39,9 @@ void main()
LOG_ERROR << "GLBasicVolumeShader: Failed to compile vertex shader\n" << vshader->log();
}

fshader = new GLShader(GL_FRAGMENT_SHADER);
fshader = ShaderArray::GetShader("basicVolumeFrag");
#if 0
new GLShader(GL_FRAGMENT_SHADER);
fshader->compileSourceCode(R"(
#version 400 core

Expand Down Expand Up @@ -241,6 +244,7 @@ void main()
return;
}
)");
#endif

if (!fshader->isCompiled()) {
LOG_ERROR << "GLBasicVolumeShader: Failed to compile fragment shader\n" << fshader->log();
Expand Down
4 changes: 3 additions & 1 deletion renderlib/graphics/glsl/shaders.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "shaders.h"

#include "shaders/basicVolume_frag_gen.hpp"
#include "shaders/pathTraceVolume_frag_gen.hpp"

struct ShaderEntry
{
Expand All @@ -9,7 +10,8 @@ struct ShaderEntry
};

static std::unordered_map<std::string, ShaderEntry> shader_src = {
{ "basicVolume", { basicVolume_frag_shader, GL_FRAGMENT_SHADER } }
{ "basicVolumeFrag", { basicVolume_frag_shader, GL_FRAGMENT_SHADER } },
{ "pathTraceVolumeFrag", { pathTraceVolume_frag_shader, GL_FRAGMENT_SHADER } }
};
static std::unordered_map<std::string, GLShader*> shaders;

Expand Down
29 changes: 26 additions & 3 deletions renderlib/graphics/glsl/shaders/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
function(convert_shader_to_cpp input_file output_file)
# Read the shader file content
file(READ "${input_file}" shader_content)
file(READ "${input_file}" file_content)
# Convert the file content to a list of strings, splitting by newlines
string(REGEX REPLACE "\n" ";" file_content_list "${file_content}")

# Print the list of strings
# foreach(line IN LISTS file_content_list)
# message("${line}")
# endforeach()

# Escape special characters in the shader content
#string(REGEX REPLACE "\\" "\\\\" shader_content "${shader_content}")
Expand All @@ -13,7 +20,7 @@ function(convert_shader_to_cpp input_file output_file)
#include <string>
const std::string ${output_file}_shader = R\"(
${shader_content}
${file_content}
)\";
")
Expand All @@ -24,6 +31,22 @@ endfunction()

# Convert your shader file
convert_shader_to_cpp("basicVolume.frag" "basicVolume.frag")
# convert_shader_to_cpp("pathTraceVolume.frag" "pathTraceVolume.frag")


set(SHADER_FILE "pathTraceVolume.frag")
set(OUTPUT_C_FILE "pathTraceVolume_frag_gen.hpp")
execute_process(COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/convert_shader.py ${SHADER_FILE} ${OUTPUT_C_FILE})
# Add a custom command to convert the shader
# add_custom_command(
# OUTPUT ${OUTPUT_C_FILE}
# COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/convert_shader.py ${SHADER_FILE} ${OUTPUT_C_FILE}
# DEPENDS ${SHADER_FILE} ${CMAKE_SOURCE_DIR}/convert_shader.py
# COMMENT "Converting ${SHADER_FILE} to C source file ${OUTPUT_C_FILE}"
# )

# Add the generated C++ file to your target
target_sources(renderlib PRIVATE basicVolume_frag_gen.hpp)
target_sources(renderlib PRIVATE
basicVolume_frag_gen.hpp
pathTraceVolume_frag_gen.hpp
)
34 changes: 34 additions & 0 deletions renderlib/graphics/glsl/shaders/convert_shader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys

def convert_shader_to_c(input_file, output_file, chunk_size=12*1024):
with open(input_file, 'r') as shader:
shader_content = shader.read()

chunks = [shader_content[i:i+chunk_size] for i in range(0, len(shader_content), chunk_size)]
shadername = input_file # input_file.split('/')[-1]
shadername = shadername.replace('-', '_').replace('.', '_').replace(' ', '_').replace('/', '_')

with open(output_file, 'w') as output:
output.write('// Generated C source file containing shader\n\n')
output.write('#include <string>\n\n')
i = 0
for chunk in chunks:
output.write(f'const std::string {shadername}_chunk{i} = R"(\n')
output.write(f'{chunk}')
output.write(')";\n\n')

output.write(f'const std::string {shadername}_src = \n')
for i in range(len(chunks)):
output.write(f' {shadername}_chunk{i}')
if i < len(chunks) - 1:
output.write(' +\n')
output.write(';\n')

if __name__ == "__main__":
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} <input_shader_file> <output_c_file>")
sys.exit(1)

input_shader = sys.argv[1]
output_c = sys.argv[2]
convert_shader_to_c(input_shader, output_c)
Loading

0 comments on commit 105aa70

Please sign in to comment.