1 #ifndef __al_included_allegro5_aintern_opengl_h
2 #define __al_included_allegro5_aintern_opengl_h
3 
4 #include "allegro5/opengl/gl_ext.h"
5 #include "allegro5/internal/aintern_bitmap.h"
6 #include "allegro5/internal/aintern_display.h"
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 
13 enum {
14    _ALLEGRO_OPENGL_VERSION_0     = 0, /* dummy */
15    _ALLEGRO_OPENGL_VERSION_1_0   = 0x01000000,
16    _ALLEGRO_OPENGL_VERSION_1_1   = 0x01010000,
17    _ALLEGRO_OPENGL_VERSION_1_2   = 0x01020000,
18    _ALLEGRO_OPENGL_VERSION_1_2_1 = 0x01020100,
19    _ALLEGRO_OPENGL_VERSION_1_3   = 0x01030000,
20    _ALLEGRO_OPENGL_VERSION_1_4   = 0x01040000,
21    _ALLEGRO_OPENGL_VERSION_1_5   = 0x01050000,
22    _ALLEGRO_OPENGL_VERSION_2_0   = 0x02000000,
23    _ALLEGRO_OPENGL_VERSION_2_1   = 0x02010000,
24    _ALLEGRO_OPENGL_VERSION_3_0   = 0x03000000,
25    _ALLEGRO_OPENGL_VERSION_3_1   = 0x03010000,
26    _ALLEGRO_OPENGL_VERSION_3_2   = 0x03020000,
27    _ALLEGRO_OPENGL_VERSION_3_3   = 0x03030000,
28    _ALLEGRO_OPENGL_VERSION_4_0   = 0x04000000
29 };
30 
31 #define ALLEGRO_MAX_OPENGL_FBOS 8
32 
33 enum {
34    FBO_INFO_UNUSED      = 0,
35    FBO_INFO_TRANSIENT   = 1,  /* may be destroyed for another bitmap */
36    FBO_INFO_PERSISTENT  = 2   /* exclusive to the owner bitmap */
37 };
38 
39 typedef struct ALLEGRO_FBO_BUFFERS
40 {
41    /* It is not easy to determine the best lifetime for these. Unlike
42     * FBOs they are heavy objects and re-creating them can be costly.
43     * However if we make them part of ALLEGRO_BITMAP_EXTRA_OPENGL
44     * below, there is no way to release them. I.e. if you create
45     * many bitmaps in the beginning of your game and need depth and/or
46     * multisampling for them, the only way to free the buffers would be
47     * to copy those bitmaps and then destroy them.
48     *
49     * By tying them to the FBO struct, there is a limit of how many
50     * buffers Allegro will create before recycling them. This will
51     * work very well in the case where you only have one or a few
52     * bitmaps you regularly draw into.
53     */
54    GLuint depth_buffer;
55    int dw, dh, depth;
56 
57    GLuint multisample_buffer;
58    int mw, mh, samples;
59 } ALLEGRO_FBO_BUFFERS;
60 
61 typedef struct ALLEGRO_FBO_INFO
62 {
63    int fbo_state;
64    GLuint fbo;
65 
66    ALLEGRO_FBO_BUFFERS buffers;
67 
68    ALLEGRO_BITMAP *owner;
69    double last_use_time;
70 } ALLEGRO_FBO_INFO;
71 
72 typedef struct ALLEGRO_BITMAP_EXTRA_OPENGL
73 {
74    /* Driver specifics. */
75 
76    int true_w;
77    int true_h;
78 
79    GLuint texture; /* 0 means, not uploaded yet. */
80 
81    ALLEGRO_FBO_INFO *fbo_info;
82 
83    /* When an OpenGL bitmap is locked, the locked region is usually backed by a
84     * temporary memory buffer pointed to by lock_buffer.
85     *
86     * On GLES, a locked backbuffer may be backed by a texture bitmap pointed to
87     * by lock_proxy instead, and lock_buffer is NULL.  Upon unlocking the proxy
88     * bitmap is drawn onto the backbuffer.
89     */
90    unsigned char *lock_buffer;
91    ALLEGRO_BITMAP *lock_proxy;
92 
93    float left, top, right, bottom; /* Texture coordinates. */
94    bool is_backbuffer; /* This is not a real bitmap, but the backbuffer. */
95 } ALLEGRO_BITMAP_EXTRA_OPENGL;
96 
97 typedef struct OPENGL_INFO {
98    uint32_t version;       /* OpenGL version */
99    int max_texture_size;   /* Maximum texture size */
100    int is_voodoo3_and_under; /* Special cases for Voodoo 1-3 */
101    int is_voodoo;          /* Special cases for Voodoo cards */
102    int is_matrox_g200;     /* Special cases for Matrox G200 boards */
103    int is_ati_rage_pro;    /* Special cases for ATI Rage Pro boards */
104    int is_ati_radeon_7000; /* Special cases for ATI Radeon 7000 */
105    int is_ati_r200_chip;	/* Special cases for ATI card with chip R200 */
106    int is_mesa_driver;     /* Special cases for MESA */
107    int is_intel_hd_graphics_3000;
108 } OPENGL_INFO;
109 
110 
111 typedef struct ALLEGRO_OGL_VARLOCS
112 {
113    /* Cached shader variable locations. */
114    GLint pos_loc;
115    GLint color_loc;
116    GLint projview_matrix_loc;
117    GLint texcoord_loc;
118    GLint use_tex_loc;
119    GLint tex_loc;
120    GLint use_tex_matrix_loc;
121    GLint tex_matrix_loc;
122    GLint alpha_test_loc;
123    GLint alpha_func_loc;
124    GLint alpha_test_val_loc;
125    GLint user_attr_loc[_ALLEGRO_PRIM_MAX_USER_ATTR];
126 } ALLEGRO_OGL_VARLOCS;
127 
128 typedef struct ALLEGRO_OGL_EXTRAS
129 {
130    /* A list of extensions supported by Allegro, for this context. */
131    ALLEGRO_OGL_EXT_LIST *extension_list;
132    /* A list of extension API, loaded by Allegro, for this context. */
133    ALLEGRO_OGL_EXT_API *extension_api;
134    /* Various info about OpenGL implementation. */
135    OPENGL_INFO ogl_info;
136 
137    ALLEGRO_BITMAP *opengl_target;
138 
139    ALLEGRO_BITMAP *backbuffer;
140 
141    /* True if display resources are shared among displays. */
142    bool is_shared;
143 
144    ALLEGRO_FBO_INFO fbos[ALLEGRO_MAX_OPENGL_FBOS];
145 
146    /* In non-programmable pipe mode this should be zero.
147     * In programmable pipeline mode this should be non-zero.
148     */
149    GLuint program_object;
150    ALLEGRO_OGL_VARLOCS varlocs;
151 
152    /* For OpenGL 3.0+ we use a single vao and vbo. */
153    GLuint vao, vbo;
154 
155 } ALLEGRO_OGL_EXTRAS;
156 
157 typedef struct ALLEGRO_OGL_BITMAP_VERTEX
158 {
159    float x, y, z;
160    float tx, ty;
161    float r, g, b, a;
162 } ALLEGRO_OGL_BITMAP_VERTEX;
163 
164 
165 /* extensions */
166 int  _al_ogl_look_for_an_extension(const char *name, const GLubyte *extensions);
167 void _al_ogl_set_extensions(ALLEGRO_OGL_EXT_API *ext);
168 void _al_ogl_manage_extensions(ALLEGRO_DISPLAY *disp);
169 void _al_ogl_unmanage_extensions(ALLEGRO_DISPLAY *disp);
170 
171 /* bitmap */
172 int _al_ogl_get_glformat(int format, int component);
173 ALLEGRO_BITMAP *_al_ogl_create_bitmap(ALLEGRO_DISPLAY *d, int w, int h,
174     int format, int flags);
175 void _al_ogl_upload_bitmap_memory(ALLEGRO_BITMAP *bitmap, int format, void *ptr);
176 
177 /* locking */
178 #ifndef ALLEGRO_CFG_OPENGLES
179    ALLEGRO_LOCKED_REGION *_al_ogl_lock_region_new(ALLEGRO_BITMAP *bitmap,
180       int x, int y, int w, int h, int format, int flags);
181    void _al_ogl_unlock_region_new(ALLEGRO_BITMAP *bitmap);
182 #else
183    ALLEGRO_LOCKED_REGION *_al_ogl_lock_region_gles(ALLEGRO_BITMAP *bitmap,
184       int x, int y, int w, int h, int format, int flags);
185    void _al_ogl_unlock_region_gles(ALLEGRO_BITMAP *bitmap);
186 #endif
187 
188 int _al_ogl_pixel_alignment(int pixel_size, bool compressed);
189 
190 /* framebuffer objects */
191 GLint _al_ogl_bind_framebuffer(GLint fbo);
192 void _al_ogl_reset_fbo_info(ALLEGRO_FBO_INFO *info);
193 bool _al_ogl_create_persistent_fbo(ALLEGRO_BITMAP *bitmap);
194 ALLEGRO_FBO_INFO *_al_ogl_persist_fbo(ALLEGRO_DISPLAY *display,
195                                       ALLEGRO_FBO_INFO *transient_fbo_info);
196 void _al_ogl_setup_fbo(ALLEGRO_DISPLAY *display, ALLEGRO_BITMAP *bitmap);
197 bool _al_ogl_setup_fbo_non_backbuffer(ALLEGRO_DISPLAY *display,
198                                       ALLEGRO_BITMAP *bitmap);
199 void _al_ogl_del_fbo(ALLEGRO_FBO_INFO *info);
200 
201 /* common driver */
202 void _al_ogl_setup_gl(ALLEGRO_DISPLAY *d);
203 void _al_ogl_set_target_bitmap(ALLEGRO_DISPLAY *display, ALLEGRO_BITMAP *bitmap);
204 void _al_ogl_unset_target_bitmap(ALLEGRO_DISPLAY *display, ALLEGRO_BITMAP *bitmap);
205 void _al_ogl_finalize_fbo(ALLEGRO_DISPLAY *display, ALLEGRO_BITMAP *bitmap);
206 void _al_ogl_setup_bitmap_clipping(const ALLEGRO_BITMAP *bitmap);
207 ALLEGRO_BITMAP *_al_ogl_get_backbuffer(ALLEGRO_DISPLAY *d);
208 ALLEGRO_BITMAP* _al_ogl_create_backbuffer(ALLEGRO_DISPLAY *disp);
209 void _al_ogl_destroy_backbuffer(ALLEGRO_BITMAP *b);
210 bool _al_ogl_resize_backbuffer(ALLEGRO_BITMAP *b, int w, int h);
211 void _al_opengl_backup_dirty_bitmaps(ALLEGRO_DISPLAY *d, bool flip);
212 
213 /* draw */
214 struct ALLEGRO_DISPLAY_INTERFACE;
215 void _al_ogl_add_drawing_functions(struct ALLEGRO_DISPLAY_INTERFACE *vt);
216 
217 AL_FUNC(bool, _al_opengl_set_blender, (ALLEGRO_DISPLAY *disp));
218 AL_FUNC(char const *, _al_gl_error_string, (GLenum e));
219 
220 void _al_ogl_update_render_state(ALLEGRO_DISPLAY *display);
221 
222 /* shader */
223 #ifdef ALLEGRO_CFG_SHADER_GLSL
224    bool _al_glsl_set_projview_matrix(GLint projview_matrix_loc,
225       const ALLEGRO_TRANSFORM *t);
226    void _al_glsl_init_shaders(void);
227    void _al_glsl_shutdown_shaders(void);
228    void _al_glsl_unuse_shaders(void);
229 #endif
230 
231 
232 #ifdef __cplusplus
233 }
234 #endif
235 
236 #endif
237