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  // scissor
289  bool scissor_test_enabled;
290  int scissor[4];
291 
292  // blending
293  bool blending_enabled;
294  int source_blending_factor;
295  int destination_blending_factor;
296 
297  // alpha blending
298  bool alpha_test_enabled;
299  int alpha_test_func;
300  int alpha_test_ref_val;
301 
302  // Internal texture size
303  int _textureSize;
304 
305  // lights
306  GLLight lights[T_MAX_LIGHTS];
307  GLLight *first_light;
308  Vector4 ambient_light_model;
309  int local_light_model;
310  bool lighting_enabled;
311  int light_model_two_side;
312 
313  // materials
314  GLMaterial materials[2];
315  bool color_material_enabled;
316  int current_color_material_mode;
317  int current_color_material_type;
318 
319  // textures
320  GLTexture *current_texture, *default_texture;
321  uint maxTextureName;
322  bool texture_2d_enabled;
323  int texture_mag_filter;
324  int texture_min_filter;
325  uint texture_wrap_s;
326  uint texture_wrap_t;
327  Common::Array<struct tglColorAssociation> colorAssociationList;
328 
329  // shared state
330  GLSharedState shared_state;
331 
332  // current list
333  GLParamBuffer *current_op_buffer;
334  int current_op_buffer_index;
335  int exec_flag, compile_flag, print_flag;
336 
337  // matrix
338  int matrix_mode;
339  Matrix4 *matrix_stack[3];
340  Matrix4 *matrix_stack_ptr[3];
341  int matrix_stack_depth_max[3];
342 
343  Matrix4 matrix_model_view_inv;
344  Matrix4 matrix_model_projection;
345  int matrix_model_projection_updated;
346  int matrix_model_projection_no_w_transform;
347  int apply_texture_matrix;
348 
349  // viewport
350  GLViewport viewport;
351 
352  // current state
353  int polygon_mode_back;
354  int polygon_mode_front;
355 
356  int current_front_face;
357  int current_shade_model;
358  int current_cull_face;
359  bool cull_face_enabled;
360  bool normalize_enabled;
361  gl_draw_triangle_func draw_triangle_front, draw_triangle_back;
362 
363  // selection
364  int render_mode;
365  uint *select_buffer;
366  int select_size;
367  uint *select_ptr, *select_hit;
368  int select_overflow;
369  int select_hits;
370 
371  // names
372  uint name_stack[MAX_NAME_STACK_DEPTH];
373  int name_stack_size;
374 
375  // clear
376  float clear_depth;
377  Vector4 clear_color;
378  int clear_stencil;
379 
380  // current vertex state
381  Vector4 current_color;
382  Vector4 current_normal;
383  Vector4 current_tex_coord;
384  int current_edge_flag;
385 
386  // glBegin / glEnd
387  int in_begin;
388  int begin_type;
389  int vertex_n, vertex_cnt;
390  int vertex_max;
391  GLVertex *vertex;
392 
393  // opengl 1.1 arrays
394  TGLvoid *vertex_array;
395  int vertex_array_size;
396  int vertex_array_stride;
397  int vertex_array_type;
398  TGLvoid *normal_array;
399  int normal_array_stride;
400  int normal_array_type;
401  TGLvoid *color_array;
402  int color_array_size;
403  int color_array_stride;
404  int color_array_type;
405  TGLvoid *texcoord_array;
406  int texcoord_array_size;
407  int texcoord_array_stride;
408  int texcoord_array_type;
409  int client_states;
410 
411  // opengl 1.1 polygon offset
412  float offset_factor;
413  float offset_units;
414  int offset_states;
415 
416  // specular buffer. could probably be shared between contexts,
417  // but that wouldn't be 100% thread safe
418  GLSpecBuf *specbuf_first;
419  int specbuf_used_counter;
420  int specbuf_num_buffers;
421 
422  // opaque structure for user's use
423  void *opaque;
424  // resize viewport function
425  int (*gl_resize_viewport)(int *xsize, int *ysize);
426 
427  // depth test
428  bool depth_test_enabled;
429  int depth_func;
430  bool depth_write_mask;
431 
432  // stencil
433  bool stencil_buffer_supported;
434  bool stencil_test_enabled;
435  int stencil_test_func;
436  int stencil_ref_val;
437  uint stencil_mask;
438  uint stencil_write_mask;
439  int stencil_sfail;
440  int stencil_dpfail;
441  int stencil_dppass;
442 
443  bool color_mask_red;
444  bool color_mask_green;
445  bool color_mask_blue;
446  bool color_mask_alpha;
447 
448  bool fog_enabled;
449  int fog_mode;
450  Vector4 fog_color;
451  float fog_density;
452  float fog_start;
453  float fog_end;
454 
455  bool _enableDirtyRectangles;
456 
457  // stipple
458  bool polygon_stipple_enabled;
459  byte polygon_stipple_pattern[128];
460 
461  // blit test
462  Common::List<BlitImage *> _blitImages;
463 
464  // Draw call queue
465  Common::List<DrawCall *> _drawCallsQueue;
466  Common::List<DrawCall *> _previousFrameDrawCallsQueue;
467  int _currentAllocatorIndex;
468  LinearAllocator _drawCallAllocator[2];
469  bool _debugRectsEnabled;
470  bool _profilingEnabled;
471 
472  void gl_vertex_transform(GLVertex *v);
473  void gl_calc_fog_factor(GLVertex *v);
474 
475  void gl_get_pname(TGLenum pname, union uglValue *data, eDataType &dataType);
476 
477 public:
478  // The glop* functions exposed to public, however they are only for internal use.
479  // Calling them from outside of TinyGL is forbidden
480  #define ADD_OP(a, b, d) void glop ## a (GLParam *p);
481  #include "graphics/tinygl/opinfo.h"
482 
483  void gl_add_op(GLParam *p);
484  void gl_compile_op(GLParam *p);
485 
486  void gl_eval_viewport();
487  void gl_transform_to_viewport(GLVertex *v);
488  void gl_draw_triangle(GLVertex *p0, GLVertex *p1, GLVertex *p2);
489  void gl_draw_line(GLVertex *p0, GLVertex *p1);
490  void gl_draw_point(GLVertex *p0);
491 
492  static void gl_draw_triangle_point(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
493  static void gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
494  static void gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
495  static void gl_draw_triangle_select(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
496  void gl_draw_triangle_clip(GLVertex *p0, GLVertex *p1, GLVertex *p2, int clip_bit);
497 
498  void gl_add_select(uint zmin, uint zmax);
499  void gl_add_select1(int z1, int z2, int z3);
500  void gl_enable_disable_light(int light, int v);
501  void gl_shade_vertex(GLVertex *v);
502 
503  void gl_GetIntegerv(TGLenum pname, TGLint *data);
504  void gl_GetFloatv(TGLenum pname, TGLfloat *data);
505  void gl_GetDoublev(TGLenum pname, TGLdouble *data);
506  void gl_GetBooleanv(TGLenum pname, TGLboolean *data);
507 
508  void gl_EnableClientState(GLParam *p);
509  void gl_DisableClientState(GLParam *p);
510  void gl_VertexPointer(GLParam *p);
511  void gl_ColorPointer(GLParam *p);
512  void gl_NormalPointer(GLParam *p);
513  void gl_TexCoordPointer(GLParam *p);
514 
515  GLTexture *alloc_texture(uint h);
516  GLTexture *find_texture(uint h);
517  void free_texture(GLTexture *t);
518  void gl_GenTextures(TGLsizei n, TGLuint *textures);
519  void gl_DeleteTextures(TGLsizei n, const TGLuint *textures);
520  void gl_PixelStore(TGLenum pname, TGLint param);
521 
522  void issueDrawCall(DrawCall *drawCall);
523  void disposeResources();
524  void disposeDrawCallLists();
525 
526  void presentBufferDirtyRects(Common::List<Common::Rect> &dirtyAreas);
527  void presentBufferSimple(Common::List<Common::Rect> &dirtyAreas);
528 
529  void debugDrawRectangle(Common::Rect rect, int r, int g, int b);
530 
531  GLSpecBuf *specbuf_get_buffer(const int shininess_i, const float shininess);
532  void specbuf_cleanup();
533 
534  TGLint gl_RenderMode(TGLenum mode);
535  void gl_SelectBuffer(TGLsizei size, TGLuint *buffer);
536 
537  GLList *alloc_list(int list);
538  GLList *find_list(uint list);
539  void delete_list(int list);
540  void gl_NewList(TGLuint list, TGLenum mode);
541  void gl_EndList();
542  TGLboolean gl_IsList(TGLuint list);
543  TGLuint gl_GenLists(TGLsizei range);
544 
545  void initSharedState();
546  void endSharedState();
547 
548  void init(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize,
549  bool enableStencilBuffer, bool dirtyRectsEnable, uint32 drawCallMemorySize);
550  void deinit();
551 
552  void gl_print_matrix(const float *m);
553  void gl_debug(int mode) {
554  print_flag = mode;
555  }
556 };
557 
558 extern GLContext *gl_ctx;
559 GLContext *gl_get_context();
560 
561 #define VERTEX_ARRAY 0x0001
562 #define COLOR_ARRAY 0x0002
563 #define NORMAL_ARRAY 0x0004
564 #define TEXCOORD_ARRAY 0x0008
565 
566 // this clip epsilon is needed to avoid some rounding errors after
567 // several clipping stages
568 
569 #define CLIP_EPSILON (1E-5)
570 
571 static inline int gl_clipcode(float x, float y, float z, float w1) {
572  float w;
573 
574  w = (float)(w1 * (1.0 + CLIP_EPSILON));
575  return (x < -w) | ((x > w) << 1) | ((y < -w) << 2) | ((y > w) << 3) | ((z < -w) << 4) | ((z > w) << 5);
576 }
577 
578 static inline float clampf(float a, float min, float max) {
579  if (a < min)
580  return min;
581  if (a > max)
582  return max;
583  else
584  return a;
585 }
586 
587 } // end of namespace TinyGL
588 
589 #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:524
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