ScummVM API documentation
zgl.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * This file is based on, or a modified version of code from TinyGL (C) 1997-2022 Fabrice Bellard,
24  * which is licensed under the MIT license (see LICENSE).
25  * It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
26  */
27 
28 #ifndef TGL_ZGL_H
29 #define TGL_ZGL_H
30 
31 #include "common/util.h"
32 #include "common/textconsole.h"
33 #include "common/array.h"
34 #include "common/list.h"
35 #include "common/scummsys.h"
36 
37 #include "graphics/pixelformat.h"
38 #include "graphics/surface.h"
39 #include "graphics/tinygl/gl.h"
40 #include "graphics/tinygl/zbuffer.h"
41 #include "graphics/tinygl/zmath.h"
42 #include "graphics/tinygl/zblit.h"
43 #include "graphics/tinygl/zdirtyrect.h"
44 #include "graphics/tinygl/texelbuffer.h"
45 
46 namespace TinyGL {
47 
48 enum {
49 
50 #define ADD_OP(a,b,c) OP_ ## a ,
51 
52 #include "graphics/tinygl/opinfo.h"
53 
54  DUMMY
55 };
56 
57 // initially # of allocated GLVertexes (will grow when necessary)
58 #define POLYGON_MAX_VERTEX 16
59 
60 // Max # of specular light pow buffers
61 #define MAX_SPECULAR_BUFFERS 8
62 // # of entries in specular buffer
63 #define SPECULAR_BUFFER_SIZE 1024
64 // specular buffer granularity
65 #define SPECULAR_BUFFER_RESOLUTION 1024
66 
67 #define MAX_MODELVIEW_STACK_DEPTH 35
68 #define MAX_PROJECTION_STACK_DEPTH 8
69 #define MAX_TEXTURE_STACK_DEPTH 8
70 #define MAX_NAME_STACK_DEPTH 64
71 #define MAX_TEXTURE_LEVELS 11
72 #define T_MAX_LIGHTS 32
73 
74 #define VERTEX_HASH_SIZE 1031
75 
76 #define MAX_DISPLAY_LISTS 1024
77 #define OP_BUFFER_MAX_SIZE 512
78 
79 #define TGL_OFFSET_FILL 0x1
80 #define TGL_OFFSET_LINE 0x2
81 #define TGL_OFFSET_POINT 0x4
82 
83 enum eDataType {
84  kIntType,
85  kInt4Type,
86  kUintType,
87  kFloatType,
88  kFloat2Type,
89  kFloat4Type,
90  kFloat16Type
91 };
92 
93 union uglValue {
94  TGLint _int;
95  TGLint _int4[4];
96  TGLfloat _float;
97  TGLfloat _float2[2];
98  TGLfloat _float4[4];
99  TGLfloat _float16[16];
100 };
101 
102 struct GLSpecBuf {
103  int shininess_i;
104  int last_used;
105  float buf[SPECULAR_BUFFER_SIZE + 1];
106  struct GLSpecBuf *next;
107 };
108 
109 struct GLLight {
110  Vector4 ambient;
111  Vector4 diffuse;
112  Vector4 specular;
113  bool has_specular;
114  Vector4 position;
115  Vector3 spot_direction;
116  float spot_exponent;
117  float spot_cutoff;
118  float attenuation[3];
119  // precomputed values
120  float cos_spot_cutoff;
121  Vector3 norm_spot_direction;
122  Vector3 norm_position;
123  // we use a linked list to know which are the enabled lights
124  int enabled;
125  struct GLLight *next, *prev;
126 };
127 
128 struct GLMaterial {
129  Vector4 emission;
130  Vector4 ambient;
131  Vector4 diffuse;
132  Vector4 specular;
133  bool has_specular;
134  float shininess;
135  // computed values
136  int shininess_i;
137  int do_specular;
138 };
139 
140 struct GLViewport {
141  int xmin, ymin, xsize, ysize;
142  Vector3 scale;
143  Vector3 trans;
144  int updated;
145 };
146 
147 union GLParam {
148  int op;
149  float f;
150  int i;
151  uint ui;
152  void *p;
153 };
154 
156  GLParam ops[OP_BUFFER_MAX_SIZE];
157  struct GLParamBuffer *next;
158 };
159 
160 struct GLList {
161  GLParamBuffer *first_op_buffer;
162  // TODO: extensions for a hash table or a better allocating scheme
163 };
164 
165 struct GLVertex {
166  int edge_flag;
167  Vector3 normal;
168  Vector4 coord;
169  Vector4 tex_coord;
170  Vector4 color;
171  float fog_factor;
172 
173  // computed values
174  Vector4 ec; // eye coordinates
175  Vector4 pc; // coordinates in the normalized volume
176  int clip_code; // clip code
177  ZBufferPoint zp; // integer coordinates for the rasterization
178 
179  bool operator==(const GLVertex &other) const {
180  return
181  edge_flag == other.edge_flag &&
182  normal == other.normal &&
183  coord == other.coord &&
184  tex_coord == other.tex_coord &&
185  color == other.color &&
186  ec == other.ec &&
187  pc == other.pc &&
188  clip_code == other.clip_code &&
189  zp == other.zp;
190  }
191 
192  bool operator!=(const GLVertex &other) const {
193  return !(*this == other);
194  }
195 };
196 
197 struct GLImage {
198  TexelBuffer *pixmap;
199  int xsize, ysize;
200 };
201 
202 // textures
203 
204 #define TEXTURE_HASH_TABLE_SIZE 256
205 
206 struct GLTexture {
207  GLImage images[MAX_TEXTURE_LEVELS];
208  uint handle;
209  int versionNumber;
210  struct GLTexture *next, *prev;
211  bool disposed;
212 };
213 
216  TGLuint format;
217  TGLuint type;
218 };
219 
220 // shared state
221 
223  GLList **lists;
224  GLTexture **texture_hash_table;
225 };
226 
236 public:
237  LinearAllocator() {
238  _memoryBuffer = nullptr;
239  _memorySize = 0;
240  _memoryPosition = 0;
241  }
242 
243  void initialize(size_t newSize) {
244  assert(_memoryBuffer == nullptr);
245  void *newBuffer = gl_malloc(newSize);
246  if (newBuffer == nullptr) {
247  error("Couldn't allocate memory for linear allocator.");
248  }
249  _memoryBuffer = newBuffer;
250  _memorySize = newSize;
251  }
252 
253  ~LinearAllocator() {
254  if (_memoryBuffer != nullptr) {
255  gl_free(_memoryBuffer);
256  }
257  }
258 
259  void *allocate(size_t size) {
260  if (_memoryPosition + size >= _memorySize) {
261  error("Allocator out of memory: couldn't allocate more memory from linear allocator.");
262  }
263  size_t returnPos = _memoryPosition;
264  _memoryPosition += size;
265  return ((byte *)_memoryBuffer) + returnPos;
266  }
267 
268  void reset() {
269  _memoryPosition = 0;
270  }
271 private:
272  void *_memoryBuffer;
273  size_t _memorySize;
274  size_t _memoryPosition;
275 };
276 
277 struct GLContext;
278 
279 typedef void (*gl_draw_triangle_func)(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
280 
281 // display context
282 
283 struct GLContext {
284  // Z buffer
285  FrameBuffer *fb;
286  Common::Rect renderRect;
287 
288  // blending
289  bool blending_enabled;
290  int source_blending_factor;
291  int destination_blending_factor;
292 
293  // alpha blending
294  bool alpha_test_enabled;
295  int alpha_test_func;
296  int alpha_test_ref_val;
297 
298  // Internal texture size
299  int _textureSize;
300 
301  // lights
302  GLLight lights[T_MAX_LIGHTS];
303  GLLight *first_light;
304  Vector4 ambient_light_model;
305  int local_light_model;
306  bool lighting_enabled;
307  int light_model_two_side;
308 
309  // materials
310  GLMaterial materials[2];
311  bool color_material_enabled;
312  int current_color_material_mode;
313  int current_color_material_type;
314 
315  // textures
316  GLTexture *current_texture, *default_texture;
317  uint maxTextureName;
318  bool texture_2d_enabled;
319  int texture_mag_filter;
320  int texture_min_filter;
321  uint texture_wrap_s;
322  uint texture_wrap_t;
323  Common::Array<struct tglColorAssociation> colorAssociationList;
324 
325  // shared state
326  GLSharedState shared_state;
327 
328  // current list
329  GLParamBuffer *current_op_buffer;
330  int current_op_buffer_index;
331  int exec_flag, compile_flag, print_flag;
332 
333  // matrix
334  int matrix_mode;
335  Matrix4 *matrix_stack[3];
336  Matrix4 *matrix_stack_ptr[3];
337  int matrix_stack_depth_max[3];
338 
339  Matrix4 matrix_model_view_inv;
340  Matrix4 matrix_model_projection;
341  int matrix_model_projection_updated;
342  int matrix_model_projection_no_w_transform;
343  int apply_texture_matrix;
344 
345  // viewport
346  GLViewport viewport;
347 
348  // current state
349  int polygon_mode_back;
350  int polygon_mode_front;
351 
352  int current_front_face;
353  int current_shade_model;
354  int current_cull_face;
355  bool cull_face_enabled;
356  bool normalize_enabled;
357  gl_draw_triangle_func draw_triangle_front, draw_triangle_back;
358 
359  // selection
360  int render_mode;
361  uint *select_buffer;
362  int select_size;
363  uint *select_ptr, *select_hit;
364  int select_overflow;
365  int select_hits;
366 
367  // names
368  uint name_stack[MAX_NAME_STACK_DEPTH];
369  int name_stack_size;
370 
371  // clear
372  float clear_depth;
373  Vector4 clear_color;
374  int clear_stencil;
375 
376  // current vertex state
377  Vector4 current_color;
378  Vector4 current_normal;
379  Vector4 current_tex_coord;
380  int current_edge_flag;
381 
382  // glBegin / glEnd
383  int in_begin;
384  int begin_type;
385  int vertex_n, vertex_cnt;
386  int vertex_max;
387  GLVertex *vertex;
388 
389  // opengl 1.1 arrays
390  TGLvoid *vertex_array;
391  int vertex_array_size;
392  int vertex_array_stride;
393  int vertex_array_type;
394  TGLvoid *normal_array;
395  int normal_array_stride;
396  int normal_array_type;
397  TGLvoid *color_array;
398  int color_array_size;
399  int color_array_stride;
400  int color_array_type;
401  TGLvoid *texcoord_array;
402  int texcoord_array_size;
403  int texcoord_array_stride;
404  int texcoord_array_type;
405  int client_states;
406 
407  // opengl 1.1 polygon offset
408  float offset_factor;
409  float offset_units;
410  int offset_states;
411 
412  // specular buffer. could probably be shared between contexts,
413  // but that wouldn't be 100% thread safe
414  GLSpecBuf *specbuf_first;
415  int specbuf_used_counter;
416  int specbuf_num_buffers;
417 
418  // opaque structure for user's use
419  void *opaque;
420  // resize viewport function
421  int (*gl_resize_viewport)(int *xsize, int *ysize);
422 
423  // depth test
424  bool depth_test_enabled;
425  int depth_func;
426  bool depth_write_mask;
427 
428  // stencil
429  bool stencil_buffer_supported;
430  bool stencil_test_enabled;
431  int stencil_test_func;
432  int stencil_ref_val;
433  uint stencil_mask;
434  uint stencil_write_mask;
435  int stencil_sfail;
436  int stencil_dpfail;
437  int stencil_dppass;
438 
439  bool color_mask_red;
440  bool color_mask_green;
441  bool color_mask_blue;
442  bool color_mask_alpha;
443 
444  bool fog_enabled;
445  int fog_mode;
446  Vector4 fog_color;
447  float fog_density;
448  float fog_start;
449  float fog_end;
450 
451  Common::Rect _scissorRect;
452 
453  bool _enableDirtyRectangles;
454 
455  // stipple
456  bool polygon_stipple_enabled;
457  byte polygon_stipple_pattern[128];
458 
459  // blit test
460  Common::List<BlitImage *> _blitImages;
461 
462  // Draw call queue
463  Common::List<DrawCall *> _drawCallsQueue;
464  Common::List<DrawCall *> _previousFrameDrawCallsQueue;
465  int _currentAllocatorIndex;
466  LinearAllocator _drawCallAllocator[2];
467  bool _debugRectsEnabled;
468  bool _profilingEnabled;
469 
470  void gl_vertex_transform(GLVertex *v);
471  void gl_calc_fog_factor(GLVertex *v);
472 
473  void gl_get_pname(TGLenum pname, union uglValue *data, eDataType &dataType);
474 
475 public:
476  // The glop* functions exposed to public, however they are only for internal use.
477  // Calling them from outside of TinyGL is forbidden
478  #define ADD_OP(a, b, d) void glop ## a (GLParam *p);
479  #include "graphics/tinygl/opinfo.h"
480 
481  void gl_add_op(GLParam *p);
482  void gl_compile_op(GLParam *p);
483 
484  void gl_eval_viewport();
485  void gl_transform_to_viewport(GLVertex *v);
486  void gl_draw_triangle(GLVertex *p0, GLVertex *p1, GLVertex *p2);
487  void gl_draw_line(GLVertex *p0, GLVertex *p1);
488  void gl_draw_point(GLVertex *p0);
489 
490  static void gl_draw_triangle_point(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
491  static void gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
492  static void gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
493  static void gl_draw_triangle_select(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
494  void gl_draw_triangle_clip(GLVertex *p0, GLVertex *p1, GLVertex *p2, int clip_bit);
495 
496  void gl_add_select(uint zmin, uint zmax);
497  void gl_add_select1(int z1, int z2, int z3);
498  void gl_enable_disable_light(int light, int v);
499  void gl_shade_vertex(GLVertex *v);
500 
501  void gl_GetIntegerv(TGLenum pname, TGLint *data);
502  void gl_GetFloatv(TGLenum pname, TGLfloat *data);
503  void gl_GetDoublev(TGLenum pname, TGLdouble *data);
504  void gl_GetBooleanv(TGLenum pname, TGLboolean *data);
505 
506  void gl_EnableClientState(GLParam *p);
507  void gl_DisableClientState(GLParam *p);
508  void gl_VertexPointer(GLParam *p);
509  void gl_ColorPointer(GLParam *p);
510  void gl_NormalPointer(GLParam *p);
511  void gl_TexCoordPointer(GLParam *p);
512 
513  GLTexture *alloc_texture(uint h);
514  GLTexture *find_texture(uint h);
515  void free_texture(GLTexture *t);
516  void gl_GenTextures(TGLsizei n, TGLuint *textures);
517  void gl_DeleteTextures(TGLsizei n, const TGLuint *textures);
518  void gl_PixelStore(TGLenum pname, TGLint param);
519 
520  void issueDrawCall(DrawCall *drawCall);
521  void disposeResources();
522  void disposeDrawCallLists();
523 
524  void presentBufferDirtyRects(Common::List<Common::Rect> &dirtyAreas);
525  void presentBufferSimple(Common::List<Common::Rect> &dirtyAreas);
526 
527  void debugDrawRectangle(Common::Rect rect, int r, int g, int b);
528 
529  GLSpecBuf *specbuf_get_buffer(const int shininess_i, const float shininess);
530  void specbuf_cleanup();
531 
532  TGLint gl_RenderMode(TGLenum mode);
533  void gl_SelectBuffer(TGLsizei size, TGLuint *buffer);
534 
535  GLList *alloc_list(int list);
536  GLList *find_list(uint list);
537  void delete_list(int list);
538  void gl_NewList(TGLuint list, TGLenum mode);
539  void gl_EndList();
540  TGLboolean gl_IsList(TGLuint list);
541  TGLuint gl_GenLists(TGLsizei range);
542 
543  void initSharedState();
544  void endSharedState();
545 
546  void init(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize,
547  bool enableStencilBuffer, bool dirtyRectsEnable, uint32 drawCallMemorySize);
548  void deinit();
549 
550  void gl_print_matrix(const float *m);
551  void gl_debug(int mode) {
552  print_flag = mode;
553  }
554 };
555 
556 extern GLContext *gl_ctx;
557 GLContext *gl_get_context();
558 
559 #define VERTEX_ARRAY 0x0001
560 #define COLOR_ARRAY 0x0002
561 #define NORMAL_ARRAY 0x0004
562 #define TEXCOORD_ARRAY 0x0008
563 
564 // this clip epsilon is needed to avoid some rounding errors after
565 // several clipping stages
566 
567 #define CLIP_EPSILON (1E-5)
568 
569 static inline int gl_clipcode(float x, float y, float z, float w1) {
570  float w;
571 
572  w = (float)(w1 * (1.0 + CLIP_EPSILON));
573  return (x < -w) | ((x > w) << 1) | ((y < -w) << 2) | ((y > w) << 3) | ((z < -w) << 4) | ((z > w) << 5);
574 }
575 
576 static inline float clampf(float a, float min, float max) {
577  if (a < min)
578  return min;
579  if (a > max)
580  return max;
581  else
582  return a;
583 }
584 
585 } // end of namespace TinyGL
586 
587 #endif
Definition: zdirtyrect.h:41
Definition: zbuffer.h:85
Definition: array.h:52
Definition: zgl.h:102
Definition: pixelformat.h:138
Definition: zgl.h:140
Definition: zgl.h:155
Definition: list.h:44
Definition: zgl.h:93
Definition: rect.h:144
Definition: zmath.h:100
Definition: zgl.h:147
Definition: zgl.h:128
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: colormasks.h:27
Definition: zmath.h:163
Definition: zmath.h:39
Definition: zgl.h:206
Definition: zgl.h:109
Definition: zgl.h:160
Definition: zgl.h:197
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: zgl.h:214
Definition: texelbuffer.h:29
Definition: zgl.h:165
Definition: zgl.h:235
Definition: zgl.h:283
Definition: zgl.h:222
Definition: zbuffer.h:106