David Li | aa1f54d | 2011-03-01 16:54:04 -0800 | [diff] [blame] | 1 | #include "gles2context.h" |
| 2 | |
| 3 | //#undef LOGD |
| 4 | //#define LOGD(...) |
| 5 | |
| 6 | static inline GLuint s2n(gl_shader * s) |
| 7 | { |
| 8 | return (GLuint)s ^ 0xaf3c532d; |
| 9 | } |
| 10 | |
| 11 | static inline gl_shader * n2s(GLuint n) |
| 12 | { |
| 13 | return (gl_shader *)(n ^ 0xaf3c532d); |
| 14 | } |
| 15 | |
| 16 | static inline GLuint p2n(gl_shader_program * p) |
| 17 | { |
| 18 | return (GLuint)p ^ 0x04dc18f9; |
| 19 | } |
| 20 | |
| 21 | static inline gl_shader_program * n2p(GLuint n) |
| 22 | { |
| 23 | return (gl_shader_program *)(n ^ 0x04dc18f9); |
| 24 | } |
| 25 | |
| 26 | void glAttachShader(GLuint program, GLuint shader) |
| 27 | { |
| 28 | GLES2_GET_CONST_CONTEXT(ctx); |
| 29 | ctx->iface->ShaderAttach(ctx->iface, n2p(program), n2s(shader)); |
| 30 | } |
| 31 | |
| 32 | void glBindAttribLocation(GLuint program, GLuint index, const GLchar* name) |
| 33 | { |
| 34 | GLES2_GET_CONST_CONTEXT(ctx); |
| 35 | ctx->iface->ShaderAttributeBind(n2p(program), index, name); |
| 36 | // assert(0); |
| 37 | } |
| 38 | |
| 39 | GLuint glCreateShader(GLenum type) |
| 40 | { |
| 41 | GLES2_GET_CONST_CONTEXT(ctx); |
| 42 | return s2n(ctx->iface->ShaderCreate(ctx->iface, type)); |
| 43 | } |
| 44 | |
| 45 | GLuint glCreateProgram(void) |
| 46 | { |
| 47 | GLES2_GET_CONST_CONTEXT(ctx); |
| 48 | return p2n(ctx->iface->ShaderProgramCreate(ctx->iface)); |
| 49 | } |
| 50 | |
| 51 | void glCompileShader(GLuint shader) |
| 52 | { |
| 53 | GLES2_GET_CONST_CONTEXT(ctx); |
| 54 | ctx->iface->ShaderCompile(ctx->iface, n2s(shader), NULL, NULL); |
| 55 | } |
| 56 | |
| 57 | void glDeleteProgram(GLuint program) |
| 58 | { |
| 59 | GLES2_GET_CONST_CONTEXT(ctx); |
| 60 | ctx->iface->ShaderProgramDelete(ctx->iface, n2p(program)); |
| 61 | } |
| 62 | |
| 63 | void glDeleteShader(GLuint shader) |
| 64 | { |
| 65 | GLES2_GET_CONST_CONTEXT(ctx); |
| 66 | ctx->iface->ShaderDelete(ctx->iface, n2s(shader)); |
| 67 | } |
| 68 | |
| 69 | void glDetachShader(GLuint program, GLuint shader) |
| 70 | { |
| 71 | GLES2_GET_CONST_CONTEXT(ctx); |
| 72 | ctx->iface->ShaderDetach(ctx->iface, n2p(program), n2s(shader)); |
| 73 | } |
| 74 | |
| 75 | GLint glGetAttribLocation(GLuint program, const GLchar* name) |
| 76 | { |
| 77 | GLES2_GET_CONST_CONTEXT(ctx); |
| 78 | GLint location = ctx->iface->ShaderAttributeLocation(n2p(program), name); |
| 79 | // LOGD("\n*\n*\n* agl2: glGetAttribLocation program=%u name=%s location=%d \n*\n*", |
| 80 | // program, name, location); |
| 81 | return location; |
| 82 | } |
| 83 | |
| 84 | void glGetProgramiv(GLuint program, GLenum pname, GLint* params) |
| 85 | { |
| 86 | GLES2_GET_CONST_CONTEXT(ctx); |
| 87 | ctx->iface->ShaderProgramGetiv(n2p(program), pname, params); |
| 88 | LOGD("agl2: glGetProgramiv 0x%.4X=%d \n", pname, *params); |
| 89 | } |
| 90 | |
| 91 | void glGetProgramInfoLog(GLuint program, GLsizei bufsize, GLsizei* length, GLchar* infolog) |
| 92 | { |
| 93 | GLES2_GET_CONST_CONTEXT(ctx); |
| 94 | ctx->iface->ShaderProgramGetInfoLog(n2p(program), bufsize, length, infolog); |
| 95 | } |
| 96 | |
| 97 | void glGetShaderiv(GLuint shader, GLenum pname, GLint* params) |
| 98 | { |
| 99 | GLES2_GET_CONST_CONTEXT(ctx); |
| 100 | ctx->iface->ShaderGetiv(n2s(shader), pname, params); |
| 101 | LOGD("agl2: glGetShaderiv 0x%.4X=%d \n", pname, *params); |
| 102 | } |
| 103 | |
| 104 | void glGetShaderInfoLog(GLuint shader, GLsizei bufsize, GLsizei* length, GLchar* infolog) |
| 105 | { |
| 106 | GLES2_GET_CONST_CONTEXT(ctx); |
| 107 | ctx->iface->ShaderGetInfoLog(n2s(shader), bufsize, length, infolog); |
| 108 | } |
| 109 | |
| 110 | int glGetUniformLocation(GLuint program, const GLchar* name) |
| 111 | { |
| 112 | GLES2_GET_CONST_CONTEXT(ctx); |
| 113 | return ctx->iface->ShaderUniformLocation(n2p(program), name); |
| 114 | } |
| 115 | |
| 116 | void glLinkProgram(GLuint program) |
| 117 | { |
| 118 | GLES2_GET_CONST_CONTEXT(ctx); |
| 119 | GLboolean linked = ctx->iface->ShaderProgramLink(n2p(program), NULL); |
| 120 | assert(linked); |
| 121 | } |
| 122 | |
| 123 | void glShaderSource(GLuint shader, GLsizei count, const GLchar** string, const GLint* length) |
| 124 | { |
| 125 | GLES2_GET_CONST_CONTEXT(ctx); |
| 126 | ctx->iface->ShaderSource(n2s(shader), count, string, length); |
| 127 | } |
| 128 | |
| 129 | void glUniform1f(GLint location, GLfloat x) |
| 130 | { |
| 131 | GLES2_GET_CONST_CONTEXT(ctx); |
| 132 | int sampler = ctx->iface->ShaderUniform(ctx->rasterizer.CurrentProgram, location, 1, &x, GL_FLOAT); |
| 133 | assert(0 > sampler); // should be assigning to sampler |
| 134 | } |
| 135 | |
| 136 | void glUniform1i(GLint location, GLint x) |
| 137 | { |
| 138 | GLES2_GET_CONST_CONTEXT(ctx); |
| 139 | const float params[1] = {x}; |
| 140 | int sampler = ctx->iface->ShaderUniform(ctx->rasterizer.CurrentProgram, location, 1, params, GL_INT); |
| 141 | if (0 <= sampler) { |
| 142 | // LOGD("\n*\n* agl2: glUniform1i updated sampler=%d tmu=%d location=%d\n*", sampler, x, location); |
| 143 | assert(0 <= x && GGL_MAXCOMBINEDTEXTUREIMAGEUNITS > x); |
| 144 | // LOGD("tmu%u: format=0x%.2X w=%u h=%u levels=%p", x, ctx->tex.tmus[x]->format, |
| 145 | // ctx->tex.tmus[x]->width, ctx->tex.tmus[x]->height, ctx->tex.tmus[x]->format); |
| 146 | ctx->tex.sampler2tmu[sampler] = x; |
| 147 | ctx->tex.UpdateSampler(ctx->iface, x); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void glUniform2f(GLint location, GLfloat x, GLfloat y) |
| 152 | { |
| 153 | GLES2_GET_CONST_CONTEXT(ctx); |
| 154 | const float params[4] = {x, y}; |
| 155 | ctx->iface->ShaderUniform(ctx->rasterizer.CurrentProgram, location, 1, params, GL_FLOAT_VEC2); |
| 156 | } |
| 157 | |
| 158 | void glUniform4f(GLint location, GLfloat x, GLfloat y, GLfloat z, GLfloat w) |
| 159 | { |
| 160 | GLES2_GET_CONST_CONTEXT(ctx); |
| 161 | const float params[4] = {x, y, z, w}; |
| 162 | // LOGD("agl2: glUniform4f location=%d %f,%f,%f,%f", location, x, y, z, w); |
| 163 | ctx->iface->ShaderUniform(ctx->rasterizer.CurrentProgram, location, 1, params, GL_FLOAT_VEC4); |
| 164 | } |
| 165 | |
| 166 | void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) |
| 167 | { |
| 168 | GLES2_GET_CONST_CONTEXT(ctx); |
| 169 | // const gl_shader_program * program = ctx->rasterizer.CurrentProgram; |
| 170 | // if (strstr(program->Shaders[MESA_SHADER_FRAGMENT]->Source, ").a;")) { |
| 171 | // LOGD("agl2: glUniformMatrix4fv location=%d count=%d transpose=%d", location, count, transpose); |
| 172 | // for (unsigned i = 0; i < 4; i++) |
| 173 | // LOGD("agl2: glUniformMatrix4fv %.2f \t %.2f \t %.2f \t %.2f", value[i * 4 + 0], |
| 174 | // value[i * 4 + 1], value[i * 4 + 2], value[i * 4 + 3]); |
| 175 | // } |
| 176 | ctx->iface->ShaderUniformMatrix(ctx->rasterizer.CurrentProgram, 4, 4, location, count, transpose, value); |
| 177 | // while (true) |
| 178 | // ; |
| 179 | // assert(0); |
| 180 | } |
| 181 | |
| 182 | void glUseProgram(GLuint program) |
| 183 | { |
| 184 | GLES2_GET_CONST_CONTEXT(ctx); |
| 185 | // LOGD("\n*\n*\n* agl2: glUseProgram %d \n*\n*\n*", program); |
| 186 | ctx->iface->ShaderUse(ctx->iface, n2p(program)); |
| 187 | ctx->iface->ShaderUniformGetSamplers(n2p(program), ctx->tex.sampler2tmu); |
| 188 | for (unsigned i = 0; i < GGL_MAXCOMBINEDTEXTUREIMAGEUNITS; i++) |
| 189 | if (0 <= ctx->tex.sampler2tmu[i]) |
| 190 | ctx->iface->SetSampler(ctx->iface, i, ctx->tex.tmus[ctx->tex.sampler2tmu[i]]); |
| 191 | } |