1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This file is based on, or a modified version of code from TinyGL (C) 1997-1998 Fabrice Bellard,
* which is licensed under the zlib-license (see LICENSE).
* It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
*/
#ifndef GRAPHICS_TINYGL_ZBUFFER_H_
#define GRAPHICS_TINYGL_ZBUFFER_H_
#include "graphics/pixelbuffer.h"
#include "graphics/tinygl/gl.h"
#include "common/rect.h"
namespace TinyGL {
// Z buffer
#define ZB_Z_BITS 16
#define ZB_POINT_Z_FRAC_BITS 14
#define ZB_POINT_ST_FRAC_BITS 14
#define ZB_POINT_ST_FRAC_SHIFT (ZB_POINT_ST_FRAC_BITS - 1)
#define ZB_POINT_ST_MAX ( (c->_textureSize << ZB_POINT_ST_FRAC_BITS) - 1 )
#define ZB_POINT_RED_BITS 16
#define ZB_POINT_RED_FRAC_BITS 8
#define ZB_POINT_RED_FRAC_SHIFT (ZB_POINT_RED_FRAC_BITS - 1)
#define ZB_POINT_RED_MAX ( (1 << ZB_POINT_RED_BITS) - 1 )
#define ZB_POINT_GREEN_BITS 16
#define ZB_POINT_GREEN_FRAC_BITS 8
#define ZB_POINT_GREEN_FRAC_SHIFT (ZB_POINT_GREEN_FRAC_BITS - 1)
#define ZB_POINT_GREEN_MAX ( (1 << ZB_POINT_GREEN_BITS) - 1 )
#define ZB_POINT_BLUE_BITS 16
#define ZB_POINT_BLUE_FRAC_BITS 8
#define ZB_POINT_BLUE_FRAC_SHIFT (ZB_POINT_BLUE_FRAC_BITS - 1)
#define ZB_POINT_BLUE_MAX ( (1 << ZB_POINT_BLUE_BITS) - 1 )
#define ZB_POINT_ALPHA_BITS 16
#define ZB_POINT_ALPHA_FRAC_BITS 8
#define ZB_POINT_ALPHA_FRAC_SHIFT (ZB_POINT_ALPHA_FRAC_BITS - 1)
#define ZB_POINT_ALPHA_MAX ( (1 << ZB_POINT_ALPHA_BITS) - 1 )
#define RGB_TO_PIXEL(r, g, b) cmode.ARGBToColor(255, r, g, b) // Default to 255 alpha aka solid colour.
static const int DRAW_DEPTH_ONLY = 0;
static const int DRAW_FLAT = 1;
static const int DRAW_SMOOTH = 2;
static const int DRAW_SHADOW_MASK = 3;
static const int DRAW_SHADOW = 4;
struct Buffer {
byte *pbuf;
unsigned int *zbuf;
bool used;
};
struct ZBufferPoint {
int x, y, z; // integer coordinates in the zbuffer
int s, t; // coordinates for the mapping
int r, g, b, a; // color indexes
float sz, tz; // temporary coordinates for mapping
bool operator==(const ZBufferPoint &other) const {
return x == other.x &&
y == other.y &&
z == other.z &&
s == other.s &&
t == other.t &&
r == other.r &&
g == other.g &&
b == other.b &&
a == other.a;
}
};
struct FrameBuffer {
FrameBuffer(int xsize, int ysize, const Graphics::PixelBuffer &frame_buffer);
~FrameBuffer();
Buffer *genOffscreenBuffer();
void delOffscreenBuffer(Buffer *buffer);
void clear(int clear_z, int z, int clear_color, int r, int g, int b);
void clearRegion(int x, int y, int w, int h,int clear_z, int z, int clear_color, int r, int g, int b);
byte *getPixelBuffer() {
return pbuf.getRawBuffer(0);
}
unsigned int *getZBuffer() {
return _zbuf;
}
FORCEINLINE void readPixelRGB(int pixel, byte &r, byte &g, byte &b) {
pbuf.getRGBAt(pixel, r, g, b);
}
FORCEINLINE bool compareDepth(unsigned int &zSrc, unsigned int &zDst) {
if (!_depthTestEnabled)
return true;
switch (_depthFunc) {
case TGL_NEVER:
break;
case TGL_LESS:
if (zDst < zSrc)
return true;
break;
case TGL_EQUAL:
if (zDst == zSrc)
return true;
break;
case TGL_LEQUAL:
if (zDst <= zSrc)
return true;
break;
case TGL_GREATER:
if (zDst > zSrc)
return true;
break;
case TGL_NOTEQUAL:
if (zDst != zSrc)
return true;
break;
case TGL_GEQUAL:
if (zDst >= zSrc)
return true;
break;
case TGL_ALWAYS:
return true;
}
return false;
}
FORCEINLINE bool checkAlphaTest(byte aSrc) {
if (!_alphaTestEnabled)
return true;
switch (_alphaTestFunc) {
case TGL_NEVER:
break;
case TGL_LESS:
if (aSrc < _alphaTestRefVal)
return true;
break;
case TGL_EQUAL:
if (aSrc == _alphaTestRefVal)
return true;
break;
case TGL_LEQUAL:
if (aSrc <= _alphaTestRefVal)
return true;
break;
case TGL_GREATER:
if (aSrc > _alphaTestRefVal)
return true;
break;
case TGL_NOTEQUAL:
if (aSrc != _alphaTestRefVal)
return true;
break;
case TGL_GEQUAL:
if (aSrc >= _alphaTestRefVal)
return true;
break;
case TGL_ALWAYS:
return true;
}
return false;
}
template <bool kEnableAlphaTest, bool kBlendingEnabled>
FORCEINLINE void writePixel(int pixel, int value) {
writePixel<kEnableAlphaTest, kBlendingEnabled, false>(pixel, value, 0);
}
template <bool kEnableAlphaTest, bool kBlendingEnabled, bool kDepthWrite>
FORCEINLINE void writePixel(int pixel, int value, unsigned int z) {
if (kBlendingEnabled == false) {
this->pbuf.setPixelAt(pixel, value);
if (kDepthWrite) {
_zbuf[pixel] = z;
}
} else {
byte rSrc, gSrc, bSrc, aSrc;
this->pbuf.getFormat().colorToARGB(value, aSrc, rSrc, gSrc, bSrc);
writePixel<kEnableAlphaTest, kBlendingEnabled, kDepthWrite>(pixel, aSrc, rSrc, gSrc, bSrc, z);
}
}
FORCEINLINE void writePixel(int pixel, int value) {
if (_alphaTestEnabled) {
writePixel<true>(pixel, value);
} else {
writePixel<false>(pixel, value);
}
}
template <bool kEnableAlphaTest>
FORCEINLINE void writePixel(int pixel, int value) {
if (_blendingEnabled) {
writePixel<kEnableAlphaTest, true>(pixel, value);
} else {
writePixel<kEnableAlphaTest, false>(pixel, value);
}
}
FORCEINLINE void writePixel(int pixel, byte rSrc, byte gSrc, byte bSrc) {
writePixel(pixel, 255, rSrc, gSrc, bSrc);
}
FORCEINLINE bool scissorPixel(int x, int y) {
return !_clipRectangle.contains(x, y);
}
FORCEINLINE void writePixel(int pixel, byte aSrc, byte rSrc, byte gSrc, byte bSrc) {
if (_alphaTestEnabled) {
writePixel<true>(pixel, aSrc, rSrc, gSrc, bSrc);
} else {
writePixel<false>(pixel, aSrc, rSrc, gSrc, bSrc);
}
}
template <bool kEnableAlphaTest>
FORCEINLINE void writePixel(int pixel, byte aSrc, byte rSrc, byte gSrc, byte bSrc) {
if (_blendingEnabled) {
writePixel<kEnableAlphaTest, true>(pixel, aSrc, rSrc, gSrc, bSrc);
} else {
writePixel<kEnableAlphaTest, false>(pixel, aSrc, rSrc, gSrc, bSrc);
}
}
template <bool kEnableAlphaTest, bool kBlendingEnabled>
FORCEINLINE void writePixel(int pixel, byte aSrc, byte rSrc, byte gSrc, byte bSrc) {
writePixel<kEnableAlphaTest, kBlendingEnabled, false>(pixel, aSrc, rSrc, gSrc, bSrc, 0);
}
template <bool kEnableAlphaTest, bool kBlendingEnabled, bool kDepthWrite>
FORCEINLINE void writePixel(int pixel, byte aSrc, byte rSrc, byte gSrc, byte bSrc, unsigned int z) {
if (kEnableAlphaTest) {
if (!checkAlphaTest(aSrc))
return;
}
if (kDepthWrite) {
_zbuf[pixel] = z;
}
if (kBlendingEnabled == false) {
this->pbuf.setPixelAt(pixel, aSrc, rSrc, gSrc, bSrc);
} else {
byte rDst, gDst, bDst, aDst;
this->pbuf.getARGBAt(pixel, aDst, rDst, gDst, bDst);
switch (_sourceBlendingFactor) {
case TGL_ZERO:
rSrc = gSrc = bSrc = 0;
break;
case TGL_ONE:
break;
case TGL_DST_COLOR:
rSrc = (rDst * rSrc) >> 8;
gSrc = (gDst * gSrc) >> 8;
bSrc = (bDst * bSrc) >> 8;
break;
case TGL_ONE_MINUS_DST_COLOR:
rSrc = (rSrc * (255 - rDst)) >> 8;
gSrc = (gSrc * (255 - gDst)) >> 8;
bSrc = (bSrc * (255 - bDst)) >> 8;
break;
case TGL_SRC_ALPHA:
rSrc = (rSrc * aSrc) >> 8;
gSrc = (gSrc * aSrc) >> 8;
bSrc = (bSrc * aSrc) >> 8;
break;
case TGL_ONE_MINUS_SRC_ALPHA:
rSrc = (rSrc * (255 - aSrc)) >> 8;
gSrc = (gSrc * (255 - aSrc)) >> 8;
bSrc = (bSrc * (255 - aSrc)) >> 8;
break;
case TGL_DST_ALPHA:
rSrc = (rSrc * aDst) >> 8;
gSrc = (gSrc * aDst) >> 8;
bSrc = (bSrc * aDst) >> 8;
break;
case TGL_ONE_MINUS_DST_ALPHA:
rSrc = (rSrc * (255 - aDst)) >> 8;
gSrc = (gSrc * (255 - aDst)) >> 8;
bSrc = (bSrc * (255 - aDst)) >> 8;
break;
default:
break;
}
switch (_destinationBlendingFactor) {
case TGL_ZERO:
rDst = gDst = bDst = 0;
break;
case TGL_ONE:
break;
case TGL_DST_COLOR:
rDst = (rDst * rSrc) >> 8;
gDst = (gDst * gSrc) >> 8;
bDst = (bDst * bSrc) >> 8;
break;
case TGL_ONE_MINUS_DST_COLOR:
rDst = (rDst * (255 - rSrc)) >> 8;
gDst = (gDst * (255 - gSrc)) >> 8;
bDst = (bDst * (255 - bSrc)) >> 8;
break;
case TGL_SRC_ALPHA:
rDst = (rDst * aSrc) >> 8;
gDst = (gDst * aSrc) >> 8;
bDst = (bDst * aSrc) >> 8;
break;
case TGL_ONE_MINUS_SRC_ALPHA:
rDst = (rDst * (255 - aSrc)) >> 8;
gDst = (gDst * (255 - aSrc)) >> 8;
bDst = (bDst * (255 - aSrc)) >> 8;
break;
case TGL_DST_ALPHA:
rDst = (rDst * aDst) >> 8;
gDst = (gDst * aDst) >> 8;
bDst = (bDst * aDst) >> 8;
break;
case TGL_ONE_MINUS_DST_ALPHA:
rDst = (rDst * (255 - aDst)) >> 8;
gDst = (gDst * (255 - aDst)) >> 8;
bDst = (bDst * (255 - aDst)) >> 8;
break;
case TGL_SRC_ALPHA_SATURATE: {
int factor = aSrc < 1 - aDst ? aSrc : 1 - aDst;
rDst = (rDst * factor) >> 8;
gDst = (gDst * factor) >> 8;
bDst = (bDst * factor) >> 8;
}
break;
default:
break;
}
int finalR, finalG, finalB;
finalR = rDst + rSrc;
finalG = gDst + gSrc;
finalB = bDst + bSrc;
if (finalR > 255) { finalR = 255; }
if (finalG > 255) { finalG = 255; }
if (finalB > 255) { finalB = 255; }
this->pbuf.setPixelAt(pixel, 255, finalR, finalG, finalB);
}
}
void copyToBuffer(Graphics::PixelBuffer &buf) {
buf.copyBuffer(0, xsize * ysize, pbuf);
}
void copyFromBuffer(Graphics::PixelBuffer buf) {
pbuf.copyBuffer(0, xsize * ysize, buf);
}
void enableBlending(bool enable) {
_blendingEnabled = enable;
}
void enableDepthTest(bool enable) {
_depthTestEnabled = enable;
}
void setBlendingFactors(int sFactor, int dFactor) {
_sourceBlendingFactor = sFactor;
_destinationBlendingFactor = dFactor;
}
void enableAlphaTest(bool enable) {
_alphaTestEnabled = enable;
}
void setAlphaTestFunc(int func, int ref) {
_alphaTestFunc = func;
_alphaTestRefVal = ref;
}
void setDepthFunc(int func) {
_depthFunc = func;
}
void enableDepthWrite(bool enable) {
this->_depthWrite = enable;
}
bool isAlphaBlendingEnabled() const {
return _sourceBlendingFactor == TGL_SRC_ALPHA && _destinationBlendingFactor == TGL_ONE_MINUS_SRC_ALPHA;
}
/**
* Blit the buffer to the screen buffer, checking the depth of the pixels.
* Eack pixel is copied if and only if its depth value is bigger than the
* depth value of the screen pixel, so if it is 'above'.
*/
void blitOffscreenBuffer(Buffer *buffer);
void selectOffscreenBuffer(Buffer *buffer);
void clearOffscreenBuffer(Buffer *buffer);
void setTexture(const Graphics::PixelBuffer &texture);
template <bool kInterpRGB, bool kInterpZ, bool kInterpST, bool kInterpSTZ, int kDrawLogic, bool kDepthWrite, bool enableAlphaTest, bool kEnableScissor, bool enableBlending>
void fillTriangle(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
template <bool kInterpRGB, bool kInterpZ, bool kInterpST, bool kInterpSTZ, int kDrawMode, bool kDepthWrite, bool enableAlphaTest, bool kEnableScissor>
void fillTriangle(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
template <bool kInterpRGB, bool kInterpZ, bool kInterpST, bool kInterpSTZ, int kDrawMode, bool kDepthWrite, bool enableAlphaTest>
void fillTriangle(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
template <bool kInterpRGB, bool kInterpZ, bool kInterpST, bool kInterpSTZ, int kDrawMode, bool kDepthWrite>
void fillTriangle(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
template <bool kInterpRGB, bool kInterpZ, bool kInterpST, bool kInterpSTZ, int kDrawMode>
void fillTriangle(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
void fillTriangleTextureMappingPerspectiveSmooth(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
void fillTriangleTextureMappingPerspectiveFlat(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
void fillTriangleDepthOnly(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
void fillTriangleFlat(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
void fillTriangleSmooth(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
void fillTriangleFlatShadowMask(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
void fillTriangleFlatShadow(ZBufferPoint *p0, ZBufferPoint *p1, ZBufferPoint *p2);
void plot(ZBufferPoint *p);
void fillLine(ZBufferPoint *p1, ZBufferPoint *p2);
void fillLineZ(ZBufferPoint *p1, ZBufferPoint *p2);
void fillLineFlatZ(ZBufferPoint *p1, ZBufferPoint *p2);
void fillLineInterpZ(ZBufferPoint *p1, ZBufferPoint *p2);
void fillLineFlat(ZBufferPoint *p1, ZBufferPoint *p2);
void fillLineInterp(ZBufferPoint *p1, ZBufferPoint *p2);
void setScissorRectangle(const Common::Rect &rect) {
_clipRectangle = rect;
_enableScissor = true;
}
void resetScissorRectangle() {
_enableScissor = false;
}
Common::Rect _clipRectangle;
bool _enableScissor;
int xsize, ysize;
int linesize; // line size, in bytes
Graphics::PixelFormat cmode;
int pixelbytes;
Buffer buffer;
unsigned char *shadow_mask_buf;
int shadow_color_r;
int shadow_color_g;
int shadow_color_b;
int frame_buffer_allocated;
unsigned char *dctable;
int *ctable;
Graphics::PixelBuffer current_texture;
int _textureSize;
int _textureSizeMask;
FORCEINLINE bool isBlendingEnabled() const { return _blendingEnabled; }
FORCEINLINE void getBlendingFactors(int &sourceFactor, int &destinationFactor) const { sourceFactor = _sourceBlendingFactor; destinationFactor = _destinationBlendingFactor; }
FORCEINLINE bool isAlphaTestEnabled() const { return _alphaTestEnabled; }
FORCEINLINE bool isDepthWriteEnabled() const { return _depthWrite; }
FORCEINLINE int getDepthFunc() const { return _depthFunc; }
FORCEINLINE int getDepthWrite() const { return _depthWrite; }
FORCEINLINE int getAlphaTestFunc() const { return _alphaTestFunc; }
FORCEINLINE int getAlphaTestRefVal() const { return _alphaTestRefVal; }
FORCEINLINE int getDepthTestEnabled() const { return _depthTestEnabled; }
private:
template <bool kDepthWrite>
FORCEINLINE void putPixel(unsigned int pixelOffset, int color, int x, int y, unsigned int z);
template <bool kDepthWrite, bool kEnableScissor>
FORCEINLINE void putPixel(unsigned int pixelOffset, int color, int x, int y, unsigned int z);
template <bool kEnableScissor>
FORCEINLINE void putPixel(unsigned int pixelOffset, int color, int x, int y);
template <bool kInterpRGB, bool kInterpZ, bool kDepthWrite>
void drawLine(const ZBufferPoint *p1, const ZBufferPoint *p2);
template <bool kInterpRGB, bool kInterpZ, bool kDepthWrite, bool kEnableScissor>
void drawLine(const ZBufferPoint *p1, const ZBufferPoint *p2);
unsigned int *_zbuf;
bool _depthWrite;
Graphics::PixelBuffer pbuf;
bool _blendingEnabled;
int _sourceBlendingFactor;
int _destinationBlendingFactor;
bool _alphaTestEnabled;
bool _depthTestEnabled;
int _alphaTestFunc;
int _alphaTestRefVal;
int _depthFunc;
};
// memory.c
void gl_free(void *p);
void *gl_malloc(int size);
void *gl_zalloc(int size);
} // end of namespace TinyGL
#endif
|