James Kuszmaul | 9a05bfd | 2019-08-03 17:03:38 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 The Emscripten Authors. All rights reserved. |
| 3 | * Emscripten is available under two separate licenses, the MIT license and the |
| 4 | * University of Illinois/NCSA Open Source License. Both these licenses can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <assert.h> |
| 11 | #include <emscripten/emscripten.h> |
| 12 | #include <emscripten/html5.h> |
| 13 | #include <GLES3/gl3.h> |
| 14 | |
| 15 | GLuint compile_shader(GLenum shaderType, const char *src) |
| 16 | { |
| 17 | GLuint shader = glCreateShader(shaderType); |
| 18 | glShaderSource(shader, 1, &src, NULL); |
| 19 | glCompileShader(shader); |
| 20 | |
| 21 | GLint isCompiled = 0; |
| 22 | glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled); |
| 23 | if (!isCompiled) |
| 24 | { |
| 25 | GLint maxLength = 0; |
| 26 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); |
| 27 | char *buf = (char*)malloc(maxLength+1); |
| 28 | glGetShaderInfoLog(shader, maxLength, &maxLength, buf); |
| 29 | printf("%s\n", buf); |
| 30 | free(buf); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | return shader; |
| 35 | } |
| 36 | |
| 37 | GLuint create_program(GLuint vertexShader, GLuint fragmentShader) |
| 38 | { |
| 39 | GLuint program = glCreateProgram(); |
| 40 | glAttachShader(program, vertexShader); |
| 41 | glAttachShader(program, fragmentShader); |
| 42 | glBindAttribLocation(program, 0, "apos"); |
| 43 | glBindAttribLocation(program, 1, "acolor"); |
| 44 | glLinkProgram(program); |
| 45 | return program; |
| 46 | } |
| 47 | |
| 48 | int main() |
| 49 | { |
| 50 | EmscriptenWebGLContextAttributes attr; |
| 51 | emscripten_webgl_init_context_attributes(&attr); |
| 52 | #ifdef EXPLICIT_SWAP |
| 53 | attr.explicitSwapControl = 1; |
| 54 | #endif |
| 55 | attr.majorVersion = 2; |
| 56 | EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr); |
| 57 | assert(ctx && "Failed to create WebGL2 context"); |
| 58 | emscripten_webgl_make_context_current(ctx); |
| 59 | |
| 60 | static const char vertex_shader[] = |
| 61 | "#version 100\n" |
| 62 | "attribute vec4 apos;" |
| 63 | "attribute vec4 acolor;" |
| 64 | "varying vec4 color;" |
| 65 | "void main() {" |
| 66 | "color = acolor;" |
| 67 | "gl_Position = apos;" |
| 68 | "}"; |
| 69 | GLuint vs = compile_shader(GL_VERTEX_SHADER, vertex_shader); |
| 70 | |
| 71 | static const char fragment_shader[] = |
| 72 | "#version 100\n" |
| 73 | "precision lowp float;" |
| 74 | "varying vec4 color;" |
| 75 | "void main() {" |
| 76 | "gl_FragColor = color;" |
| 77 | "}"; |
| 78 | GLuint fs = compile_shader(GL_FRAGMENT_SHADER, fragment_shader); |
| 79 | |
| 80 | GLuint program = create_program(vs, fs); |
| 81 | glUseProgram(program); |
| 82 | |
| 83 | static const uint32_t pos_and_color[] = { |
| 84 | // 1,0,y,x, a,b,g,r |
| 85 | 0x400b36cd, 0xc00003ff, |
| 86 | 0x400b3533, 0xc00ffc00, |
| 87 | 0x4004cc00, 0xfff00000, |
| 88 | }; |
| 89 | |
| 90 | GLuint vbo; |
| 91 | glGenBuffers(1, &vbo); |
| 92 | glBindBuffer(GL_ARRAY_BUFFER, vbo); |
| 93 | glBufferData(GL_ARRAY_BUFFER, sizeof(pos_and_color), pos_and_color, GL_STATIC_DRAW); |
| 94 | glVertexAttribPointer(0, 4, GL_INT_2_10_10_10_REV, GL_TRUE, 8, 0); |
| 95 | // printf("%d\n", glGetError()); |
| 96 | #if 0 |
| 97 | switch (glGetError()) { |
| 98 | case GL_NO_ERROR: |
| 99 | std::cout << "No error!"; |
| 100 | break; |
| 101 | case GL_INVALID_ENUM: |
| 102 | std::cout << "Invliad enum."; |
| 103 | break; |
| 104 | } |
| 105 | #endif |
| 106 | assert(glGetError() == GL_NO_ERROR && "glVertexAttribPointer with GL_INT_2_10_10_10_REV failed"); |
| 107 | glVertexAttribPointer(1, 4, GL_UNSIGNED_INT_2_10_10_10_REV, GL_TRUE, 8, (void*)4); |
| 108 | assert(glGetError() == GL_NO_ERROR && "glVertexAttribPointer with GL_UNSIGNED_INT_2_10_10_10_REV failed"); |
| 109 | |
| 110 | glEnableVertexAttribArray(0); |
| 111 | glEnableVertexAttribArray(1); |
| 112 | |
| 113 | glClearColor(0.3f,0.3f,0.3f,1); |
| 114 | glClear(GL_COLOR_BUFFER_BIT); |
| 115 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 116 | |
| 117 | #ifdef EXPLICIT_SWAP |
| 118 | emscripten_webgl_commit_frame(); |
| 119 | #endif |
| 120 | |
| 121 | #ifdef REPORT_RESULT |
| 122 | REPORT_RESULT(0); |
| 123 | #endif |
| 124 | } |