Skip to content

Instantly share code, notes, and snippets.

@evanw
Created February 7, 2014 04:34
Show Gist options
  • Save evanw/8857422 to your computer and use it in GitHub Desktop.
Save evanw/8857422 to your computer and use it in GitHub Desktop.

Problem with emscripten GL and #version

The emscripten library call for glShaderSource() will insert #extension GL_OES_standard_derivatives : enable if it detects dFdx, dFdy, or fwidth in the source. Unfortunately, that makes shaders that start with #version crash with the following error:

ERROR: 0:2: 'version' : #version directive must occur before anything else, except for comments and white space

This error is especially confusing because the source did start with #version.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
Module = this.Module || {};
Module.ctx = document.createElement('canvas').getContext('experimental-webgl');
</script>
<script src="a.out.js"></script>
</body>
</html>
#include <GLES2/gl2.h>
#include <stdio.h>
int main() {
const char *source =
"#version 100\n"
"void main() {\n"
" gl_FragColor = vec4(dFdx(gl_FragCoord.x));\n"
"}\n";
int shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
int compileStatus = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
int length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
char data[length];
glGetShaderInfoLog(shader, length, NULL, data);
puts(data);
return 0;
}
SDK = ~/.emsdk_portable
EMSCRIPTEN_PATH = $(shell $(SDK)/emsdk active_path emscripten-1.7.8)
CLANG_PATH = $(shell dirname $(shell PATH=$(shell $(SDK)/emsdk active_path clang-3.2-64bit):$(PATH) which clang))
EMCC = LLVM=$(CLANG_PATH) python $(shell PATH=$(EMSCRIPTEN_PATH):$(PATH) which emcc)
default: build
build:
$(EMCC) main.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment