Skip to content

Commit

Permalink
enabling glsl es 300/webGL 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Dec 28, 2023
1 parent b955b38 commit c9abbbf
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 26 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,13 @@ target_link_libraries(SamplinSafari PRIVATE samplerlib linalg fmt::fmt)
if(EMSCRIPTEN)
target_link_libraries(SamplinSafari PRIVATE emscripten-browser-file)
target_link_options(
SamplinSafari PRIVATE -sEXPORTED_RUNTIME_METHODS=[ccall] -sEXPORTED_FUNCTIONS=[_main,_malloc,_free]
SamplinSafari
PRIVATE
-sEXPORTED_RUNTIME_METHODS=[ccall]
-sEXPORTED_FUNCTIONS=[_main,_malloc,_free]
-sNO_DISABLE_EXCEPTION_CATCHING
-sMAX_WEBGL_VERSION=2
-sMIN_WEBGL_VERSION=2
)
hello_imgui_set_emscripten_target_initial_memory_megabytes(SamplinSafari 120)
else()
Expand Down
7 changes: 5 additions & 2 deletions assets/shaders/grid.frag
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
precision mediump float;

out vec4 frag_color;
uniform float alpha;
uniform ivec2 size;
in vec2 v_texcoord;
Expand Down Expand Up @@ -89,6 +92,6 @@ void main()
vec2 w = 1.5 * max(abs(dFdx(uv)), abs(dFdy(uv)));
vec2 lineWidth = min(vec2(0.001) * vec2(size), vec2(3) * w);

float v = pristineGrid(uv, lineWidth, w);
fo_FragColor = vec4(vec3(1.0), alpha * v);
float v = pristineGrid(uv, lineWidth, w);
frag_color = vec4(vec3(1.0), alpha * v);
}
2 changes: 2 additions & 0 deletions assets/shaders/grid.vert
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
precision mediump float;

uniform mat4 mvp;
in vec3 position;
out vec2 v_texcoord;
Expand Down
11 changes: 7 additions & 4 deletions assets/shaders/point_instance.frag
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
precision mediump float;

out vec4 frag_color;
uniform mat4 mvp;
uniform mat3 rotation;
uniform vec3 color;
Expand All @@ -22,8 +25,8 @@ void main()
float radius2 = dot(v_texcoord, v_texcoord);
if (radius2 > 1.0)
discard;
vec3 n = rotation * vec3(v_texcoord, sqrt(1.0 - radius2));
vec3 sh = Irradiance_SphericalHarmonics(n);
sh = mix(mix(sh, vec3(dot(sh, vec3(1.0 / 3.0))), 0.5), vec3(1.0), 0.25);
fo_FragColor = vec4(sh * color * alpha, alpha);
vec3 n = rotation * vec3(v_texcoord, sqrt(1.0 - radius2));
vec3 sh = Irradiance_SphericalHarmonics(n);
sh = mix(mix(sh, vec3(dot(sh, vec3(1.0 / 3.0))), 0.5), vec3(1.0), 0.25);
frag_color = vec4(sh * color * alpha, alpha);
}
2 changes: 2 additions & 0 deletions assets/shaders/point_instance.vert
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
precision mediump float;

uniform mat4 mvp;
uniform mat4 smash;
uniform mat3 rotation;
Expand Down
1 change: 0 additions & 1 deletion src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ SampleViewer::SampleViewer()
splitMainConsole};

consoleWindow.dockSpaceName = "EditorSpace";
consoleWindow.isVisible = true;

portrait_layout.layoutName = "Mobile device (portrait orientation)";
portrait_layout.dockableWindows = {editorWindow, consoleWindow};
Expand Down
34 changes: 16 additions & 18 deletions src/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ static GLuint compile_gl_shader(GLenum type, const std::string &name, const std:
if (shader_string.empty())
return (GLuint)0;

GLuint id = glCreateShader(type);
const char *shader_string_const = shader_string.c_str();
CHK(glShaderSource(id, 1, &shader_string_const, nullptr));
GLuint id;
CHK(id = glCreateShader(type));
const GLchar *files[] = {
#ifdef __EMSCRIPTEN__
"#version 300 es\n",
#else
"#version 330 core\n",
#endif
shader_string.c_str()};
CHK(glShaderSource(id, 2, files, nullptr));
CHK(glCompileShader(id));

GLint status;
Expand Down Expand Up @@ -73,17 +80,6 @@ static GLuint compile_gl_shader(GLenum type, const std::string &name, const std:
return id;
}

// Hack to make simple shaders work in both GLSL and GLSL ES
// clang-format off
#ifdef __EMSCRIPTEN__
#define VERT_SHADER_HEADER "#version 100\n#define in attribute\n#define out varying\nprecision mediump float;\n"
#define FRAG_SHADER_HEADER "#version 100\n#extension GL_OES_standard_derivatives : require\n#define in varying\n#define fo_FragColor gl_FragColor\nprecision mediump float;\n"
#else
#define VERT_SHADER_HEADER "#version 330 core\n"
#define FRAG_SHADER_HEADER "#version 330 core\nout vec4 fo_FragColor;\n"
#endif
// clang-format on

Shader::Shader(const std::string &name, const std::string &vs_filename, const std::string &fs_filename,
BlendMode blend_mode) :
m_name(name),
Expand All @@ -102,8 +98,8 @@ Shader::Shader(const std::string &name, const std::string &vs_filename, const st
auto vs = load_shader_file(vs_filename);
auto fs = load_shader_file(fs_filename);

vertex_shader = string(VERT_SHADER_HEADER) + string((char *)vs.data, vs.dataSize);
fragment_shader = string(FRAG_SHADER_HEADER) + string((char *)fs.data, fs.dataSize);
vertex_shader = string((char *)vs.data, vs.dataSize);
fragment_shader = string((char *)fs.data, fs.dataSize);

HelloImGui::FreeAssetFileData(&vs);
HelloImGui::FreeAssetFileData(&fs);
Expand Down Expand Up @@ -278,7 +274,8 @@ Shader::Shader(const std::string &name, const std::string &vs_filename, const st
GLenum type = 0;
GLint size = 0;
CHK(glGetActiveAttrib(m_shader_handle, i, sizeof(attr_name), nullptr, &size, &type, attr_name));
GLint index = glGetAttribLocation(m_shader_handle, attr_name);
GLint index;
CHK(index = glGetAttribLocation(m_shader_handle, attr_name));
register_buffer(VertexBuffer, attr_name, index, type);
}

Expand All @@ -288,7 +285,8 @@ Shader::Shader(const std::string &name, const std::string &vs_filename, const st
GLenum type = 0;
GLint size = 0;
CHK(glGetActiveUniform(m_shader_handle, i, sizeof(uniform_name), nullptr, &size, &type, uniform_name));
GLint index = glGetUniformLocation(m_shader_handle, uniform_name);
GLint index;
CHK(index = glGetUniformLocation(m_shader_handle, uniform_name));
register_buffer(UniformBuffer, uniform_name, index, type);
}

Expand Down

0 comments on commit c9abbbf

Please sign in to comment.