3 * Emscripten example of using the single-file \c zstddeclib. Draws a rotating
4 * textured quad with data from the in-line Zstd compressed DXT1 texture (DXT1
5 * being hardware compression, further compressed with Zstd).
9 * export CC_FLAGS="-Wall -Wextra -Werror -Os -g0 -flto --llvm-lto 3 -lGL -DNDEBUG=1"
10 * export EM_FLAGS="-s WASM=1 -s ENVIRONMENT=web --shell-file shell.html --closure 1"
11 * emcc $CC_FLAGS $EM_FLAGS -o out.html emscripten.c
14 * \author Carl Woffenden, Numfum GmbH (released under a CC0 license)
22 #include <emscripten/emscripten.h>
23 #include <emscripten/html5.h>
25 #include <GLES2/gl2.h>
26 #include <GLES2/gl2ext.h>
28 #include "../zstddeclib.c"
30 //************************* Test Data (DXT texture) **************************/
33 * Zstd compressed DXT1 256x256 texture source.
35 * See \c testcard.png for the original.
37 static uint8_t const srcZstd[] = {
38 #include "testcard-zstd.inl"
42 * Uncompressed size of \c #srcZstd.
44 #define DXT1_256x256 32768
47 * Destination for decoding \c #srcZstd.
49 static uint8_t dstDxt1[DXT1_256x256] = {};
51 #ifndef ZSTD_VERSION_MAJOR
53 * For the case where the decompression library hasn't been included we add a
54 * dummy function to fake the process and stop the buffers being optimised out.
56 size_t ZSTD_decompress(void* dst, size_t dstLen, const void* src, size_t srcLen) {
57 return (memcmp(dst, src, (srcLen < dstLen) ? srcLen : dstLen)) ? dstLen : 0;
61 //*************************** Program and Shaders ***************************/
66 static GLuint progId = 0;
71 static GLuint vertId = 0;
76 static GLuint fragId = 0;
78 //********************************* Uniforms *********************************/
81 * Quad rotation angle ID.
83 static GLint uRotId = -1;
88 static GLint uTx0Id = -1;
90 //******************************* Shader Source ******************************/
93 * Vertex shader to draw texture mapped polys with an applied rotation.
95 static GLchar const vertShader2D[] =
98 "precision mediump float;\n"
102 "uniform float uRot;" // rotation
103 "attribute vec2 aPos;" // vertex position coords
104 "attribute vec2 aUV0;" // vertex texture UV0
105 "varying vec2 vUV0;" // (passed to fragment shader)
107 " float cosA = cos(radians(uRot));"
108 " float sinA = sin(radians(uRot));"
109 " mat3 rot = mat3(cosA, -sinA, 0.0,"
112 " gl_Position = vec4(rot * vec3(aPos, 1.0), 1.0);"
117 * Fragment shader for the above polys.
119 static GLchar const fragShader2D[] =
120 #if GL_ES_VERSION_2_0
122 "precision mediump float;\n"
126 "uniform sampler2D uTx0;"
127 "varying vec2 vUV0;" // (passed from fragment shader)
129 " gl_FragColor = texture2D(uTx0, vUV0);"
133 * Helper to compile a shader.
135 * \param type shader type
136 * \param text shader source
137 * \return the shader ID (or zero if compilation failed)
139 static GLuint compileShader(GLenum const type, const GLchar* text) {
140 GLuint shader = glCreateShader(type);
142 glShaderSource (shader, 1, &text, NULL);
143 glCompileShader(shader);
145 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
150 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLen);
152 GLchar* logStr = malloc(logLen);
153 glGetShaderInfoLog(shader, logLen, NULL, logStr);
155 printf("Shader compilation error: %s\n", logStr);
159 glDeleteShader(shader);
165 //********************************** Helpers *********************************/
168 * Vertex position index.
170 #define GL_VERT_POSXY_ID 0
175 #define GL_VERT_TXUV0_ID 1
178 * \c GL vec2 storage type.
186 * Combined 2D vertex and 2D texture coordinates.
193 //****************************************************************************/
196 * Current quad rotation angle (in degrees, updated per frame).
198 static float rotDeg = 0.0f;
201 * Emscripten (single) GL context.
203 static EMSCRIPTEN_WEBGL_CONTEXT_HANDLE glCtx = 0;
206 * Emscripten resize handler.
208 static EM_BOOL resize(int type, const EmscriptenUiEvent* e, void* data) {
211 if (emscripten_get_element_css_size ("#canvas", &surfaceW, &surfaceH) == EMSCRIPTEN_RESULT_SUCCESS) {
212 emscripten_set_canvas_element_size("#canvas", surfaceW, surfaceH);
214 glViewport(0, 0, (int) surfaceW, (int) surfaceH);
224 * Boilerplate to create a WebGL context.
226 static EM_BOOL initContext() {
227 // Default attributes
228 EmscriptenWebGLContextAttributes attr;
229 emscripten_webgl_init_context_attributes(&attr);
230 if ((glCtx = emscripten_webgl_create_context("#canvas", &attr))) {
231 // Bind the context and fire a resize to get the initial size
232 emscripten_webgl_make_context_current(glCtx);
233 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, NULL, EM_FALSE, resize);
234 resize(0, NULL, NULL);
241 * Called once per frame (clears the screen and draws the rotating quad).
244 glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
245 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
248 glUniform1f(uRotId, rotDeg);
250 if (rotDeg >= 360.0f) {
255 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
260 * Creates the GL context, shaders and quad data, decompresses the Zstd data
261 * and 'uploads' the resulting texture.
263 * As a (naive) comparison, removing Zstd and building with "-Os -g0 s WASM=1
264 * -lGL emscripten.c" results in a 15kB WebAssembly file; re-adding Zstd
265 * increases the Wasm by 26kB.
269 // Compile shaders and set the initial GL state
270 if ((progId = glCreateProgram())) {
271 vertId = compileShader(GL_VERTEX_SHADER, vertShader2D);
272 fragId = compileShader(GL_FRAGMENT_SHADER, fragShader2D);
274 glBindAttribLocation(progId, GL_VERT_POSXY_ID, "aPos");
275 glBindAttribLocation(progId, GL_VERT_TXUV0_ID, "aUV0");
277 glAttachShader(progId, vertId);
278 glAttachShader(progId, fragId);
279 glLinkProgram (progId);
280 glUseProgram (progId);
281 uRotId = glGetUniformLocation(progId, "uRot");
282 uTx0Id = glGetUniformLocation(progId, "uTx0");
284 glUniform1i(uTx0Id, 0);
287 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
289 glDisable(GL_DITHER);
292 glEnable(GL_CULL_FACE);
298 // Create the textured quad (vert positions then UVs)
299 struct posTex2d verts2d[] = {
300 {{-0.85f, -0.85f}, {0.0f, 0.0f}}, // BL
301 {{ 0.85f, -0.85f}, {1.0f, 0.0f}}, // BR
302 {{-0.85f, 0.85f}, {0.0f, 1.0f}}, // TL
303 {{ 0.85f, 0.85f}, {1.0f, 1.0f}}, // TR
305 uint16_t index2d[] = {
309 glGenBuffers(1, &vertsBuf);
310 glBindBuffer(GL_ARRAY_BUFFER, vertsBuf);
311 glBufferData(GL_ARRAY_BUFFER,
312 sizeof(verts2d), verts2d, GL_STATIC_DRAW);
313 glVertexAttribPointer(GL_VERT_POSXY_ID, 2,
314 GL_FLOAT, GL_FALSE, sizeof(struct posTex2d), 0);
315 glVertexAttribPointer(GL_VERT_TXUV0_ID, 2,
316 GL_FLOAT, GL_FALSE, sizeof(struct posTex2d),
317 (void*) offsetof(struct posTex2d, uv0));
318 glGenBuffers(1, &indexBuf);
319 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuf);
320 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
321 sizeof(index2d), index2d, GL_STATIC_DRAW);
322 glEnableVertexAttribArray(GL_VERT_POSXY_ID);
323 glEnableVertexAttribArray(GL_VERT_TXUV0_ID);
325 // Decode the Zstd data and create the texture
326 if (ZSTD_decompress(dstDxt1, DXT1_256x256, srcZstd, sizeof srcZstd) == DXT1_256x256) {
327 glGenTextures(1, &txName);
328 glBindTexture(GL_TEXTURE_2D, txName);
329 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
330 glCompressedTexImage2D(GL_TEXTURE_2D, 0,
331 GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
332 256, 256, 0, DXT1_256x256, dstDxt1);
334 printf("Failed to decode Zstd data\n");
336 emscripten_set_main_loop(tick, 0, EM_FALSE);
337 emscripten_exit_with_live_runtime();