Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Integrate shaderc
Browse files Browse the repository at this point in the history
  • Loading branch information
lf94 committed Feb 3, 2023
1 parent 86788b4 commit e976f11
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
[submodule "extern/libfive"]
path = extern/libfive
url = https://github.com/libfive/libfive
[submodule "extern/shaderc"]
path = extern/shaderc
url = [email protected]:google/shaderc.git
14 changes: 11 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if (MSYS)
# On MSYS we use MinGW packages for OpenVDB and GoogleTest
else ()
include_directories(
extern/googletest/googletest/include
#extern/googletest/googletest/include
extern/openvdb/openvdb)
endif ()

Expand Down Expand Up @@ -104,6 +104,13 @@ file(GLOB DoubleConversionSrc "extern/double-conversion/double-conversion/*.cc")
add_library(double-conversion ${DoubleConversionSrc})
set_property(TARGET double-conversion PROPERTY CXX_STANDARD 17)

# shaderc library
set(SHADERC_SKIP_INSTALL true)
set(SHADERC_SKIP_TESTS true)
set(SHADERC_SKIP_EXAMPLES true)
set(SHADERC_SKIP_COPYRIGHT_CHECK true)
add_subdirectory(extern/shaderc/)

# glad library, an OpenGL loader
add_library(glad
extern/glad/glad/glad.c)
Expand Down Expand Up @@ -190,7 +197,8 @@ set(Libs
libcurv_io libcurv imgui glfw glad ${LibOpenGL} replxx double-conversion
tmc ${FatLibraries}
Boost::iostreams Boost::system
pthread ${LibDl})
pthread ${LibDl}
shaderc)

target_link_libraries(curv PUBLIC ${Libs})

Expand Down Expand Up @@ -268,7 +276,7 @@ if (NOT DEFINED MSYS)
add_subdirectory(extern/openvdb/openvdb/openvdb EXCLUDE_FROM_ALL)
set_property(TARGET openvdb_static PROPERTY CXX_STANDARD 17)
endif()
add_subdirectory(extern/googletest/googletest EXCLUDE_FROM_ALL)
#add_subdirectory(extern/googletest/googletest EXCLUDE_FROM_ALL)
endif ()

add_subdirectory(extern/glfw EXCLUDE_FROM_ALL)
1 change: 1 addition & 0 deletions extern/shaderc
Submodule shaderc added at ca4c38
56 changes: 38 additions & 18 deletions libcurv/viewer/shader.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "shaderc/shaderc.h"

#include "shader.h"

#include "text.h"
Expand Down Expand Up @@ -148,64 +150,82 @@ bool Shader::isInUse() const {
GLuint Shader::compileShader(
const std::string& _src, GLenum _type, bool verbose)
{
std::string prolog = "";
const char* epilog = "";
shaderc_compiler_t compiler = shaderc_compiler_initialize();
shaderc_compile_options_t options = shaderc_compile_options_initialize();
shaderc_compile_options_set_optimization_level(options, shaderc_optimization_level_size);
shaderc_compilation_result_t result;
GLuint shader;

prolog += curv::viewer::glsl_version;
prolog += "\n#define GLSLVIEWER 1\n";
std::string src_copy = "";
src_copy += curv::viewer::glsl_version;
src_copy += "\n#define GLSLVIEWER 1\n";

// Test if this is a shadertoy.com image shader. If it is, we need to
// define some uniforms with different names than the glslViewer standard,
// and we need to add prolog and epilog code.
if (_type == GL_FRAGMENT_SHADER && find_id(_src, "mainImage")) {
prolog +=
src_copy +=
"uniform vec2 u_resolution;\n"
"#define iResolution vec3(u_resolution, 1.0)\n"
"out vec4 oFragColour;\n"
"\n";
m_time = true;
prolog +=
src_copy +=
"uniform float u_time;\n"
"#define iGlobalTime u_time\n"
"#define iTime u_time\n"
"\n";
m_delta = find_id(_src, "iTimeDelta");
if (m_delta) {
prolog +=
src_copy +=
"uniform float u_delta;\n"
"#define iTimeDelta u_delta\n"
"\n";
}
m_date = find_id(_src, "iDate");
if (m_date) {
prolog +=
src_copy +=
"uniform vec4 u_date;\n"
"#define iDate u_date\n"
"\n";
}
m_imouse = find_id(_src, "iMouse");
if (m_imouse) {
prolog +=
src_copy +=
"uniform vec4 iMouse;\n"
"\n";
}
epilog =

src_copy += _src;

src_copy +=
"\n"
"void main(void) {\n"
" mainImage(oFragColour, gl_FragCoord.st);\n"
"}\n";

result = shaderc_compile_into_preprocessed_text(
compiler, src_copy.c_str(), src_copy.length(), shaderc_glsl_fragment_shader,
"main.frag", "main", options);
shader = glCreateShader(GL_FRAGMENT_SHADER);
} else {
src_copy += _src;
result = shaderc_compile_into_preprocessed_text(
compiler, src_copy.c_str(), src_copy.length(), shaderc_glsl_vertex_shader,
"main.vert", "main", options);
shader = glCreateShader(GL_VERTEX_SHADER);
}

const GLchar* sources[3] = {
(const GLchar*) prolog.c_str(),
(const GLchar*) _src.c_str(),
(const GLchar*) epilog,
};
const char* spirv_bytes = shaderc_result_get_bytes(result);

GLuint shader = glCreateShader(_type);
glShaderSource(shader, 3, sources, NULL);
const GLchar *source = (const GLchar *)spirv_bytes;
glShaderSource(shader, 1, &source, 0);
glCompileShader(shader);

shaderc_result_release(result);
shaderc_compile_options_release(options);
shaderc_compiler_release(compiler);

GLint isCompiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);

Expand All @@ -224,7 +244,7 @@ GLuint Shader::compileShader(
}
std::cerr << "shader:\n" << &infoLog[0] << std::endl
<< "---source---\n"
<< prolog << _src << epilog
<< src_copy
<< "---EOF---\n";
}

Expand Down

0 comments on commit e976f11

Please sign in to comment.