1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/*
* Copyright 2014 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
// Built from glbook/hello triange and sdl_ogl, see details there
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_opengl.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
GLuint programObject;
int width = 512;
int height = 256;
GLuint LoadShader ( GLenum type, const char *shaderSrc )
{
GLuint shader;
GLint compiled;
shader = glCreateShader ( type );
if ( shader == 0 )
return 0;
glShaderSource ( shader, 1, &shaderSrc, NULL );
glCompileShader ( shader );
glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled )
{
GLint infoLen = 0;
glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );
if ( infoLen > 1 )
{
char* infoLog = malloc (sizeof(char) * infoLen );
glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );
printf ( "Error compiling shader:\n%s\n", infoLog );
free ( infoLog );
}
glDeleteShader ( shader );
return 0;
}
return shader;
}
int Init ()
{
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"} \n";
GLbyte fShaderStr[] =
"precision mediump float;\n"\
"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( 0.0, 0.0, 1.0, 1.0 );\n"
"} \n";
GLuint vertexShader;
GLuint fragmentShader;
GLint linked;
vertexShader = LoadShader ( GL_VERTEX_SHADER, (const char *)vShaderStr );
fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, (const char *)fShaderStr );
programObject = glCreateProgram ( );
if ( programObject == 0 )
return 0;
glAttachShader ( programObject, vertexShader );
glAttachShader ( programObject, fragmentShader );
glBindAttribLocation ( programObject, 0, "vPosition" );
glLinkProgram ( programObject );
glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
if ( !linked )
{
GLint infoLen = 0;
glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
if ( infoLen > 1 )
{
char* infoLog = malloc (sizeof(char) * infoLen );
glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
printf ( "Error linking program:\n%s\n", infoLog );
free ( infoLog );
}
glDeleteProgram ( programObject );
return GL_FALSE;
}
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
return GL_TRUE;
}
static const int kNegativeTest = 0;
static const int kPositiveTest = 1;
///
// Draw a triangle using the shader pair created in Init()
//
void Draw (GLenum accessBits, int testMode)
{
void *buffer;
GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f };
// No clientside arrays, so do this in a webgl-friendly manner
GLuint vertexPosObject;
glGenBuffers(1, &vertexPosObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices), NULL, GL_STATIC_DRAW);
buffer = glMapBufferRange(
GL_ARRAY_BUFFER,
0,
sizeof(vVertices),
accessBits
);
if (testMode == kPositiveTest && buffer == NULL) {
fprintf(stderr, "Could not map buffer: %x\n", glGetError());
return;
}
if (testMode == kNegativeTest) {
// When negative testing, stop after the call to glMapBufferRange.
return;
}
memcpy(buffer, vVertices, sizeof(vVertices));
if (accessBits & GL_MAP_FLUSH_EXPLICIT_BIT)
{
glFlushMappedBufferRange(GL_ARRAY_BUFFER, 0, sizeof(vVertices));
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glViewport ( 0, 0, width, height );
glClear ( GL_COLOR_BUFFER_BIT );
glUseProgram ( programObject );
glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject);
glVertexAttribPointer(0 /* ? */, 3, GL_FLOAT, 0, 0, 0);
glEnableVertexAttribArray(0);
glDrawArrays ( GL_TRIANGLES, 0, 3 );
}
void Verify() {
unsigned char *data = malloc(width*height*4 + 16);
int *last = (int*)(data + width*height*4 - 4);
int *after = (int*)(data + width*height*4);
*last = 0xdeadbeef;
*after = 0x12345678;
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
assert(*last != 0xdeadbeef); // should overwrite the buffer to the end
assert(*after == 0x12345678); // nothing should be written afterwards!
// Should see some blue, and nothing else
int seen = 0;
int ok = 1;
for (int x = 0; x < width*height; x++) {
seen = seen || data[x*4+2] != 0;
ok = ok && (data[x*4+0] == 0);
ok = ok && (data[x*4+1] == 0);
}
assert(seen);
assert(ok);
}
int main(int argc, char *argv[])
{
SDL_Surface *screen;
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
screen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL);
if (!screen) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
Init();
// Positive Tests.
// Test both invalidate modes and write.
Draw(GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_WRITE_BIT, kPositiveTest);
Verify();
Draw(GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_WRITE_BIT, kPositiveTest);
Verify();
// Test both invalidate modes and write with flush explicit.
Draw(GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_WRITE_BIT, kPositiveTest);
Verify();
Draw(GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_WRITE_BIT, kPositiveTest);
Verify();
// Negative tests.
// Test write bit alone is unsupported.
Draw(GL_MAP_WRITE_BIT, kNegativeTest);
// Test read bit is unsupported.
Draw(GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_WRITE_BIT | GL_MAP_READ_BIT, kNegativeTest);
// Test unsynchronized bit is unsupported.
Draw(GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT, kNegativeTest);
return 0;
}
|