Skip to content

Commit c9abbbf

Browse files
committed
enabling glsl es 300/webGL 2.0
1 parent b955b38 commit c9abbbf

File tree

7 files changed

+38
-26
lines changed

7 files changed

+38
-26
lines changed

CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,13 @@ target_link_libraries(SamplinSafari PRIVATE samplerlib linalg fmt::fmt)
344344
if(EMSCRIPTEN)
345345
target_link_libraries(SamplinSafari PRIVATE emscripten-browser-file)
346346
target_link_options(
347-
SamplinSafari PRIVATE -sEXPORTED_RUNTIME_METHODS=[ccall] -sEXPORTED_FUNCTIONS=[_main,_malloc,_free]
347+
SamplinSafari
348+
PRIVATE
349+
-sEXPORTED_RUNTIME_METHODS=[ccall]
350+
-sEXPORTED_FUNCTIONS=[_main,_malloc,_free]
348351
-sNO_DISABLE_EXCEPTION_CATCHING
352+
-sMAX_WEBGL_VERSION=2
353+
-sMIN_WEBGL_VERSION=2
349354
)
350355
hello_imgui_set_emscripten_target_initial_memory_megabytes(SamplinSafari 120)
351356
else()

assets/shaders/grid.frag

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
precision mediump float;
2+
3+
out vec4 frag_color;
14
uniform float alpha;
25
uniform ivec2 size;
36
in vec2 v_texcoord;
@@ -89,6 +92,6 @@ void main()
8992
vec2 w = 1.5 * max(abs(dFdx(uv)), abs(dFdy(uv)));
9093
vec2 lineWidth = min(vec2(0.001) * vec2(size), vec2(3) * w);
9194

92-
float v = pristineGrid(uv, lineWidth, w);
93-
fo_FragColor = vec4(vec3(1.0), alpha * v);
95+
float v = pristineGrid(uv, lineWidth, w);
96+
frag_color = vec4(vec3(1.0), alpha * v);
9497
}

assets/shaders/grid.vert

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
precision mediump float;
2+
13
uniform mat4 mvp;
24
in vec3 position;
35
out vec2 v_texcoord;

assets/shaders/point_instance.frag

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
precision mediump float;
2+
3+
out vec4 frag_color;
14
uniform mat4 mvp;
25
uniform mat3 rotation;
36
uniform vec3 color;
@@ -22,8 +25,8 @@ void main()
2225
float radius2 = dot(v_texcoord, v_texcoord);
2326
if (radius2 > 1.0)
2427
discard;
25-
vec3 n = rotation * vec3(v_texcoord, sqrt(1.0 - radius2));
26-
vec3 sh = Irradiance_SphericalHarmonics(n);
27-
sh = mix(mix(sh, vec3(dot(sh, vec3(1.0 / 3.0))), 0.5), vec3(1.0), 0.25);
28-
fo_FragColor = vec4(sh * color * alpha, alpha);
28+
vec3 n = rotation * vec3(v_texcoord, sqrt(1.0 - radius2));
29+
vec3 sh = Irradiance_SphericalHarmonics(n);
30+
sh = mix(mix(sh, vec3(dot(sh, vec3(1.0 / 3.0))), 0.5), vec3(1.0), 0.25);
31+
frag_color = vec4(sh * color * alpha, alpha);
2932
}

assets/shaders/point_instance.vert

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
precision mediump float;
2+
13
uniform mat4 mvp;
24
uniform mat4 smash;
35
uniform mat3 rotation;

src/app.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ SampleViewer::SampleViewer()
219219
splitMainConsole};
220220

221221
consoleWindow.dockSpaceName = "EditorSpace";
222-
consoleWindow.isVisible = true;
223222

224223
portrait_layout.layoutName = "Mobile device (portrait orientation)";
225224
portrait_layout.dockableWindows = {editorWindow, consoleWindow};

src/shader.cpp

+16-18
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ static GLuint compile_gl_shader(GLenum type, const std::string &name, const std:
4040
if (shader_string.empty())
4141
return (GLuint)0;
4242

43-
GLuint id = glCreateShader(type);
44-
const char *shader_string_const = shader_string.c_str();
45-
CHK(glShaderSource(id, 1, &shader_string_const, nullptr));
43+
GLuint id;
44+
CHK(id = glCreateShader(type));
45+
const GLchar *files[] = {
46+
#ifdef __EMSCRIPTEN__
47+
"#version 300 es\n",
48+
#else
49+
"#version 330 core\n",
50+
#endif
51+
shader_string.c_str()};
52+
CHK(glShaderSource(id, 2, files, nullptr));
4653
CHK(glCompileShader(id));
4754

4855
GLint status;
@@ -73,17 +80,6 @@ static GLuint compile_gl_shader(GLenum type, const std::string &name, const std:
7380
return id;
7481
}
7582

76-
// Hack to make simple shaders work in both GLSL and GLSL ES
77-
// clang-format off
78-
#ifdef __EMSCRIPTEN__
79-
#define VERT_SHADER_HEADER "#version 100\n#define in attribute\n#define out varying\nprecision mediump float;\n"
80-
#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"
81-
#else
82-
#define VERT_SHADER_HEADER "#version 330 core\n"
83-
#define FRAG_SHADER_HEADER "#version 330 core\nout vec4 fo_FragColor;\n"
84-
#endif
85-
// clang-format on
86-
8783
Shader::Shader(const std::string &name, const std::string &vs_filename, const std::string &fs_filename,
8884
BlendMode blend_mode) :
8985
m_name(name),
@@ -102,8 +98,8 @@ Shader::Shader(const std::string &name, const std::string &vs_filename, const st
10298
auto vs = load_shader_file(vs_filename);
10399
auto fs = load_shader_file(fs_filename);
104100

105-
vertex_shader = string(VERT_SHADER_HEADER) + string((char *)vs.data, vs.dataSize);
106-
fragment_shader = string(FRAG_SHADER_HEADER) + string((char *)fs.data, fs.dataSize);
101+
vertex_shader = string((char *)vs.data, vs.dataSize);
102+
fragment_shader = string((char *)fs.data, fs.dataSize);
107103

108104
HelloImGui::FreeAssetFileData(&vs);
109105
HelloImGui::FreeAssetFileData(&fs);
@@ -278,7 +274,8 @@ Shader::Shader(const std::string &name, const std::string &vs_filename, const st
278274
GLenum type = 0;
279275
GLint size = 0;
280276
CHK(glGetActiveAttrib(m_shader_handle, i, sizeof(attr_name), nullptr, &size, &type, attr_name));
281-
GLint index = glGetAttribLocation(m_shader_handle, attr_name);
277+
GLint index;
278+
CHK(index = glGetAttribLocation(m_shader_handle, attr_name));
282279
register_buffer(VertexBuffer, attr_name, index, type);
283280
}
284281

@@ -288,7 +285,8 @@ Shader::Shader(const std::string &name, const std::string &vs_filename, const st
288285
GLenum type = 0;
289286
GLint size = 0;
290287
CHK(glGetActiveUniform(m_shader_handle, i, sizeof(uniform_name), nullptr, &size, &type, uniform_name));
291-
GLint index = glGetUniformLocation(m_shader_handle, uniform_name);
288+
GLint index;
289+
CHK(index = glGetUniformLocation(m_shader_handle, uniform_name));
292290
register_buffer(UniformBuffer, uniform_name, index, type);
293291
}
294292

0 commit comments

Comments
 (0)