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