| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* OpenGL drawing routines |
| 12 |
* |
| 13 |
* By Elias Pschernig. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "allegro5/allegro.h" |
| 17 |
#include "allegro5/allegro_opengl.h" |
| 18 |
#include "allegro5/internal/aintern_display.h" |
| 19 |
#include "allegro5/internal/aintern_memdraw.h" |
| 20 |
#include "allegro5/internal/aintern_opengl.h" |
| 21 |
#include "allegro5/internal/aintern_primitives.h" |
| 22 |
#include "allegro5/internal/aintern_prim_soft.h" |
| 23 |
|
| 24 |
#ifdef ALLEGRO_ANDROID |
| 25 |
#include "allegro5/internal/aintern_android.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "ogl_helpers.h" |
| 29 |
|
| 30 |
ALLEGRO_DEBUG_CHANNEL("opengl") |
| 31 |
|
| 32 |
/* FIXME: For some reason x86_64 Android crashes for me when calling |
| 33 |
* glBlendColor - so adding this hack to disable it. |
| 34 |
*/ |
| 35 |
#ifdef ALLEGRO_ANDROID |
| 36 |
#if defined(__x86_64__) || defined(__i686__) |
| 37 |
#define ALLEGRO_ANDROID_HACK_X86_64 |
| 38 |
#endif |
| 39 |
#endif |
| 40 |
|
| 41 |
static void convert_storage(ALLEGRO_PRIM_STORAGE storage, GLenum* type, int* ncoord, bool* normalized) |
| 42 |
{ |
| 43 |
switch(storage) { |
| 44 |
case ALLEGRO_PRIM_FLOAT_2: |
| 45 |
*type = GL_FLOAT; |
| 46 |
*ncoord = 2; |
| 47 |
*normalized = false; |
| 48 |
break; |
| 49 |
case ALLEGRO_PRIM_FLOAT_3: |
| 50 |
*type = GL_FLOAT; |
| 51 |
*ncoord = 3; |
| 52 |
*normalized = false; |
| 53 |
break; |
| 54 |
case ALLEGRO_PRIM_SHORT_2: |
| 55 |
*type = GL_SHORT; |
| 56 |
*ncoord = 2; |
| 57 |
*normalized = false; |
| 58 |
break; |
| 59 |
case ALLEGRO_PRIM_FLOAT_1: |
| 60 |
*type = GL_FLOAT; |
| 61 |
*ncoord = 1; |
| 62 |
*normalized = false; |
| 63 |
break; |
| 64 |
case ALLEGRO_PRIM_FLOAT_4: |
| 65 |
*type = GL_FLOAT; |
| 66 |
*ncoord = 4; |
| 67 |
*normalized = false; |
| 68 |
break; |
| 69 |
case ALLEGRO_PRIM_UBYTE_4: |
| 70 |
*type = GL_UNSIGNED_BYTE; |
| 71 |
*ncoord = 4; |
| 72 |
*normalized = false; |
| 73 |
break; |
| 74 |
case ALLEGRO_PRIM_SHORT_4: |
| 75 |
*type = GL_SHORT; |
| 76 |
*ncoord = 4; |
| 77 |
*normalized = false; |
| 78 |
break; |
| 79 |
case ALLEGRO_PRIM_NORMALIZED_UBYTE_4: |
| 80 |
*type = GL_UNSIGNED_BYTE; |
| 81 |
*ncoord = 4; |
| 82 |
*normalized = true; |
| 83 |
break; |
| 84 |
case ALLEGRO_PRIM_NORMALIZED_SHORT_2: |
| 85 |
*type = GL_SHORT; |
| 86 |
*ncoord = 2; |
| 87 |
*normalized = true; |
| 88 |
break; |
| 89 |
case ALLEGRO_PRIM_NORMALIZED_SHORT_4: |
| 90 |
*type = GL_SHORT; |
| 91 |
*ncoord = 4; |
| 92 |
*normalized = true; |
| 93 |
break; |
| 94 |
case ALLEGRO_PRIM_NORMALIZED_USHORT_2: |
| 95 |
*type = GL_UNSIGNED_SHORT; |
| 96 |
*ncoord = 2; |
| 97 |
*normalized = true; |
| 98 |
break; |
| 99 |
case ALLEGRO_PRIM_NORMALIZED_USHORT_4: |
| 100 |
*type = GL_UNSIGNED_SHORT; |
| 101 |
*ncoord = 4; |
| 102 |
*normalized = true; |
| 103 |
break; |
| 104 |
#ifndef ALLEGRO_CFG_OPENGLES |
| 105 |
case ALLEGRO_PRIM_HALF_FLOAT_2: |
| 106 |
*type = GL_HALF_FLOAT; |
| 107 |
*ncoord = 2; |
| 108 |
*normalized = false; |
| 109 |
break; |
| 110 |
case ALLEGRO_PRIM_HALF_FLOAT_4: |
| 111 |
*type = GL_HALF_FLOAT; |
| 112 |
*ncoord = 4; |
| 113 |
*normalized = false; |
| 114 |
break; |
| 115 |
#endif |
| 116 |
default: |
| 117 |
ASSERT(0); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static void setup_state(ALLEGRO_DISPLAY *display, const char* vtxs, const ALLEGRO_VERTEX_DECL* decl, ALLEGRO_BITMAP* texture) |
| 122 |
{ |
| 123 |
GLenum type; |
| 124 |
int ncoord; |
| 125 |
bool normalized; |
| 126 |
|
| 127 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 128 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 129 |
if(decl) { |
| 130 |
ALLEGRO_VERTEX_ELEMENT* e; |
| 131 |
int i; |
| 132 |
|
| 133 |
e = &decl->elements[ALLEGRO_PRIM_POSITION]; |
| 134 |
if(e->attribute) { |
| 135 |
convert_storage(e->storage, &type, &ncoord, &normalized); |
| 136 |
|
| 137 |
if (display->ogl_extras->varlocs.pos_loc >= 0) { |
| 138 |
glVertexAttribPointer(display->ogl_extras->varlocs.pos_loc, ncoord, type, normalized, decl->stride, vtxs + e->offset); |
| 139 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.pos_loc); |
| 140 |
} |
| 141 |
} else { |
| 142 |
if (display->ogl_extras->varlocs.pos_loc >= 0) { |
| 143 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.pos_loc); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
e = &decl->elements[ALLEGRO_PRIM_TEX_COORD]; |
| 148 |
if(!e->attribute) |
| 149 |
e = &decl->elements[ALLEGRO_PRIM_TEX_COORD_PIXEL]; |
| 150 |
if(e->attribute) { |
| 151 |
convert_storage(e->storage, &type, &ncoord, &normalized); |
| 152 |
|
| 153 |
if (display->ogl_extras->varlocs.texcoord_loc >= 0) { |
| 154 |
glVertexAttribPointer(display->ogl_extras->varlocs.texcoord_loc, ncoord, type, normalized, decl->stride, vtxs + e->offset); |
| 155 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.texcoord_loc); |
| 156 |
} |
| 157 |
} else { |
| 158 |
if (display->ogl_extras->varlocs.texcoord_loc >= 0) { |
| 159 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.texcoord_loc); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
e = &decl->elements[ALLEGRO_PRIM_COLOR_ATTR]; |
| 164 |
if(e->attribute) { |
| 165 |
if (display->ogl_extras->varlocs.color_loc >= 0) { |
| 166 |
glVertexAttribPointer(display->ogl_extras->varlocs.color_loc, 4, GL_FLOAT, true, decl->stride, vtxs + e->offset); |
| 167 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.color_loc); |
| 168 |
} |
| 169 |
} else { |
| 170 |
if (display->ogl_extras->varlocs.color_loc >= 0) { |
| 171 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.color_loc); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
for (i = 0; i < ALLEGRO_PRIM_MAX_USER_ATTR; i++) { |
| 176 |
e = &decl->elements[ALLEGRO_PRIM_USER_ATTR + i]; |
| 177 |
if (e->attribute) { |
| 178 |
convert_storage(e->storage, &type, &ncoord, &normalized); |
| 179 |
|
| 180 |
if (display->ogl_extras->varlocs.user_attr_loc[i] >= 0) { |
| 181 |
glVertexAttribPointer(display->ogl_extras->varlocs.user_attr_loc[i], ncoord, type, normalized, decl->stride, vtxs + e->offset); |
| 182 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.user_attr_loc[i]); |
| 183 |
} |
| 184 |
} else { |
| 185 |
if (display->ogl_extras->varlocs.user_attr_loc[i] >= 0) { |
| 186 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.user_attr_loc[i]); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} else { |
| 191 |
if (display->ogl_extras->varlocs.pos_loc >= 0) { |
| 192 |
glVertexAttribPointer(display->ogl_extras->varlocs.pos_loc, 3, GL_FLOAT, false, sizeof(ALLEGRO_VERTEX), vtxs + offsetof(ALLEGRO_VERTEX, x)); |
| 193 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.pos_loc); |
| 194 |
} |
| 195 |
|
| 196 |
if (display->ogl_extras->varlocs.texcoord_loc >= 0) { |
| 197 |
glVertexAttribPointer(display->ogl_extras->varlocs.texcoord_loc, 2, GL_FLOAT, false, sizeof(ALLEGRO_VERTEX), vtxs + offsetof(ALLEGRO_VERTEX, u)); |
| 198 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.texcoord_loc); |
| 199 |
} |
| 200 |
|
| 201 |
if (display->ogl_extras->varlocs.color_loc >= 0) { |
| 202 |
glVertexAttribPointer(display->ogl_extras->varlocs.color_loc, 4, GL_FLOAT, true, sizeof(ALLEGRO_VERTEX), vtxs + offsetof(ALLEGRO_VERTEX, color)); |
| 203 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.color_loc); |
| 204 |
} |
| 205 |
} |
| 206 |
#endif |
| 207 |
} |
| 208 |
else { |
| 209 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 210 |
if(decl) { |
| 211 |
ALLEGRO_VERTEX_ELEMENT* e; |
| 212 |
e = &decl->elements[ALLEGRO_PRIM_POSITION]; |
| 213 |
if(e->attribute) { |
| 214 |
glEnableClientState(GL_VERTEX_ARRAY); |
| 215 |
|
| 216 |
convert_storage(e->storage, &type, &ncoord, &normalized); |
| 217 |
|
| 218 |
glVertexPointer(ncoord, type, decl->stride, vtxs + e->offset); |
| 219 |
} else { |
| 220 |
glDisableClientState(GL_VERTEX_ARRAY); |
| 221 |
} |
| 222 |
|
| 223 |
e = &decl->elements[ALLEGRO_PRIM_TEX_COORD]; |
| 224 |
if(!e->attribute) |
| 225 |
e = &decl->elements[ALLEGRO_PRIM_TEX_COORD_PIXEL]; |
| 226 |
if(texture && e->attribute) { |
| 227 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 228 |
|
| 229 |
convert_storage(e->storage, &type, &ncoord, &normalized); |
| 230 |
|
| 231 |
glTexCoordPointer(ncoord, type, decl->stride, vtxs + e->offset); |
| 232 |
} else { |
| 233 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 234 |
} |
| 235 |
|
| 236 |
e = &decl->elements[ALLEGRO_PRIM_COLOR_ATTR]; |
| 237 |
if(e->attribute) { |
| 238 |
glEnableClientState(GL_COLOR_ARRAY); |
| 239 |
|
| 240 |
glColorPointer(4, GL_FLOAT, decl->stride, vtxs + e->offset); |
| 241 |
} else { |
| 242 |
glDisableClientState(GL_COLOR_ARRAY); |
| 243 |
glColor4f(1, 1, 1, 1); |
| 244 |
} |
| 245 |
} else { |
| 246 |
glEnableClientState(GL_COLOR_ARRAY); |
| 247 |
glEnableClientState(GL_VERTEX_ARRAY); |
| 248 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 249 |
if (!(display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE)) |
| 250 |
glDisableClientState(GL_NORMAL_ARRAY); |
| 251 |
|
| 252 |
glVertexPointer(3, GL_FLOAT, sizeof(ALLEGRO_VERTEX), vtxs + offsetof(ALLEGRO_VERTEX, x)); |
| 253 |
glColorPointer(4, GL_FLOAT, sizeof(ALLEGRO_VERTEX), vtxs + offsetof(ALLEGRO_VERTEX, color)); |
| 254 |
glTexCoordPointer(2, GL_FLOAT, sizeof(ALLEGRO_VERTEX), vtxs + offsetof(ALLEGRO_VERTEX, u)); |
| 255 |
} |
| 256 |
#endif |
| 257 |
} |
| 258 |
|
| 259 |
if (texture) { |
| 260 |
GLuint gl_texture = al_get_opengl_texture(texture); |
| 261 |
int true_w, true_h; |
| 262 |
int tex_x, tex_y; |
| 263 |
float mat[4][4] = { |
| 264 |
{1, 0, 0, 0}, |
| 265 |
{0, -1, 0, 0}, |
| 266 |
{0, 0, 1, 0}, |
| 267 |
{0, 0, 0, 1} |
| 268 |
}; |
| 269 |
int height; |
| 270 |
|
| 271 |
if (texture->parent) |
| 272 |
height = texture->parent->h; |
| 273 |
else |
| 274 |
height = texture->h; |
| 275 |
|
| 276 |
al_get_opengl_texture_size(texture, &true_w, &true_h); |
| 277 |
al_get_opengl_texture_position(texture, &tex_x, &tex_y); |
| 278 |
|
| 279 |
mat[3][0] = (float)tex_x / true_w; |
| 280 |
mat[3][1] = (float)(height - tex_y) / true_h; |
| 281 |
|
| 282 |
if(decl) { |
| 283 |
if(decl->elements[ALLEGRO_PRIM_TEX_COORD_PIXEL].attribute) { |
| 284 |
mat[0][0] = 1.0f / true_w; |
| 285 |
mat[1][1] = -1.0f / true_h; |
| 286 |
} else { |
| 287 |
mat[0][0] = (float)al_get_bitmap_width(texture) / true_w; |
| 288 |
mat[1][1] = -(float)al_get_bitmap_height(texture) / true_h; |
| 289 |
} |
| 290 |
} else { |
| 291 |
mat[0][0] = 1.0f / true_w; |
| 292 |
mat[1][1] = -1.0f / true_h; |
| 293 |
} |
| 294 |
|
| 295 |
if (!(display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE)) { |
| 296 |
glBindTexture(GL_TEXTURE_2D, gl_texture); |
| 297 |
} |
| 298 |
|
| 299 |
ALLEGRO_BITMAP_WRAP wrap_u, wrap_v; |
| 300 |
_al_get_bitmap_wrap(texture, &wrap_u, &wrap_v); |
| 301 |
|
| 302 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 303 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 304 |
GLint handle; |
| 305 |
|
| 306 |
handle = display->ogl_extras->varlocs.tex_matrix_loc; |
| 307 |
if (handle >= 0) |
| 308 |
glUniformMatrix4fv(handle, 1, false, (float *)mat); |
| 309 |
|
| 310 |
handle = display->ogl_extras->varlocs.use_tex_matrix_loc; |
| 311 |
if (handle >= 0) |
| 312 |
glUniform1i(handle, 1); |
| 313 |
|
| 314 |
if (display->ogl_extras->varlocs.use_tex_loc >= 0) { |
| 315 |
glUniform1i(display->ogl_extras->varlocs.use_tex_loc, 1); |
| 316 |
} |
| 317 |
if (display->ogl_extras->varlocs.tex_loc >= 0) { |
| 318 |
glActiveTexture(GL_TEXTURE0); |
| 319 |
glBindTexture(GL_TEXTURE_2D, al_get_opengl_texture(texture)); |
| 320 |
glUniform1i(display->ogl_extras->varlocs.tex_loc, 0); // 0th sampler |
| 321 |
|
| 322 |
if (wrap_u == ALLEGRO_BITMAP_WRAP_DEFAULT) |
| 323 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 324 |
if (wrap_v == ALLEGRO_BITMAP_WRAP_DEFAULT) |
| 325 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 326 |
} |
| 327 |
#endif |
| 328 |
} |
| 329 |
else { |
| 330 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 331 |
glMatrixMode(GL_TEXTURE); |
| 332 |
glLoadMatrixf(mat[0]); |
| 333 |
glMatrixMode(GL_MODELVIEW); |
| 334 |
glEnable(GL_TEXTURE_2D); |
| 335 |
if (wrap_u == ALLEGRO_BITMAP_WRAP_DEFAULT) |
| 336 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 337 |
if (wrap_v == ALLEGRO_BITMAP_WRAP_DEFAULT) |
| 338 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 339 |
#endif |
| 340 |
} |
| 341 |
} else { |
| 342 |
/* Don't unbind the texture here if shaders are used, since the user may |
| 343 |
* have set the 0'th texture unit manually via the shader API. */ |
| 344 |
if (!(display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE)) { |
| 345 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
static void revert_state(ALLEGRO_DISPLAY *display, ALLEGRO_BITMAP* texture) |
| 351 |
{ |
| 352 |
if(texture) { |
| 353 |
ALLEGRO_BITMAP_WRAP wrap_u, wrap_v; |
| 354 |
_al_get_bitmap_wrap(texture, &wrap_u, &wrap_v); |
| 355 |
|
| 356 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 357 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 358 |
float identity[16] = { |
| 359 |
1, 0, 0, 0, |
| 360 |
0, 1, 0, 0, |
| 361 |
0, 0, 1, 0, |
| 362 |
0, 0, 0, 1 |
| 363 |
}; |
| 364 |
GLint handle; |
| 365 |
handle = display->ogl_extras->varlocs.tex_matrix_loc; |
| 366 |
if (handle >= 0) |
| 367 |
glUniformMatrix4fv(handle, 1, false, identity); |
| 368 |
handle = display->ogl_extras->varlocs.use_tex_matrix_loc; |
| 369 |
if (handle >= 0) |
| 370 |
glUniform1i(handle, 0); |
| 371 |
if (display->ogl_extras->varlocs.use_tex_loc >= 0) |
| 372 |
glUniform1i(display->ogl_extras->varlocs.use_tex_loc, 0); |
| 373 |
|
| 374 |
if (display->ogl_extras->varlocs.tex_loc >= 0) { |
| 375 |
if (wrap_u == ALLEGRO_BITMAP_WRAP_DEFAULT) |
| 376 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 377 |
if (wrap_v == ALLEGRO_BITMAP_WRAP_DEFAULT) |
| 378 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 379 |
} |
| 380 |
#endif |
| 381 |
} |
| 382 |
else { |
| 383 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 384 |
if (wrap_u == ALLEGRO_BITMAP_WRAP_DEFAULT) |
| 385 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 386 |
if (wrap_v == ALLEGRO_BITMAP_WRAP_DEFAULT) |
| 387 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 388 |
|
| 389 |
glDisable(GL_TEXTURE_2D); |
| 390 |
glMatrixMode(GL_TEXTURE); |
| 391 |
glLoadIdentity(); |
| 392 |
glMatrixMode(GL_MODELVIEW); |
| 393 |
#endif |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 398 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 399 |
if (display->ogl_extras->varlocs.pos_loc >= 0) |
| 400 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.pos_loc); |
| 401 |
if (display->ogl_extras->varlocs.color_loc >= 0) |
| 402 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.color_loc); |
| 403 |
if (display->ogl_extras->varlocs.texcoord_loc >= 0) |
| 404 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.texcoord_loc); |
| 405 |
#endif |
| 406 |
} |
| 407 |
else { |
| 408 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 409 |
glDisableClientState(GL_COLOR_ARRAY); |
| 410 |
glDisableClientState(GL_VERTEX_ARRAY); |
| 411 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 412 |
#endif |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
static int draw_prim_common(ALLEGRO_BITMAP* target, ALLEGRO_BITMAP* texture, |
| 417 |
ALLEGRO_VERTEX_BUFFER* vertex_buffer, |
| 418 |
const void* vtx, const ALLEGRO_VERTEX_DECL* decl, |
| 419 |
int start, int end, int type) |
| 420 |
{ |
| 421 |
int num_primitives = 0; |
| 422 |
ALLEGRO_DISPLAY *disp = _al_get_bitmap_display(target); |
| 423 |
ALLEGRO_BITMAP *opengl_target = target; |
| 424 |
ALLEGRO_BITMAP_EXTRA_OPENGL *extra; |
| 425 |
int num_vtx = end - start; |
| 426 |
|
| 427 |
if (target->parent) { |
| 428 |
opengl_target = target->parent; |
| 429 |
} |
| 430 |
extra = opengl_target->extra; |
| 431 |
|
| 432 |
if ((!extra->is_backbuffer && disp->ogl_extras->opengl_target != |
| 433 |
opengl_target) || al_is_bitmap_locked(target)) { |
| 434 |
if (vertex_buffer) { |
| 435 |
return _al_draw_buffer_common_soft(vertex_buffer, texture, NULL, start, end, type); |
| 436 |
} |
| 437 |
else { |
| 438 |
return _al_draw_prim_soft(texture, vtx, decl, start, end, type); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
if (vertex_buffer) { |
| 443 |
glBindBuffer(GL_ARRAY_BUFFER, (GLuint)vertex_buffer->common.handle); |
| 444 |
} |
| 445 |
|
| 446 |
_al_opengl_set_blender(disp); |
| 447 |
setup_state(disp, vtx, decl, texture); |
| 448 |
|
| 449 |
switch (type) { |
| 450 |
case ALLEGRO_PRIM_LINE_LIST: { |
| 451 |
glDrawArrays(GL_LINES, start, num_vtx); |
| 452 |
num_primitives = num_vtx / 2; |
| 453 |
break; |
| 454 |
}; |
| 455 |
case ALLEGRO_PRIM_LINE_STRIP: { |
| 456 |
glDrawArrays(GL_LINE_STRIP, start, num_vtx); |
| 457 |
num_primitives = num_vtx - 1; |
| 458 |
break; |
| 459 |
}; |
| 460 |
case ALLEGRO_PRIM_LINE_LOOP: { |
| 461 |
glDrawArrays(GL_LINE_LOOP, start, num_vtx); |
| 462 |
num_primitives = num_vtx; |
| 463 |
break; |
| 464 |
}; |
| 465 |
case ALLEGRO_PRIM_TRIANGLE_LIST: { |
| 466 |
glDrawArrays(GL_TRIANGLES, start, num_vtx); |
| 467 |
num_primitives = num_vtx / 3; |
| 468 |
break; |
| 469 |
}; |
| 470 |
case ALLEGRO_PRIM_TRIANGLE_STRIP: { |
| 471 |
glDrawArrays(GL_TRIANGLE_STRIP, start, num_vtx); |
| 472 |
num_primitives = num_vtx - 2; |
| 473 |
break; |
| 474 |
}; |
| 475 |
case ALLEGRO_PRIM_TRIANGLE_FAN: { |
| 476 |
glDrawArrays(GL_TRIANGLE_FAN, start, num_vtx); |
| 477 |
num_primitives = num_vtx - 2; |
| 478 |
break; |
| 479 |
}; |
| 480 |
case ALLEGRO_PRIM_POINT_LIST: { |
| 481 |
glDrawArrays(GL_POINTS, start, num_vtx); |
| 482 |
num_primitives = num_vtx; |
| 483 |
break; |
| 484 |
}; |
| 485 |
} |
| 486 |
|
| 487 |
revert_state(disp, texture); |
| 488 |
|
| 489 |
if (vertex_buffer) { |
| 490 |
glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 491 |
} |
| 492 |
|
| 493 |
return num_primitives; |
| 494 |
} |
| 495 |
|
| 496 |
static int draw_prim_indexed_common(ALLEGRO_BITMAP* target, ALLEGRO_BITMAP* texture, |
| 497 |
ALLEGRO_VERTEX_BUFFER* vertex_buffer, |
| 498 |
const void* vtx, const ALLEGRO_VERTEX_DECL* decl, |
| 499 |
ALLEGRO_INDEX_BUFFER* index_buffer, |
| 500 |
const int* indices, |
| 501 |
int start, int end, int type) |
| 502 |
{ |
| 503 |
int num_primitives = 0; |
| 504 |
ALLEGRO_DISPLAY *disp = _al_get_bitmap_display(target); |
| 505 |
ALLEGRO_BITMAP *opengl_target = target; |
| 506 |
ALLEGRO_BITMAP_EXTRA_OPENGL *extra; |
| 507 |
const char* idx = (const char*)indices; |
| 508 |
int start_offset = 0; |
| 509 |
GLenum idx_size = GL_UNSIGNED_INT; |
| 510 |
bool use_buffers = index_buffer != NULL; |
| 511 |
int num_vtx = end - start; |
| 512 |
#if defined ALLEGRO_IPHONE |
| 513 |
GLushort* iphone_idx = NULL; |
| 514 |
#endif |
| 515 |
|
| 516 |
if (use_buffers) { |
| 517 |
idx_size = index_buffer->index_size == 4 ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT; |
| 518 |
start_offset = start * index_buffer->index_size; |
| 519 |
} |
| 520 |
|
| 521 |
if (target->parent) { |
| 522 |
opengl_target = target->parent; |
| 523 |
} |
| 524 |
extra = opengl_target->extra; |
| 525 |
|
| 526 |
if ((!extra->is_backbuffer && disp->ogl_extras->opengl_target != |
| 527 |
opengl_target) || al_is_bitmap_locked(target)) { |
| 528 |
if (use_buffers) { |
| 529 |
return _al_draw_buffer_common_soft(vertex_buffer, texture, index_buffer, start, end, type); |
| 530 |
} |
| 531 |
else { |
| 532 |
return _al_draw_prim_indexed_soft(texture, vtx, decl, indices, num_vtx, type); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
#if defined ALLEGRO_IPHONE |
| 537 |
if (!use_buffers) { |
| 538 |
int ii; |
| 539 |
iphone_idx = al_malloc(num_vtx * sizeof(GLushort)); |
| 540 |
for (ii = start; ii < end; ii++) { |
| 541 |
iphone_idx[ii] = (GLushort)indices[ii]; |
| 542 |
} |
| 543 |
idx = iphone_idx; |
| 544 |
start = 0; |
| 545 |
idx_size = GL_UNSIGNED_SHORT; |
| 546 |
} |
| 547 |
#endif |
| 548 |
|
| 549 |
_al_opengl_set_blender(disp); |
| 550 |
|
| 551 |
if (use_buffers) { |
| 552 |
glBindBuffer(GL_ARRAY_BUFFER, (GLuint)vertex_buffer->common.handle); |
| 553 |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GLuint)index_buffer->common.handle); |
| 554 |
} |
| 555 |
|
| 556 |
setup_state(disp, vtx, decl, texture); |
| 557 |
|
| 558 |
switch (type) { |
| 559 |
case ALLEGRO_PRIM_LINE_LIST: { |
| 560 |
glDrawElements(GL_LINES, num_vtx, idx_size, idx + start_offset); |
| 561 |
num_primitives = num_vtx / 2; |
| 562 |
break; |
| 563 |
}; |
| 564 |
case ALLEGRO_PRIM_LINE_STRIP: { |
| 565 |
glDrawElements(GL_LINE_STRIP, num_vtx, idx_size, idx + start_offset); |
| 566 |
num_primitives = num_vtx - 1; |
| 567 |
break; |
| 568 |
}; |
| 569 |
case ALLEGRO_PRIM_LINE_LOOP: { |
| 570 |
/* Unimplemented, as it's too hard to do for Direct3D */ |
| 571 |
break; |
| 572 |
}; |
| 573 |
case ALLEGRO_PRIM_TRIANGLE_LIST: { |
| 574 |
glDrawElements(GL_TRIANGLES, num_vtx, idx_size, idx + start_offset); |
| 575 |
num_primitives = num_vtx / 3; |
| 576 |
break; |
| 577 |
}; |
| 578 |
case ALLEGRO_PRIM_TRIANGLE_STRIP: { |
| 579 |
glDrawElements(GL_TRIANGLE_STRIP, num_vtx, idx_size, idx + start_offset); |
| 580 |
num_primitives = num_vtx - 2; |
| 581 |
break; |
| 582 |
}; |
| 583 |
case ALLEGRO_PRIM_TRIANGLE_FAN: { |
| 584 |
glDrawElements(GL_TRIANGLE_FAN, num_vtx, idx_size, idx + start_offset); |
| 585 |
num_primitives = num_vtx - 2; |
| 586 |
break; |
| 587 |
}; |
| 588 |
case ALLEGRO_PRIM_POINT_LIST: { |
| 589 |
/* Unimplemented, as it's too hard to do for Direct3D */ |
| 590 |
break; |
| 591 |
}; |
| 592 |
} |
| 593 |
|
| 594 |
revert_state(disp, texture); |
| 595 |
|
| 596 |
if (use_buffers) { |
| 597 |
glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 598 |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 599 |
} |
| 600 |
|
| 601 |
#if defined ALLEGRO_IPHONE |
| 602 |
al_free(iphone_idx); |
| 603 |
#endif |
| 604 |
|
| 605 |
return num_primitives; |
| 606 |
} |
| 607 |
|
| 608 |
static int ogl_draw_prim(ALLEGRO_BITMAP* target, ALLEGRO_BITMAP* texture, const void* vtxs, const ALLEGRO_VERTEX_DECL* decl, int start, int end, int type) |
| 609 |
{ |
| 610 |
return draw_prim_common(target, texture, 0, vtxs, decl, start, end, type); |
| 611 |
} |
| 612 |
|
| 613 |
static int ogl_draw_vertex_buffer(ALLEGRO_BITMAP* target, ALLEGRO_BITMAP* texture, ALLEGRO_VERTEX_BUFFER* vertex_buffer, int start, int end, int type) |
| 614 |
{ |
| 615 |
return draw_prim_common(target, texture, vertex_buffer, 0, vertex_buffer->decl, start, end, type); |
| 616 |
} |
| 617 |
|
| 618 |
static int ogl_draw_prim_indexed(ALLEGRO_BITMAP *target, ALLEGRO_BITMAP* texture, const void* vtxs, const ALLEGRO_VERTEX_DECL* decl, const int* indices, int num_vtx, int type) |
| 619 |
{ |
| 620 |
return draw_prim_indexed_common(target, texture, NULL, vtxs, decl, NULL, indices, 0, num_vtx, type); |
| 621 |
} |
| 622 |
|
| 623 |
static int ogl_draw_indexed_buffer(ALLEGRO_BITMAP* target, ALLEGRO_BITMAP* texture, ALLEGRO_VERTEX_BUFFER* vertex_buffer, ALLEGRO_INDEX_BUFFER* index_buffer, int start, int end, int type) |
| 624 |
{ |
| 625 |
return draw_prim_indexed_common(target, texture, vertex_buffer, NULL, vertex_buffer->decl, index_buffer, NULL, start, end, type); |
| 626 |
} |
| 627 |
|
| 628 |
static bool create_buffer_common(_AL_BUFFER_COMMON* common, GLenum type, const void* initial_data, GLsizeiptr size, int flags) |
| 629 |
{ |
| 630 |
GLuint vbo; |
| 631 |
GLenum usage; |
| 632 |
|
| 633 |
switch (flags) |
| 634 |
{ |
| 635 |
#if !defined ALLEGRO_CFG_OPENGLES |
| 636 |
case ALLEGRO_PRIM_BUFFER_STREAM: |
| 637 |
usage = GL_STREAM_DRAW; |
| 638 |
break; |
| 639 |
#endif |
| 640 |
case ALLEGRO_PRIM_BUFFER_STATIC: |
| 641 |
usage = GL_STATIC_DRAW; |
| 642 |
break; |
| 643 |
case ALLEGRO_PRIM_BUFFER_DYNAMIC: |
| 644 |
usage = GL_DYNAMIC_DRAW; |
| 645 |
break; |
| 646 |
default: |
| 647 |
usage = GL_STATIC_DRAW; |
| 648 |
} |
| 649 |
|
| 650 |
glGenBuffers(1, &vbo); |
| 651 |
glBindBuffer(type, vbo); |
| 652 |
glBufferData(type, size, initial_data, usage); |
| 653 |
glBindBuffer(type, 0); |
| 654 |
|
| 655 |
if (glGetError()) |
| 656 |
return false; |
| 657 |
|
| 658 |
common->handle = vbo; |
| 659 |
common->local_buffer_length = 0; |
| 660 |
return true; |
| 661 |
} |
| 662 |
|
| 663 |
static bool ogl_create_vertex_buffer(ALLEGRO_VERTEX_BUFFER* buf, const void* initial_data, size_t num_vertices, int flags) |
| 664 |
{ |
| 665 |
int stride = buf->decl ? buf->decl->stride : (int)sizeof(ALLEGRO_VERTEX); |
| 666 |
return create_buffer_common(&buf->common, GL_ARRAY_BUFFER, initial_data, num_vertices * stride, flags); |
| 667 |
} |
| 668 |
|
| 669 |
static bool ogl_create_index_buffer(ALLEGRO_INDEX_BUFFER* buf, const void* initial_data, size_t num_indices, int flags) |
| 670 |
{ |
| 671 |
return create_buffer_common(&buf->common, GL_ELEMENT_ARRAY_BUFFER, initial_data, num_indices * buf->index_size, flags);; |
| 672 |
} |
| 673 |
|
| 674 |
static void ogl_destroy_vertex_buffer(ALLEGRO_VERTEX_BUFFER* buf) |
| 675 |
{ |
| 676 |
glDeleteBuffers(1, (GLuint*)&buf->common.handle); |
| 677 |
al_free(buf->common.locked_memory); |
| 678 |
} |
| 679 |
|
| 680 |
static void ogl_destroy_index_buffer(ALLEGRO_INDEX_BUFFER* buf) |
| 681 |
{ |
| 682 |
glDeleteBuffers(1, (GLuint*)&buf->common.handle); |
| 683 |
al_free(buf->common.locked_memory); |
| 684 |
} |
| 685 |
|
| 686 |
static void* lock_buffer_common(_AL_BUFFER_COMMON* common, GLenum type) |
| 687 |
{ |
| 688 |
if (common->local_buffer_length < common->lock_length) { |
| 689 |
common->locked_memory = al_realloc(common->locked_memory, common->lock_length); |
| 690 |
common->local_buffer_length = common->lock_length; |
| 691 |
} |
| 692 |
|
| 693 |
if (common->lock_flags != ALLEGRO_LOCK_WRITEONLY) { |
| 694 |
#if !defined ALLEGRO_CFG_OPENGLES |
| 695 |
glBindBuffer(type, (GLuint)common->handle); |
| 696 |
glGetBufferSubData(type, common->lock_offset, common->lock_length, common->locked_memory); |
| 697 |
glBindBuffer(type, 0); |
| 698 |
if (glGetError()) |
| 699 |
return 0; |
| 700 |
#else |
| 701 |
(void)type; |
| 702 |
return 0; |
| 703 |
#endif |
| 704 |
} |
| 705 |
return common->locked_memory; |
| 706 |
} |
| 707 |
|
| 708 |
static void* ogl_lock_vertex_buffer(ALLEGRO_VERTEX_BUFFER* buf) |
| 709 |
{ |
| 710 |
return lock_buffer_common(&buf->common, GL_ARRAY_BUFFER); |
| 711 |
} |
| 712 |
|
| 713 |
static void* ogl_lock_index_buffer(ALLEGRO_INDEX_BUFFER* buf) |
| 714 |
{ |
| 715 |
return lock_buffer_common(&buf->common, GL_ELEMENT_ARRAY_BUFFER); |
| 716 |
} |
| 717 |
|
| 718 |
static void unlock_buffer_common(_AL_BUFFER_COMMON* common, GLenum type) |
| 719 |
{ |
| 720 |
if (common->lock_flags != ALLEGRO_LOCK_READONLY) { |
| 721 |
glBindBuffer(type, (GLuint)common->handle); |
| 722 |
glBufferSubData(type, common->lock_offset, common->lock_length, common->locked_memory); |
| 723 |
glBindBuffer(type, 0); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
static void ogl_unlock_vertex_buffer(ALLEGRO_VERTEX_BUFFER* buf) |
| 728 |
{ |
| 729 |
unlock_buffer_common(&buf->common, GL_ARRAY_BUFFER); |
| 730 |
} |
| 731 |
|
| 732 |
static void ogl_unlock_index_buffer(ALLEGRO_INDEX_BUFFER* buf) |
| 733 |
{ |
| 734 |
unlock_buffer_common(&buf->common, GL_ELEMENT_ARRAY_BUFFER); |
| 735 |
} |
| 736 |
|
| 737 |
static void try_const_color(ALLEGRO_DISPLAY *ogl_disp, ALLEGRO_COLOR *c) |
| 738 |
{ |
| 739 |
#ifdef ALLEGRO_CFG_OPENGLES |
| 740 |
#ifndef ALLEGRO_CFG_OPENGLES2 |
| 741 |
return; |
| 742 |
#endif |
| 743 |
// Only OpenGL ES 2.0 has glBlendColor |
| 744 |
if (ogl_disp->ogl_extras->ogl_info.version < _ALLEGRO_OPENGL_VERSION_2_0) { |
| 745 |
return; |
| 746 |
} |
| 747 |
#else |
| 748 |
(void)ogl_disp; |
| 749 |
#endif |
| 750 |
glBlendColor(c->r, c->g, c->b, c->a); |
| 751 |
} |
| 752 |
|
| 753 |
bool _al_opengl_set_blender(ALLEGRO_DISPLAY *ogl_disp) |
| 754 |
{ |
| 755 |
int op, src_color, dst_color, op_alpha, src_alpha, dst_alpha; |
| 756 |
ALLEGRO_COLOR const_color; |
| 757 |
const int blend_modes[10] = { |
| 758 |
GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
| 759 |
GL_SRC_COLOR, GL_DST_COLOR, GL_ONE_MINUS_SRC_COLOR, |
| 760 |
GL_ONE_MINUS_DST_COLOR, |
| 761 |
#if defined(ALLEGRO_CFG_OPENGLES2) || !defined(ALLEGRO_CFG_OPENGLES) |
| 762 |
GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR |
| 763 |
#else |
| 764 |
GL_ONE, GL_ONE |
| 765 |
#endif |
| 766 |
}; |
| 767 |
const int blend_equations[3] = { |
| 768 |
GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT |
| 769 |
}; |
| 770 |
|
| 771 |
(void)ogl_disp; |
| 772 |
|
| 773 |
al_get_separate_bitmap_blender(&op, &src_color, &dst_color, |
| 774 |
&op_alpha, &src_alpha, &dst_alpha); |
| 775 |
const_color = al_get_bitmap_blend_color(); |
| 776 |
/* glBlendFuncSeparate was only included with OpenGL 1.4 */ |
| 777 |
#if !defined ALLEGRO_CFG_OPENGLES |
| 778 |
if (ogl_disp->ogl_extras->ogl_info.version >= _ALLEGRO_OPENGL_VERSION_1_4) { |
| 779 |
#else |
| 780 |
/* FIXME: At this time (09/2014) there are a lot of Android phones that |
| 781 |
* don't support glBlendFuncSeparate even though they claim OpenGL ES 2.0 |
| 782 |
* support. Rather than not work on 20-25% of phones, we just don't support |
| 783 |
* separate blending on Android for now. |
| 784 |
*/ |
| 785 |
#if defined ALLEGRO_ANDROID && !defined ALLEGRO_CFG_OPENGLES3 |
| 786 |
if (false) { |
| 787 |
#else |
| 788 |
if (ogl_disp->ogl_extras->ogl_info.version >= _ALLEGRO_OPENGL_VERSION_2_0) { |
| 789 |
#endif |
| 790 |
#endif |
| 791 |
glEnable(GL_BLEND); |
| 792 |
try_const_color(ogl_disp, &const_color); |
| 793 |
glBlendFuncSeparate(blend_modes[src_color], blend_modes[dst_color], |
| 794 |
blend_modes[src_alpha], blend_modes[dst_alpha]); |
| 795 |
if (ogl_disp->ogl_extras->ogl_info.version >= _ALLEGRO_OPENGL_VERSION_2_0) { |
| 796 |
glBlendEquationSeparate( |
| 797 |
blend_equations[op], |
| 798 |
blend_equations[op_alpha]); |
| 799 |
} |
| 800 |
else { |
| 801 |
glBlendEquation(blend_equations[op]); |
| 802 |
} |
| 803 |
} |
| 804 |
else { |
| 805 |
if (src_color == src_alpha && dst_color == dst_alpha) { |
| 806 |
glEnable(GL_BLEND); |
| 807 |
try_const_color(ogl_disp, &const_color); |
| 808 |
glBlendFunc(blend_modes[src_color], blend_modes[dst_color]); |
| 809 |
} |
| 810 |
else { |
| 811 |
ALLEGRO_ERROR("Blender unsupported with this OpenGL version (%d %d %d %d %d %d)\n", |
| 812 |
op, src_color, dst_color, op_alpha, src_alpha, dst_alpha); |
| 813 |
return false; |
| 814 |
} |
| 815 |
} |
| 816 |
return true; |
| 817 |
} |
| 818 |
|
| 819 |
/* These functions make drawing calls use shaders or the fixed pipeline |
| 820 |
* based on what the user has set up. FIXME: OpenGL only right now. |
| 821 |
*/ |
| 822 |
|
| 823 |
static void vert_ptr_on(ALLEGRO_DISPLAY *display, int n, GLint t, int stride, void *v) |
| 824 |
{ |
| 825 |
/* Only use this shader stuff with GLES2+ or equivalent */ |
| 826 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 827 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 828 |
if (display->ogl_extras->varlocs.pos_loc >= 0) { |
| 829 |
glVertexAttribPointer(display->ogl_extras->varlocs.pos_loc, n, t, false, stride, v); |
| 830 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.pos_loc); |
| 831 |
} |
| 832 |
#endif |
| 833 |
} |
| 834 |
else { |
| 835 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 836 |
glEnableClientState(GL_VERTEX_ARRAY); |
| 837 |
glVertexPointer(n, t, stride, v); |
| 838 |
#endif |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
static void vert_ptr_off(ALLEGRO_DISPLAY *display) |
| 843 |
{ |
| 844 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 845 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 846 |
if (display->ogl_extras->varlocs.pos_loc >= 0) { |
| 847 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.pos_loc); |
| 848 |
} |
| 849 |
#endif |
| 850 |
} |
| 851 |
else { |
| 852 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 853 |
glDisableClientState(GL_VERTEX_ARRAY); |
| 854 |
#endif |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
static void color_ptr_on(ALLEGRO_DISPLAY *display, int n, GLint t, int stride, void *v) |
| 859 |
{ |
| 860 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 861 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 862 |
if (display->ogl_extras->varlocs.color_loc >= 0) { |
| 863 |
glVertexAttribPointer(display->ogl_extras->varlocs.color_loc, n, t, false, stride, v); |
| 864 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.color_loc); |
| 865 |
} |
| 866 |
#endif |
| 867 |
} |
| 868 |
else { |
| 869 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 870 |
glEnableClientState(GL_COLOR_ARRAY); |
| 871 |
glColorPointer(n, t, stride, v); |
| 872 |
#endif |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
static void color_ptr_off(ALLEGRO_DISPLAY *display) |
| 877 |
{ |
| 878 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 879 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 880 |
if (display->ogl_extras->varlocs.color_loc >= 0) { |
| 881 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.color_loc); |
| 882 |
} |
| 883 |
#endif |
| 884 |
} |
| 885 |
else { |
| 886 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 887 |
glDisableClientState(GL_COLOR_ARRAY); |
| 888 |
#endif |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
static void tex_ptr_on(ALLEGRO_DISPLAY *display, int n, GLint t, int stride, void *v) |
| 893 |
{ |
| 894 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 895 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 896 |
if (display->ogl_extras->varlocs.texcoord_loc >= 0) { |
| 897 |
glVertexAttribPointer(display->ogl_extras->varlocs.texcoord_loc, n, t, false, stride, v); |
| 898 |
glEnableVertexAttribArray(display->ogl_extras->varlocs.texcoord_loc); |
| 899 |
} |
| 900 |
#endif |
| 901 |
} |
| 902 |
else { |
| 903 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 904 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 905 |
glTexCoordPointer(n, t, stride, v); |
| 906 |
#endif |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
static void tex_ptr_off(ALLEGRO_DISPLAY *display) |
| 911 |
{ |
| 912 |
if (display->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 913 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 914 |
if (display->ogl_extras->varlocs.texcoord_loc >= 0) { |
| 915 |
glDisableVertexAttribArray(display->ogl_extras->varlocs.texcoord_loc); |
| 916 |
} |
| 917 |
#endif |
| 918 |
} |
| 919 |
else { |
| 920 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 921 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 922 |
#endif |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
static void ogl_clear(ALLEGRO_DISPLAY *d, ALLEGRO_COLOR *color) |
| 927 |
{ |
| 928 |
ALLEGRO_DISPLAY *ogl_disp = (void *)d; |
| 929 |
ALLEGRO_BITMAP *target = al_get_target_bitmap(); |
| 930 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_target; |
| 931 |
float r, g, b, a; |
| 932 |
|
| 933 |
if (target->parent) |
| 934 |
target = target->parent; |
| 935 |
|
| 936 |
ogl_target = target->extra; |
| 937 |
|
| 938 |
if ((!ogl_target->is_backbuffer && |
| 939 |
ogl_disp->ogl_extras->opengl_target != target) |
| 940 |
|| target->locked) |
| 941 |
{ |
| 942 |
_al_clear_bitmap_by_locking(target, color); |
| 943 |
return; |
| 944 |
} |
| 945 |
|
| 946 |
al_unmap_rgba_f(*color, &r, &g, &b, &a); |
| 947 |
|
| 948 |
glClearColor(r, g, b, a); |
| 949 |
glClear(GL_COLOR_BUFFER_BIT); |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
static void ogl_draw_pixel(ALLEGRO_DISPLAY *d, float x, float y, |
| 954 |
ALLEGRO_COLOR *color) |
| 955 |
{ |
| 956 |
ALLEGRO_BITMAP *target = al_get_target_bitmap(); |
| 957 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_target; |
| 958 |
GLfloat vert[2]; |
| 959 |
GLfloat color_array[4]; |
| 960 |
|
| 961 |
/* For sub-bitmaps */ |
| 962 |
if (target->parent) { |
| 963 |
target = target->parent; |
| 964 |
} |
| 965 |
|
| 966 |
ogl_target = target->extra; |
| 967 |
|
| 968 |
if ((!ogl_target->is_backbuffer && |
| 969 |
d->ogl_extras->opengl_target != target) || |
| 970 |
target->locked || !_al_opengl_set_blender(d)) { |
| 971 |
_al_draw_pixel_memory(target, x, y, color); |
| 972 |
return; |
| 973 |
} |
| 974 |
|
| 975 |
vert[0] = x; |
| 976 |
vert[1] = y; |
| 977 |
|
| 978 |
color_array[0] = color->r; |
| 979 |
color_array[1] = color->g; |
| 980 |
color_array[2] = color->b; |
| 981 |
color_array[3] = color->a; |
| 982 |
|
| 983 |
vert_ptr_on(d, 2, GL_FLOAT, 2*sizeof(float), vert); |
| 984 |
color_ptr_on(d, 4, GL_FLOAT, 4*sizeof(float), color_array); |
| 985 |
|
| 986 |
// Should this be here if it's in the if above? |
| 987 |
if (!_al_opengl_set_blender(d)) { |
| 988 |
return; |
| 989 |
} |
| 990 |
|
| 991 |
glDrawArrays(GL_POINTS, 0, 1); |
| 992 |
|
| 993 |
vert_ptr_off(d); |
| 994 |
color_ptr_off(d); |
| 995 |
} |
| 996 |
|
| 997 |
static void* ogl_prepare_vertex_cache(ALLEGRO_DISPLAY* disp, |
| 998 |
int num_new_vertices) |
| 999 |
{ |
| 1000 |
disp->num_cache_vertices += num_new_vertices; |
| 1001 |
if (!disp->vertex_cache) { |
| 1002 |
disp->vertex_cache = al_malloc(num_new_vertices * sizeof(ALLEGRO_OGL_BITMAP_VERTEX)); |
| 1003 |
|
| 1004 |
disp->vertex_cache_size = num_new_vertices; |
| 1005 |
} else if (disp->num_cache_vertices > disp->vertex_cache_size) { |
| 1006 |
disp->vertex_cache = al_realloc(disp->vertex_cache, |
| 1007 |
2 * disp->num_cache_vertices * sizeof(ALLEGRO_OGL_BITMAP_VERTEX)); |
| 1008 |
|
| 1009 |
disp->vertex_cache_size = 2 * disp->num_cache_vertices; |
| 1010 |
} |
| 1011 |
return (ALLEGRO_OGL_BITMAP_VERTEX*)disp->vertex_cache + |
| 1012 |
(disp->num_cache_vertices - num_new_vertices); |
| 1013 |
} |
| 1014 |
|
| 1015 |
static void ogl_flush_vertex_cache(ALLEGRO_DISPLAY *disp) |
| 1016 |
{ |
| 1017 |
GLuint current_texture; |
| 1018 |
ALLEGRO_OGL_EXTRAS *o = disp->ogl_extras; |
| 1019 |
(void)o; /* not used in all ports */ |
| 1020 |
|
| 1021 |
if (!disp->vertex_cache) |
| 1022 |
return; |
| 1023 |
if (disp->num_cache_vertices == 0) |
| 1024 |
return; |
| 1025 |
|
| 1026 |
if (!_al_opengl_set_blender(disp)) { |
| 1027 |
disp->num_cache_vertices = 0; |
| 1028 |
return; |
| 1029 |
} |
| 1030 |
|
| 1031 |
if (disp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 1032 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 1033 |
if (disp->ogl_extras->varlocs.use_tex_loc >= 0) { |
| 1034 |
glUniform1i(disp->ogl_extras->varlocs.use_tex_loc, 1); |
| 1035 |
} |
| 1036 |
if (disp->ogl_extras->varlocs.use_tex_matrix_loc >= 0) { |
| 1037 |
glUniform1i(disp->ogl_extras->varlocs.use_tex_matrix_loc, 0); |
| 1038 |
} |
| 1039 |
#endif |
| 1040 |
} |
| 1041 |
else { |
| 1042 |
glEnable(GL_TEXTURE_2D); |
| 1043 |
} |
| 1044 |
|
| 1045 |
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)¤t_texture); |
| 1046 |
if (current_texture != disp->cache_texture) { |
| 1047 |
if (disp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 1048 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 1049 |
/* Use texture unit 0 */ |
| 1050 |
glActiveTexture(GL_TEXTURE0); |
| 1051 |
if (disp->ogl_extras->varlocs.tex_loc >= 0) |
| 1052 |
glUniform1i(disp->ogl_extras->varlocs.tex_loc, 0); |
| 1053 |
#endif |
| 1054 |
} |
| 1055 |
glBindTexture(GL_TEXTURE_2D, disp->cache_texture); |
| 1056 |
} |
| 1057 |
|
| 1058 |
#if !defined(ALLEGRO_CFG_OPENGLES) |
| 1059 |
#if defined(ALLEGRO_MACOSX) |
| 1060 |
if ((disp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) && (disp->flags & ALLEGRO_OPENGL_3_0)) { |
| 1061 |
#else |
| 1062 |
if (disp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 1063 |
#endif |
| 1064 |
int stride = sizeof(ALLEGRO_OGL_BITMAP_VERTEX); |
| 1065 |
int bytes = disp->num_cache_vertices * stride; |
| 1066 |
|
| 1067 |
/* We create the VAO and VBO on first use. */ |
| 1068 |
if (o->vao == 0) { |
| 1069 |
glGenVertexArrays(1, &o->vao); |
| 1070 |
ALLEGRO_DEBUG("new VAO: %u\n", o->vao); |
| 1071 |
} |
| 1072 |
glBindVertexArray(o->vao); |
| 1073 |
|
| 1074 |
if (o->vbo == 0) { |
| 1075 |
glGenBuffers(1, &o->vbo); |
| 1076 |
ALLEGRO_DEBUG("new VBO: %u\n", o->vbo); |
| 1077 |
} |
| 1078 |
glBindBuffer(GL_ARRAY_BUFFER, o->vbo); |
| 1079 |
|
| 1080 |
/* Then we upload data into it. */ |
| 1081 |
glBufferData(GL_ARRAY_BUFFER, bytes, disp->vertex_cache, GL_STREAM_DRAW); |
| 1082 |
|
| 1083 |
/* Finally set the "pos", "texccord" and "color" attributes used by our |
| 1084 |
* shader and enable them. |
| 1085 |
*/ |
| 1086 |
if (o->varlocs.pos_loc >= 0) { |
| 1087 |
glVertexAttribPointer(o->varlocs.pos_loc, 3, GL_FLOAT, false, stride, |
| 1088 |
(void *)offsetof(ALLEGRO_OGL_BITMAP_VERTEX, x)); |
| 1089 |
glEnableVertexAttribArray(o->varlocs.pos_loc); |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (o->varlocs.texcoord_loc >= 0) { |
| 1093 |
glVertexAttribPointer(o->varlocs.texcoord_loc, 2, GL_FLOAT, false, stride, |
| 1094 |
(void *)offsetof(ALLEGRO_OGL_BITMAP_VERTEX, tx)); |
| 1095 |
glEnableVertexAttribArray(o->varlocs.texcoord_loc); |
| 1096 |
} |
| 1097 |
|
| 1098 |
if (o->varlocs.color_loc >= 0) { |
| 1099 |
glVertexAttribPointer(o->varlocs.color_loc, 4, GL_FLOAT, false, stride, |
| 1100 |
(void *)offsetof(ALLEGRO_OGL_BITMAP_VERTEX, r)); |
| 1101 |
glEnableVertexAttribArray(o->varlocs.color_loc); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
else |
| 1105 |
#endif |
| 1106 |
{ |
| 1107 |
vert_ptr_on(disp, 3, GL_FLOAT, sizeof(ALLEGRO_OGL_BITMAP_VERTEX), |
| 1108 |
(char *)(disp->vertex_cache) + offsetof(ALLEGRO_OGL_BITMAP_VERTEX, x)); |
| 1109 |
tex_ptr_on(disp, 2, GL_FLOAT, sizeof(ALLEGRO_OGL_BITMAP_VERTEX), |
| 1110 |
(char*)(disp->vertex_cache) + offsetof(ALLEGRO_OGL_BITMAP_VERTEX, tx)); |
| 1111 |
color_ptr_on(disp, 4, GL_FLOAT, sizeof(ALLEGRO_OGL_BITMAP_VERTEX), |
| 1112 |
(char*)(disp->vertex_cache) + offsetof(ALLEGRO_OGL_BITMAP_VERTEX, r)); |
| 1113 |
|
| 1114 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 1115 |
if (!(disp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE)) |
| 1116 |
glDisableClientState(GL_NORMAL_ARRAY); |
| 1117 |
#endif |
| 1118 |
} |
| 1119 |
|
| 1120 |
glGetError(); /* clear error */ |
| 1121 |
glDrawArrays(GL_TRIANGLES, 0, disp->num_cache_vertices); |
| 1122 |
|
| 1123 |
#ifdef DEBUGMODE |
| 1124 |
{ |
| 1125 |
int e = glGetError(); |
| 1126 |
if (e) { |
| 1127 |
ALLEGRO_WARN("glDrawArrays failed: %s\n", _al_gl_error_string(e)); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
#endif |
| 1131 |
|
| 1132 |
#if !defined ALLEGRO_CFG_OPENGLES && !defined ALLEGRO_MACOSX |
| 1133 |
if (disp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 1134 |
if (o->varlocs.pos_loc >= 0) |
| 1135 |
glDisableVertexAttribArray(o->varlocs.pos_loc); |
| 1136 |
if (o->varlocs.texcoord_loc >= 0) |
| 1137 |
glDisableVertexAttribArray(o->varlocs.texcoord_loc); |
| 1138 |
if (o->varlocs.color_loc >= 0) |
| 1139 |
glDisableVertexAttribArray(o->varlocs.color_loc); |
| 1140 |
glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 1141 |
glBindVertexArray(0); |
| 1142 |
} |
| 1143 |
else |
| 1144 |
#endif |
| 1145 |
{ |
| 1146 |
vert_ptr_off(disp); |
| 1147 |
tex_ptr_off(disp); |
| 1148 |
color_ptr_off(disp); |
| 1149 |
} |
| 1150 |
|
| 1151 |
disp->num_cache_vertices = 0; |
| 1152 |
|
| 1153 |
if (disp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 1154 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 1155 |
if (disp->ogl_extras->varlocs.use_tex_loc >= 0) |
| 1156 |
glUniform1i(disp->ogl_extras->varlocs.use_tex_loc, 0); |
| 1157 |
#endif |
| 1158 |
} |
| 1159 |
else { |
| 1160 |
glDisable(GL_TEXTURE_2D); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
static void ogl_update_transformation(ALLEGRO_DISPLAY* disp, |
| 1165 |
ALLEGRO_BITMAP *target) |
| 1166 |
{ |
| 1167 |
if (disp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) { |
| 1168 |
#ifdef ALLEGRO_CFG_SHADER_GLSL |
| 1169 |
GLint loc = disp->ogl_extras->varlocs.projview_matrix_loc; |
| 1170 |
ALLEGRO_TRANSFORM projview; |
| 1171 |
al_copy_transform(&projview, &target->transform); |
| 1172 |
al_compose_transform(&projview, &target->proj_transform); |
| 1173 |
al_copy_transform(&disp->projview_transform, &projview); |
| 1174 |
|
| 1175 |
if (disp->ogl_extras->program_object > 0 && loc >= 0) { |
| 1176 |
_al_glsl_set_projview_matrix(loc, &disp->projview_transform); |
| 1177 |
} |
| 1178 |
#endif |
| 1179 |
} else { |
| 1180 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 1181 |
glMatrixMode(GL_PROJECTION); |
| 1182 |
glLoadMatrixf((float *)target->proj_transform.m); |
| 1183 |
glMatrixMode(GL_MODELVIEW); |
| 1184 |
glLoadMatrixf((float *)target->transform.m); |
| 1185 |
#endif |
| 1186 |
} |
| 1187 |
|
| 1188 |
if (target->parent) { |
| 1189 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_extra = target->parent->extra; |
| 1190 |
/* glViewport requires the bottom-left coordinate of the corner. */ |
| 1191 |
glViewport(target->xofs, ogl_extra->true_h - (target->yofs + target->h), target->w, target->h); |
| 1192 |
} else { |
| 1193 |
glViewport(0, 0, target->w, target->h); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
static void ogl_clear_depth_buffer(ALLEGRO_DISPLAY *display, float x) |
| 1198 |
{ |
| 1199 |
(void)display; |
| 1200 |
|
| 1201 |
#if defined(ALLEGRO_CFG_OPENGLES) |
| 1202 |
glClearDepthf(x); |
| 1203 |
#else |
| 1204 |
glClearDepth(x); |
| 1205 |
#endif |
| 1206 |
|
| 1207 |
/* We may want to defer this to the next glClear call as a combined |
| 1208 |
* color/depth clear may be faster. |
| 1209 |
*/ |
| 1210 |
glClear(GL_DEPTH_BUFFER_BIT); |
| 1211 |
} |
| 1212 |
|
| 1213 |
/* Add drawing commands to the vtable. */ |
| 1214 |
void _al_ogl_add_drawing_functions(ALLEGRO_DISPLAY_INTERFACE *vt) |
| 1215 |
{ |
| 1216 |
vt->clear = ogl_clear; |
| 1217 |
vt->draw_pixel = ogl_draw_pixel; |
| 1218 |
vt->clear_depth_buffer = ogl_clear_depth_buffer; |
| 1219 |
|
| 1220 |
vt->flush_vertex_cache = ogl_flush_vertex_cache; |
| 1221 |
vt->prepare_vertex_cache = ogl_prepare_vertex_cache; |
| 1222 |
vt->update_transformation = ogl_update_transformation; |
| 1223 |
|
| 1224 |
vt->draw_prim = ogl_draw_prim; |
| 1225 |
vt->draw_prim_indexed = ogl_draw_prim_indexed; |
| 1226 |
|
| 1227 |
vt->create_vertex_buffer = ogl_create_vertex_buffer; |
| 1228 |
vt->destroy_vertex_buffer = ogl_destroy_vertex_buffer; |
| 1229 |
vt->lock_vertex_buffer = ogl_lock_vertex_buffer; |
| 1230 |
vt->unlock_vertex_buffer = ogl_unlock_vertex_buffer; |
| 1231 |
|
| 1232 |
vt->create_index_buffer = ogl_create_index_buffer; |
| 1233 |
vt->destroy_index_buffer = ogl_destroy_index_buffer; |
| 1234 |
vt->lock_index_buffer = ogl_lock_index_buffer; |
| 1235 |
vt->unlock_index_buffer = ogl_unlock_index_buffer; |
| 1236 |
|
| 1237 |
vt->draw_vertex_buffer = ogl_draw_vertex_buffer; |
| 1238 |
vt->draw_indexed_buffer = ogl_draw_indexed_buffer; |
| 1239 |
} |
| 1240 |
|
| 1241 |
/* vim: set sts=3 sw=3 et: */ |