| 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 |
#ifndef RLSW_H |
| 88 |
#define RLSW_H |
| 89 |
|
| 90 |
#define RLSW_VERSION "1.5" |
| 91 |
|
| 92 |
#include <stdbool.h> |
| 93 |
#include <stdint.h> |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#ifndef SWAPI |
| 100 |
#define SWAPI |
| 101 |
#endif |
| 102 |
|
| 103 |
#ifndef SW_MALLOC |
| 104 |
#define SW_MALLOC(sz) malloc(sz) |
| 105 |
#endif |
| 106 |
#ifndef SW_CALLOC |
| 107 |
#define SW_CALLOC(n,sz) calloc(n,sz) |
| 108 |
#endif |
| 109 |
#ifndef SW_REALLOC |
| 110 |
#define SW_REALLOC(ptr, newSz) realloc(ptr, newSz) |
| 111 |
#endif |
| 112 |
#ifndef SW_FREE |
| 113 |
#define SW_FREE(ptr) free(ptr) |
| 114 |
#endif |
| 115 |
|
| 116 |
#ifndef SW_RESTRICT |
| 117 |
#ifdef _MSC_VER |
| 118 |
#define SW_RESTRICT __restrict |
| 119 |
#else |
| 120 |
#define SW_RESTRICT restrict |
| 121 |
#endif |
| 122 |
#endif |
| 123 |
|
| 124 |
#ifndef SW_FRAMEBUFFER_OUTPUT_BGRA |
| 125 |
#define SW_FRAMEBUFFER_OUTPUT_BGRA true |
| 126 |
#endif |
| 127 |
|
| 128 |
#ifndef SW_FRAMEBUFFER_COLOR_TYPE |
| 129 |
#define SW_FRAMEBUFFER_COLOR_TYPE R8G8B8A8 // Or R5G6B5, R3G3B2, etc; see `sw_pixelformat_t` |
| 130 |
#endif |
| 131 |
|
| 132 |
#ifndef SW_FRAMEBUFFER_DEPTH_TYPE |
| 133 |
#define SW_FRAMEBUFFER_DEPTH_TYPE D32 // Or D16, D8 |
| 134 |
#endif |
| 135 |
|
| 136 |
#ifndef SW_MAX_PROJECTION_STACK_SIZE |
| 137 |
#define SW_MAX_PROJECTION_STACK_SIZE 2 |
| 138 |
#endif |
| 139 |
|
| 140 |
#ifndef SW_MAX_MODELVIEW_STACK_SIZE |
| 141 |
#define SW_MAX_MODELVIEW_STACK_SIZE 8 |
| 142 |
#endif |
| 143 |
|
| 144 |
#ifndef SW_MAX_TEXTURE_STACK_SIZE |
| 145 |
#define SW_MAX_TEXTURE_STACK_SIZE 2 |
| 146 |
#endif |
| 147 |
|
| 148 |
#ifndef SW_MAX_FRAMEBUFFERS |
| 149 |
#define SW_MAX_FRAMEBUFFERS 8 |
| 150 |
#endif |
| 151 |
|
| 152 |
#ifndef SW_MAX_TEXTURES |
| 153 |
#define SW_MAX_TEXTURES 128 |
| 154 |
#endif |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#ifndef SW_USE_COLOR_LUT |
| 160 |
#if RLSW_USE_SIMD_INTRINSICS |
| 161 |
#define SW_USE_COLOR_LUT false |
| 162 |
#else |
| 163 |
#define SW_USE_COLOR_LUT true |
| 164 |
#endif |
| 165 |
#endif |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
typedef unsigned int GLenum; |
| 171 |
typedef unsigned char GLboolean; |
| 172 |
typedef unsigned int GLbitfield; |
| 173 |
typedef void GLvoid; |
| 174 |
typedef signed char GLbyte; |
| 175 |
typedef short GLshort; |
| 176 |
typedef int GLint; |
| 177 |
typedef unsigned char GLubyte; |
| 178 |
typedef unsigned short GLushort; |
| 179 |
typedef unsigned int GLuint; |
| 180 |
typedef int GLsizei; |
| 181 |
typedef float GLfloat; |
| 182 |
typedef float GLclampf; |
| 183 |
typedef double GLdouble; |
| 184 |
typedef double GLclampd; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#define GL_FALSE 0 |
| 191 |
#define GL_TRUE 1 |
| 192 |
|
| 193 |
#define GL_SCISSOR_TEST 0x0C11 |
| 194 |
#define GL_TEXTURE_2D 0x0DE1 |
| 195 |
#define GL_DEPTH_TEST 0x0B71 |
| 196 |
#define GL_CULL_FACE 0x0B44 |
| 197 |
#define GL_BLEND 0x0BE2 |
| 198 |
|
| 199 |
#define GL_VENDOR 0x1F00 |
| 200 |
#define GL_RENDERER 0x1F01 |
| 201 |
#define GL_VERSION 0x1F02 |
| 202 |
#define GL_EXTENSIONS 0x1F03 |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
#define GL_COLOR_CLEAR_VALUE 0x0C22 |
| 207 |
#define GL_DEPTH_CLEAR_VALUE 0x0B73 |
| 208 |
|
| 209 |
|
| 210 |
#define GL_CURRENT_COLOR 0x0B00 |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
#define GL_CURRENT_TEXTURE_COORDS 0x0B03 |
| 219 |
#define GL_POINT_SIZE 0x0B11 |
| 220 |
#define GL_LINE_WIDTH 0x0B21 |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
#define GL_MODELVIEW_MATRIX 0x0BA6 |
| 225 |
#define GL_MODELVIEW_STACK_DEPTH 0x0BA3 |
| 226 |
|
| 227 |
#define GL_PROJECTION_MATRIX 0x0BA7 |
| 228 |
#define GL_PROJECTION_STACK_DEPTH 0x0BA4 |
| 229 |
|
| 230 |
|
| 231 |
#define GL_TEXTURE_MATRIX 0x0BA8 |
| 232 |
#define GL_TEXTURE_STACK_DEPTH 0x0BA5 |
| 233 |
#define GL_VIEWPORT 0x0BA2 |
| 234 |
|
| 235 |
#define GL_COLOR_BUFFER_BIT 0x00004000 |
| 236 |
#define GL_DEPTH_BUFFER_BIT 0x00000100 |
| 237 |
|
| 238 |
#define GL_MODELVIEW 0x1700 |
| 239 |
#define GL_PROJECTION 0x1701 |
| 240 |
#define GL_TEXTURE 0x1702 |
| 241 |
|
| 242 |
#define GL_VERTEX_ARRAY 0x8074 |
| 243 |
#define GL_NORMAL_ARRAY 0x8075 // WARNING: Not implemented (defined for RLGL) |
| 244 |
#define GL_COLOR_ARRAY 0x8076 |
| 245 |
|
| 246 |
#define GL_TEXTURE_COORD_ARRAY 0x8078 |
| 247 |
|
| 248 |
#define GL_POINTS 0x0000 |
| 249 |
#define GL_LINES 0x0001 |
| 250 |
|
| 251 |
|
| 252 |
#define GL_TRIANGLES 0x0004 |
| 253 |
|
| 254 |
|
| 255 |
#define GL_QUADS 0x0007 |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
#define GL_POINT 0x1B00 |
| 260 |
#define GL_LINE 0x1B01 |
| 261 |
#define GL_FILL 0x1B02 |
| 262 |
|
| 263 |
#define GL_FRONT 0x0404 |
| 264 |
#define GL_BACK 0x0405 |
| 265 |
|
| 266 |
#define GL_ZERO 0 |
| 267 |
#define GL_ONE 1 |
| 268 |
#define GL_SRC_COLOR 0x0300 |
| 269 |
#define GL_ONE_MINUS_SRC_COLOR 0x0301 |
| 270 |
#define GL_SRC_ALPHA 0x0302 |
| 271 |
#define GL_ONE_MINUS_SRC_ALPHA 0x0303 |
| 272 |
#define GL_DST_ALPHA 0x0304 |
| 273 |
#define GL_ONE_MINUS_DST_ALPHA 0x0305 |
| 274 |
#define GL_DST_COLOR 0x0306 |
| 275 |
#define GL_ONE_MINUS_DST_COLOR 0x0307 |
| 276 |
#define GL_SRC_ALPHA_SATURATE 0x0308 |
| 277 |
|
| 278 |
#define GL_NEAREST 0x2600 |
| 279 |
#define GL_LINEAR 0x2601 |
| 280 |
|
| 281 |
#define GL_REPEAT 0x2901 |
| 282 |
#define GL_CLAMP 0x2900 |
| 283 |
|
| 284 |
#define GL_TEXTURE_MAG_FILTER 0x2800 |
| 285 |
#define GL_TEXTURE_MIN_FILTER 0x2801 |
| 286 |
|
| 287 |
#define GL_TEXTURE_WRAP_S 0x2802 |
| 288 |
#define GL_TEXTURE_WRAP_T 0x2803 |
| 289 |
|
| 290 |
#define GL_NO_ERROR 0 |
| 291 |
#define GL_INVALID_ENUM 0x0500 |
| 292 |
#define GL_INVALID_VALUE 0x0501 |
| 293 |
#define GL_INVALID_OPERATION 0x0502 |
| 294 |
#define GL_STACK_OVERFLOW 0x0503 |
| 295 |
#define GL_STACK_UNDERFLOW 0x0504 |
| 296 |
#define GL_OUT_OF_MEMORY 0x0505 |
| 297 |
|
| 298 |
#define GL_ALPHA 0x1906 |
| 299 |
#define GL_LUMINANCE 0x1909 |
| 300 |
#define GL_LUMINANCE_ALPHA 0x190A |
| 301 |
#define GL_RGB 0x1907 |
| 302 |
#define GL_RGBA 0x1908 |
| 303 |
#define GL_DEPTH_COMPONENT 0x1902 |
| 304 |
|
| 305 |
#define GL_BYTE 0x1400 |
| 306 |
#define GL_UNSIGNED_BYTE 0x1401 |
| 307 |
#define GL_SHORT 0x1402 |
| 308 |
#define GL_UNSIGNED_SHORT 0x1403 |
| 309 |
#define GL_INT 0x1404 |
| 310 |
#define GL_UNSIGNED_INT 0x1405 |
| 311 |
#define GL_FLOAT 0x1406 |
| 312 |
#define GL_UNSIGNED_BYTE_3_3_2 0x8032 |
| 313 |
#define GL_UNSIGNED_SHORT_5_6_5 0x8363 |
| 314 |
#define GL_UNSIGNED_SHORT_4_4_4_4 0x8033 |
| 315 |
#define GL_UNSIGNED_SHORT_5_5_5_1 0x8034 |
| 316 |
|
| 317 |
|
| 318 |
#define GL_ALPHA4 0x803B |
| 319 |
#define GL_ALPHA8 0x803C |
| 320 |
#define GL_ALPHA12 0x803D |
| 321 |
#define GL_ALPHA16 0x803E |
| 322 |
#define GL_LUMINANCE4 0x803F |
| 323 |
#define GL_LUMINANCE8 0x8040 |
| 324 |
#define GL_LUMINANCE12 0x8041 |
| 325 |
#define GL_LUMINANCE16 0x8042 |
| 326 |
#define GL_LUMINANCE4_ALPHA4 0x8043 |
| 327 |
#define GL_LUMINANCE6_ALPHA2 0x8044 |
| 328 |
#define GL_LUMINANCE8_ALPHA8 0x8045 |
| 329 |
#define GL_LUMINANCE12_ALPHA4 0x8046 |
| 330 |
#define GL_LUMINANCE12_ALPHA12 0x8047 |
| 331 |
#define GL_LUMINANCE16_ALPHA16 0x8048 |
| 332 |
#define GL_INTENSITY 0x8049 |
| 333 |
#define GL_INTENSITY4 0x804A |
| 334 |
#define GL_INTENSITY8 0x804B |
| 335 |
#define GL_INTENSITY12 0x804C |
| 336 |
#define GL_INTENSITY16 0x804D |
| 337 |
#define GL_R3_G3_B2 0x2A10 |
| 338 |
#define GL_RGB4 0x804F |
| 339 |
#define GL_RGB5 0x8050 |
| 340 |
#define GL_RGB8 0x8051 |
| 341 |
#define GL_RGB10 0x8052 |
| 342 |
#define GL_RGB12 0x8053 |
| 343 |
#define GL_RGB16 0x8054 |
| 344 |
#define GL_RGBA2 0x8055 |
| 345 |
#define GL_RGBA4 0x8056 |
| 346 |
#define GL_RGB5_A1 0x8057 |
| 347 |
#define GL_RGBA8 0x8058 |
| 348 |
#define GL_RGB10_A2 0x8059 |
| 349 |
#define GL_RGBA12 0x805A |
| 350 |
#define GL_RGBA16 0x805B |
| 351 |
|
| 352 |
|
| 353 |
#define GL_DEPTH_COMPONENT16 0x81A5 |
| 354 |
#define GL_DEPTH_COMPONENT24 0x81A6 |
| 355 |
#define GL_DEPTH_COMPONENT32 0x81A7 |
| 356 |
#define GL_DEPTH_COMPONENT32F 0x8CAC |
| 357 |
#define GL_R16F 0x822D |
| 358 |
#define GL_RGB16F 0x881B |
| 359 |
#define GL_RGBA16F 0x881A |
| 360 |
#define GL_R32F 0x822E |
| 361 |
#define GL_RGB32F 0x8815 |
| 362 |
#define GL_RGBA32F 0x8814 |
| 363 |
|
| 364 |
|
| 365 |
#define GL_DRAW_FRAMEBUFFER_BINDING 0x8CA6 |
| 366 |
#define GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE 0x8CD0 |
| 367 |
#define GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME 0x8CD1 |
| 368 |
#define GL_FRAMEBUFFER_COMPLETE 0x8CD5 |
| 369 |
#define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT 0x8CD6 |
| 370 |
#define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT 0x8CD7 |
| 371 |
#define GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS 0x8CD9 |
| 372 |
#define GL_FRAMEBUFFER_UNSUPPORTED 0x8CDD |
| 373 |
#define GL_COLOR_ATTACHMENT0 0x8CE0 |
| 374 |
#define GL_DEPTH_ATTACHMENT 0x8D00 |
| 375 |
#define GL_STENCIL_ATTACHMENT 0x8D20 // WARNING: Not implemented (defined for RLGL) |
| 376 |
|
| 377 |
|
| 378 |
#define GL_PERSPECTIVE_CORRECTION_HINT 0x0C50 |
| 379 |
#define GL_PACK_ALIGNMENT 0x0D05 |
| 380 |
#define GL_UNPACK_ALIGNMENT 0x0CF5 |
| 381 |
#define GL_LINE_SMOOTH 0x0B20 |
| 382 |
#define GL_SMOOTH 0x1D01 |
| 383 |
#define GL_NICEST 0x1102 |
| 384 |
#define GL_CCW 0x0901 |
| 385 |
#define GL_CW 0x0900 |
| 386 |
#define GL_NEVER 0x0200 |
| 387 |
#define GL_LESS 0x0201 |
| 388 |
#define GL_EQUAL 0x0202 |
| 389 |
#define GL_LEQUAL 0x0203 |
| 390 |
#define GL_GREATER 0x0204 |
| 391 |
#define GL_NOTEQUAL 0x0205 |
| 392 |
#define GL_GEQUAL 0x0206 |
| 393 |
#define GL_ALWAYS 0x0207 |
| 394 |
#define GL_RENDERBUFFER 0x8D41 |
| 395 |
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516 |
| 396 |
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518 |
| 397 |
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A |
| 398 |
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515 |
| 399 |
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517 |
| 400 |
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519 |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
#define glReadPixels(x, y, w, h, f, t, p) swReadPixels((x), (y), (w), (h), (f), (t), (p)) |
| 406 |
#define glEnable(state) swEnable((state)) |
| 407 |
#define glDisable(state) swDisable((state)) |
| 408 |
#define glGetIntegerv(pname, params) swGetIntegerv((pname), (params)) |
| 409 |
#define glGetFloatv(pname, params) swGetFloatv((pname), (params)) |
| 410 |
#define glGetString(pname) swGetString((pname)) |
| 411 |
#define glGetError() swGetError() |
| 412 |
#define glViewport(x, y, w, h) swViewport((x), (y), (w), (h)) |
| 413 |
#define glScissor(x, y, w, h) swScissor((x), (y), (w), (h)) |
| 414 |
#define glClearColor(r, g, b, a) swClearColor((r), (g), (b), (a)) |
| 415 |
#define glClearDepth(d) swClearDepth((d)) |
| 416 |
#define glClear(bitmask) swClear((bitmask)) |
| 417 |
#define glBlendFunc(sfactor, dfactor) swBlendFunc((sfactor), (dfactor)) |
| 418 |
#define glPolygonMode(face, mode) swPolygonMode((mode)) |
| 419 |
#define glCullFace(face) swCullFace((face)) |
| 420 |
#define glPointSize(size) swPointSize((size)) |
| 421 |
#define glLineWidth(width) swLineWidth((width)) |
| 422 |
#define glMatrixMode(mode) swMatrixMode((mode)) |
| 423 |
#define glPushMatrix() swPushMatrix() |
| 424 |
#define glPopMatrix() swPopMatrix() |
| 425 |
#define glLoadIdentity() swLoadIdentity() |
| 426 |
#define glTranslatef(x, y, z) swTranslatef((x), (y), (z)) |
| 427 |
#define glRotatef(a, x, y, z) swRotatef((a), (x), (y), (z)) |
| 428 |
#define glScalef(x, y, z) swScalef((x), (y), (z)) |
| 429 |
#define glMultMatrixf(v) swMultMatrixf((v)) |
| 430 |
#define glFrustum(l, r, b, t, n, f) swFrustum((l), (r), (b), (t), (n), (f)) |
| 431 |
#define glOrtho(l, r, b, t, n, f) swOrtho((l), (r), (b), (t), (n), (f)) |
| 432 |
#define glBegin(mode) swBegin((mode)) |
| 433 |
#define glEnd() swEnd() |
| 434 |
#define glVertex2i(x, y) swVertex2i((x), (y)) |
| 435 |
#define glVertex2f(x, y) swVertex2f((x), (y)) |
| 436 |
#define glVertex2fv(v) swVertex2fv((v)) |
| 437 |
#define glVertex3i(x, y, z) swVertex3i((x), (y), (z)) |
| 438 |
#define glVertex3f(x, y, z) swVertex3f((x), (y), (z)) |
| 439 |
#define glvertex3fv(v) swVertex3fv((v)) |
| 440 |
#define glVertex4i(x, y, z, w) swVertex4i((x), (y), (z), (w)) |
| 441 |
#define glVertex4f(x, y, z, w) swVertex4f((x), (y), (z), (w)) |
| 442 |
#define glVertex4fv(v) swVertex4fv((v)) |
| 443 |
#define glColor3ub(r, g, b) swColor3ub((r), (g), (b)) |
| 444 |
#define glColor3ubv(v) swColor3ubv((v)) |
| 445 |
#define glColor3f(r, g, b) swColor3f((r), (g), (b)) |
| 446 |
#define glColor3fv(v) swColor3fv((v)) |
| 447 |
#define glColor4ub(r, g, b, a) swColor4ub((r), (g), (b), (a)) |
| 448 |
#define glColor4ubv(v) swColor4ubv((v)) |
| 449 |
#define glColor4f(r, g, b, a) swColor4f((r), (g), (b), (a)) |
| 450 |
#define glColor4fv(v) swColor4fv((v)) |
| 451 |
#define glTexCoord2f(u, v) swTexCoord2f((u), (v)) |
| 452 |
#define glTexCoord2fv(v) swTexCoord2fv((v)) |
| 453 |
|
| 454 |
#define glEnableClientState(t) ((void)(t)) |
| 455 |
#define glDisableClientState(t) swBindArray((t), 0) |
| 456 |
#define glVertexPointer(sz, t, s, p) swBindArray(SW_VERTEX_ARRAY, (p)) |
| 457 |
#define glTexCoordPointer(sz, t, s, p) swBindArray(SW_TEXTURE_COORD_ARRAY, (p)) |
| 458 |
#define glColorPointer(sz, t, s, p) swBindArray(SW_COLOR_ARRAY, (p)) |
| 459 |
#define glDrawArrays(m, o, c) swDrawArrays((m), (o), (c)) |
| 460 |
#define glDrawElements(m,c,t,i) swDrawElements((m),(c),(t),(i)) |
| 461 |
#define glGenTextures(c, v) swGenTextures((c), (v)) |
| 462 |
#define glDeleteTextures(c, v) swDeleteTextures((c), (v)) |
| 463 |
#define glBindTexture(tr, id) swBindTexture((id)) |
| 464 |
#define glTexImage2D(tr, l, if, w, h, b, f, t, p) swTexImage2D((w), (h), (f), (t), (p)) |
| 465 |
#define glTexSubImage2D(tr, l, x, y, w, h, f, t, p) swTexSubImage2D((x), (y), (w), (h), (f), (t), (p)); |
| 466 |
#define glTexParameteri(tr, pname, param) swTexParameteri((pname), (param)) |
| 467 |
|
| 468 |
|
| 469 |
#define glGenFramebuffers(c, v) swGenFramebuffers((c), (v)) |
| 470 |
#define glDeleteFramebuffers(c, v) swDeleteFramebuffers((c), (v)) |
| 471 |
#define glBindFramebuffer(tr, id) swBindFramebuffer((id)) |
| 472 |
#define glFramebufferTexture2D(tr, a, t, tex, ml) swFramebufferTexture2D((a), (tex)) |
| 473 |
#define glFramebufferRenderbuffer(tr, a, rbtr, rb) swFramebufferTexture2D((a), (rb)) |
| 474 |
#define glCheckFramebufferStatus(tr) swCheckFramebufferStatus() |
| 475 |
#define glGetFramebufferAttachmentParameteriv(tr, a, p, v) swGetFramebufferAttachmentParameteriv((a), (p), (v)) |
| 476 |
#define glGenRenderbuffers(c, v) swGenTextures((c), (v)) |
| 477 |
#define glDeleteRenderbuffers(c, v) swDeleteTextures((c), (v)) |
| 478 |
#define glBindRenderbuffer(tr, rb) swBindTexture((rb)) |
| 479 |
#define glRenderbufferStorage(tr, f, w, h) swTexStorage2D((w), (h), (f)) |
| 480 |
|
| 481 |
|
| 482 |
#define glDepthMask(X) ((void)(X)) |
| 483 |
#define glColorMask(X,Y,Z,W) ((void)(X),(void)(Y),(void)(Z),(void)(W)) |
| 484 |
#define glPixelStorei(X,Y) ((void)(X),(void)(Y)) |
| 485 |
#define glHint(X,Y) ((void)(X),(void)(Y)) |
| 486 |
#define glShadeModel(X) ((void)(X)) |
| 487 |
#define glFrontFace(X) ((void)(X)) |
| 488 |
#define glDepthFunc(X) ((void)(X)) |
| 489 |
#define glGetTexImage(X,Y,Z,W,A) ((void)(X),(void)(Y),(void)(Z),(void)(W),(void)(A)) |
| 490 |
#define glNormal3f(X,Y,Z) ((void)(X),(void)(Y),(void)(Z)) |
| 491 |
#define glNormal3fv(X) ((void)(X)) |
| 492 |
#define glNormalPointer(X,Y,Z) ((void)(X),(void)(Y),(void)(Z)) |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
typedef enum { |
| 498 |
SW_SCISSOR_TEST = GL_SCISSOR_TEST, |
| 499 |
SW_TEXTURE_2D = GL_TEXTURE_2D, |
| 500 |
SW_DEPTH_TEST = GL_DEPTH_TEST, |
| 501 |
SW_CULL_FACE = GL_CULL_FACE, |
| 502 |
SW_BLEND = GL_BLEND |
| 503 |
} SWstate; |
| 504 |
|
| 505 |
typedef enum { |
| 506 |
SW_VENDOR = GL_VENDOR, |
| 507 |
SW_RENDERER = GL_RENDERER, |
| 508 |
SW_VERSION = GL_VERSION, |
| 509 |
SW_EXTENSIONS = GL_EXTENSIONS, |
| 510 |
SW_COLOR_CLEAR_VALUE = GL_COLOR_CLEAR_VALUE, |
| 511 |
SW_DEPTH_CLEAR_VALUE = GL_DEPTH_CLEAR_VALUE, |
| 512 |
SW_CURRENT_COLOR = GL_CURRENT_COLOR, |
| 513 |
SW_CURRENT_TEXTURE_COORDS = GL_CURRENT_TEXTURE_COORDS, |
| 514 |
SW_POINT_SIZE = GL_POINT_SIZE, |
| 515 |
SW_LINE_WIDTH = GL_LINE_WIDTH, |
| 516 |
SW_MODELVIEW_MATRIX = GL_MODELVIEW_MATRIX, |
| 517 |
SW_MODELVIEW_STACK_DEPTH = GL_MODELVIEW_STACK_DEPTH, |
| 518 |
SW_PROJECTION_MATRIX = GL_PROJECTION_MATRIX, |
| 519 |
SW_PROJECTION_STACK_DEPTH = GL_PROJECTION_STACK_DEPTH, |
| 520 |
SW_TEXTURE_MATRIX = GL_TEXTURE_MATRIX, |
| 521 |
SW_TEXTURE_STACK_DEPTH = GL_TEXTURE_STACK_DEPTH, |
| 522 |
SW_VIEWPORT = GL_VIEWPORT, |
| 523 |
SW_DRAW_FRAMEBUFFER_BINDING = GL_DRAW_FRAMEBUFFER_BINDING, |
| 524 |
} SWget; |
| 525 |
|
| 526 |
typedef enum { |
| 527 |
SW_COLOR_BUFFER_BIT = GL_COLOR_BUFFER_BIT, |
| 528 |
SW_DEPTH_BUFFER_BIT = GL_DEPTH_BUFFER_BIT |
| 529 |
} SWbuffer; |
| 530 |
|
| 531 |
typedef enum { |
| 532 |
SW_PROJECTION = GL_PROJECTION, |
| 533 |
SW_MODELVIEW = GL_MODELVIEW, |
| 534 |
SW_TEXTURE = GL_TEXTURE |
| 535 |
} SWmatrix; |
| 536 |
|
| 537 |
typedef enum { |
| 538 |
SW_VERTEX_ARRAY = GL_VERTEX_ARRAY, |
| 539 |
SW_TEXTURE_COORD_ARRAY = GL_TEXTURE_COORD_ARRAY, |
| 540 |
SW_COLOR_ARRAY = GL_COLOR_ARRAY |
| 541 |
} SWarray; |
| 542 |
|
| 543 |
typedef enum { |
| 544 |
SW_DRAW_INVALID = -1, |
| 545 |
SW_POINTS = GL_POINTS, |
| 546 |
SW_LINES = GL_LINES, |
| 547 |
SW_TRIANGLES = GL_TRIANGLES, |
| 548 |
SW_QUADS = GL_QUADS |
| 549 |
} SWdraw; |
| 550 |
|
| 551 |
typedef enum { |
| 552 |
SW_POINT = GL_POINT, |
| 553 |
SW_LINE = GL_LINE, |
| 554 |
SW_FILL = GL_FILL |
| 555 |
} SWpoly; |
| 556 |
|
| 557 |
typedef enum { |
| 558 |
SW_FRONT = GL_FRONT, |
| 559 |
SW_BACK = GL_BACK, |
| 560 |
} SWface; |
| 561 |
|
| 562 |
typedef enum { |
| 563 |
SW_ZERO = GL_ZERO, |
| 564 |
SW_ONE = GL_ONE, |
| 565 |
SW_SRC_COLOR = GL_SRC_COLOR, |
| 566 |
SW_ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR, |
| 567 |
SW_SRC_ALPHA = GL_SRC_ALPHA, |
| 568 |
SW_ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA, |
| 569 |
SW_DST_ALPHA = GL_DST_ALPHA, |
| 570 |
SW_ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA, |
| 571 |
SW_DST_COLOR = GL_DST_COLOR, |
| 572 |
SW_ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR, |
| 573 |
SW_SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE |
| 574 |
} SWfactor; |
| 575 |
|
| 576 |
typedef enum { |
| 577 |
SW_LUMINANCE = GL_LUMINANCE, |
| 578 |
SW_LUMINANCE_ALPHA = GL_LUMINANCE_ALPHA, |
| 579 |
SW_RGB = GL_RGB, |
| 580 |
SW_RGBA = GL_RGBA, |
| 581 |
SW_DEPTH_COMPONENT = GL_DEPTH_COMPONENT, |
| 582 |
} SWformat; |
| 583 |
|
| 584 |
typedef enum { |
| 585 |
SW_UNSIGNED_BYTE = GL_UNSIGNED_BYTE, |
| 586 |
SW_UNSIGNED_BYTE_3_3_2 = GL_UNSIGNED_BYTE_3_3_2, |
| 587 |
SW_BYTE = GL_BYTE, |
| 588 |
SW_UNSIGNED_SHORT = GL_UNSIGNED_SHORT, |
| 589 |
SW_UNSIGNED_SHORT_5_6_5 = GL_UNSIGNED_SHORT_5_6_5, |
| 590 |
SW_UNSIGNED_SHORT_4_4_4_4 = GL_UNSIGNED_SHORT_4_4_4_4, |
| 591 |
SW_UNSIGNED_SHORT_5_5_5_1 = GL_UNSIGNED_SHORT_5_5_5_1, |
| 592 |
SW_SHORT = GL_SHORT, |
| 593 |
SW_UNSIGNED_INT = GL_UNSIGNED_INT, |
| 594 |
SW_INT = GL_INT, |
| 595 |
SW_FLOAT = GL_FLOAT |
| 596 |
} SWtype; |
| 597 |
|
| 598 |
typedef enum { |
| 599 |
SW_LUMINANCE8 = GL_LUMINANCE8, |
| 600 |
SW_LUMINANCE8_ALPHA8 = GL_LUMINANCE8_ALPHA8, |
| 601 |
SW_R3_G3_B2 = GL_R3_G3_B2, |
| 602 |
SW_RGB8 = GL_RGB8, |
| 603 |
SW_RGBA4 = GL_RGBA4, |
| 604 |
SW_RGB5_A1 = GL_RGB5_A1, |
| 605 |
SW_RGBA8 = GL_RGBA8, |
| 606 |
SW_R16F = GL_R16F, |
| 607 |
SW_RGB16F = GL_RGB16F, |
| 608 |
SW_RGBA16F = GL_RGBA16F, |
| 609 |
SW_R32F = GL_R32F, |
| 610 |
SW_RGB32F = GL_RGB32F, |
| 611 |
SW_RGBA32F = GL_RGBA32F, |
| 612 |
SW_DEPTH_COMPONENT16 = GL_DEPTH_COMPONENT16, |
| 613 |
SW_DEPTH_COMPONENT24 = GL_DEPTH_COMPONENT24, |
| 614 |
SW_DEPTH_COMPONENT32 = GL_DEPTH_COMPONENT32, |
| 615 |
SW_DEPTH_COMPONENT32F = GL_DEPTH_COMPONENT32F, |
| 616 |
|
| 617 |
} SWinternalformat; |
| 618 |
|
| 619 |
typedef enum { |
| 620 |
SW_NEAREST = GL_NEAREST, |
| 621 |
SW_LINEAR = GL_LINEAR |
| 622 |
} SWfilter; |
| 623 |
|
| 624 |
typedef enum { |
| 625 |
SW_REPEAT = GL_REPEAT, |
| 626 |
SW_CLAMP = GL_CLAMP, |
| 627 |
} SWwrap; |
| 628 |
|
| 629 |
typedef enum { |
| 630 |
SW_TEXTURE_MIN_FILTER = GL_TEXTURE_MIN_FILTER, |
| 631 |
SW_TEXTURE_MAG_FILTER = GL_TEXTURE_MAG_FILTER, |
| 632 |
SW_TEXTURE_WRAP_S = GL_TEXTURE_WRAP_S, |
| 633 |
SW_TEXTURE_WRAP_T = GL_TEXTURE_WRAP_T |
| 634 |
} SWtexparam; |
| 635 |
|
| 636 |
typedef enum { |
| 637 |
SW_COLOR_ATTACHMENT = GL_COLOR_ATTACHMENT0, |
| 638 |
SW_DEPTH_ATTACHMENT = GL_DEPTH_ATTACHMENT, |
| 639 |
} SWattachment; |
| 640 |
|
| 641 |
typedef enum { |
| 642 |
SW_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, |
| 643 |
SW_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, |
| 644 |
} SWattachget; |
| 645 |
|
| 646 |
typedef enum { |
| 647 |
SW_FRAMEBUFFER_COMPLETE = GL_FRAMEBUFFER_COMPLETE, |
| 648 |
SW_FRAMEBUFFER_INCOMPLETE_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT, |
| 649 |
SW_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT, |
| 650 |
SW_FRAMEBUFFER_INCOMPLETE_DIMENSIONS = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS, |
| 651 |
} SWfbstatus; |
| 652 |
|
| 653 |
typedef enum { |
| 654 |
SW_NO_ERROR = GL_NO_ERROR, |
| 655 |
SW_INVALID_ENUM = GL_INVALID_ENUM, |
| 656 |
SW_INVALID_VALUE = GL_INVALID_VALUE, |
| 657 |
SW_STACK_OVERFLOW = GL_STACK_OVERFLOW, |
| 658 |
SW_STACK_UNDERFLOW = GL_STACK_UNDERFLOW, |
| 659 |
SW_INVALID_OPERATION = GL_INVALID_OPERATION, |
| 660 |
SW_OUT_OF_MEMORY = GL_OUT_OF_MEMORY, |
| 661 |
} SWerrcode; |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
SWAPI bool swInit(int w, int h); |
| 667 |
SWAPI void swClose(void); |
| 668 |
|
| 669 |
SWAPI bool swResize(int w, int h); |
| 670 |
SWAPI void swReadPixels(int x, int y, int w, int h, SWformat format, SWtype type, void *pixels); |
| 671 |
SWAPI void swBlitPixels(int xDst, int yDst, int wDst, int hDst, int xSrc, int ySrc, int wSrc, int hSrc, SWformat format, SWtype type, void *pixels); |
| 672 |
SWAPI void *swGetColorBuffer(int *width, int *height); |
| 673 |
|
| 674 |
SWAPI void swEnable(SWstate state); |
| 675 |
SWAPI void swDisable(SWstate state); |
| 676 |
|
| 677 |
SWAPI void swGetFloatv(SWget name, float *v); |
| 678 |
SWAPI const char *swGetString(SWget name); |
| 679 |
SWAPI SWerrcode swGetError(void); |
| 680 |
|
| 681 |
SWAPI void swViewport(int x, int y, int width, int height); |
| 682 |
SWAPI void swScissor(int x, int y, int width, int height); |
| 683 |
|
| 684 |
SWAPI void swClearColor(float r, float g, float b, float a); |
| 685 |
SWAPI void swClearDepth(float depth); |
| 686 |
SWAPI void swClear(uint32_t bitmask); |
| 687 |
|
| 688 |
SWAPI void swBlendFunc(SWfactor sfactor, SWfactor dfactor); |
| 689 |
SWAPI void swPolygonMode(SWpoly mode); |
| 690 |
SWAPI void swCullFace(SWface face); |
| 691 |
|
| 692 |
SWAPI void swPointSize(float size); |
| 693 |
SWAPI void swLineWidth(float width); |
| 694 |
|
| 695 |
SWAPI void swMatrixMode(SWmatrix mode); |
| 696 |
SWAPI void swPushMatrix(void); |
| 697 |
SWAPI void swPopMatrix(void); |
| 698 |
SWAPI void swLoadIdentity(void); |
| 699 |
SWAPI void swTranslatef(float x, float y, float z); |
| 700 |
SWAPI void swRotatef(float angle, float x, float y, float z); |
| 701 |
SWAPI void swScalef(float x, float y, float z); |
| 702 |
SWAPI void swMultMatrixf(const float *mat); |
| 703 |
SWAPI void swFrustum(double left, double right, double bottom, double top, double znear, double zfar); |
| 704 |
SWAPI void swOrtho(double left, double right, double bottom, double top, double znear, double zfar); |
| 705 |
|
| 706 |
SWAPI void swBegin(SWdraw mode); |
| 707 |
SWAPI void swEnd(void); |
| 708 |
|
| 709 |
SWAPI void swVertex2i(int x, int y); |
| 710 |
SWAPI void swVertex2f(float x, float y); |
| 711 |
SWAPI void swVertex2fv(const float *v); |
| 712 |
SWAPI void swVertex3i(int x, int y, int z); |
| 713 |
SWAPI void swVertex3f(float x, float y, float z); |
| 714 |
SWAPI void swVertex3fv(const float *v); |
| 715 |
SWAPI void swVertex4i(int x, int y, int z, int w); |
| 716 |
SWAPI void swVertex4f(float x, float y, float z, float w); |
| 717 |
SWAPI void swVertex4fv(const float *v); |
| 718 |
|
| 719 |
SWAPI void swColor3ub(uint8_t r, uint8_t g, uint8_t b); |
| 720 |
SWAPI void swColor3ubv(const uint8_t *v); |
| 721 |
SWAPI void swColor3f(float r, float g, float b); |
| 722 |
SWAPI void swColor3fv(const float *v); |
| 723 |
SWAPI void swColor4ub(uint8_t r, uint8_t g, uint8_t b, uint8_t a); |
| 724 |
SWAPI void swColor4ubv(const uint8_t *v); |
| 725 |
SWAPI void swColor4f(float r, float g, float b, float a); |
| 726 |
SWAPI void swColor4fv(const float *v); |
| 727 |
|
| 728 |
SWAPI void swTexCoord2f(float u, float v); |
| 729 |
SWAPI void swTexCoord2fv(const float *v); |
| 730 |
|
| 731 |
SWAPI void swBindArray(SWarray type, void *buffer); |
| 732 |
SWAPI void swDrawArrays(SWdraw mode, int offset, int count); |
| 733 |
SWAPI void swDrawElements(SWdraw mode, int count, int type, const void *indices); |
| 734 |
|
| 735 |
SWAPI void swGenTextures(int count, uint32_t *textures); |
| 736 |
SWAPI void swDeleteTextures(int count, uint32_t *textures); |
| 737 |
SWAPI void swBindTexture(uint32_t id); |
| 738 |
SWAPI void swTexImage2D(int width, int height, SWformat format, SWtype type, const void *data); |
| 739 |
SWAPI void swTexSubImage2D(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); |
| 740 |
SWAPI void swTexParameteri(int param, int value); |
| 741 |
|
| 742 |
SWAPI void swGenFramebuffers(int count, uint32_t *framebuffers); |
| 743 |
SWAPI void swDeleteFramebuffers(int count, uint32_t *framebuffers); |
| 744 |
SWAPI void swBindFramebuffer(uint32_t id); |
| 745 |
SWAPI void swFramebufferTexture2D(SWattachment attach, uint32_t texture); |
| 746 |
SWAPI SWfbstatus swCheckFramebufferStatus(void); |
| 747 |
SWAPI void swGetFramebufferAttachmentParameteriv(SWattachment attachment, SWattachget property, int *v); |
| 748 |
|
| 749 |
#endif |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
#if defined(RLSW_IMPLEMENTATION) |
| 757 |
|
| 758 |
#undef RLSW_IMPLEMENTATION // Undef to allow template expanding without implementation redefinition |
| 759 |
|
| 760 |
#include <stdlib.h> |
| 761 |
#include <stddef.h> |
| 762 |
#include <math.h> |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
#define SW_SUPPORT_LOG_INFO |
| 767 |
#if defined(SW_SUPPORT_LOG_INFO) |
| 768 |
#include <stdio.h> |
| 769 |
#define SW_LOG(...) printf(__VA_ARGS__) |
| 770 |
#else |
| 771 |
#define SW_LOG(...) |
| 772 |
#endif |
| 773 |
|
| 774 |
#if defined(_MSC_VER) |
| 775 |
#define SW_ALIGN(x) __declspec(align(x)) |
| 776 |
#elif defined(__GNUC__) || defined(__clang__) |
| 777 |
#define SW_ALIGN(x) __attribute__((aligned(x))) |
| 778 |
#else |
| 779 |
#define SW_ALIGN(x) |
| 780 |
#endif |
| 781 |
|
| 782 |
#if defined(_M_X64) || defined(__x86_64__) |
| 783 |
#define SW_ARCH_X86_64 |
| 784 |
#elif defined(_M_IX86) || defined(__i386__) |
| 785 |
#define SW_ARCH_X86 |
| 786 |
#elif defined(_M_ARM) || defined(__arm__) |
| 787 |
#define SW_ARCH_ARM32 |
| 788 |
#elif defined(_M_ARM64) || defined(__aarch64__) |
| 789 |
#define SW_ARCH_ARM64 |
| 790 |
#elif defined(__riscv) |
| 791 |
#define SW_ARCH_RISCV |
| 792 |
#endif |
| 793 |
|
| 794 |
#if RLSW_USE_SIMD_INTRINSICS |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
#if defined(__FMA__) && defined(__AVX2__) |
| 801 |
#define SW_HAS_FMA_AVX2 |
| 802 |
#include <immintrin.h> |
| 803 |
#elif defined(__FMA__) && defined(__AVX__) |
| 804 |
#define SW_HAS_FMA_AVX |
| 805 |
#include <immintrin.h> |
| 806 |
#elif defined(__AVX2__) |
| 807 |
#define SW_HAS_AVX2 |
| 808 |
#include <immintrin.h> |
| 809 |
#elif defined(__AVX__) |
| 810 |
#define SW_HAS_AVX |
| 811 |
#include <immintrin.h> |
| 812 |
#endif |
| 813 |
#if defined(__SSE4_2__) |
| 814 |
#define SW_HAS_SSE42 |
| 815 |
#include <nmmintrin.h> |
| 816 |
#elif defined(__SSE4_1__) |
| 817 |
#define SW_HAS_SSE41 |
| 818 |
#include <smmintrin.h> |
| 819 |
#elif defined(__SSSE3__) |
| 820 |
#define SW_HAS_SSSE3 |
| 821 |
#include <tmmintrin.h> |
| 822 |
#elif defined(__SSE3__) |
| 823 |
#define SW_HAS_SSE3 |
| 824 |
#include <pmmintrin.h> |
| 825 |
#elif defined(__SSE2__) || (defined(_M_AMD64) || defined(_M_X64)) |
| 826 |
#define SW_HAS_SSE2 |
| 827 |
#include <emmintrin.h> |
| 828 |
#elif defined(__SSE__) |
| 829 |
#define SW_HAS_SSE |
| 830 |
#include <xmmintrin.h> |
| 831 |
#endif |
| 832 |
#if defined(__ARM_NEON) || defined(__aarch64__) |
| 833 |
#if defined(__ARM_FEATURE_FMA) |
| 834 |
#define SW_HAS_NEON_FMA |
| 835 |
#else |
| 836 |
#define SW_HAS_NEON |
| 837 |
#endif |
| 838 |
#include <arm_neon.h> |
| 839 |
#endif |
| 840 |
#if defined(__riscv_vector) |
| 841 |
|
| 842 |
#define SW_HAS_RVV |
| 843 |
#include <riscv_vector.h> |
| 844 |
#endif |
| 845 |
#endif |
| 846 |
|
| 847 |
#ifdef __cplusplus |
| 848 |
#define SW_CURLY_INIT(name) name |
| 849 |
#else |
| 850 |
#define SW_CURLY_INIT(name) (name) |
| 851 |
#endif |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
#define SW_PI 3.14159265358979323846f |
| 857 |
#define SW_INV_255 0.00392156862745098f // 1.0f/255.0f |
| 858 |
#define SW_DEG2RAD (SW_PI/180.0f) |
| 859 |
#define SW_RAD2DEG (180.0f/SW_PI) |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
#define SW_MAX_CLIPPED_POLYGON_VERTICES 14 |
| 866 |
#define SW_CLIP_EPSILON 1e-4f |
| 867 |
|
| 868 |
#define SW_HANDLE_NULL 0u |
| 869 |
#define SW_POOL_SLOT_LIVE 0x80u // bit7 of the generation byte |
| 870 |
#define SW_POOL_SLOT_VER_MASK 0x7Fu // bits6:0 = anti-ABA counter |
| 871 |
|
| 872 |
#define SW_CONCAT(a, b) a##b |
| 873 |
#define SW_CONCATX(a, b) SW_CONCAT(a, b) |
| 874 |
|
| 875 |
#define SW_FRAMEBUFFER_COLOR8_GET(c,p,o) SW_CONCATX(sw_pixel_read_color8_, SW_FRAMEBUFFER_COLOR_TYPE)((c),(p),(o)) |
| 876 |
#define SW_FRAMEBUFFER_COLOR_GET(c,p,o) SW_CONCATX(sw_pixel_read_color_, SW_FRAMEBUFFER_COLOR_TYPE)((c),(p),(o)) |
| 877 |
#define SW_FRAMEBUFFER_COLOR_SET(p,c,o) SW_CONCATX(sw_pixel_write_color_, SW_FRAMEBUFFER_COLOR_TYPE)((p),(c),(o)) |
| 878 |
|
| 879 |
#define SW_FRAMEBUFFER_DEPTH_GET(p,o) SW_CONCATX(sw_pixel_read_depth_, SW_FRAMEBUFFER_DEPTH_TYPE)((p),(o)) |
| 880 |
#define SW_FRAMEBUFFER_DEPTH_SET(p,d,o) SW_CONCATX(sw_pixel_write_depth_, SW_FRAMEBUFFER_DEPTH_TYPE)((p),(d),(o)) |
| 881 |
|
| 882 |
#define SW_FRAMEBUFFER_COLOR_FORMAT SW_CONCATX(SW_PIXELFORMAT_COLOR_, SW_FRAMEBUFFER_COLOR_TYPE) |
| 883 |
#define SW_FRAMEBUFFER_DEPTH_FORMAT SW_CONCATX(SW_PIXELFORMAT_DEPTH_, SW_FRAMEBUFFER_DEPTH_TYPE) |
| 884 |
|
| 885 |
#define SW_FRAMEBUFFER_COLOR_SIZE SW_PIXELFORMAT_SIZE[SW_FRAMEBUFFER_COLOR_FORMAT] |
| 886 |
#define SW_FRAMEBUFFER_DEPTH_SIZE SW_PIXELFORMAT_SIZE[SW_FRAMEBUFFER_DEPTH_FORMAT] |
| 887 |
|
| 888 |
#define SW_STATE_SCISSOR_TEST (1 << 0) |
| 889 |
#define SW_STATE_TEXTURE_2D (1 << 1) |
| 890 |
#define SW_STATE_DEPTH_TEST (1 << 2) |
| 891 |
#define SW_STATE_CULL_FACE (1 << 3) |
| 892 |
#define SW_STATE_BLEND (1 << 4) |
| 893 |
|
| 894 |
#define SW_BLEND_FLAG_NOOP (1 << 0) |
| 895 |
#define SW_BLEND_FLAG_NEEDS_ALPHA (1 << 1) |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
typedef uint32_t sw_handle_t; |
| 902 |
typedef uint16_t sw_half_t; |
| 903 |
|
| 904 |
|
| 905 |
typedef enum { |
| 906 |
SW_PIXELFORMAT_UNKNOWN = 0, |
| 907 |
SW_PIXELFORMAT_COLOR_GRAYSCALE, |
| 908 |
SW_PIXELFORMAT_COLOR_GRAYALPHA, |
| 909 |
SW_PIXELFORMAT_COLOR_R3G3B2, |
| 910 |
SW_PIXELFORMAT_COLOR_R5G6B5, |
| 911 |
SW_PIXELFORMAT_COLOR_R8G8B8, |
| 912 |
SW_PIXELFORMAT_COLOR_R5G5B5A1, |
| 913 |
SW_PIXELFORMAT_COLOR_R4G4B4A4, |
| 914 |
SW_PIXELFORMAT_COLOR_R8G8B8A8, |
| 915 |
SW_PIXELFORMAT_COLOR_R32, |
| 916 |
SW_PIXELFORMAT_COLOR_R32G32B32, |
| 917 |
SW_PIXELFORMAT_COLOR_R32G32B32A32, |
| 918 |
SW_PIXELFORMAT_COLOR_R16, |
| 919 |
SW_PIXELFORMAT_COLOR_R16G16B16, |
| 920 |
SW_PIXELFORMAT_COLOR_R16G16B16A16, |
| 921 |
SW_PIXELFORMAT_DEPTH_D8, |
| 922 |
SW_PIXELFORMAT_DEPTH_D16, |
| 923 |
SW_PIXELFORMAT_DEPTH_D32, |
| 924 |
SW_PIXELFORMAT_COUNT |
| 925 |
} sw_pixelformat_t; |
| 926 |
|
| 927 |
typedef enum { |
| 928 |
SW_PIXEL_ALPHA_NONE = 0, |
| 929 |
SW_PIXEL_ALPHA_BIN, |
| 930 |
SW_PIXEL_ALPHA_YES, |
| 931 |
} sw_pixel_alpha_t; |
| 932 |
|
| 933 |
|
| 934 |
typedef struct sw_vertex sw_vertex_t; |
| 935 |
|
| 936 |
|
| 937 |
typedef void (*sw_pixel_read_color8_f)(uint8_t *SW_RESTRICT, const void *SW_RESTRICT, uint32_t); |
| 938 |
typedef void (*sw_pixel_read_color_f)(float *SW_RESTRICT, const void *SW_RESTRICT, uint32_t); |
| 939 |
|
| 940 |
|
| 941 |
typedef void (*sw_pixel_write_color8_f)(void *SW_RESTRICT, const uint8_t *SW_RESTRICT, uint32_t); |
| 942 |
typedef void (*sw_pixel_write_color_f)(void *SW_RESTRICT, const float *SW_RESTRICT, uint32_t); |
| 943 |
|
| 944 |
|
| 945 |
typedef void (*sw_blend_f)(float *SW_RESTRICT, const float *SW_RESTRICT); |
| 946 |
|
| 947 |
|
| 948 |
typedef void (*sw_raster_triangle_f)(const sw_vertex_t*, const sw_vertex_t*, const sw_vertex_t*); |
| 949 |
typedef void (*sw_raster_quad_f)(const sw_vertex_t*, const sw_vertex_t*, const sw_vertex_t*, const sw_vertex_t*); |
| 950 |
typedef void (*sw_raster_line_f)(const sw_vertex_t*, const sw_vertex_t*); |
| 951 |
typedef void (*sw_raster_point_f)(const sw_vertex_t*); |
| 952 |
|
| 953 |
typedef float sw_matrix_t[4*4]; |
| 954 |
|
| 955 |
typedef struct sw_vertex { |
| 956 |
float position[4]; |
| 957 |
float color[4]; |
| 958 |
float texcoord[2]; |
| 959 |
} sw_vertex_t; |
| 960 |
|
| 961 |
typedef struct { |
| 962 |
void *pixels; |
| 963 |
sw_pixel_read_color8_f readColor8; |
| 964 |
sw_pixel_read_color_f readColor; |
| 965 |
sw_pixelformat_t format; |
| 966 |
sw_pixel_alpha_t alpha; |
| 967 |
int width, height; |
| 968 |
int wMinus1, hMinus1; |
| 969 |
int allocSz; |
| 970 |
SWfilter minFilter; |
| 971 |
SWfilter magFilter; |
| 972 |
SWwrap sWrap; |
| 973 |
SWwrap tWrap; |
| 974 |
float tx; |
| 975 |
float ty; |
| 976 |
} sw_texture_t; |
| 977 |
|
| 978 |
typedef struct { |
| 979 |
sw_texture_t color; |
| 980 |
sw_texture_t depth; |
| 981 |
} sw_default_framebuffer_t; |
| 982 |
|
| 983 |
typedef struct { |
| 984 |
sw_handle_t colorAttachment; |
| 985 |
sw_handle_t depthAttachment; |
| 986 |
} sw_framebuffer_t; |
| 987 |
|
| 988 |
typedef struct { |
| 989 |
void *data; |
| 990 |
uint8_t *gen; |
| 991 |
uint32_t *freeList; |
| 992 |
int freeCount; |
| 993 |
int watermark; |
| 994 |
int capacity; |
| 995 |
size_t stride; |
| 996 |
} sw_pool_t; |
| 997 |
|
| 998 |
|
| 999 |
typedef struct { |
| 1000 |
sw_default_framebuffer_t framebuffer; |
| 1001 |
float clearColor[4]; |
| 1002 |
float clearDepth; |
| 1003 |
|
| 1004 |
float vpCenter[2]; |
| 1005 |
float vpHalf[2]; |
| 1006 |
int vpSize[2]; |
| 1007 |
|
| 1008 |
int scMin[2]; |
| 1009 |
int scMax[2]; |
| 1010 |
float scClipMin[2]; |
| 1011 |
float scClipMax[2]; |
| 1012 |
|
| 1013 |
struct { |
| 1014 |
sw_vertex_t buffer[SW_MAX_CLIPPED_POLYGON_VERTICES]; |
| 1015 |
int vertexCount; |
| 1016 |
float color[4]; |
| 1017 |
float texcoord[2]; |
| 1018 |
bool hasColorAlpha; |
| 1019 |
} primitive; |
| 1020 |
|
| 1021 |
struct { |
| 1022 |
float *positions; |
| 1023 |
float *texcoords; |
| 1024 |
uint8_t *colors; |
| 1025 |
} array; |
| 1026 |
|
| 1027 |
SWdraw drawMode; |
| 1028 |
SWpoly polyMode; |
| 1029 |
float pointRadius; |
| 1030 |
float lineWidth; |
| 1031 |
|
| 1032 |
sw_matrix_t stackProjection[SW_MAX_PROJECTION_STACK_SIZE]; |
| 1033 |
sw_matrix_t stackModelview[SW_MAX_MODELVIEW_STACK_SIZE]; |
| 1034 |
sw_matrix_t stackTexture[SW_MAX_TEXTURE_STACK_SIZE]; |
| 1035 |
uint32_t stackProjectionCounter; |
| 1036 |
uint32_t stackModelviewCounter; |
| 1037 |
uint32_t stackTextureCounter; |
| 1038 |
SWmatrix currentMatrixMode; |
| 1039 |
sw_matrix_t *currentMatrix; |
| 1040 |
sw_matrix_t matMVP; |
| 1041 |
bool isDirtyMVP; |
| 1042 |
|
| 1043 |
sw_handle_t boundFramebufferId; |
| 1044 |
sw_texture_t *colorBuffer; |
| 1045 |
sw_texture_t *depthBuffer; |
| 1046 |
sw_pool_t framebufferPool; |
| 1047 |
|
| 1048 |
sw_texture_t *boundTexture; |
| 1049 |
sw_pool_t texturePool; |
| 1050 |
|
| 1051 |
SWfactor srcFactor; |
| 1052 |
SWfactor dstFactor; |
| 1053 |
uint32_t blendFlags; |
| 1054 |
sw_blend_f blendFunc; |
| 1055 |
|
| 1056 |
SWface cullFace; |
| 1057 |
SWerrcode errCode; |
| 1058 |
|
| 1059 |
uint32_t userState; |
| 1060 |
uint32_t rasterState; |
| 1061 |
} sw_context_t; |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
static sw_context_t RLSW = { 0 }; |
| 1067 |
|
| 1068 |
#if SW_USE_COLOR_LUT |
| 1069 |
static float SW_LUT_UINT8_TO_FLOAT[256] = { 0 }; |
| 1070 |
#endif |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
static const sw_pixel_alpha_t SW_PIXELFORMAT_ALPHA[SW_PIXELFORMAT_COUNT] = |
| 1077 |
{ |
| 1078 |
[SW_PIXELFORMAT_COLOR_GRAYSCALE] = SW_PIXEL_ALPHA_NONE, |
| 1079 |
[SW_PIXELFORMAT_COLOR_GRAYALPHA] = SW_PIXEL_ALPHA_YES, |
| 1080 |
[SW_PIXELFORMAT_COLOR_R3G3B2] = SW_PIXEL_ALPHA_NONE, |
| 1081 |
[SW_PIXELFORMAT_COLOR_R5G6B5] = SW_PIXEL_ALPHA_NONE, |
| 1082 |
[SW_PIXELFORMAT_COLOR_R8G8B8] = SW_PIXEL_ALPHA_NONE, |
| 1083 |
[SW_PIXELFORMAT_COLOR_R5G5B5A1] = SW_PIXEL_ALPHA_BIN, |
| 1084 |
[SW_PIXELFORMAT_COLOR_R4G4B4A4] = SW_PIXEL_ALPHA_YES, |
| 1085 |
[SW_PIXELFORMAT_COLOR_R8G8B8A8] = SW_PIXEL_ALPHA_YES, |
| 1086 |
[SW_PIXELFORMAT_COLOR_R32] = SW_PIXEL_ALPHA_NONE, |
| 1087 |
[SW_PIXELFORMAT_COLOR_R32G32B32] = SW_PIXEL_ALPHA_NONE, |
| 1088 |
[SW_PIXELFORMAT_COLOR_R32G32B32A32] = SW_PIXEL_ALPHA_YES, |
| 1089 |
[SW_PIXELFORMAT_COLOR_R16] = SW_PIXEL_ALPHA_NONE, |
| 1090 |
[SW_PIXELFORMAT_COLOR_R16G16B16] = SW_PIXEL_ALPHA_NONE, |
| 1091 |
[SW_PIXELFORMAT_COLOR_R16G16B16A16] = SW_PIXEL_ALPHA_YES, |
| 1092 |
[SW_PIXELFORMAT_DEPTH_D8] = SW_PIXEL_ALPHA_NONE, |
| 1093 |
[SW_PIXELFORMAT_DEPTH_D16] = SW_PIXEL_ALPHA_NONE, |
| 1094 |
[SW_PIXELFORMAT_DEPTH_D32] = SW_PIXEL_ALPHA_NONE, |
| 1095 |
}; |
| 1096 |
|
| 1097 |
|
| 1098 |
static const int SW_PIXELFORMAT_SIZE[SW_PIXELFORMAT_COUNT] = |
| 1099 |
{ |
| 1100 |
[SW_PIXELFORMAT_COLOR_GRAYSCALE] = 1, |
| 1101 |
[SW_PIXELFORMAT_COLOR_GRAYALPHA] = 2, |
| 1102 |
[SW_PIXELFORMAT_COLOR_R3G3B2] = 1, |
| 1103 |
[SW_PIXELFORMAT_COLOR_R5G6B5] = 2, |
| 1104 |
[SW_PIXELFORMAT_COLOR_R8G8B8] = 3, |
| 1105 |
[SW_PIXELFORMAT_COLOR_R5G5B5A1] = 2, |
| 1106 |
[SW_PIXELFORMAT_COLOR_R4G4B4A4] = 2, |
| 1107 |
[SW_PIXELFORMAT_COLOR_R8G8B8A8] = 4, |
| 1108 |
[SW_PIXELFORMAT_COLOR_R32] = 4, |
| 1109 |
[SW_PIXELFORMAT_COLOR_R32G32B32] = 12, |
| 1110 |
[SW_PIXELFORMAT_COLOR_R32G32B32A32] = 16, |
| 1111 |
[SW_PIXELFORMAT_COLOR_R16] = 2, |
| 1112 |
[SW_PIXELFORMAT_COLOR_R16G16B16] = 6, |
| 1113 |
[SW_PIXELFORMAT_COLOR_R16G16B16A16] = 8, |
| 1114 |
[SW_PIXELFORMAT_DEPTH_D8] = 1, |
| 1115 |
[SW_PIXELFORMAT_DEPTH_D16] = 2, |
| 1116 |
[SW_PIXELFORMAT_DEPTH_D32] = 4, |
| 1117 |
}; |
| 1118 |
|
| 1119 |
static const int SW_PRIMITIVE_VERTEX_COUNT[] = |
| 1120 |
{ |
| 1121 |
|
| 1122 |
[SW_POINTS] = 1, |
| 1123 |
[SW_LINES] = 2, |
| 1124 |
[SW_TRIANGLES] = 3, |
| 1125 |
[SW_QUADS] = 4, |
| 1126 |
}; |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
static inline void sw_matrix_id(sw_matrix_t dst) |
| 1135 |
{ |
| 1136 |
dst[0] = 1, dst[1] = 0, dst[2] = 0, dst[3] = 0; |
| 1137 |
dst[4] = 0, dst[5] = 1, dst[6] = 0, dst[7] = 0; |
| 1138 |
dst[8] = 0, dst[9] = 0, dst[10] = 1, dst[11] = 0; |
| 1139 |
dst[12] = 0, dst[13] = 0, dst[14] = 0, dst[15] = 1; |
| 1140 |
} |
| 1141 |
|
| 1142 |
static inline void sw_matrix_mul_rst(float *SW_RESTRICT dst, const float *SW_RESTRICT left, const float *SW_RESTRICT right) |
| 1143 |
{ |
| 1144 |
float l00 = left[0], l01 = left[1], l02 = left[2], l03 = left[3]; |
| 1145 |
float l10 = left[4], l11 = left[5], l12 = left[6], l13 = left[7]; |
| 1146 |
float l20 = left[8], l21 = left[9], l22 = left[10], l23 = left[11]; |
| 1147 |
float l30 = left[12], l31 = left[13], l32 = left[14], l33 = left[15]; |
| 1148 |
|
| 1149 |
dst[0] = l00*right[0] + l01*right[4] + l02*right[8] + l03*right[12]; |
| 1150 |
dst[4] = l10*right[0] + l11*right[4] + l12*right[8] + l13*right[12]; |
| 1151 |
dst[8] = l20*right[0] + l21*right[4] + l22*right[8] + l23*right[12]; |
| 1152 |
dst[12] = l30*right[0] + l31*right[4] + l32*right[8] + l33*right[12]; |
| 1153 |
|
| 1154 |
dst[1] = l00*right[1] + l01*right[5] + l02*right[9] + l03*right[13]; |
| 1155 |
dst[5] = l10*right[1] + l11*right[5] + l12*right[9] + l13*right[13]; |
| 1156 |
dst[9] = l20*right[1] + l21*right[5] + l22*right[9] + l23*right[13]; |
| 1157 |
dst[13] = l30*right[1] + l31*right[5] + l32*right[9] + l33*right[13]; |
| 1158 |
|
| 1159 |
dst[2] = l00*right[2] + l01*right[6] + l02*right[10] + l03*right[14]; |
| 1160 |
dst[6] = l10*right[2] + l11*right[6] + l12*right[10] + l13*right[14]; |
| 1161 |
dst[10] = l20*right[2] + l21*right[6] + l22*right[10] + l23*right[14]; |
| 1162 |
dst[14] = l30*right[2] + l31*right[6] + l32*right[10] + l33*right[14]; |
| 1163 |
|
| 1164 |
dst[3] = l00*right[3] + l01*right[7] + l02*right[11] + l03*right[15]; |
| 1165 |
dst[7] = l10*right[3] + l11*right[7] + l12*right[11] + l13*right[15]; |
| 1166 |
dst[11] = l20*right[3] + l21*right[7] + l22*right[11] + l23*right[15]; |
| 1167 |
dst[15] = l30*right[3] + l31*right[7] + l32*right[11] + l33*right[15]; |
| 1168 |
} |
| 1169 |
|
| 1170 |
static inline void sw_matrix_mul(sw_matrix_t dst, const sw_matrix_t left, const sw_matrix_t right) |
| 1171 |
{ |
| 1172 |
float result[16]; |
| 1173 |
sw_matrix_mul_rst(result, left, right); |
| 1174 |
for (int i = 0; i < 16; i++) dst[i] = result[i]; |
| 1175 |
} |
| 1176 |
|
| 1177 |
static inline int sw_clamp_int(int v, int min, int max) |
| 1178 |
{ |
| 1179 |
if (v < min) return min; |
| 1180 |
if (v > max) return max; |
| 1181 |
return v; |
| 1182 |
} |
| 1183 |
|
| 1184 |
static inline int sw_clamp(float v, float min, float max) |
| 1185 |
{ |
| 1186 |
if (v < min) return min; |
| 1187 |
if (v > max) return max; |
| 1188 |
return v; |
| 1189 |
} |
| 1190 |
|
| 1191 |
static inline float sw_saturate(float x) |
| 1192 |
{ |
| 1193 |
union { float f; uint32_t u; } fb; |
| 1194 |
fb.f = x; |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
if ((fb.u & 0x80000000) != 0) return 0.0f; |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
if (fb.u > 0x3F800000) return 1.0f; |
| 1203 |
|
| 1204 |
|
| 1205 |
return x; |
| 1206 |
} |
| 1207 |
|
| 1208 |
static inline float sw_fract(float x) |
| 1209 |
{ |
| 1210 |
return (x - floorf(x)); |
| 1211 |
} |
| 1212 |
|
| 1213 |
static inline uint8_t sw_luminance8(const uint8_t *color) |
| 1214 |
{ |
| 1215 |
return (uint8_t)((color[0]*77 + color[1]*150 + color[2]*29) >> 8); |
| 1216 |
} |
| 1217 |
|
| 1218 |
static inline float sw_luminance(const float *color) |
| 1219 |
{ |
| 1220 |
return color[0]*0.299f + color[1]*0.587f + color[2]*0.114f; |
| 1221 |
} |
| 1222 |
|
| 1223 |
static inline void sw_lerp_vertex_PCT(sw_vertex_t *SW_RESTRICT out, const sw_vertex_t *SW_RESTRICT a, const sw_vertex_t *SW_RESTRICT b, float t) |
| 1224 |
{ |
| 1225 |
const float tInv = 1.0f - t; |
| 1226 |
|
| 1227 |
|
| 1228 |
out->position[0] = a->position[0]*tInv + b->position[0]*t; |
| 1229 |
out->position[1] = a->position[1]*tInv + b->position[1]*t; |
| 1230 |
out->position[2] = a->position[2]*tInv + b->position[2]*t; |
| 1231 |
out->position[3] = a->position[3]*tInv + b->position[3]*t; |
| 1232 |
|
| 1233 |
|
| 1234 |
out->color[0] = a->color[0]*tInv + b->color[0]*t; |
| 1235 |
out->color[1] = a->color[1]*tInv + b->color[1]*t; |
| 1236 |
out->color[2] = a->color[2]*tInv + b->color[2]*t; |
| 1237 |
out->color[3] = a->color[3]*tInv + b->color[3]*t; |
| 1238 |
|
| 1239 |
|
| 1240 |
out->texcoord[0] = a->texcoord[0]*tInv + b->texcoord[0]*t; |
| 1241 |
out->texcoord[1] = a->texcoord[1]*tInv + b->texcoord[1]*t; |
| 1242 |
} |
| 1243 |
|
| 1244 |
static inline void sw_get_vertex_grad_PCT(sw_vertex_t *SW_RESTRICT out, const sw_vertex_t *SW_RESTRICT a, const sw_vertex_t *SW_RESTRICT b, float scale) |
| 1245 |
{ |
| 1246 |
|
| 1247 |
out->position[0] = (b->position[0] - a->position[0])*scale; |
| 1248 |
out->position[1] = (b->position[1] - a->position[1])*scale; |
| 1249 |
out->position[2] = (b->position[2] - a->position[2])*scale; |
| 1250 |
out->position[3] = (b->position[3] - a->position[3])*scale; |
| 1251 |
|
| 1252 |
|
| 1253 |
out->color[0] = (b->color[0] - a->color[0])*scale; |
| 1254 |
out->color[1] = (b->color[1] - a->color[1])*scale; |
| 1255 |
out->color[2] = (b->color[2] - a->color[2])*scale; |
| 1256 |
out->color[3] = (b->color[3] - a->color[3])*scale; |
| 1257 |
|
| 1258 |
|
| 1259 |
out->texcoord[0] = (b->texcoord[0] - a->texcoord[0])*scale; |
| 1260 |
out->texcoord[1] = (b->texcoord[1] - a->texcoord[1])*scale; |
| 1261 |
} |
| 1262 |
|
| 1263 |
static inline void sw_add_vertex_grad_PCT(sw_vertex_t *SW_RESTRICT out, const sw_vertex_t *SW_RESTRICT gradients) |
| 1264 |
{ |
| 1265 |
|
| 1266 |
out->position[0] += gradients->position[0]; |
| 1267 |
out->position[1] += gradients->position[1]; |
| 1268 |
out->position[2] += gradients->position[2]; |
| 1269 |
out->position[3] += gradients->position[3]; |
| 1270 |
|
| 1271 |
|
| 1272 |
out->color[0] += gradients->color[0]; |
| 1273 |
out->color[1] += gradients->color[1]; |
| 1274 |
out->color[2] += gradients->color[2]; |
| 1275 |
out->color[3] += gradients->color[3]; |
| 1276 |
|
| 1277 |
|
| 1278 |
out->texcoord[0] += gradients->texcoord[0]; |
| 1279 |
out->texcoord[1] += gradients->texcoord[1]; |
| 1280 |
} |
| 1281 |
|
| 1282 |
static inline void sw_add_vertex_grad_scaled_PCT(sw_vertex_t *SW_RESTRICT out, const sw_vertex_t *SW_RESTRICT gradients, float scale) |
| 1283 |
{ |
| 1284 |
|
| 1285 |
out->position[0] += gradients->position[0]*scale; |
| 1286 |
out->position[1] += gradients->position[1]*scale; |
| 1287 |
out->position[2] += gradients->position[2]*scale; |
| 1288 |
out->position[3] += gradients->position[3]*scale; |
| 1289 |
|
| 1290 |
|
| 1291 |
out->color[0] += gradients->color[0]*scale; |
| 1292 |
out->color[1] += gradients->color[1]*scale; |
| 1293 |
out->color[2] += gradients->color[2]*scale; |
| 1294 |
out->color[3] += gradients->color[3]*scale; |
| 1295 |
|
| 1296 |
|
| 1297 |
out->texcoord[0] += gradients->texcoord[0]*scale; |
| 1298 |
out->texcoord[1] += gradients->texcoord[1]*scale; |
| 1299 |
} |
| 1300 |
|
| 1301 |
static inline void sw_get_vertex_grad_PC(sw_vertex_t *SW_RESTRICT out, const sw_vertex_t *SW_RESTRICT a, const sw_vertex_t *SW_RESTRICT b, float scale) |
| 1302 |
{ |
| 1303 |
|
| 1304 |
out->position[0] = (b->position[0] - a->position[0])*scale; |
| 1305 |
out->position[1] = (b->position[1] - a->position[1])*scale; |
| 1306 |
out->position[2] = (b->position[2] - a->position[2])*scale; |
| 1307 |
out->position[3] = (b->position[3] - a->position[3])*scale; |
| 1308 |
|
| 1309 |
|
| 1310 |
out->color[0] = (b->color[0] - a->color[0])*scale; |
| 1311 |
out->color[1] = (b->color[1] - a->color[1])*scale; |
| 1312 |
out->color[2] = (b->color[2] - a->color[2])*scale; |
| 1313 |
out->color[3] = (b->color[3] - a->color[3])*scale; |
| 1314 |
} |
| 1315 |
|
| 1316 |
static inline void sw_add_vertex_grad_PC(sw_vertex_t *SW_RESTRICT out, const sw_vertex_t *SW_RESTRICT gradients) |
| 1317 |
{ |
| 1318 |
|
| 1319 |
out->position[0] += gradients->position[0]; |
| 1320 |
out->position[1] += gradients->position[1]; |
| 1321 |
out->position[2] += gradients->position[2]; |
| 1322 |
out->position[3] += gradients->position[3]; |
| 1323 |
|
| 1324 |
|
| 1325 |
out->color[0] += gradients->color[0]; |
| 1326 |
out->color[1] += gradients->color[1]; |
| 1327 |
out->color[2] += gradients->color[2]; |
| 1328 |
out->color[3] += gradients->color[3]; |
| 1329 |
} |
| 1330 |
|
| 1331 |
static inline void sw_add_vertex_grad_scaled_PC(sw_vertex_t *SW_RESTRICT out, const sw_vertex_t *SW_RESTRICT gradients, float scale) |
| 1332 |
{ |
| 1333 |
|
| 1334 |
out->position[0] += gradients->position[0]*scale; |
| 1335 |
out->position[1] += gradients->position[1]*scale; |
| 1336 |
out->position[2] += gradients->position[2]*scale; |
| 1337 |
out->position[3] += gradients->position[3]*scale; |
| 1338 |
|
| 1339 |
|
| 1340 |
out->color[0] += gradients->color[0]*scale; |
| 1341 |
out->color[1] += gradients->color[1]*scale; |
| 1342 |
out->color[2] += gradients->color[2]*scale; |
| 1343 |
out->color[3] += gradients->color[3]*scale; |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
static inline uint16_t sw_float_to_half_ui(uint32_t ui) |
| 1348 |
{ |
| 1349 |
int32_t s = (ui >> 16) & 0x8000; |
| 1350 |
int32_t em = ui & 0x7fffffff; |
| 1351 |
|
| 1352 |
|
| 1353 |
int32_t h = (em - (112 << 23) + (1 << 12)) >> 13; |
| 1354 |
|
| 1355 |
|
| 1356 |
h = (em < (113 << 23))? 0 : h; |
| 1357 |
|
| 1358 |
|
| 1359 |
h = (em >= (143 << 23))? 0x7c00 : h; |
| 1360 |
|
| 1361 |
|
| 1362 |
h = (em > (255 << 23))? 0x7e00 : h; |
| 1363 |
|
| 1364 |
return (uint16_t)(s | h); |
| 1365 |
} |
| 1366 |
|
| 1367 |
static inline uint32_t sw_half_to_float_ui(uint16_t h) |
| 1368 |
{ |
| 1369 |
uint32_t s = (unsigned)(h & 0x8000) << 16; |
| 1370 |
int32_t em = h & 0x7fff; |
| 1371 |
|
| 1372 |
|
| 1373 |
int32_t r = (em + (112 << 10)) << 13; |
| 1374 |
|
| 1375 |
|
| 1376 |
r = (em < (1 << 10))? 0 : r; |
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
r += (em >= (31 << 10))? (112 << 23) : 0; |
| 1381 |
|
| 1382 |
return s | r; |
| 1383 |
} |
| 1384 |
|
| 1385 |
static inline sw_half_t sw_float_to_half(float i) |
| 1386 |
{ |
| 1387 |
union { float f; uint32_t i; } v; |
| 1388 |
v.f = i; |
| 1389 |
return sw_float_to_half_ui(v.i); |
| 1390 |
} |
| 1391 |
|
| 1392 |
static inline float sw_half_to_float(sw_half_t y) |
| 1393 |
{ |
| 1394 |
union { float f; uint32_t i; } v; |
| 1395 |
v.i = sw_half_to_float_ui(y); |
| 1396 |
return v.f; |
| 1397 |
} |
| 1398 |
|
| 1399 |
static inline uint8_t sw_expand_1to8(uint32_t v) { return v? 255 : 0; } |
| 1400 |
static inline uint8_t sw_expand_2to8(uint32_t v) { return (uint8_t)(v*85); } |
| 1401 |
static inline uint8_t sw_expand_3to8(uint32_t v) { return (uint8_t)((v << 5) | (v << 2) | (v >> 1)); } |
| 1402 |
static inline uint8_t sw_expand_4to8(uint32_t v) { return (uint8_t)((v << 4) | v); } |
| 1403 |
static inline uint8_t sw_expand_5to8(uint32_t v) { return (uint8_t)((v << 3) | (v >> 2)); } |
| 1404 |
static inline uint8_t sw_expand_6to8(uint32_t v) { return (uint8_t)((v << 2) | (v >> 4)); } |
| 1405 |
|
| 1406 |
static inline uint32_t sw_compress_8to1(uint8_t v) { return v >> 7; } |
| 1407 |
static inline uint32_t sw_compress_8to2(uint8_t v) { return v >> 6; } |
| 1408 |
static inline uint32_t sw_compress_8to3(uint8_t v) { return v >> 5; } |
| 1409 |
static inline uint32_t sw_compress_8to4(uint8_t v) { return v >> 4; } |
| 1410 |
static inline uint32_t sw_compress_8to5(uint8_t v) { return v >> 3; } |
| 1411 |
static inline uint32_t sw_compress_8to6(uint8_t v) { return v >> 2; } |
| 1412 |
|
| 1413 |
static inline void sw_color8_to_color(float *SW_RESTRICT dst, const uint8_t *SW_RESTRICT src) |
| 1414 |
{ |
| 1415 |
#if defined(SW_HAS_NEON) |
| 1416 |
uint8x8_t bytes = vreinterpret_u8_u32(vld1_dup_u32((const uint32_t *)src)); |
| 1417 |
uint16x8_t words = vmovl_u8(bytes); |
| 1418 |
uint32x4_t dwords = vmovl_u16(vget_low_u16(words)); |
| 1419 |
float32x4_t fvals = vmulq_f32(vcvtq_f32_u32(dwords), vdupq_n_f32(SW_INV_255)); |
| 1420 |
vst1q_f32(dst, fvals); |
| 1421 |
|
| 1422 |
#elif defined(SW_HAS_SSE41) |
| 1423 |
__m128i bytes = _mm_loadu_si32(src); |
| 1424 |
__m128 fvals = _mm_mul_ps(_mm_cvtepi32_ps(_mm_cvtepu8_epi32(bytes)), _mm_set1_ps(SW_INV_255)); |
| 1425 |
_mm_storeu_ps(dst, fvals); |
| 1426 |
|
| 1427 |
#elif defined(SW_HAS_SSE2) |
| 1428 |
__m128i zero = _mm_setzero_si128(); |
| 1429 |
__m128i bytes = _mm_loadu_si32(src); |
| 1430 |
__m128i words = _mm_unpacklo_epi8(bytes, zero); |
| 1431 |
__m128i dwords = _mm_unpacklo_epi16(words, zero); |
| 1432 |
__m128 fvals = _mm_mul_ps(_mm_cvtepi32_ps(dwords), _mm_set1_ps(SW_INV_255)); |
| 1433 |
_mm_storeu_ps(dst, fvals); |
| 1434 |
|
| 1435 |
#elif defined(SW_HAS_RVV) |
| 1436 |
|
| 1437 |
size_t vl = __riscv_vsetvl_e8m1(4); |
| 1438 |
vuint8m1_t vsrc_u8 = __riscv_vle8_v_u8m1(src, vl); |
| 1439 |
vuint32m1_t vsrc_u32 = __riscv_vwcvt_xu_u_v_u32m1(vsrc_u8, vl); |
| 1440 |
vfloat32m1_t vsrc_f32 = __riscv_vfcvt_f_xu_v_f32m1(vsrc_u32, vl); |
| 1441 |
vfloat32m1_t vnorm = __riscv_vfmul_vf_f32m1(vsrc_f32, SW_INV_255, vl); |
| 1442 |
__riscv_vse32_v_f32m1(dst, vnorm, vl); |
| 1443 |
|
| 1444 |
#elif SW_USE_COLOR_LUT |
| 1445 |
dst[0] = SW_LUT_UINT8_TO_FLOAT[src[0]]; |
| 1446 |
dst[1] = SW_LUT_UINT8_TO_FLOAT[src[1]]; |
| 1447 |
dst[2] = SW_LUT_UINT8_TO_FLOAT[src[2]]; |
| 1448 |
dst[3] = SW_LUT_UINT8_TO_FLOAT[src[3]]; |
| 1449 |
|
| 1450 |
#else |
| 1451 |
dst[0] = (float)src[0]*SW_INV_255; |
| 1452 |
dst[1] = (float)src[1]*SW_INV_255; |
| 1453 |
dst[2] = (float)src[2]*SW_INV_255; |
| 1454 |
dst[3] = (float)src[3]*SW_INV_255; |
| 1455 |
#endif |
| 1456 |
} |
| 1457 |
|
| 1458 |
static inline void sw_color_to_color8(uint8_t *SW_RESTRICT dst, const float *SW_RESTRICT src) |
| 1459 |
{ |
| 1460 |
#if defined(SW_HAS_NEON) |
| 1461 |
float32x4_t fvals = vmulq_f32(vld1q_f32(src), vdupq_n_f32(255.0f)); |
| 1462 |
uint32x4_t i32 = vcvtq_u32_f32(fvals); |
| 1463 |
uint16x4_t i16 = vmovn_u32(i32); |
| 1464 |
uint8x8_t i8 = vmovn_u16(vcombine_u16(i16, i16)); |
| 1465 |
vst1_lane_u32((uint32_t *)dst, vreinterpret_u32_u8(i8), 0); |
| 1466 |
|
| 1467 |
#elif defined(SW_HAS_SSE41) |
| 1468 |
__m128 fvals = _mm_mul_ps(_mm_loadu_ps(src), _mm_set1_ps(255.0f)); |
| 1469 |
__m128i i32 = _mm_cvttps_epi32(fvals); |
| 1470 |
__m128i i16 = _mm_packus_epi32(i32, i32); |
| 1471 |
__m128i i8 = _mm_packus_epi16(i16, i16); |
| 1472 |
_mm_storeu_si32(dst, i8); |
| 1473 |
|
| 1474 |
#elif defined(SW_HAS_SSE2) |
| 1475 |
__m128 fvals = _mm_mul_ps(_mm_loadu_ps(src), _mm_set1_ps(255.0f)); |
| 1476 |
__m128i i32 = _mm_cvttps_epi32(fvals); |
| 1477 |
__m128i i16 = _mm_packs_epi32(i32, i32); |
| 1478 |
__m128i i8 = _mm_packus_epi16(i16, i16); |
| 1479 |
_mm_storeu_si32(dst, i8); |
| 1480 |
|
| 1481 |
#elif defined(SW_HAS_RVV) |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
size_t vl = __riscv_vsetvl_e32m1(4); |
| 1486 |
vfloat32m1_t vsrc = __riscv_vle32_v_f32m1(src, vl); |
| 1487 |
|
| 1488 |
|
| 1489 |
vfloat32m1_t vscaled = __riscv_vfmul_vf_f32m1(vsrc, 255.0f, vl); |
| 1490 |
vscaled = __riscv_vfadd_vf_f32m1(vscaled, 0.5f, vl); |
| 1491 |
|
| 1492 |
|
| 1493 |
vuint32m1_t vu32 = __riscv_vfcvt_xu_f_v_u32m1(vscaled, vl); |
| 1494 |
|
| 1495 |
|
| 1496 |
vuint8m1_t vu8 = __riscv_vnclipu_wx_u8m1(vu32, 0, vl); |
| 1497 |
__riscv_vse8_v_u8m1(dst, vu8, vl); |
| 1498 |
|
| 1499 |
#else |
| 1500 |
dst[0] = (uint8_t)(src[0]*255.0f); |
| 1501 |
dst[1] = (uint8_t)(src[1]*255.0f); |
| 1502 |
dst[2] = (uint8_t)(src[2]*255.0f); |
| 1503 |
dst[3] = (uint8_t)(src[3]*255.0f); |
| 1504 |
#endif |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
static bool sw_pool_init(sw_pool_t *pool, int capacity, size_t stride) |
| 1511 |
{ |
| 1512 |
*pool = (sw_pool_t) { 0 }; |
| 1513 |
|
| 1514 |
pool->data = SW_CALLOC(capacity, stride); |
| 1515 |
pool->gen = SW_CALLOC(capacity, sizeof(uint8_t)); |
| 1516 |
pool->freeList = SW_MALLOC(capacity*sizeof(uint32_t)); |
| 1517 |
|
| 1518 |
if (!pool->data || !pool->gen || !pool->freeList) |
| 1519 |
{ |
| 1520 |
SW_FREE(pool->data); |
| 1521 |
SW_FREE(pool->gen); |
| 1522 |
SW_FREE(pool->freeList); |
| 1523 |
return false; |
| 1524 |
} |
| 1525 |
|
| 1526 |
pool->watermark = 1; |
| 1527 |
pool->capacity = capacity; |
| 1528 |
pool->stride = stride; |
| 1529 |
|
| 1530 |
return true; |
| 1531 |
} |
| 1532 |
|
| 1533 |
static void sw_pool_destroy(sw_pool_t *pool) |
| 1534 |
{ |
| 1535 |
SW_FREE(pool->data); |
| 1536 |
SW_FREE(pool->gen); |
| 1537 |
SW_FREE(pool->freeList); |
| 1538 |
*pool = (sw_pool_t) { 0 }; |
| 1539 |
} |
| 1540 |
|
| 1541 |
static sw_handle_t sw_pool_alloc(sw_pool_t *pool) |
| 1542 |
{ |
| 1543 |
uint32_t index; |
| 1544 |
|
| 1545 |
if (pool->freeCount > 0) |
| 1546 |
{ |
| 1547 |
index = pool->freeList[--pool->freeCount]; |
| 1548 |
} |
| 1549 |
else |
| 1550 |
{ |
| 1551 |
if (pool->watermark >= pool->capacity) return SW_HANDLE_NULL; |
| 1552 |
index = (uint32_t)pool->watermark++; |
| 1553 |
} |
| 1554 |
|
| 1555 |
uint8_t ver = ((pool->gen[index] & SW_POOL_SLOT_VER_MASK) + 1) & SW_POOL_SLOT_VER_MASK; |
| 1556 |
if (ver == 0) ver = 1; |
| 1557 |
pool->gen[index] = SW_POOL_SLOT_LIVE | ver; |
| 1558 |
|
| 1559 |
uint8_t *slot = (uint8_t *)pool->data + index*pool->stride; |
| 1560 |
for (size_t i = 0; i < pool->stride; i++) slot[i] = 0; |
| 1561 |
|
| 1562 |
return (sw_handle_t)index; |
| 1563 |
} |
| 1564 |
|
| 1565 |
static void *sw_pool_get(const sw_pool_t *pool, sw_handle_t handle) |
| 1566 |
{ |
| 1567 |
if (handle == SW_HANDLE_NULL) return NULL; |
| 1568 |
if ((int)handle >= pool->capacity) return NULL; |
| 1569 |
if (!(pool->gen[handle] & SW_POOL_SLOT_LIVE)) return NULL; |
| 1570 |
|
| 1571 |
return (char *)pool->data + handle*pool->stride; |
| 1572 |
} |
| 1573 |
|
| 1574 |
static bool sw_pool_valid(const sw_pool_t *pool, sw_handle_t handle) |
| 1575 |
{ |
| 1576 |
return sw_pool_get(pool, handle) != NULL; |
| 1577 |
} |
| 1578 |
|
| 1579 |
static bool sw_pool_free(sw_pool_t *pool, sw_handle_t handle) |
| 1580 |
{ |
| 1581 |
if (!sw_pool_valid(pool, handle)) return false; |
| 1582 |
|
| 1583 |
pool->gen[handle] &= SW_POOL_SLOT_VER_MASK; |
| 1584 |
pool->freeList[pool->freeCount++] = handle; |
| 1585 |
return true; |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
static inline bool sw_is_texture_valid(sw_handle_t id) |
| 1592 |
{ |
| 1593 |
return sw_pool_valid(&RLSW.texturePool, id); |
| 1594 |
} |
| 1595 |
|
| 1596 |
static inline bool sw_is_texture_complete(sw_texture_t *tex) |
| 1597 |
{ |
| 1598 |
return (tex != NULL) && (tex->pixels != NULL); |
| 1599 |
} |
| 1600 |
|
| 1601 |
static inline bool sw_is_texture_filter_valid(int filter) |
| 1602 |
{ |
| 1603 |
return ((filter == SW_NEAREST) || (filter == SW_LINEAR)); |
| 1604 |
} |
| 1605 |
|
| 1606 |
static inline bool sw_is_texture_wrap_valid(int wrap) |
| 1607 |
{ |
| 1608 |
return ((wrap == SW_REPEAT) || (wrap == SW_CLAMP)); |
| 1609 |
} |
| 1610 |
|
| 1611 |
static inline bool sw_is_draw_mode_valid(int mode) |
| 1612 |
{ |
| 1613 |
bool result = false; |
| 1614 |
|
| 1615 |
switch (mode) |
| 1616 |
{ |
| 1617 |
case SW_POINTS: |
| 1618 |
case SW_LINES: |
| 1619 |
case SW_TRIANGLES: |
| 1620 |
case SW_QUADS: result = true; break; |
| 1621 |
default: break; |
| 1622 |
} |
| 1623 |
|
| 1624 |
return result; |
| 1625 |
} |
| 1626 |
|
| 1627 |
static inline bool sw_is_poly_mode_valid(int mode) |
| 1628 |
{ |
| 1629 |
bool result = false; |
| 1630 |
|
| 1631 |
switch (mode) |
| 1632 |
{ |
| 1633 |
case SW_POINT: |
| 1634 |
case SW_LINE: |
| 1635 |
case SW_FILL: result = true; break; |
| 1636 |
default: break; |
| 1637 |
} |
| 1638 |
|
| 1639 |
return result; |
| 1640 |
} |
| 1641 |
|
| 1642 |
static inline bool sw_is_face_valid(int face) |
| 1643 |
{ |
| 1644 |
return (face == SW_FRONT || face == SW_BACK); |
| 1645 |
} |
| 1646 |
|
| 1647 |
static inline bool sw_is_ready_to_render(void) |
| 1648 |
{ |
| 1649 |
return (swCheckFramebufferStatus() == SW_FRAMEBUFFER_COMPLETE); |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
static inline int sw_pixel_get_format(SWformat format, SWtype type) |
| 1656 |
{ |
| 1657 |
int channels = 0; |
| 1658 |
int bitsPerChannel = 8; |
| 1659 |
|
| 1660 |
|
| 1661 |
if (format == SW_DEPTH_COMPONENT) |
| 1662 |
{ |
| 1663 |
switch (type) |
| 1664 |
{ |
| 1665 |
case SW_UNSIGNED_BYTE: return SW_PIXELFORMAT_DEPTH_D8; |
| 1666 |
case SW_BYTE: return SW_PIXELFORMAT_DEPTH_D8; |
| 1667 |
case SW_UNSIGNED_SHORT: return SW_PIXELFORMAT_DEPTH_D16; |
| 1668 |
case SW_SHORT: return SW_PIXELFORMAT_DEPTH_D16; |
| 1669 |
case SW_UNSIGNED_INT: return SW_PIXELFORMAT_DEPTH_D32; |
| 1670 |
case SW_INT: return SW_PIXELFORMAT_DEPTH_D32; |
| 1671 |
case SW_FLOAT: return SW_PIXELFORMAT_DEPTH_D32; |
| 1672 |
default: return SW_PIXELFORMAT_UNKNOWN; |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
|
| 1677 |
switch (type) |
| 1678 |
{ |
| 1679 |
case SW_UNSIGNED_BYTE_3_3_2: return SW_PIXELFORMAT_COLOR_R3G3B2; |
| 1680 |
case SW_UNSIGNED_SHORT_5_6_5: return SW_PIXELFORMAT_COLOR_R5G6B5; |
| 1681 |
case SW_UNSIGNED_SHORT_4_4_4_4: return SW_PIXELFORMAT_COLOR_R4G4B4A4; |
| 1682 |
case SW_UNSIGNED_SHORT_5_5_5_1: return SW_PIXELFORMAT_COLOR_R5G5B5A1; |
| 1683 |
default: break; |
| 1684 |
} |
| 1685 |
|
| 1686 |
|
| 1687 |
switch (format) |
| 1688 |
{ |
| 1689 |
case SW_LUMINANCE: channels = 1; break; |
| 1690 |
case SW_LUMINANCE_ALPHA: channels = 2; break; |
| 1691 |
case SW_RGB: channels = 3; break; |
| 1692 |
case SW_RGBA: channels = 4; break; |
| 1693 |
default: return SW_PIXELFORMAT_UNKNOWN; |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
switch (type) |
| 1698 |
{ |
| 1699 |
case SW_UNSIGNED_BYTE: bitsPerChannel = 8; break; |
| 1700 |
case SW_BYTE: bitsPerChannel = 8; break; |
| 1701 |
case SW_UNSIGNED_SHORT: bitsPerChannel = 16; break; |
| 1702 |
case SW_SHORT: bitsPerChannel = 16; break; |
| 1703 |
case SW_UNSIGNED_INT: bitsPerChannel = 32; break; |
| 1704 |
case SW_INT: bitsPerChannel = 32; break; |
| 1705 |
case SW_FLOAT: bitsPerChannel = 32; break; |
| 1706 |
default: return SW_PIXELFORMAT_UNKNOWN; |
| 1707 |
} |
| 1708 |
|
| 1709 |
|
| 1710 |
if (bitsPerChannel == 8) |
| 1711 |
{ |
| 1712 |
if (channels == 1) return SW_PIXELFORMAT_COLOR_GRAYSCALE; |
| 1713 |
if (channels == 2) return SW_PIXELFORMAT_COLOR_GRAYALPHA; |
| 1714 |
if (channels == 3) return SW_PIXELFORMAT_COLOR_R8G8B8; |
| 1715 |
if (channels == 4) return SW_PIXELFORMAT_COLOR_R8G8B8A8; |
| 1716 |
} |
| 1717 |
else if (bitsPerChannel == 16) |
| 1718 |
{ |
| 1719 |
if (channels == 1) return SW_PIXELFORMAT_COLOR_R16; |
| 1720 |
if (channels == 3) return SW_PIXELFORMAT_COLOR_R16G16B16; |
| 1721 |
if (channels == 4) return SW_PIXELFORMAT_COLOR_R16G16B16A16; |
| 1722 |
} |
| 1723 |
else if (bitsPerChannel == 32) |
| 1724 |
{ |
| 1725 |
if (channels == 1) return SW_PIXELFORMAT_COLOR_R32; |
| 1726 |
if (channels == 3) return SW_PIXELFORMAT_COLOR_R32G32B32; |
| 1727 |
if (channels == 4) return SW_PIXELFORMAT_COLOR_R32G32B32A32; |
| 1728 |
} |
| 1729 |
|
| 1730 |
return SW_PIXELFORMAT_UNKNOWN; |
| 1731 |
} |
| 1732 |
|
| 1733 |
static inline bool sw_pixel_is_depth_format(sw_pixelformat_t format) |
| 1734 |
{ |
| 1735 |
switch (format) |
| 1736 |
{ |
| 1737 |
case SW_PIXELFORMAT_DEPTH_D8: |
| 1738 |
case SW_PIXELFORMAT_DEPTH_D16: |
| 1739 |
case SW_PIXELFORMAT_DEPTH_D32: return true; |
| 1740 |
default: break; |
| 1741 |
} |
| 1742 |
|
| 1743 |
return false; |
| 1744 |
} |
| 1745 |
|
| 1746 |
static inline void sw_pixel_read_color8_GRAYSCALE(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1747 |
{ |
| 1748 |
uint8_t gray = ((const uint8_t *)pixels)[offset]; |
| 1749 |
color[0] = gray; |
| 1750 |
color[1] = gray; |
| 1751 |
color[2] = gray; |
| 1752 |
color[3] = 255; |
| 1753 |
} |
| 1754 |
|
| 1755 |
static inline void sw_pixel_read_color8_GRAYALPHA(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1756 |
{ |
| 1757 |
const uint8_t *src = &((const uint8_t *)pixels)[offset*2]; |
| 1758 |
color[0] = src[0]; |
| 1759 |
color[1] = src[0]; |
| 1760 |
color[2] = src[0]; |
| 1761 |
color[3] = src[1]; |
| 1762 |
} |
| 1763 |
|
| 1764 |
static inline void sw_pixel_read_color8_R3G3B2(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1765 |
{ |
| 1766 |
uint8_t pixel = ((const uint8_t *)pixels)[offset]; |
| 1767 |
color[0] = sw_expand_3to8((pixel >> 5) & 0x07); |
| 1768 |
color[1] = sw_expand_3to8((pixel >> 2) & 0x07); |
| 1769 |
color[2] = sw_expand_2to8( pixel & 0x03); |
| 1770 |
color[3] = 255; |
| 1771 |
} |
| 1772 |
|
| 1773 |
static inline void sw_pixel_read_color8_R5G6B5(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1774 |
{ |
| 1775 |
uint16_t pixel = ((const uint16_t *)pixels)[offset]; |
| 1776 |
color[0] = sw_expand_5to8((pixel >> 11) & 0x1F); |
| 1777 |
color[1] = sw_expand_6to8((pixel >> 5) & 0x3F); |
| 1778 |
color[2] = sw_expand_5to8( pixel & 0x1F); |
| 1779 |
color[3] = 255; |
| 1780 |
} |
| 1781 |
|
| 1782 |
static inline void sw_pixel_read_color8_R8G8B8(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1783 |
{ |
| 1784 |
const uint8_t *src = &((const uint8_t *)pixels)[offset*3]; |
| 1785 |
color[0] = src[0]; |
| 1786 |
color[1] = src[1]; |
| 1787 |
color[2] = src[2]; |
| 1788 |
color[3] = 255; |
| 1789 |
} |
| 1790 |
|
| 1791 |
static inline void sw_pixel_read_color8_R5G5B5A1(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1792 |
{ |
| 1793 |
uint16_t pixel = ((const uint16_t *)pixels)[offset]; |
| 1794 |
color[0] = sw_expand_5to8((pixel >> 11) & 0x1F); |
| 1795 |
color[1] = sw_expand_5to8((pixel >> 6) & 0x1F); |
| 1796 |
color[2] = sw_expand_5to8((pixel >> 1) & 0x1F); |
| 1797 |
color[3] = sw_expand_1to8( pixel & 0x01); |
| 1798 |
} |
| 1799 |
|
| 1800 |
static inline void sw_pixel_read_color8_R4G4B4A4(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1801 |
{ |
| 1802 |
uint16_t pixel = ((const uint16_t *)pixels)[offset]; |
| 1803 |
color[0] = sw_expand_4to8((pixel >> 12) & 0x0F); |
| 1804 |
color[1] = sw_expand_4to8((pixel >> 8) & 0x0F); |
| 1805 |
color[2] = sw_expand_4to8((pixel >> 4) & 0x0F); |
| 1806 |
color[3] = sw_expand_4to8( pixel & 0x0F); |
| 1807 |
} |
| 1808 |
|
| 1809 |
static inline void sw_pixel_read_color8_R8G8B8A8(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1810 |
{ |
| 1811 |
const uint8_t *src = &((const uint8_t *)pixels)[offset*4]; |
| 1812 |
color[0] = src[0]; |
| 1813 |
color[1] = src[1]; |
| 1814 |
color[2] = src[2]; |
| 1815 |
color[3] = src[3]; |
| 1816 |
} |
| 1817 |
|
| 1818 |
static inline void sw_pixel_read_color8_R32(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1819 |
{ |
| 1820 |
uint8_t gray = (uint8_t)(((const float *)pixels)[offset]*255.0f); |
| 1821 |
color[0] = gray; |
| 1822 |
color[1] = gray; |
| 1823 |
color[2] = gray; |
| 1824 |
color[3] = 255; |
| 1825 |
} |
| 1826 |
|
| 1827 |
static inline void sw_pixel_read_color8_R32G32B32(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1828 |
{ |
| 1829 |
const float *src = &((const float *)pixels)[offset*3]; |
| 1830 |
color[0] = (uint8_t)(src[0]*255.0f); |
| 1831 |
color[1] = (uint8_t)(src[1]*255.0f); |
| 1832 |
color[2] = (uint8_t)(src[2]*255.0f); |
| 1833 |
color[3] = 255; |
| 1834 |
} |
| 1835 |
|
| 1836 |
static inline void sw_pixel_read_color8_R32G32B32A32(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1837 |
{ |
| 1838 |
const float *src = &((const float *)pixels)[offset*4]; |
| 1839 |
color[0] = (uint8_t)(src[0]*255.0f); |
| 1840 |
color[1] = (uint8_t)(src[1]*255.0f); |
| 1841 |
color[2] = (uint8_t)(src[2]*255.0f); |
| 1842 |
color[3] = (uint8_t)(src[3]*255.0f); |
| 1843 |
} |
| 1844 |
|
| 1845 |
static inline void sw_pixel_read_color8_R16(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1846 |
{ |
| 1847 |
uint8_t gray = (uint8_t)(sw_half_to_float(((const uint16_t *)pixels)[offset])*255.0f); |
| 1848 |
color[0] = gray; |
| 1849 |
color[1] = gray; |
| 1850 |
color[2] = gray; |
| 1851 |
color[3] = 255; |
| 1852 |
} |
| 1853 |
|
| 1854 |
static inline void sw_pixel_read_color8_R16G16B16(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1855 |
{ |
| 1856 |
const uint16_t *src = &((const uint16_t *)pixels)[offset*3]; |
| 1857 |
color[0] = (uint8_t)(sw_half_to_float(src[0])*255.0f); |
| 1858 |
color[1] = (uint8_t)(sw_half_to_float(src[1])*255.0f); |
| 1859 |
color[2] = (uint8_t)(sw_half_to_float(src[2])*255.0f); |
| 1860 |
color[3] = 255; |
| 1861 |
} |
| 1862 |
|
| 1863 |
static inline void sw_pixel_read_color8_R16G16B16A16(uint8_t *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 1864 |
{ |
| 1865 |
const uint16_t *src = &((const uint16_t *)pixels)[offset*4]; |
| 1866 |
color[0] = (uint8_t)(sw_half_to_float(src[0])*255.0f); |
| 1867 |
color[1] = (uint8_t)(sw_half_to_float(src[1])*255.0f); |
| 1868 |
color[2] = (uint8_t)(sw_half_to_float(src[2])*255.0f); |
| 1869 |
color[3] = (uint8_t)(sw_half_to_float(src[3])*255.0f); |
| 1870 |
} |
| 1871 |
|
| 1872 |
static inline sw_pixel_read_color8_f sw_pixel_get_read_color8_func(sw_pixelformat_t format) |
| 1873 |
{ |
| 1874 |
switch (format) |
| 1875 |
{ |
| 1876 |
case SW_PIXELFORMAT_COLOR_GRAYSCALE: return sw_pixel_read_color8_GRAYSCALE; |
| 1877 |
case SW_PIXELFORMAT_COLOR_GRAYALPHA: return sw_pixel_read_color8_GRAYALPHA; |
| 1878 |
case SW_PIXELFORMAT_COLOR_R3G3B2: return sw_pixel_read_color8_R3G3B2; |
| 1879 |
case SW_PIXELFORMAT_COLOR_R5G6B5: return sw_pixel_read_color8_R5G6B5; |
| 1880 |
case SW_PIXELFORMAT_COLOR_R8G8B8: return sw_pixel_read_color8_R8G8B8; |
| 1881 |
case SW_PIXELFORMAT_COLOR_R5G5B5A1: return sw_pixel_read_color8_R5G5B5A1; |
| 1882 |
case SW_PIXELFORMAT_COLOR_R4G4B4A4: return sw_pixel_read_color8_R4G4B4A4; |
| 1883 |
case SW_PIXELFORMAT_COLOR_R8G8B8A8: return sw_pixel_read_color8_R8G8B8A8; |
| 1884 |
case SW_PIXELFORMAT_COLOR_R32: return sw_pixel_read_color8_R32; |
| 1885 |
case SW_PIXELFORMAT_COLOR_R32G32B32: return sw_pixel_read_color8_R32G32B32; |
| 1886 |
case SW_PIXELFORMAT_COLOR_R32G32B32A32: return sw_pixel_read_color8_R32G32B32A32; |
| 1887 |
case SW_PIXELFORMAT_COLOR_R16: return sw_pixel_read_color8_R16; |
| 1888 |
case SW_PIXELFORMAT_COLOR_R16G16B16: return sw_pixel_read_color8_R16G16B16; |
| 1889 |
case SW_PIXELFORMAT_COLOR_R16G16B16A16: return sw_pixel_read_color8_R16G16B16A16; |
| 1890 |
|
| 1891 |
case SW_PIXELFORMAT_UNKNOWN: |
| 1892 |
case SW_PIXELFORMAT_DEPTH_D8: |
| 1893 |
case SW_PIXELFORMAT_DEPTH_D16: |
| 1894 |
case SW_PIXELFORMAT_DEPTH_D32: |
| 1895 |
case SW_PIXELFORMAT_COUNT: |
| 1896 |
default: break; |
| 1897 |
} |
| 1898 |
|
| 1899 |
return NULL; |
| 1900 |
} |
| 1901 |
|
| 1902 |
static inline void sw_pixel_write_color8_GRAYSCALE(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1903 |
{ |
| 1904 |
((uint8_t *)pixels)[offset] = sw_luminance8(color); |
| 1905 |
} |
| 1906 |
|
| 1907 |
static inline void sw_pixel_write_color8_GRAYALPHA(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1908 |
{ |
| 1909 |
uint8_t *dst = &((uint8_t *)pixels)[offset*2]; |
| 1910 |
dst[0] = sw_luminance8(color); |
| 1911 |
dst[1] = color[3]; |
| 1912 |
} |
| 1913 |
|
| 1914 |
static inline void sw_pixel_write_color8_R3G3B2(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1915 |
{ |
| 1916 |
uint8_t pixel = (sw_compress_8to3(color[0]) << 5) |
| 1917 |
| (sw_compress_8to3(color[1]) << 2) |
| 1918 |
| sw_compress_8to2(color[2]); |
| 1919 |
((uint8_t *)pixels)[offset] = pixel; |
| 1920 |
} |
| 1921 |
|
| 1922 |
static inline void sw_pixel_write_color8_R5G6B5(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1923 |
{ |
| 1924 |
uint16_t pixel = (sw_compress_8to5(color[0]) << 11) |
| 1925 |
| (sw_compress_8to6(color[1]) << 5) |
| 1926 |
| sw_compress_8to5(color[2]); |
| 1927 |
((uint16_t *)pixels)[offset] = pixel; |
| 1928 |
} |
| 1929 |
|
| 1930 |
static inline void sw_pixel_write_color8_R8G8B8(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1931 |
{ |
| 1932 |
uint8_t *dst = &((uint8_t *)pixels)[offset*3]; |
| 1933 |
dst[0] = color[0]; |
| 1934 |
dst[1] = color[1]; |
| 1935 |
dst[2] = color[2]; |
| 1936 |
} |
| 1937 |
|
| 1938 |
static inline void sw_pixel_write_color8_R5G5B5A1(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1939 |
{ |
| 1940 |
uint16_t pixel = (sw_compress_8to5(color[0]) << 11) |
| 1941 |
| (sw_compress_8to5(color[1]) << 6) |
| 1942 |
| (sw_compress_8to5(color[2]) << 1) |
| 1943 |
| sw_compress_8to1(color[3]); |
| 1944 |
((uint16_t *)pixels)[offset] = pixel; |
| 1945 |
} |
| 1946 |
|
| 1947 |
static inline void sw_pixel_write_color8_R4G4B4A4(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1948 |
{ |
| 1949 |
uint16_t pixel = (sw_compress_8to4(color[0]) << 12) |
| 1950 |
| (sw_compress_8to4(color[1]) << 8) |
| 1951 |
| (sw_compress_8to4(color[2]) << 4) |
| 1952 |
| sw_compress_8to4(color[3]); |
| 1953 |
((uint16_t *)pixels)[offset] = pixel; |
| 1954 |
} |
| 1955 |
|
| 1956 |
static inline void sw_pixel_write_color8_R8G8B8A8(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1957 |
{ |
| 1958 |
uint8_t *dst = &((uint8_t *)pixels)[offset*4]; |
| 1959 |
dst[0] = color[0]; |
| 1960 |
dst[1] = color[1]; |
| 1961 |
dst[2] = color[2]; |
| 1962 |
dst[3] = color[3]; |
| 1963 |
} |
| 1964 |
|
| 1965 |
static inline void sw_pixel_write_color8_R32(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1966 |
{ |
| 1967 |
((float *)pixels)[offset] = sw_luminance8(color)*SW_INV_255; |
| 1968 |
} |
| 1969 |
|
| 1970 |
static inline void sw_pixel_write_color8_R32G32B32(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1971 |
{ |
| 1972 |
float *dst = &((float *)pixels)[offset*3]; |
| 1973 |
dst[0] = color[0]*SW_INV_255; |
| 1974 |
dst[1] = color[1]*SW_INV_255; |
| 1975 |
dst[2] = color[2]*SW_INV_255; |
| 1976 |
} |
| 1977 |
|
| 1978 |
static inline void sw_pixel_write_color8_R32G32B32A32(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1979 |
{ |
| 1980 |
float *dst = &((float *)pixels)[offset*4]; |
| 1981 |
dst[0] = color[0]*SW_INV_255; |
| 1982 |
dst[1] = color[1]*SW_INV_255; |
| 1983 |
dst[2] = color[2]*SW_INV_255; |
| 1984 |
dst[3] = color[3]*SW_INV_255; |
| 1985 |
} |
| 1986 |
|
| 1987 |
static inline void sw_pixel_write_color8_R16(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1988 |
{ |
| 1989 |
((uint16_t *)pixels)[offset] = sw_float_to_half(sw_luminance8(color)*SW_INV_255); |
| 1990 |
} |
| 1991 |
|
| 1992 |
static inline void sw_pixel_write_color8_R16G16B16(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 1993 |
{ |
| 1994 |
uint16_t *dst = &((uint16_t *)pixels)[offset*3]; |
| 1995 |
dst[0] = sw_float_to_half(color[0]*SW_INV_255); |
| 1996 |
dst[1] = sw_float_to_half(color[1]*SW_INV_255); |
| 1997 |
dst[2] = sw_float_to_half(color[2]*SW_INV_255); |
| 1998 |
} |
| 1999 |
|
| 2000 |
static inline void sw_pixel_write_color8_R16G16B16A16(void *SW_RESTRICT pixels, const uint8_t *SW_RESTRICT color, uint32_t offset) |
| 2001 |
{ |
| 2002 |
uint16_t *dst = &((uint16_t *)pixels)[offset*4]; |
| 2003 |
dst[0] = sw_float_to_half(color[0]*SW_INV_255); |
| 2004 |
dst[1] = sw_float_to_half(color[1]*SW_INV_255); |
| 2005 |
dst[2] = sw_float_to_half(color[2]*SW_INV_255); |
| 2006 |
dst[3] = sw_float_to_half(color[3]*SW_INV_255); |
| 2007 |
} |
| 2008 |
|
| 2009 |
static inline sw_pixel_write_color8_f sw_pixel_get_write_color8_func(sw_pixelformat_t format) |
| 2010 |
{ |
| 2011 |
switch (format) |
| 2012 |
{ |
| 2013 |
case SW_PIXELFORMAT_COLOR_GRAYSCALE: return sw_pixel_write_color8_GRAYSCALE; |
| 2014 |
case SW_PIXELFORMAT_COLOR_GRAYALPHA: return sw_pixel_write_color8_GRAYALPHA; |
| 2015 |
case SW_PIXELFORMAT_COLOR_R3G3B2: return sw_pixel_write_color8_R3G3B2; |
| 2016 |
case SW_PIXELFORMAT_COLOR_R5G6B5: return sw_pixel_write_color8_R5G6B5; |
| 2017 |
case SW_PIXELFORMAT_COLOR_R8G8B8: return sw_pixel_write_color8_R8G8B8; |
| 2018 |
case SW_PIXELFORMAT_COLOR_R5G5B5A1: return sw_pixel_write_color8_R5G5B5A1; |
| 2019 |
case SW_PIXELFORMAT_COLOR_R4G4B4A4: return sw_pixel_write_color8_R4G4B4A4; |
| 2020 |
case SW_PIXELFORMAT_COLOR_R8G8B8A8: return sw_pixel_write_color8_R8G8B8A8; |
| 2021 |
case SW_PIXELFORMAT_COLOR_R32: return sw_pixel_write_color8_R32; |
| 2022 |
case SW_PIXELFORMAT_COLOR_R32G32B32: return sw_pixel_write_color8_R32G32B32; |
| 2023 |
case SW_PIXELFORMAT_COLOR_R32G32B32A32: return sw_pixel_write_color8_R32G32B32A32; |
| 2024 |
case SW_PIXELFORMAT_COLOR_R16: return sw_pixel_write_color8_R16; |
| 2025 |
case SW_PIXELFORMAT_COLOR_R16G16B16: return sw_pixel_write_color8_R16G16B16; |
| 2026 |
case SW_PIXELFORMAT_COLOR_R16G16B16A16: return sw_pixel_write_color8_R16G16B16A16; |
| 2027 |
|
| 2028 |
case SW_PIXELFORMAT_UNKNOWN: |
| 2029 |
case SW_PIXELFORMAT_DEPTH_D8: |
| 2030 |
case SW_PIXELFORMAT_DEPTH_D16: |
| 2031 |
case SW_PIXELFORMAT_DEPTH_D32: |
| 2032 |
case SW_PIXELFORMAT_COUNT: |
| 2033 |
default: break; |
| 2034 |
} |
| 2035 |
|
| 2036 |
return NULL; |
| 2037 |
} |
| 2038 |
|
| 2039 |
static inline void sw_pixel_read_color_GRAYSCALE(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2040 |
{ |
| 2041 |
float gray = ((const uint8_t *)pixels)[offset]*SW_INV_255; |
| 2042 |
color[0] = gray; |
| 2043 |
color[1] = gray; |
| 2044 |
color[2] = gray; |
| 2045 |
color[3] = 1.0f; |
| 2046 |
} |
| 2047 |
|
| 2048 |
static inline void sw_pixel_read_color_GRAYALPHA(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2049 |
{ |
| 2050 |
const uint8_t *src = &((const uint8_t *)pixels)[offset*2]; |
| 2051 |
float gray = src[0]*SW_INV_255; |
| 2052 |
color[0] = gray; |
| 2053 |
color[1] = gray; |
| 2054 |
color[2] = gray; |
| 2055 |
color[3] = src[1]*SW_INV_255; |
| 2056 |
} |
| 2057 |
|
| 2058 |
static inline void sw_pixel_read_color_R3G3B2(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2059 |
{ |
| 2060 |
uint8_t unpack[4]; |
| 2061 |
sw_pixel_read_color8_R3G3B2(unpack, pixels, offset); |
| 2062 |
sw_color8_to_color(color, unpack); |
| 2063 |
} |
| 2064 |
|
| 2065 |
static inline void sw_pixel_read_color_R5G6B5(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2066 |
{ |
| 2067 |
uint8_t unpack[4]; |
| 2068 |
sw_pixel_read_color8_R5G6B5(unpack, pixels, offset); |
| 2069 |
sw_color8_to_color(color, unpack); |
| 2070 |
} |
| 2071 |
|
| 2072 |
static inline void sw_pixel_read_color_R8G8B8(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2073 |
{ |
| 2074 |
uint8_t unpack[4]; |
| 2075 |
sw_pixel_read_color8_R8G8B8(unpack, pixels, offset); |
| 2076 |
sw_color8_to_color(color, unpack); |
| 2077 |
} |
| 2078 |
|
| 2079 |
static inline void sw_pixel_read_color_R5G5B5A1(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2080 |
{ |
| 2081 |
uint8_t unpack[4]; |
| 2082 |
sw_pixel_read_color8_R5G5B5A1(unpack, pixels, offset); |
| 2083 |
sw_color8_to_color(color, unpack); |
| 2084 |
} |
| 2085 |
|
| 2086 |
static inline void sw_pixel_read_color_R4G4B4A4(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2087 |
{ |
| 2088 |
uint8_t unpack[4]; |
| 2089 |
sw_pixel_read_color8_R4G4B4A4(unpack, pixels, offset); |
| 2090 |
sw_color8_to_color(color, unpack); |
| 2091 |
} |
| 2092 |
|
| 2093 |
static inline void sw_pixel_read_color_R8G8B8A8(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2094 |
{ |
| 2095 |
sw_color8_to_color(color, &((const uint8_t *)pixels)[offset*4]); |
| 2096 |
} |
| 2097 |
|
| 2098 |
static inline void sw_pixel_read_color_R32(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2099 |
{ |
| 2100 |
float val = ((const float *)pixels)[offset]; |
| 2101 |
color[0] = val; |
| 2102 |
color[1] = val; |
| 2103 |
color[2] = val; |
| 2104 |
color[3] = 1.0f; |
| 2105 |
} |
| 2106 |
|
| 2107 |
static inline void sw_pixel_read_color_R32G32B32(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2108 |
{ |
| 2109 |
const float *src = &((const float *)pixels)[offset*3]; |
| 2110 |
color[0] = src[0]; |
| 2111 |
color[1] = src[1]; |
| 2112 |
color[2] = src[2]; |
| 2113 |
color[3] = 1.0f; |
| 2114 |
} |
| 2115 |
|
| 2116 |
static inline void sw_pixel_read_color_R32G32B32A32(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2117 |
{ |
| 2118 |
const float *src = &((const float *)pixels)[offset*4]; |
| 2119 |
color[0] = src[0]; |
| 2120 |
color[1] = src[1]; |
| 2121 |
color[2] = src[2]; |
| 2122 |
color[3] = src[3]; |
| 2123 |
} |
| 2124 |
|
| 2125 |
static inline void sw_pixel_read_color_R16(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2126 |
{ |
| 2127 |
float val = sw_half_to_float(((const uint16_t *)pixels)[offset]); |
| 2128 |
color[0] = val; |
| 2129 |
color[1] = val; |
| 2130 |
color[2] = val; |
| 2131 |
color[3] = 1.0f; |
| 2132 |
} |
| 2133 |
|
| 2134 |
static inline void sw_pixel_read_color_R16G16B16(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2135 |
{ |
| 2136 |
const uint16_t *src = &((const uint16_t *)pixels)[offset*3]; |
| 2137 |
color[0] = sw_half_to_float(src[0]); |
| 2138 |
color[1] = sw_half_to_float(src[1]); |
| 2139 |
color[2] = sw_half_to_float(src[2]); |
| 2140 |
color[3] = 1.0f; |
| 2141 |
} |
| 2142 |
|
| 2143 |
static inline void sw_pixel_read_color_R16G16B16A16(float *SW_RESTRICT color, const void *SW_RESTRICT pixels, uint32_t offset) |
| 2144 |
{ |
| 2145 |
const uint16_t *src = &((const uint16_t *)pixels)[offset*4]; |
| 2146 |
color[0] = sw_half_to_float(src[0]); |
| 2147 |
color[1] = sw_half_to_float(src[1]); |
| 2148 |
color[2] = sw_half_to_float(src[2]); |
| 2149 |
color[3] = sw_half_to_float(src[3]); |
| 2150 |
} |
| 2151 |
|
| 2152 |
static inline sw_pixel_read_color_f sw_pixel_get_read_color_func(sw_pixelformat_t format) |
| 2153 |
{ |
| 2154 |
switch (format) |
| 2155 |
{ |
| 2156 |
case SW_PIXELFORMAT_COLOR_GRAYSCALE: return sw_pixel_read_color_GRAYSCALE; |
| 2157 |
case SW_PIXELFORMAT_COLOR_GRAYALPHA: return sw_pixel_read_color_GRAYALPHA; |
| 2158 |
case SW_PIXELFORMAT_COLOR_R3G3B2: return sw_pixel_read_color_R3G3B2; |
| 2159 |
case SW_PIXELFORMAT_COLOR_R5G6B5: return sw_pixel_read_color_R5G6B5; |
| 2160 |
case SW_PIXELFORMAT_COLOR_R8G8B8: return sw_pixel_read_color_R8G8B8; |
| 2161 |
case SW_PIXELFORMAT_COLOR_R5G5B5A1: return sw_pixel_read_color_R5G5B5A1; |
| 2162 |
case SW_PIXELFORMAT_COLOR_R4G4B4A4: return sw_pixel_read_color_R4G4B4A4; |
| 2163 |
case SW_PIXELFORMAT_COLOR_R8G8B8A8: return sw_pixel_read_color_R8G8B8A8; |
| 2164 |
case SW_PIXELFORMAT_COLOR_R32: return sw_pixel_read_color_R32; |
| 2165 |
case SW_PIXELFORMAT_COLOR_R32G32B32: return sw_pixel_read_color_R32G32B32; |
| 2166 |
case SW_PIXELFORMAT_COLOR_R32G32B32A32: return sw_pixel_read_color_R32G32B32A32; |
| 2167 |
case SW_PIXELFORMAT_COLOR_R16: return sw_pixel_read_color_R16; |
| 2168 |
case SW_PIXELFORMAT_COLOR_R16G16B16: return sw_pixel_read_color_R16G16B16; |
| 2169 |
case SW_PIXELFORMAT_COLOR_R16G16B16A16: return sw_pixel_read_color_R16G16B16A16; |
| 2170 |
|
| 2171 |
case SW_PIXELFORMAT_UNKNOWN: |
| 2172 |
case SW_PIXELFORMAT_DEPTH_D8: |
| 2173 |
case SW_PIXELFORMAT_DEPTH_D16: |
| 2174 |
case SW_PIXELFORMAT_DEPTH_D32: |
| 2175 |
case SW_PIXELFORMAT_COUNT: |
| 2176 |
default: break; |
| 2177 |
} |
| 2178 |
|
| 2179 |
return NULL; |
| 2180 |
} |
| 2181 |
|
| 2182 |
static inline void sw_pixel_write_color_GRAYSCALE(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2183 |
{ |
| 2184 |
((uint8_t *)pixels)[offset] = (uint8_t)(sw_luminance(color)*255.0f); |
| 2185 |
} |
| 2186 |
|
| 2187 |
static inline void sw_pixel_write_color_GRAYALPHA(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2188 |
{ |
| 2189 |
uint8_t *dst = &((uint8_t *)pixels)[offset*2]; |
| 2190 |
dst[0] = (uint8_t)(sw_luminance(color)*255.0f); |
| 2191 |
dst[1] = (uint8_t)(color[3]*255.0f); |
| 2192 |
} |
| 2193 |
|
| 2194 |
static inline void sw_pixel_write_color_R3G3B2(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2195 |
{ |
| 2196 |
uint8_t color8[4]; |
| 2197 |
sw_color_to_color8(color8, color); |
| 2198 |
sw_pixel_write_color8_R3G3B2(pixels, color8, offset); |
| 2199 |
} |
| 2200 |
|
| 2201 |
static inline void sw_pixel_write_color_R5G6B5(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2202 |
{ |
| 2203 |
uint8_t color8[4]; |
| 2204 |
sw_color_to_color8(color8, color); |
| 2205 |
sw_pixel_write_color8_R5G6B5(pixels, color8, offset); |
| 2206 |
} |
| 2207 |
|
| 2208 |
static inline void sw_pixel_write_color_R8G8B8(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2209 |
{ |
| 2210 |
uint8_t color8[4]; |
| 2211 |
sw_color_to_color8(color8, color); |
| 2212 |
sw_pixel_write_color8_R8G8B8(pixels, color8, offset); |
| 2213 |
} |
| 2214 |
|
| 2215 |
static inline void sw_pixel_write_color_R5G5B5A1(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2216 |
{ |
| 2217 |
uint8_t color8[4]; |
| 2218 |
sw_color_to_color8(color8, color); |
| 2219 |
sw_pixel_write_color8_R5G5B5A1(pixels, color8, offset); |
| 2220 |
} |
| 2221 |
|
| 2222 |
static inline void sw_pixel_write_color_R4G4B4A4(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2223 |
{ |
| 2224 |
uint8_t color8[4]; |
| 2225 |
sw_color_to_color8(color8, color); |
| 2226 |
sw_pixel_write_color8_R4G4B4A4(pixels, color8, offset); |
| 2227 |
} |
| 2228 |
|
| 2229 |
static inline void sw_pixel_write_color_R8G8B8A8(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2230 |
{ |
| 2231 |
sw_color_to_color8(&((uint8_t *)pixels)[offset*4], color); |
| 2232 |
} |
| 2233 |
|
| 2234 |
static inline void sw_pixel_write_color_R32(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2235 |
{ |
| 2236 |
((float *)pixels)[offset] = sw_luminance(color); |
| 2237 |
} |
| 2238 |
|
| 2239 |
static inline void sw_pixel_write_color_R32G32B32(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2240 |
{ |
| 2241 |
float *dst = &((float *)pixels)[offset*3]; |
| 2242 |
dst[0] = color[0]; |
| 2243 |
dst[1] = color[1]; |
| 2244 |
dst[2] = color[2]; |
| 2245 |
} |
| 2246 |
|
| 2247 |
static inline void sw_pixel_write_color_R32G32B32A32(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2248 |
{ |
| 2249 |
float *dst = &((float *)pixels)[offset*4]; |
| 2250 |
dst[0] = color[0]; |
| 2251 |
dst[1] = color[1]; |
| 2252 |
dst[2] = color[2]; |
| 2253 |
dst[3] = color[3]; |
| 2254 |
} |
| 2255 |
|
| 2256 |
static inline void sw_pixel_write_color_R16(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2257 |
{ |
| 2258 |
((uint16_t *)pixels)[offset] = sw_float_to_half(sw_luminance(color)); |
| 2259 |
} |
| 2260 |
|
| 2261 |
static inline void sw_pixel_write_color_R16G16B16(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2262 |
{ |
| 2263 |
uint16_t *dst = &((uint16_t *)pixels)[offset*3]; |
| 2264 |
dst[0] = sw_float_to_half(color[0]); |
| 2265 |
dst[1] = sw_float_to_half(color[1]); |
| 2266 |
dst[2] = sw_float_to_half(color[2]); |
| 2267 |
} |
| 2268 |
|
| 2269 |
static inline void sw_pixel_write_color_R16G16B16A16(void *SW_RESTRICT pixels, const float *SW_RESTRICT color, uint32_t offset) |
| 2270 |
{ |
| 2271 |
uint16_t *dst = &((uint16_t *)pixels)[offset*4]; |
| 2272 |
dst[0] = sw_float_to_half(color[0]); |
| 2273 |
dst[1] = sw_float_to_half(color[1]); |
| 2274 |
dst[2] = sw_float_to_half(color[2]); |
| 2275 |
dst[3] = sw_float_to_half(color[3]); |
| 2276 |
} |
| 2277 |
|
| 2278 |
static inline sw_pixel_write_color_f sw_pixel_get_write_color_func(sw_pixelformat_t format) |
| 2279 |
{ |
| 2280 |
switch (format) |
| 2281 |
{ |
| 2282 |
case SW_PIXELFORMAT_COLOR_GRAYSCALE: return sw_pixel_write_color_GRAYSCALE; |
| 2283 |
case SW_PIXELFORMAT_COLOR_GRAYALPHA: return sw_pixel_write_color_GRAYALPHA; |
| 2284 |
case SW_PIXELFORMAT_COLOR_R3G3B2: return sw_pixel_write_color_R3G3B2; |
| 2285 |
case SW_PIXELFORMAT_COLOR_R5G6B5: return sw_pixel_write_color_R5G6B5; |
| 2286 |
case SW_PIXELFORMAT_COLOR_R8G8B8: return sw_pixel_write_color_R8G8B8; |
| 2287 |
case SW_PIXELFORMAT_COLOR_R5G5B5A1: return sw_pixel_write_color_R5G5B5A1; |
| 2288 |
case SW_PIXELFORMAT_COLOR_R4G4B4A4: return sw_pixel_write_color_R4G4B4A4; |
| 2289 |
case SW_PIXELFORMAT_COLOR_R8G8B8A8: return sw_pixel_write_color_R8G8B8A8; |
| 2290 |
case SW_PIXELFORMAT_COLOR_R32: return sw_pixel_write_color_R32; |
| 2291 |
case SW_PIXELFORMAT_COLOR_R32G32B32: return sw_pixel_write_color_R32G32B32; |
| 2292 |
case SW_PIXELFORMAT_COLOR_R32G32B32A32: return sw_pixel_write_color_R32G32B32A32; |
| 2293 |
case SW_PIXELFORMAT_COLOR_R16: return sw_pixel_write_color_R16; |
| 2294 |
case SW_PIXELFORMAT_COLOR_R16G16B16: return sw_pixel_write_color_R16G16B16; |
| 2295 |
case SW_PIXELFORMAT_COLOR_R16G16B16A16: return sw_pixel_write_color_R16G16B16A16; |
| 2296 |
|
| 2297 |
case SW_PIXELFORMAT_UNKNOWN: |
| 2298 |
case SW_PIXELFORMAT_DEPTH_D8: |
| 2299 |
case SW_PIXELFORMAT_DEPTH_D16: |
| 2300 |
case SW_PIXELFORMAT_DEPTH_D32: |
| 2301 |
case SW_PIXELFORMAT_COUNT: |
| 2302 |
default: break; |
| 2303 |
} |
| 2304 |
|
| 2305 |
return NULL; |
| 2306 |
} |
| 2307 |
|
| 2308 |
static inline float sw_pixel_read_depth_D8(const void *pixels, uint32_t offset) |
| 2309 |
{ |
| 2310 |
return (float)((uint8_t *)pixels)[offset]*SW_INV_255; |
| 2311 |
} |
| 2312 |
|
| 2313 |
static inline float sw_pixel_read_depth_D16(const void *pixels, uint32_t offset) |
| 2314 |
{ |
| 2315 |
return (float)((uint16_t *)pixels)[offset]/UINT16_MAX; |
| 2316 |
} |
| 2317 |
|
| 2318 |
static inline float sw_pixel_read_depth_D32(const void *pixels, uint32_t offset) |
| 2319 |
{ |
| 2320 |
return ((float *)pixels)[offset]; |
| 2321 |
} |
| 2322 |
|
| 2323 |
static inline void sw_pixel_write_depth_D8(void *pixels, float depth, uint32_t offset) |
| 2324 |
{ |
| 2325 |
((uint8_t *)pixels)[offset] = (uint8_t)(depth*UINT8_MAX); |
| 2326 |
} |
| 2327 |
|
| 2328 |
static inline void sw_pixel_write_depth_D16(void *pixels, float depth, uint32_t offset) |
| 2329 |
{ |
| 2330 |
((uint16_t *)pixels)[offset] = (uint16_t)(depth*UINT16_MAX); |
| 2331 |
} |
| 2332 |
|
| 2333 |
static inline void sw_pixel_write_depth_D32(void *pixels, float depth, uint32_t offset) |
| 2334 |
{ |
| 2335 |
((float *)pixels)[offset] = depth; |
| 2336 |
} |
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
static inline bool sw_texture_alloc(sw_texture_t *texture, const void *data, int w, int h, sw_pixelformat_t format) |
| 2342 |
{ |
| 2343 |
bool isDepth = sw_pixel_is_depth_format(format); |
| 2344 |
int bpp = SW_PIXELFORMAT_SIZE[format]; |
| 2345 |
int newSize = w*h*bpp; |
| 2346 |
|
| 2347 |
if (newSize > texture->allocSz) |
| 2348 |
{ |
| 2349 |
void *ptr = SW_REALLOC(texture->pixels, newSize); |
| 2350 |
if (!ptr) { RLSW.errCode = SW_OUT_OF_MEMORY; return false; } |
| 2351 |
texture->allocSz = newSize; |
| 2352 |
texture->pixels = ptr; |
| 2353 |
} |
| 2354 |
|
| 2355 |
uint8_t *dst = texture->pixels; |
| 2356 |
const uint8_t *src = data; |
| 2357 |
|
| 2358 |
sw_pixel_read_color8_f readColor8 = NULL; |
| 2359 |
sw_pixel_read_color_f readColor = NULL; |
| 2360 |
if (!isDepth) |
| 2361 |
{ |
| 2362 |
readColor8 = sw_pixel_get_read_color8_func(format); |
| 2363 |
readColor = sw_pixel_get_read_color_func(format); |
| 2364 |
} |
| 2365 |
|
| 2366 |
sw_pixel_alpha_t pixelAlpha = SW_PIXELFORMAT_ALPHA[format]; |
| 2367 |
bool alphaFound = !data; |
| 2368 |
|
| 2369 |
if (data && !isDepth) |
| 2370 |
{ |
| 2371 |
for (int i = 0; i < newSize; i++) dst[i] = src[i]; |
| 2372 |
|
| 2373 |
if (pixelAlpha != SW_PIXEL_ALPHA_NONE) |
| 2374 |
{ |
| 2375 |
for (int i = 0; i < newSize; i += bpp) |
| 2376 |
{ |
| 2377 |
uint8_t color[4] = { 0 }; |
| 2378 |
readColor8(color, &src[i], 0); |
| 2379 |
if (color[3] < 255) { alphaFound = true; break; } |
| 2380 |
} |
| 2381 |
} |
| 2382 |
} |
| 2383 |
else |
| 2384 |
{ |
| 2385 |
for (int i = 0; i < newSize; i++) dst[i] = 0; |
| 2386 |
} |
| 2387 |
|
| 2388 |
texture->readColor8 = readColor8; |
| 2389 |
texture->readColor = readColor; |
| 2390 |
texture->format = format; |
| 2391 |
texture->alpha = alphaFound? pixelAlpha : SW_PIXEL_ALPHA_NONE; |
| 2392 |
texture->width = w; |
| 2393 |
texture->height = h; |
| 2394 |
texture->wMinus1 = w - 1; |
| 2395 |
texture->hMinus1 = h - 1; |
| 2396 |
texture->tx = 1.0f/w; |
| 2397 |
texture->ty = 1.0f/h; |
| 2398 |
|
| 2399 |
return true; |
| 2400 |
} |
| 2401 |
|
| 2402 |
static inline void sw_texture_free(sw_texture_t *texture) |
| 2403 |
{ |
| 2404 |
SW_FREE(texture->pixels); |
| 2405 |
} |
| 2406 |
|
| 2407 |
static inline void sw_texture_sample_nearest(float *SW_RESTRICT color, const sw_texture_t *SW_RESTRICT tex, float u, float v) |
| 2408 |
{ |
| 2409 |
u = (tex->sWrap == SW_REPEAT)? sw_fract(u) : sw_saturate(u); |
| 2410 |
v = (tex->tWrap == SW_REPEAT)? sw_fract(v) : sw_saturate(v); |
| 2411 |
|
| 2412 |
int x = u*tex->width; |
| 2413 |
int y = v*tex->height; |
| 2414 |
|
| 2415 |
tex->readColor(color, tex->pixels, y*tex->width + x); |
| 2416 |
} |
| 2417 |
|
| 2418 |
static inline void sw_texture_sample_linear(float *SW_RESTRICT color, const sw_texture_t *SW_RESTRICT tex, float u, float v) |
| 2419 |
{ |
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
float xf = (u*tex->width) - 0.5f; |
| 2424 |
float yf = (v*tex->height) - 0.5f; |
| 2425 |
|
| 2426 |
float fx = sw_fract(xf); |
| 2427 |
float fy = sw_fract(yf); |
| 2428 |
|
| 2429 |
int x0 = (int)xf; |
| 2430 |
int y0 = (int)yf; |
| 2431 |
|
| 2432 |
int x1 = x0 + 1; |
| 2433 |
int y1 = y0 + 1; |
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
if (tex->sWrap == SW_CLAMP) |
| 2438 |
{ |
| 2439 |
x0 = (x0 > tex->wMinus1)? tex->wMinus1 : x0; |
| 2440 |
x1 = (x1 > tex->wMinus1)? tex->wMinus1 : x1; |
| 2441 |
} |
| 2442 |
else |
| 2443 |
{ |
| 2444 |
x0 = (x0%tex->width + tex->width)%tex->width; |
| 2445 |
x1 = (x1%tex->width + tex->width)%tex->width; |
| 2446 |
} |
| 2447 |
|
| 2448 |
if (tex->tWrap == SW_CLAMP) |
| 2449 |
{ |
| 2450 |
y0 = (y0 > tex->hMinus1)? tex->hMinus1 : y0; |
| 2451 |
y1 = (y1 > tex->hMinus1)? tex->hMinus1 : y1; |
| 2452 |
} |
| 2453 |
else |
| 2454 |
{ |
| 2455 |
y0 = (y0%tex->height + tex->height)%tex->height; |
| 2456 |
y1 = (y1%tex->height + tex->height)%tex->height; |
| 2457 |
} |
| 2458 |
|
| 2459 |
float c00[4], c10[4], c01[4], c11[4]; |
| 2460 |
tex->readColor(c00, tex->pixels, y0*tex->width + x0); |
| 2461 |
tex->readColor(c10, tex->pixels, y0*tex->width + x1); |
| 2462 |
tex->readColor(c01, tex->pixels, y1*tex->width + x0); |
| 2463 |
tex->readColor(c11, tex->pixels, y1*tex->width + x1); |
| 2464 |
|
| 2465 |
for (int i = 0; i < 4; i++) |
| 2466 |
{ |
| 2467 |
float t = c00[i] + fx*(c10[i] - c00[i]); |
| 2468 |
float b = c01[i] + fx*(c11[i] - c01[i]); |
| 2469 |
color[i] = t + fy*(b - t); |
| 2470 |
} |
| 2471 |
} |
| 2472 |
|
| 2473 |
static inline void sw_texture_sample(float *SW_RESTRICT color, const sw_texture_t *SW_RESTRICT tex, |
| 2474 |
float u, float v, float dUdx, float dUdy, float dVdx, float dVdy) |
| 2475 |
{ |
| 2476 |
|
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
|
| 2484 |
float dU2 = dUdx*dUdx + dUdy*dUdy; |
| 2485 |
float dV2 = dVdx*dVdx + dVdy*dVdy; |
| 2486 |
float L2 = (dU2 > dV2)? dU2 : dV2; |
| 2487 |
|
| 2488 |
SWfilter filter = (L2 > 1.0f)? tex->minFilter : tex->magFilter; |
| 2489 |
|
| 2490 |
switch (filter) |
| 2491 |
{ |
| 2492 |
case SW_NEAREST: sw_texture_sample_nearest(color, tex, u, v); break; |
| 2493 |
case SW_LINEAR: sw_texture_sample_linear(color, tex, u, v); break; |
| 2494 |
default: break; |
| 2495 |
} |
| 2496 |
} |
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
static inline bool sw_default_framebuffer_alloc(sw_default_framebuffer_t *fb, int w, int h) |
| 2502 |
{ |
| 2503 |
if (!sw_texture_alloc(&fb->color, NULL, w, h, SW_FRAMEBUFFER_COLOR_FORMAT)) |
| 2504 |
{ |
| 2505 |
return false; |
| 2506 |
} |
| 2507 |
|
| 2508 |
if (!sw_texture_alloc(&fb->depth, NULL, w, h, SW_FRAMEBUFFER_DEPTH_FORMAT)) |
| 2509 |
{ |
| 2510 |
return false; |
| 2511 |
} |
| 2512 |
|
| 2513 |
return true; |
| 2514 |
} |
| 2515 |
|
| 2516 |
static inline void sw_default_framebuffer_free(sw_default_framebuffer_t *fb) |
| 2517 |
{ |
| 2518 |
sw_texture_free(&fb->color); |
| 2519 |
sw_texture_free(&fb->depth); |
| 2520 |
} |
| 2521 |
|
| 2522 |
static inline void sw_framebuffer_fill_color(sw_texture_t *colorBuffer, const float color[4]) |
| 2523 |
{ |
| 2524 |
|
| 2525 |
|
| 2526 |
uint8_t pixel[16] = { 0 }; |
| 2527 |
SW_FRAMEBUFFER_COLOR_SET(pixel, color, 0); |
| 2528 |
|
| 2529 |
uint8_t *dst = (uint8_t *)colorBuffer->pixels; |
| 2530 |
|
| 2531 |
if (RLSW.userState & SW_STATE_SCISSOR_TEST) |
| 2532 |
{ |
| 2533 |
int xMin = sw_clamp_int(RLSW.scMin[0], 0, colorBuffer->width - 1); |
| 2534 |
int xMax = sw_clamp_int(RLSW.scMax[0], 0, colorBuffer->width - 1); |
| 2535 |
int yMin = sw_clamp_int(RLSW.scMin[1], 0, colorBuffer->height - 1); |
| 2536 |
int yMax = sw_clamp_int(RLSW.scMax[1], 0, colorBuffer->height - 1); |
| 2537 |
|
| 2538 |
int w = xMax - xMin; |
| 2539 |
for (int y = yMin; y <= yMax; y++) |
| 2540 |
{ |
| 2541 |
uint8_t *row = dst + (y*colorBuffer->width + xMin)*SW_FRAMEBUFFER_COLOR_SIZE; |
| 2542 |
for (int x = 0; x <= w; x++, row += SW_FRAMEBUFFER_COLOR_SIZE) |
| 2543 |
{ |
| 2544 |
for (int b = 0; b < SW_FRAMEBUFFER_COLOR_SIZE; b++) row[b] = pixel[b]; |
| 2545 |
} |
| 2546 |
} |
| 2547 |
} |
| 2548 |
else |
| 2549 |
{ |
| 2550 |
int size = colorBuffer->width*colorBuffer->height; |
| 2551 |
for (int i = 0; i < size; i++) |
| 2552 |
{ |
| 2553 |
for (int b = 0; b < SW_FRAMEBUFFER_COLOR_SIZE; b++) |
| 2554 |
{ |
| 2555 |
dst[i*SW_FRAMEBUFFER_COLOR_SIZE + b] = pixel[b]; |
| 2556 |
} |
| 2557 |
} |
| 2558 |
} |
| 2559 |
} |
| 2560 |
|
| 2561 |
static inline void sw_framebuffer_fill_depth(sw_texture_t *depthBuffer, float depth) |
| 2562 |
{ |
| 2563 |
|
| 2564 |
|
| 2565 |
uint8_t pixel[4] = { 0 }; |
| 2566 |
SW_FRAMEBUFFER_DEPTH_SET(pixel, depth, 0); |
| 2567 |
|
| 2568 |
uint8_t *dst = (uint8_t *)depthBuffer->pixels; |
| 2569 |
|
| 2570 |
if (RLSW.userState & SW_STATE_SCISSOR_TEST) |
| 2571 |
{ |
| 2572 |
int xMin = sw_clamp_int(RLSW.scMin[0], 0, depthBuffer->width - 1); |
| 2573 |
int xMax = sw_clamp_int(RLSW.scMax[0], 0, depthBuffer->width - 1); |
| 2574 |
int yMin = sw_clamp_int(RLSW.scMin[1], 0, depthBuffer->height - 1); |
| 2575 |
int yMax = sw_clamp_int(RLSW.scMax[1], 0, depthBuffer->height - 1); |
| 2576 |
|
| 2577 |
int w = xMax - xMin; |
| 2578 |
for (int y = yMin; y <= yMax; y++) |
| 2579 |
{ |
| 2580 |
uint8_t *row = dst + (y*depthBuffer->width + xMin)*SW_FRAMEBUFFER_DEPTH_SIZE; |
| 2581 |
for (int x = 0; x <= w; x++, row += SW_FRAMEBUFFER_DEPTH_SIZE) |
| 2582 |
{ |
| 2583 |
for (int b = 0; b < SW_FRAMEBUFFER_DEPTH_SIZE; b++) row[b] = pixel[b]; |
| 2584 |
} |
| 2585 |
} |
| 2586 |
} |
| 2587 |
else |
| 2588 |
{ |
| 2589 |
int size = depthBuffer->width*depthBuffer->height; |
| 2590 |
for (int i = 0; i < size; i++) |
| 2591 |
{ |
| 2592 |
for (int b = 0; b < SW_FRAMEBUFFER_DEPTH_SIZE; b++) |
| 2593 |
{ |
| 2594 |
dst[i*SW_FRAMEBUFFER_DEPTH_SIZE + b] = pixel[b]; |
| 2595 |
} |
| 2596 |
} |
| 2597 |
} |
| 2598 |
} |
| 2599 |
|
| 2600 |
static inline void sw_framebuffer_output_fast(void *dst, const sw_texture_t *buffer) |
| 2601 |
{ |
| 2602 |
int width = buffer->width; |
| 2603 |
int height = buffer->height; |
| 2604 |
|
| 2605 |
uint8_t *d = (uint8_t *)dst; |
| 2606 |
|
| 2607 |
#if SW_FRAMEBUFFER_OUTPUT_BGRA && (SW_FRAMEBUFFER_COLOR_FORMAT == SW_PIXELFORMAT_COLOR_R8G8B8A8) |
| 2608 |
for (int y = height - 1; y >= 0; y--) |
| 2609 |
{ |
| 2610 |
const uint8_t *src = (uint8_t *)(buffer->pixels) + y*width*4; |
| 2611 |
for (int x = 0; x < width; x++, src += 4, d += 4) |
| 2612 |
{ |
| 2613 |
d[0] = src[2]; |
| 2614 |
d[1] = src[1]; |
| 2615 |
d[2] = src[0]; |
| 2616 |
d[3] = src[3]; |
| 2617 |
} |
| 2618 |
} |
| 2619 |
#elif SW_FRAMEBUFFER_OUTPUT_BGRA && (SW_FRAMEBUFFER_COLOR_FORMAT == SW_PIXELFORMAT_COLOR_R8G8B8) |
| 2620 |
for (int y = height - 1; y >= 0; y--) |
| 2621 |
{ |
| 2622 |
const uint8_t *src = (uint8_t *)(buffer->pixels) + y*width*3; |
| 2623 |
for (int x = 0; x < width; x++, src += 3, d += 3) |
| 2624 |
{ |
| 2625 |
d[0] = src[2]; |
| 2626 |
d[1] = src[1]; |
| 2627 |
d[2] = src[0]; |
| 2628 |
} |
| 2629 |
} |
| 2630 |
#else |
| 2631 |
int rowBytes = width*SW_FRAMEBUFFER_COLOR_SIZE; |
| 2632 |
for (int y = height - 1; y >= 0; y--) |
| 2633 |
{ |
| 2634 |
const uint8_t *src = (uint8_t *)(buffer->pixels) + y*rowBytes; |
| 2635 |
for (int i = 0; i < rowBytes; i++) d[i] = src[i]; |
| 2636 |
d += rowBytes; |
| 2637 |
} |
| 2638 |
#endif |
| 2639 |
} |
| 2640 |
|
| 2641 |
static inline void sw_framebuffer_output_copy(void *dst, const sw_texture_t *buffer, int x, int y, int w, int h, sw_pixelformat_t format) |
| 2642 |
{ |
| 2643 |
int stride = buffer->width; |
| 2644 |
int dstPixelSize = SW_PIXELFORMAT_SIZE[format]; |
| 2645 |
sw_pixel_write_color8_f setColor8 = sw_pixel_get_write_color8_func(format); |
| 2646 |
|
| 2647 |
const uint8_t *src = (uint8_t *)(buffer->pixels) + ((y + h - 1)*stride + x)*SW_FRAMEBUFFER_COLOR_SIZE; |
| 2648 |
uint8_t *d = dst; |
| 2649 |
|
| 2650 |
for (int iy = 0; iy < h; iy++) |
| 2651 |
{ |
| 2652 |
const uint8_t *line = src; |
| 2653 |
uint8_t *dline = d; |
| 2654 |
|
| 2655 |
for (int ix = 0; ix < w; ix++) |
| 2656 |
{ |
| 2657 |
uint8_t color[4]; |
| 2658 |
SW_FRAMEBUFFER_COLOR8_GET(color, line, 0); |
| 2659 |
|
| 2660 |
#if SW_FRAMEBUFFER_OUTPUT_BGRA |
| 2661 |
if (format == SW_PIXELFORMAT_COLOR_R8G8B8A8 || format == SW_PIXELFORMAT_COLOR_R8G8B8) |
| 2662 |
{ |
| 2663 |
uint8_t tmp = color[0]; color[0] = color[2]; color[2] = tmp; |
| 2664 |
} |
| 2665 |
#endif |
| 2666 |
|
| 2667 |
setColor8(dline, color, 0); |
| 2668 |
line += SW_FRAMEBUFFER_COLOR_SIZE; |
| 2669 |
dline += dstPixelSize; |
| 2670 |
} |
| 2671 |
|
| 2672 |
src -= stride*SW_FRAMEBUFFER_COLOR_SIZE; |
| 2673 |
d += w*dstPixelSize; |
| 2674 |
} |
| 2675 |
} |
| 2676 |
|
| 2677 |
static inline void sw_framebuffer_output_blit(void *dst, const sw_texture_t *buffer, |
| 2678 |
int xDst, int yDst, int wDst, int hDst, int xSrc, int ySrc, int wSrc, int hSrc, sw_pixelformat_t format) |
| 2679 |
{ |
| 2680 |
const uint8_t *srcBase = buffer->pixels; |
| 2681 |
|
| 2682 |
int fbWidth = buffer->width; |
| 2683 |
int dstPixelSize = SW_PIXELFORMAT_SIZE[format]; |
| 2684 |
sw_pixel_write_color8_f setColor8 = sw_pixel_get_write_color8_func(format); |
| 2685 |
|
| 2686 |
uint32_t xScale = ((uint32_t)wSrc << 16)/(uint32_t)wDst; |
| 2687 |
uint32_t yScale = ((uint32_t)hSrc << 16)/(uint32_t)hDst; |
| 2688 |
|
| 2689 |
int ySrcLast = ySrc + hSrc - 1; |
| 2690 |
uint8_t *d = (uint8_t *)dst; |
| 2691 |
|
| 2692 |
for (int dy = 0; dy < hDst; dy++) |
| 2693 |
{ |
| 2694 |
int sy = ySrcLast - (int)(dy*yScale >> 16); |
| 2695 |
const uint8_t *srcLine = srcBase + (sy*fbWidth + xSrc)*SW_FRAMEBUFFER_COLOR_SIZE; |
| 2696 |
uint8_t *dline = d; |
| 2697 |
|
| 2698 |
for (int dx = 0; dx < wDst; dx++) |
| 2699 |
{ |
| 2700 |
int sx = (int)(dx*xScale >> 16); |
| 2701 |
const uint8_t *pixel = srcLine + sx*SW_FRAMEBUFFER_COLOR_SIZE; |
| 2702 |
|
| 2703 |
uint8_t color[4]; |
| 2704 |
SW_FRAMEBUFFER_COLOR8_GET(color, pixel, 0); |
| 2705 |
|
| 2706 |
#if SW_FRAMEBUFFER_OUTPUT_BGRA |
| 2707 |
if (format == SW_PIXELFORMAT_COLOR_R8G8B8A8 || format == SW_PIXELFORMAT_COLOR_R8G8B8) |
| 2708 |
{ |
| 2709 |
uint8_t tmp = color[0]; color[0] = color[2]; color[2] = tmp; |
| 2710 |
} |
| 2711 |
#endif |
| 2712 |
|
| 2713 |
setColor8(dline, color, 0); |
| 2714 |
dline += dstPixelSize; |
| 2715 |
} |
| 2716 |
|
| 2717 |
d += wDst*dstPixelSize; |
| 2718 |
} |
| 2719 |
} |
| 2720 |
|
| 2721 |
|
| 2722 |
|
| 2723 |
|
| 2724 |
|
| 2725 |
|
| 2726 |
#define SW_BF_ZERO(s,d,i) 0.0f |
| 2727 |
#define SW_BF_ONE(s,d,i) 1.0f |
| 2728 |
#define SW_BF_SRC_COLOR(s,d,i) (s)[i] |
| 2729 |
#define SW_BF_ONE_MINUS_SRC_COLOR(s,d,i) (1.0f-(s)[i]) |
| 2730 |
#define SW_BF_SRC_ALPHA(s,d,i) (s)[3] |
| 2731 |
#define SW_BF_ONE_MINUS_SRC_ALPHA(s,d,i) (1.0f-(s)[3]) |
| 2732 |
#define SW_BF_DST_ALPHA(s,d,i) (d)[3] |
| 2733 |
#define SW_BF_ONE_MINUS_DST_ALPHA(s,d,i) (1.0f-(d)[3]) |
| 2734 |
#define SW_BF_DST_COLOR(s,d,i) (d)[i] |
| 2735 |
#define SW_BF_ONE_MINUS_DST_COLOR(s,d,i) (1.0f-(d)[i]) |
| 2736 |
#define SW_BF_SRC_ALPHA_SATURATE(s,d,i) (((i)<3)? 1.0f : (((s)[3]<1.0f)?(s)[3]:1.0f)) |
| 2737 |
|
| 2738 |
|
| 2739 |
#define DEFINE_BLEND_FUNC(sn, dn, SF, DF) \ |
| 2740 |
static void sw_blend_##sn##_##dn(float *SW_RESTRICT dst, const float *SW_RESTRICT src) \ |
| 2741 |
{ \ |
| 2742 |
dst[0] = SF(src,dst,0)*src[0] + DF(src,dst,0)*dst[0]; \ |
| 2743 |
dst[1] = SF(src,dst,1)*src[1] + DF(src,dst,1)*dst[1]; \ |
| 2744 |
dst[2] = SF(src,dst,2)*src[2] + DF(src,dst,2)*dst[2]; \ |
| 2745 |
dst[3] = SF(src,dst,3)*src[3] + DF(src,dst,3)*dst[3]; \ |
| 2746 |
} |
| 2747 |
|
| 2748 |
|
| 2749 |
|
| 2750 |
#define FOREACH_FACTOR(X) \ |
| 2751 |
X(ZERO, SW_ZERO, 0, SW_BF_ZERO ) \ |
| 2752 |
X(ONE, SW_ONE, 1, SW_BF_ONE ) \ |
| 2753 |
X(SRC_COLOR, SW_SRC_COLOR, 2, SW_BF_SRC_COLOR ) \ |
| 2754 |
X(ONE_MINUS_SRC_COLOR, SW_ONE_MINUS_SRC_COLOR, 3, SW_BF_ONE_MINUS_SRC_COLOR) \ |
| 2755 |
X(SRC_ALPHA, SW_SRC_ALPHA, 4, SW_BF_SRC_ALPHA ) \ |
| 2756 |
X(ONE_MINUS_SRC_ALPHA, SW_ONE_MINUS_SRC_ALPHA, 5, SW_BF_ONE_MINUS_SRC_ALPHA) \ |
| 2757 |
X(DST_ALPHA, SW_DST_ALPHA, 6, SW_BF_DST_ALPHA ) \ |
| 2758 |
X(ONE_MINUS_DST_ALPHA, SW_ONE_MINUS_DST_ALPHA, 7, SW_BF_ONE_MINUS_DST_ALPHA) \ |
| 2759 |
X(DST_COLOR, SW_DST_COLOR, 8, SW_BF_DST_COLOR ) \ |
| 2760 |
X(ONE_MINUS_DST_COLOR, SW_ONE_MINUS_DST_COLOR, 9, SW_BF_ONE_MINUS_DST_COLOR) \ |
| 2761 |
X(SRC_ALPHA_SATURATE, SW_SRC_ALPHA_SATURATE, 10, SW_BF_SRC_ALPHA_SATURATE ) |
| 2762 |
|
| 2763 |
|
| 2764 |
#define FOREACH_FACTOR_WITH(X, A, B, C) \ |
| 2765 |
X(ZERO, SW_ZERO, 0, SW_BF_ZERO, A,B,C) \ |
| 2766 |
X(ONE, SW_ONE, 1, SW_BF_ONE, A,B,C) \ |
| 2767 |
X(SRC_COLOR, SW_SRC_COLOR, 2, SW_BF_SRC_COLOR, A,B,C) \ |
| 2768 |
X(ONE_MINUS_SRC_COLOR, SW_ONE_MINUS_SRC_COLOR, 3, SW_BF_ONE_MINUS_SRC_COLOR,A,B,C) \ |
| 2769 |
X(SRC_ALPHA, SW_SRC_ALPHA, 4, SW_BF_SRC_ALPHA, A,B,C) \ |
| 2770 |
X(ONE_MINUS_SRC_ALPHA, SW_ONE_MINUS_SRC_ALPHA, 5, SW_BF_ONE_MINUS_SRC_ALPHA,A,B,C) \ |
| 2771 |
X(DST_ALPHA, SW_DST_ALPHA, 6, SW_BF_DST_ALPHA, A,B,C) \ |
| 2772 |
X(ONE_MINUS_DST_ALPHA, SW_ONE_MINUS_DST_ALPHA, 7, SW_BF_ONE_MINUS_DST_ALPHA,A,B,C) \ |
| 2773 |
X(DST_COLOR, SW_DST_COLOR, 8, SW_BF_DST_COLOR, A,B,C) \ |
| 2774 |
X(ONE_MINUS_DST_COLOR, SW_ONE_MINUS_DST_COLOR, 9, SW_BF_ONE_MINUS_DST_COLOR,A,B,C) \ |
| 2775 |
X(SRC_ALPHA_SATURATE, SW_SRC_ALPHA_SATURATE, 10, SW_BF_SRC_ALPHA_SATURATE, A,B,C) |
| 2776 |
|
| 2777 |
|
| 2778 |
#define GEN_COMBO(dn, de, di, df, sn, si, sf) DEFINE_BLEND_FUNC(sn, dn, sf, df) |
| 2779 |
|
| 2780 |
|
| 2781 |
#define GEN_ROW(sn, se, si, sf) FOREACH_FACTOR_WITH(GEN_COMBO, sn, si, sf) |
| 2782 |
|
| 2783 |
|
| 2784 |
FOREACH_FACTOR(GEN_ROW) |
| 2785 |
|
| 2786 |
#undef GEN_COMBO |
| 2787 |
#undef GEN_ROW |
| 2788 |
|
| 2789 |
|
| 2790 |
#define GEN_TABLE_ENTRY(dn, de, di, df, sn, si, sf) [si][di] = sw_blend_##sn##_##dn, |
| 2791 |
|
| 2792 |
|
| 2793 |
#define GEN_TABLE_ROW(sn, se, si, sf) FOREACH_FACTOR_WITH(GEN_TABLE_ENTRY, sn, si, sf) |
| 2794 |
|
| 2795 |
|
| 2796 |
#define SW_BLEND_FACTOR_COUNT 11 |
| 2797 |
static const sw_blend_f SW_BLEND_TABLE[SW_BLEND_FACTOR_COUNT][SW_BLEND_FACTOR_COUNT] = { |
| 2798 |
FOREACH_FACTOR(GEN_TABLE_ROW) |
| 2799 |
}; |
| 2800 |
|
| 2801 |
#undef GEN_TABLE_ENTRY |
| 2802 |
#undef GEN_TABLE_ROW |
| 2803 |
|
| 2804 |
|
| 2805 |
static inline int sw_blend_factor_index(SWfactor f) |
| 2806 |
{ |
| 2807 |
switch (f) |
| 2808 |
{ |
| 2809 |
case SW_ZERO: return 0; |
| 2810 |
case SW_ONE: return 1; |
| 2811 |
case SW_SRC_COLOR: return 2; |
| 2812 |
case SW_ONE_MINUS_SRC_COLOR: return 3; |
| 2813 |
case SW_SRC_ALPHA: return 4; |
| 2814 |
case SW_ONE_MINUS_SRC_ALPHA: return 5; |
| 2815 |
case SW_DST_ALPHA: return 6; |
| 2816 |
case SW_ONE_MINUS_DST_ALPHA: return 7; |
| 2817 |
case SW_DST_COLOR: return 8; |
| 2818 |
case SW_ONE_MINUS_DST_COLOR: return 9; |
| 2819 |
case SW_SRC_ALPHA_SATURATE: return 10; |
| 2820 |
default: return -1; |
| 2821 |
} |
| 2822 |
} |
| 2823 |
|
| 2824 |
static bool sw_blend_factor_needs_alpha(SWfactor f) |
| 2825 |
{ |
| 2826 |
switch (f) |
| 2827 |
{ |
| 2828 |
case SW_SRC_ALPHA: |
| 2829 |
case SW_ONE_MINUS_SRC_ALPHA: |
| 2830 |
case SW_DST_ALPHA: |
| 2831 |
case SW_ONE_MINUS_DST_ALPHA: |
| 2832 |
case SW_SRC_ALPHA_SATURATE: return true; |
| 2833 |
default: break; |
| 2834 |
} |
| 2835 |
|
| 2836 |
return false; |
| 2837 |
} |
| 2838 |
|
| 2839 |
static uint32_t sw_blend_compute_flags(SWfactor src, SWfactor dst) |
| 2840 |
{ |
| 2841 |
uint32_t flags = 0; |
| 2842 |
|
| 2843 |
|
| 2844 |
if (src == SW_ONE && dst == SW_ZERO) flags |= SW_BLEND_FLAG_NOOP; |
| 2845 |
|
| 2846 |
|
| 2847 |
if (sw_blend_factor_needs_alpha(src) || sw_blend_factor_needs_alpha(dst)) flags |= SW_BLEND_FLAG_NEEDS_ALPHA; |
| 2848 |
|
| 2849 |
return flags; |
| 2850 |
} |
| 2851 |
|
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
static inline void sw_project_ndc_to_screen(float ndc[4]) |
| 2856 |
{ |
| 2857 |
ndc[0] = RLSW.vpCenter[0] + ndc[0]*RLSW.vpHalf[0] + 0.5f; |
| 2858 |
ndc[1] = RLSW.vpCenter[1] + ndc[1]*RLSW.vpHalf[1] + 0.5f; |
| 2859 |
} |
| 2860 |
|
| 2861 |
|
| 2862 |
|
| 2863 |
|
| 2864 |
#define DEFINE_CLIP_FUNC(name, FUNC_IS_INSIDE, FUNC_COMPUTE_T) \ |
| 2865 |
static int sw_clip_##name( \ |
| 2866 |
sw_vertex_t output[SW_MAX_CLIPPED_POLYGON_VERTICES], \ |
| 2867 |
const sw_vertex_t input[SW_MAX_CLIPPED_POLYGON_VERTICES], \ |
| 2868 |
int n) \ |
| 2869 |
{ \ |
| 2870 |
const sw_vertex_t *prev = &input[n - 1]; \ |
| 2871 |
int prevInside = FUNC_IS_INSIDE(prev->position); \ |
| 2872 |
int outputCount = 0; \ |
| 2873 |
\ |
| 2874 |
for (int i = 0; i < n; i++) { \ |
| 2875 |
const sw_vertex_t *curr = &input[i]; \ |
| 2876 |
int currInside = FUNC_IS_INSIDE(curr->position); \ |
| 2877 |
\ |
| 2878 |
\ |
| 2879 |
if (prevInside != currInside) { \ |
| 2880 |
float t = FUNC_COMPUTE_T(prev->position, curr->position); \ |
| 2881 |
sw_lerp_vertex_PCT(&output[outputCount++], prev, curr, t); \ |
| 2882 |
} \ |
| 2883 |
\ |
| 2884 |
\ |
| 2885 |
if (currInside) { \ |
| 2886 |
output[outputCount++] = *curr; \ |
| 2887 |
} \ |
| 2888 |
\ |
| 2889 |
prev = curr; \ |
| 2890 |
prevInside = currInside; \ |
| 2891 |
} \ |
| 2892 |
\ |
| 2893 |
return outputCount; \ |
| 2894 |
} |
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
#define IS_INSIDE_PLANE_W(h) ((h)[3] >= SW_CLIP_EPSILON) |
| 2900 |
#define IS_INSIDE_PLANE_X_POS(h) ( (h)[0] < (h)[3]) // Exclusive for +X |
| 2901 |
#define IS_INSIDE_PLANE_X_NEG(h) (-(h)[0] < (h)[3]) // Exclusive for -X |
| 2902 |
#define IS_INSIDE_PLANE_Y_POS(h) ( (h)[1] < (h)[3]) // Exclusive for +Y |
| 2903 |
#define IS_INSIDE_PLANE_Y_NEG(h) (-(h)[1] < (h)[3]) // Exclusive for -Y |
| 2904 |
#define IS_INSIDE_PLANE_Z_POS(h) ( (h)[2] <= (h)[3]) // Inclusive for +Z |
| 2905 |
#define IS_INSIDE_PLANE_Z_NEG(h) (-(h)[2] <= (h)[3]) // Inclusive for -Z |
| 2906 |
|
| 2907 |
#define COMPUTE_T_PLANE_W(hPrev, hCurr) ((SW_CLIP_EPSILON - (hPrev)[3])/((hCurr)[3] - (hPrev)[3])) |
| 2908 |
#define COMPUTE_T_PLANE_X_POS(hPrev, hCurr) (((hPrev)[3] - (hPrev)[0])/(((hPrev)[3] - (hPrev)[0]) - ((hCurr)[3] - (hCurr)[0]))) |
| 2909 |
#define COMPUTE_T_PLANE_X_NEG(hPrev, hCurr) (((hPrev)[3] + (hPrev)[0])/(((hPrev)[3] + (hPrev)[0]) - ((hCurr)[3] + (hCurr)[0]))) |
| 2910 |
#define COMPUTE_T_PLANE_Y_POS(hPrev, hCurr) (((hPrev)[3] - (hPrev)[1])/(((hPrev)[3] - (hPrev)[1]) - ((hCurr)[3] - (hCurr)[1]))) |
| 2911 |
#define COMPUTE_T_PLANE_Y_NEG(hPrev, hCurr) (((hPrev)[3] + (hPrev)[1])/(((hPrev)[3] + (hPrev)[1]) - ((hCurr)[3] + (hCurr)[1]))) |
| 2912 |
#define COMPUTE_T_PLANE_Z_POS(hPrev, hCurr) (((hPrev)[3] - (hPrev)[2])/(((hPrev)[3] - (hPrev)[2]) - ((hCurr)[3] - (hCurr)[2]))) |
| 2913 |
#define COMPUTE_T_PLANE_Z_NEG(hPrev, hCurr) (((hPrev)[3] + (hPrev)[2])/(((hPrev)[3] + (hPrev)[2]) - ((hCurr)[3] + (hCurr)[2]))) |
| 2914 |
|
| 2915 |
DEFINE_CLIP_FUNC(w, IS_INSIDE_PLANE_W, COMPUTE_T_PLANE_W) |
| 2916 |
DEFINE_CLIP_FUNC(x_pos, IS_INSIDE_PLANE_X_POS, COMPUTE_T_PLANE_X_POS) |
| 2917 |
DEFINE_CLIP_FUNC(x_neg, IS_INSIDE_PLANE_X_NEG, COMPUTE_T_PLANE_X_NEG) |
| 2918 |
DEFINE_CLIP_FUNC(y_pos, IS_INSIDE_PLANE_Y_POS, COMPUTE_T_PLANE_Y_POS) |
| 2919 |
DEFINE_CLIP_FUNC(y_neg, IS_INSIDE_PLANE_Y_NEG, COMPUTE_T_PLANE_Y_NEG) |
| 2920 |
DEFINE_CLIP_FUNC(z_pos, IS_INSIDE_PLANE_Z_POS, COMPUTE_T_PLANE_Z_POS) |
| 2921 |
DEFINE_CLIP_FUNC(z_neg, IS_INSIDE_PLANE_Z_NEG, COMPUTE_T_PLANE_Z_NEG) |
| 2922 |
|
| 2923 |
|
| 2924 |
|
| 2925 |
|
| 2926 |
#define COMPUTE_T_SCISSOR_X_MIN(hPrev, hCurr) (((RLSW.scClipMin[0])*(hPrev)[3] - (hPrev)[0])/(((hCurr)[0] - (RLSW.scClipMin[0])*(hCurr)[3]) - ((hPrev)[0] - (RLSW.scClipMin[0])*(hPrev)[3]))) |
| 2927 |
#define COMPUTE_T_SCISSOR_X_MAX(hPrev, hCurr) (((RLSW.scClipMax[0])*(hPrev)[3] - (hPrev)[0])/(((hCurr)[0] - (RLSW.scClipMax[0])*(hCurr)[3]) - ((hPrev)[0] - (RLSW.scClipMax[0])*(hPrev)[3]))) |
| 2928 |
#define COMPUTE_T_SCISSOR_Y_MIN(hPrev, hCurr) (((RLSW.scClipMin[1])*(hPrev)[3] - (hPrev)[1])/(((hCurr)[1] - (RLSW.scClipMin[1])*(hCurr)[3]) - ((hPrev)[1] - (RLSW.scClipMin[1])*(hPrev)[3]))) |
| 2929 |
#define COMPUTE_T_SCISSOR_Y_MAX(hPrev, hCurr) (((RLSW.scClipMax[1])*(hPrev)[3] - (hPrev)[1])/(((hCurr)[1] - (RLSW.scClipMax[1])*(hCurr)[3]) - ((hPrev)[1] - (RLSW.scClipMax[1])*(hPrev)[3]))) |
| 2930 |
|
| 2931 |
#define IS_INSIDE_SCISSOR_X_MIN(h) ((h)[0] >= (RLSW.scClipMin[0])*(h)[3]) |
| 2932 |
#define IS_INSIDE_SCISSOR_X_MAX(h) ((h)[0] <= (RLSW.scClipMax[0])*(h)[3]) |
| 2933 |
#define IS_INSIDE_SCISSOR_Y_MIN(h) ((h)[1] >= (RLSW.scClipMin[1])*(h)[3]) |
| 2934 |
#define IS_INSIDE_SCISSOR_Y_MAX(h) ((h)[1] <= (RLSW.scClipMax[1])*(h)[3]) |
| 2935 |
|
| 2936 |
DEFINE_CLIP_FUNC(scissor_x_min, IS_INSIDE_SCISSOR_X_MIN, COMPUTE_T_SCISSOR_X_MIN) |
| 2937 |
DEFINE_CLIP_FUNC(scissor_x_max, IS_INSIDE_SCISSOR_X_MAX, COMPUTE_T_SCISSOR_X_MAX) |
| 2938 |
DEFINE_CLIP_FUNC(scissor_y_min, IS_INSIDE_SCISSOR_Y_MIN, COMPUTE_T_SCISSOR_Y_MIN) |
| 2939 |
DEFINE_CLIP_FUNC(scissor_y_max, IS_INSIDE_SCISSOR_Y_MAX, COMPUTE_T_SCISSOR_Y_MAX) |
| 2940 |
|
| 2941 |
|
| 2942 |
|
| 2943 |
static bool sw_polygon_clip(sw_vertex_t polygon[SW_MAX_CLIPPED_POLYGON_VERTICES], int *vertexCounter) |
| 2944 |
{ |
| 2945 |
static sw_vertex_t tmp[SW_MAX_CLIPPED_POLYGON_VERTICES]; |
| 2946 |
|
| 2947 |
int n = *vertexCounter; |
| 2948 |
|
| 2949 |
#define CLIP_AGAINST_PLANE(FUNC_CLIP) \ |
| 2950 |
{ \ |
| 2951 |
n = FUNC_CLIP(tmp, polygon, n); \ |
| 2952 |
if (n < 3) \ |
| 2953 |
{ \ |
| 2954 |
*vertexCounter = 0; \ |
| 2955 |
return false; \ |
| 2956 |
} \ |
| 2957 |
for (int i = 0; i < n; i++) polygon[i] = tmp[i]; \ |
| 2958 |
} |
| 2959 |
|
| 2960 |
CLIP_AGAINST_PLANE(sw_clip_w); |
| 2961 |
CLIP_AGAINST_PLANE(sw_clip_x_pos); |
| 2962 |
CLIP_AGAINST_PLANE(sw_clip_x_neg); |
| 2963 |
CLIP_AGAINST_PLANE(sw_clip_y_pos); |
| 2964 |
CLIP_AGAINST_PLANE(sw_clip_y_neg); |
| 2965 |
CLIP_AGAINST_PLANE(sw_clip_z_pos); |
| 2966 |
CLIP_AGAINST_PLANE(sw_clip_z_neg); |
| 2967 |
|
| 2968 |
if (RLSW.userState & SW_STATE_SCISSOR_TEST) |
| 2969 |
{ |
| 2970 |
CLIP_AGAINST_PLANE(sw_clip_scissor_x_min); |
| 2971 |
CLIP_AGAINST_PLANE(sw_clip_scissor_x_max); |
| 2972 |
CLIP_AGAINST_PLANE(sw_clip_scissor_y_min); |
| 2973 |
CLIP_AGAINST_PLANE(sw_clip_scissor_y_max); |
| 2974 |
} |
| 2975 |
|
| 2976 |
*vertexCounter = n; |
| 2977 |
|
| 2978 |
return (n >= 3); |
| 2979 |
} |
| 2980 |
|
| 2981 |
|
| 2982 |
|
| 2983 |
#ifndef RLSW_TEMPLATE_RASTER_TRIANGLE_EXPANDING |
| 2984 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE_EXPANDING |
| 2985 |
|
| 2986 |
|
| 2987 |
#define SW_RASTER_TRIANGLE_STATE_MASK \ |
| 2988 |
(SW_STATE_TEXTURE_2D | SW_STATE_DEPTH_TEST | SW_STATE_BLEND) |
| 2989 |
|
| 2990 |
|
| 2991 |
|
| 2992 |
#define SW_RASTER_VARIANTS(X) \ |
| 2993 |
X(BASE, 0) \ |
| 2994 |
X(TEX, SW_STATE_TEXTURE_2D) \ |
| 2995 |
X(DEPTH, SW_STATE_DEPTH_TEST) \ |
| 2996 |
X(BLEND, SW_STATE_BLEND) \ |
| 2997 |
X(TEX_DEPTH, SW_STATE_TEXTURE_2D | SW_STATE_DEPTH_TEST) \ |
| 2998 |
X(TEX_BLEND, SW_STATE_TEXTURE_2D | SW_STATE_BLEND) \ |
| 2999 |
X(DEPTH_BLEND, SW_STATE_DEPTH_TEST | SW_STATE_BLEND) \ |
| 3000 |
X(TEX_DEPTH_BLEND, SW_STATE_TEXTURE_2D | SW_STATE_DEPTH_TEST | SW_STATE_BLEND) |
| 3001 |
|
| 3002 |
|
| 3003 |
|
| 3004 |
#define SW_FWD_DECL(NAME, _FLAGS) \ |
| 3005 |
static void sw_raster_triangle_##NAME(const sw_vertex_t*, const sw_vertex_t*, const sw_vertex_t*); |
| 3006 |
SW_RASTER_VARIANTS(SW_FWD_DECL) |
| 3007 |
#undef SW_FWD_DECL |
| 3008 |
|
| 3009 |
|
| 3010 |
|
| 3011 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE BASE |
| 3012 |
#include __FILE__ |
| 3013 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 3014 |
|
| 3015 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE TEX |
| 3016 |
#define SW_ENABLE_TEXTURE |
| 3017 |
#include __FILE__ |
| 3018 |
#undef SW_ENABLE_TEXTURE |
| 3019 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 3020 |
|
| 3021 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE DEPTH |
| 3022 |
#define SW_ENABLE_DEPTH_TEST |
| 3023 |
#include __FILE__ |
| 3024 |
#undef SW_ENABLE_DEPTH_TEST |
| 3025 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 3026 |
|
| 3027 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE BLEND |
| 3028 |
#define SW_ENABLE_BLEND |
| 3029 |
#include __FILE__ |
| 3030 |
#undef SW_ENABLE_BLEND |
| 3031 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 3032 |
|
| 3033 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE TEX_DEPTH |
| 3034 |
#define SW_ENABLE_TEXTURE |
| 3035 |
#define SW_ENABLE_DEPTH_TEST |
| 3036 |
#include __FILE__ |
| 3037 |
#undef SW_ENABLE_DEPTH_TEST |
| 3038 |
#undef SW_ENABLE_TEXTURE |
| 3039 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 3040 |
|
| 3041 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE TEX_BLEND |
| 3042 |
#define SW_ENABLE_TEXTURE |
| 3043 |
#define SW_ENABLE_BLEND |
| 3044 |
#include __FILE__ |
| 3045 |
#undef SW_ENABLE_BLEND |
| 3046 |
#undef SW_ENABLE_TEXTURE |
| 3047 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 3048 |
|
| 3049 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE DEPTH_BLEND |
| 3050 |
#define SW_ENABLE_DEPTH_TEST |
| 3051 |
#define SW_ENABLE_BLEND |
| 3052 |
#include __FILE__ |
| 3053 |
#undef SW_ENABLE_BLEND |
| 3054 |
#undef SW_ENABLE_DEPTH_TEST |
| 3055 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 3056 |
|
| 3057 |
#define RLSW_TEMPLATE_RASTER_TRIANGLE TEX_DEPTH_BLEND |
| 3058 |
#define SW_ENABLE_TEXTURE |
| 3059 |
#define SW_ENABLE_DEPTH_TEST |
| 3060 |
#define SW_ENABLE_BLEND |
| 3061 |
#include __FILE__ |
| 3062 |
#undef SW_ENABLE_BLEND |
| 3063 |
#undef SW_ENABLE_DEPTH_TEST |
| 3064 |
#undef SW_ENABLE_TEXTURE |
| 3065 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 3066 |
|
| 3067 |
|
| 3068 |
#define SW_TABLE_ENTRY(NAME, FLAGS) [FLAGS] = sw_raster_triangle_##NAME, |
| 3069 |
static const sw_raster_triangle_f SW_RASTER_TRIANGLE_TABLE[] = { |
| 3070 |
SW_RASTER_VARIANTS(SW_TABLE_ENTRY) |
| 3071 |
}; |
| 3072 |
#undef SW_TABLE_ENTRY |
| 3073 |
|
| 3074 |
#undef SW_RASTER_VARIANTS |
| 3075 |
#undef RLSW_TEMPLATE_RASTER_TRIANGLE_EXPANDING |
| 3076 |
|
| 3077 |
#endif |
| 3078 |
|
| 3079 |
|
| 3080 |
|
| 3081 |
|
| 3082 |
#ifndef RLSW_TEMPLATE_RASTER_QUAD_EXPANDING |
| 3083 |
#define RLSW_TEMPLATE_RASTER_QUAD_EXPANDING |
| 3084 |
|
| 3085 |
|
| 3086 |
#define SW_RASTER_QUAD_STATE_MASK \ |
| 3087 |
(SW_STATE_TEXTURE_2D | SW_STATE_DEPTH_TEST | SW_STATE_BLEND) |
| 3088 |
|
| 3089 |
|
| 3090 |
|
| 3091 |
#define SW_RASTER_VARIANTS(X) \ |
| 3092 |
X(BASE, 0) \ |
| 3093 |
X(TEX, SW_STATE_TEXTURE_2D) \ |
| 3094 |
X(DEPTH, SW_STATE_DEPTH_TEST) \ |
| 3095 |
X(BLEND, SW_STATE_BLEND) \ |
| 3096 |
X(TEX_DEPTH, SW_STATE_TEXTURE_2D | SW_STATE_DEPTH_TEST) \ |
| 3097 |
X(TEX_BLEND, SW_STATE_TEXTURE_2D | SW_STATE_BLEND) \ |
| 3098 |
X(DEPTH_BLEND, SW_STATE_DEPTH_TEST | SW_STATE_BLEND) \ |
| 3099 |
X(TEX_DEPTH_BLEND, SW_STATE_TEXTURE_2D | SW_STATE_DEPTH_TEST | SW_STATE_BLEND) |
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
#define SW_FWD_DECL(NAME, _FLAGS) \ |
| 3104 |
static void sw_raster_quad_##NAME(const sw_vertex_t *v0, const sw_vertex_t *v1, const sw_vertex_t *v2, const sw_vertex_t *v3); |
| 3105 |
SW_RASTER_VARIANTS(SW_FWD_DECL) |
| 3106 |
#undef SW_FWD_DECL |
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
#define RLSW_TEMPLATE_RASTER_QUAD BASE |
| 3111 |
#include __FILE__ |
| 3112 |
#undef RLSW_TEMPLATE_RASTER_QUAD |
| 3113 |
|
| 3114 |
#define RLSW_TEMPLATE_RASTER_QUAD TEX |
| 3115 |
#define SW_ENABLE_TEXTURE |
| 3116 |
#include __FILE__ |
| 3117 |
#undef SW_ENABLE_TEXTURE |
| 3118 |
#undef RLSW_TEMPLATE_RASTER_QUAD |
| 3119 |
|
| 3120 |
#define RLSW_TEMPLATE_RASTER_QUAD DEPTH |
| 3121 |
#define SW_ENABLE_DEPTH_TEST |
| 3122 |
#include __FILE__ |
| 3123 |
#undef SW_ENABLE_DEPTH_TEST |
| 3124 |
#undef RLSW_TEMPLATE_RASTER_QUAD |
| 3125 |
|
| 3126 |
#define RLSW_TEMPLATE_RASTER_QUAD BLEND |
| 3127 |
#define SW_ENABLE_BLEND |
| 3128 |
#include __FILE__ |
| 3129 |
#undef SW_ENABLE_BLEND |
| 3130 |
#undef RLSW_TEMPLATE_RASTER_QUAD |
| 3131 |
|
| 3132 |
#define RLSW_TEMPLATE_RASTER_QUAD TEX_DEPTH |
| 3133 |
#define SW_ENABLE_TEXTURE |
| 3134 |
#define SW_ENABLE_DEPTH_TEST |
| 3135 |
#include __FILE__ |
| 3136 |
#undef SW_ENABLE_DEPTH_TEST |
| 3137 |
#undef SW_ENABLE_TEXTURE |
| 3138 |
#undef RLSW_TEMPLATE_RASTER_QUAD |
| 3139 |
|
| 3140 |
#define RLSW_TEMPLATE_RASTER_QUAD TEX_BLEND |
| 3141 |
#define SW_ENABLE_TEXTURE |
| 3142 |
#define SW_ENABLE_BLEND |
| 3143 |
#include __FILE__ |
| 3144 |
#undef SW_ENABLE_BLEND |
| 3145 |
#undef SW_ENABLE_TEXTURE |
| 3146 |
#undef RLSW_TEMPLATE_RASTER_QUAD |
| 3147 |
|
| 3148 |
#define RLSW_TEMPLATE_RASTER_QUAD DEPTH_BLEND |
| 3149 |
#define SW_ENABLE_DEPTH_TEST |
| 3150 |
#define SW_ENABLE_BLEND |
| 3151 |
#include __FILE__ |
| 3152 |
#undef SW_ENABLE_BLEND |
| 3153 |
#undef SW_ENABLE_DEPTH_TEST |
| 3154 |
#undef RLSW_TEMPLATE_RASTER_QUAD |
| 3155 |
|
| 3156 |
#define RLSW_TEMPLATE_RASTER_QUAD TEX_DEPTH_BLEND |
| 3157 |
#define SW_ENABLE_TEXTURE |
| 3158 |
#define SW_ENABLE_DEPTH_TEST |
| 3159 |
#define SW_ENABLE_BLEND |
| 3160 |
#include __FILE__ |
| 3161 |
#undef SW_ENABLE_BLEND |
| 3162 |
#undef SW_ENABLE_DEPTH_TEST |
| 3163 |
#undef SW_ENABLE_TEXTURE |
| 3164 |
#undef RLSW_TEMPLATE_RASTER_QUAD |
| 3165 |
|
| 3166 |
|
| 3167 |
#define SW_TABLE_ENTRY(NAME, FLAGS) [FLAGS] = sw_raster_quad_##NAME, |
| 3168 |
static const sw_raster_quad_f SW_RASTER_QUAD_TABLE[] = { |
| 3169 |
SW_RASTER_VARIANTS(SW_TABLE_ENTRY) |
| 3170 |
}; |
| 3171 |
#undef SW_TABLE_ENTRY |
| 3172 |
|
| 3173 |
#undef SW_RASTER_VARIANTS |
| 3174 |
#undef RLSW_TEMPLATE_RASTER_QUAD_EXPANDING |
| 3175 |
|
| 3176 |
#endif |
| 3177 |
|
| 3178 |
|
| 3179 |
|
| 3180 |
|
| 3181 |
#ifndef RLSW_TEMPLATE_RASTER_LINE_EXPANDING |
| 3182 |
#define RLSW_TEMPLATE_RASTER_LINE_EXPANDING |
| 3183 |
|
| 3184 |
|
| 3185 |
#define SW_RASTER_LINE_STATE_MASK \ |
| 3186 |
(SW_STATE_DEPTH_TEST | SW_STATE_BLEND) |
| 3187 |
|
| 3188 |
|
| 3189 |
|
| 3190 |
#define SW_RASTER_VARIANTS(X) \ |
| 3191 |
X(BASE, 0) \ |
| 3192 |
X(DEPTH, SW_STATE_DEPTH_TEST) \ |
| 3193 |
X(BLEND, SW_STATE_BLEND) \ |
| 3194 |
X(DEPTH_BLEND, SW_STATE_DEPTH_TEST | SW_STATE_BLEND) |
| 3195 |
|
| 3196 |
|
| 3197 |
|
| 3198 |
#define SW_FWD_DECL(NAME, _FLAGS) \ |
| 3199 |
static void sw_raster_line_##NAME(const sw_vertex_t *v0, const sw_vertex_t *v1); \ |
| 3200 |
static void sw_raster_line_thick_##NAME(const sw_vertex_t *v0, const sw_vertex_t *v1); |
| 3201 |
SW_RASTER_VARIANTS(SW_FWD_DECL) |
| 3202 |
#undef SW_FWD_DECL |
| 3203 |
|
| 3204 |
|
| 3205 |
|
| 3206 |
#define RLSW_TEMPLATE_RASTER_LINE BASE |
| 3207 |
#include __FILE__ |
| 3208 |
#undef RLSW_TEMPLATE_RASTER_LINE |
| 3209 |
|
| 3210 |
#define RLSW_TEMPLATE_RASTER_LINE DEPTH |
| 3211 |
#define SW_ENABLE_DEPTH_TEST |
| 3212 |
#include __FILE__ |
| 3213 |
#undef SW_ENABLE_DEPTH_TEST |
| 3214 |
#undef RLSW_TEMPLATE_RASTER_LINE |
| 3215 |
|
| 3216 |
#define RLSW_TEMPLATE_RASTER_LINE BLEND |
| 3217 |
#define SW_ENABLE_BLEND |
| 3218 |
#include __FILE__ |
| 3219 |
#undef SW_ENABLE_BLEND |
| 3220 |
#undef RLSW_TEMPLATE_RASTER_LINE |
| 3221 |
|
| 3222 |
#define RLSW_TEMPLATE_RASTER_LINE DEPTH_BLEND |
| 3223 |
#define SW_ENABLE_DEPTH_TEST |
| 3224 |
#define SW_ENABLE_BLEND |
| 3225 |
#include __FILE__ |
| 3226 |
#undef SW_ENABLE_BLEND |
| 3227 |
#undef SW_ENABLE_DEPTH_TEST |
| 3228 |
#undef RLSW_TEMPLATE_RASTER_LINE |
| 3229 |
|
| 3230 |
|
| 3231 |
#define SW_TABLE_ENTRY0(NAME, FLAGS) [FLAGS] = sw_raster_line_##NAME, |
| 3232 |
#define SW_TABLE_ENTRY1(NAME, FLAGS) [FLAGS] = sw_raster_line_thick_##NAME, |
| 3233 |
static const sw_raster_line_f SW_RASTER_LINE_TABLE[] = { SW_RASTER_VARIANTS(SW_TABLE_ENTRY0) }; |
| 3234 |
static const sw_raster_line_f SW_RASTER_LINE_THICK_TABLE[] = { SW_RASTER_VARIANTS(SW_TABLE_ENTRY1) }; |
| 3235 |
#undef SW_TABLE_ENTRY0 |
| 3236 |
#undef SW_TABLE_ENTRY1 |
| 3237 |
|
| 3238 |
#undef SW_RASTER_VARIANTS |
| 3239 |
#undef RLSW_TEMPLATE_RASTER_LINE_EXPANDING |
| 3240 |
|
| 3241 |
#endif |
| 3242 |
|
| 3243 |
|
| 3244 |
|
| 3245 |
|
| 3246 |
#ifndef RLSW_TEMPLATE_RASTER_POINT_EXPANDING |
| 3247 |
#define RLSW_TEMPLATE_RASTER_POINT_EXPANDING |
| 3248 |
|
| 3249 |
|
| 3250 |
#define SW_RASTER_POINT_STATE_MASK \ |
| 3251 |
(SW_STATE_DEPTH_TEST | SW_STATE_BLEND) |
| 3252 |
|
| 3253 |
|
| 3254 |
|
| 3255 |
#define SW_RASTER_VARIANTS(X) \ |
| 3256 |
X(BASE, 0) \ |
| 3257 |
X(DEPTH, SW_STATE_DEPTH_TEST) \ |
| 3258 |
X(BLEND, SW_STATE_BLEND) \ |
| 3259 |
X(DEPTH_BLEND, SW_STATE_DEPTH_TEST | SW_STATE_BLEND) |
| 3260 |
|
| 3261 |
|
| 3262 |
|
| 3263 |
#define SW_FWD_DECL(NAME, _FLAGS) \ |
| 3264 |
static void sw_raster_point_##NAME(const sw_vertex_t *v); |
| 3265 |
SW_RASTER_VARIANTS(SW_FWD_DECL) |
| 3266 |
#undef SW_FWD_DECL |
| 3267 |
|
| 3268 |
|
| 3269 |
|
| 3270 |
#define RLSW_TEMPLATE_RASTER_POINT BASE |
| 3271 |
#include __FILE__ |
| 3272 |
#undef RLSW_TEMPLATE_RASTER_POINT |
| 3273 |
|
| 3274 |
#define RLSW_TEMPLATE_RASTER_POINT DEPTH |
| 3275 |
#define SW_ENABLE_DEPTH_TEST |
| 3276 |
#include __FILE__ |
| 3277 |
#undef SW_ENABLE_DEPTH_TEST |
| 3278 |
#undef RLSW_TEMPLATE_RASTER_POINT |
| 3279 |
|
| 3280 |
#define RLSW_TEMPLATE_RASTER_POINT BLEND |
| 3281 |
#define SW_ENABLE_BLEND |
| 3282 |
#include __FILE__ |
| 3283 |
#undef SW_ENABLE_BLEND |
| 3284 |
#undef RLSW_TEMPLATE_RASTER_POINT |
| 3285 |
|
| 3286 |
#define RLSW_TEMPLATE_RASTER_POINT DEPTH_BLEND |
| 3287 |
#define SW_ENABLE_DEPTH_TEST |
| 3288 |
#define SW_ENABLE_BLEND |
| 3289 |
#include __FILE__ |
| 3290 |
#undef SW_ENABLE_BLEND |
| 3291 |
#undef SW_ENABLE_DEPTH_TEST |
| 3292 |
#undef RLSW_TEMPLATE_RASTER_POINT |
| 3293 |
|
| 3294 |
|
| 3295 |
#define SW_TABLE_ENTRY(NAME, FLAGS) [FLAGS] = sw_raster_point_##NAME, |
| 3296 |
static const sw_raster_point_f SW_RASTER_POINT_TABLE[] = { |
| 3297 |
SW_RASTER_VARIANTS(SW_TABLE_ENTRY) |
| 3298 |
}; |
| 3299 |
#undef SW_TABLE_ENTRY |
| 3300 |
|
| 3301 |
#undef SW_RASTER_VARIANTS |
| 3302 |
#undef RLSW_TEMPLATE_RASTER_POINT_EXPANDING |
| 3303 |
|
| 3304 |
#endif |
| 3305 |
|
| 3306 |
|
| 3307 |
|
| 3308 |
|
| 3309 |
static inline bool sw_triangle_face_culling(void) |
| 3310 |
{ |
| 3311 |
|
| 3312 |
|
| 3313 |
|
| 3314 |
|
| 3315 |
|
| 3316 |
|
| 3317 |
|
| 3318 |
|
| 3319 |
|
| 3320 |
const float *p0 = RLSW.primitive.buffer[0].position; |
| 3321 |
const float *p1 = RLSW.primitive.buffer[1].position; |
| 3322 |
const float *p2 = RLSW.primitive.buffer[2].position; |
| 3323 |
|
| 3324 |
|
| 3325 |
|
| 3326 |
|
| 3327 |
|
| 3328 |
|
| 3329 |
|
| 3330 |
|
| 3331 |
|
| 3332 |
|
| 3333 |
|
| 3334 |
|
| 3335 |
const float sgnArea = |
| 3336 |
p0[0]*(p1[1]*p2[3] - p2[1]*p1[3]) + |
| 3337 |
p1[0]*(p2[1]*p0[3] - p0[1]*p2[3]) + |
| 3338 |
p2[0]*(p0[1]*p1[3] - p1[1]*p0[3]); |
| 3339 |
|
| 3340 |
|
| 3341 |
|
| 3342 |
|
| 3343 |
|
| 3344 |
|
| 3345 |
|
| 3346 |
|
| 3347 |
|
| 3348 |
|
| 3349 |
|
| 3350 |
|
| 3351 |
|
| 3352 |
return (RLSW.cullFace == SW_FRONT)? (sgnArea < 0) : (sgnArea > 0); |
| 3353 |
} |
| 3354 |
|
| 3355 |
static void sw_triangle_clip_and_project(void) |
| 3356 |
{ |
| 3357 |
sw_vertex_t *polygon = RLSW.primitive.buffer; |
| 3358 |
int *vertexCounter = &RLSW.primitive.vertexCount; |
| 3359 |
|
| 3360 |
if (sw_polygon_clip(polygon, vertexCounter)) |
| 3361 |
{ |
| 3362 |
|
| 3363 |
for (int i = 0; i < *vertexCounter; i++) |
| 3364 |
{ |
| 3365 |
sw_vertex_t *v = &polygon[i]; |
| 3366 |
|
| 3367 |
|
| 3368 |
|
| 3369 |
const float wRcp = 1.0f/v->position[3]; |
| 3370 |
|
| 3371 |
|
| 3372 |
v->position[0] *= wRcp; |
| 3373 |
v->position[1] *= wRcp; |
| 3374 |
v->position[2] *= wRcp; |
| 3375 |
v->position[3] = wRcp; |
| 3376 |
|
| 3377 |
|
| 3378 |
v->texcoord[0] *= wRcp; |
| 3379 |
v->texcoord[1] *= wRcp; |
| 3380 |
|
| 3381 |
|
| 3382 |
v->color[0] *= wRcp; |
| 3383 |
v->color[1] *= wRcp; |
| 3384 |
v->color[2] *= wRcp; |
| 3385 |
v->color[3] *= wRcp; |
| 3386 |
|
| 3387 |
|
| 3388 |
sw_project_ndc_to_screen(v->position); |
| 3389 |
} |
| 3390 |
} |
| 3391 |
} |
| 3392 |
|
| 3393 |
static void sw_triangle_render(uint32_t state) |
| 3394 |
{ |
| 3395 |
if (RLSW.userState & SW_STATE_CULL_FACE) |
| 3396 |
{ |
| 3397 |
if (!sw_triangle_face_culling()) return; |
| 3398 |
} |
| 3399 |
|
| 3400 |
sw_triangle_clip_and_project(); |
| 3401 |
if (RLSW.primitive.vertexCount < 3) return; |
| 3402 |
|
| 3403 |
state &= SW_RASTER_TRIANGLE_STATE_MASK; |
| 3404 |
|
| 3405 |
for (int i = 0; i < RLSW.primitive.vertexCount - 2; i++) |
| 3406 |
{ |
| 3407 |
SW_RASTER_TRIANGLE_TABLE[state]( |
| 3408 |
&RLSW.primitive.buffer[0], |
| 3409 |
&RLSW.primitive.buffer[i + 1], |
| 3410 |
&RLSW.primitive.buffer[i + 2] |
| 3411 |
); |
| 3412 |
} |
| 3413 |
} |
| 3414 |
|
| 3415 |
|
| 3416 |
|
| 3417 |
|
| 3418 |
static inline bool sw_quad_face_culling(void) |
| 3419 |
{ |
| 3420 |
|
| 3421 |
|
| 3422 |
|
| 3423 |
|
| 3424 |
|
| 3425 |
|
| 3426 |
|
| 3427 |
|
| 3428 |
|
| 3429 |
|
| 3430 |
const float *p0 = RLSW.primitive.buffer[0].position; |
| 3431 |
const float *p1 = RLSW.primitive.buffer[1].position; |
| 3432 |
const float *p2 = RLSW.primitive.buffer[2].position; |
| 3433 |
|
| 3434 |
|
| 3435 |
|
| 3436 |
|
| 3437 |
|
| 3438 |
|
| 3439 |
|
| 3440 |
|
| 3441 |
|
| 3442 |
|
| 3443 |
|
| 3444 |
|
| 3445 |
|
| 3446 |
|
| 3447 |
|
| 3448 |
|
| 3449 |
const float sgnArea = |
| 3450 |
p0[0]*(p1[1]*p2[3] - p2[1]*p1[3]) + |
| 3451 |
p1[0]*(p2[1]*p0[3] - p0[1]*p2[3]) + |
| 3452 |
p2[0]*(p0[1]*p1[3] - p1[1]*p0[3]); |
| 3453 |
|
| 3454 |
|
| 3455 |
|
| 3456 |
|
| 3457 |
|
| 3458 |
|
| 3459 |
|
| 3460 |
|
| 3461 |
|
| 3462 |
|
| 3463 |
|
| 3464 |
|
| 3465 |
|
| 3466 |
|
| 3467 |
return (RLSW.cullFace == SW_FRONT)? (sgnArea < 0.0f) : (sgnArea > 0.0f); |
| 3468 |
} |
| 3469 |
|
| 3470 |
static void sw_quad_clip_and_project(void) |
| 3471 |
{ |
| 3472 |
sw_vertex_t *polygon = RLSW.primitive.buffer; |
| 3473 |
int *vertexCounter = &RLSW.primitive.vertexCount; |
| 3474 |
|
| 3475 |
if (sw_polygon_clip(polygon, vertexCounter)) |
| 3476 |
{ |
| 3477 |
|
| 3478 |
for (int i = 0; i < *vertexCounter; i++) |
| 3479 |
{ |
| 3480 |
sw_vertex_t *v = &polygon[i]; |
| 3481 |
|
| 3482 |
|
| 3483 |
|
| 3484 |
const float wRcp = 1.0f/v->position[3]; |
| 3485 |
|
| 3486 |
|
| 3487 |
v->position[0] *= wRcp; |
| 3488 |
v->position[1] *= wRcp; |
| 3489 |
v->position[2] *= wRcp; |
| 3490 |
v->position[3] = wRcp; |
| 3491 |
|
| 3492 |
|
| 3493 |
v->texcoord[0] *= wRcp; |
| 3494 |
v->texcoord[1] *= wRcp; |
| 3495 |
|
| 3496 |
|
| 3497 |
v->color[0] *= wRcp; |
| 3498 |
v->color[1] *= wRcp; |
| 3499 |
v->color[2] *= wRcp; |
| 3500 |
v->color[3] *= wRcp; |
| 3501 |
|
| 3502 |
|
| 3503 |
sw_project_ndc_to_screen(v->position); |
| 3504 |
} |
| 3505 |
} |
| 3506 |
} |
| 3507 |
|
| 3508 |
static bool sw_quad_is_axis_aligned(void) |
| 3509 |
{ |
| 3510 |
|
| 3511 |
|
| 3512 |
|
| 3513 |
for (int i = 0; i < 4; i++) |
| 3514 |
{ |
| 3515 |
if (RLSW.primitive.buffer[i].position[3] != 1.0f) return false; |
| 3516 |
} |
| 3517 |
|
| 3518 |
|
| 3519 |
const float epsilon = 0.5f; |
| 3520 |
|
| 3521 |
|
| 3522 |
const float *p0 = RLSW.primitive.buffer[0].position; |
| 3523 |
const float *p1 = RLSW.primitive.buffer[1].position; |
| 3524 |
const float *p2 = RLSW.primitive.buffer[2].position; |
| 3525 |
const float *p3 = RLSW.primitive.buffer[3].position; |
| 3526 |
|
| 3527 |
|
| 3528 |
|
| 3529 |
float dx01 = p1[0] - p0[0], dy01 = p1[1] - p0[1]; |
| 3530 |
float dx12 = p2[0] - p1[0], dy12 = p2[1] - p1[1]; |
| 3531 |
float dx23 = p3[0] - p2[0], dy23 = p3[1] - p2[1]; |
| 3532 |
float dx30 = p0[0] - p3[0], dy30 = p0[1] - p3[1]; |
| 3533 |
|
| 3534 |
|
| 3535 |
|
| 3536 |
if (!((fabsf(dy01) < epsilon) || (fabsf(dx01) < epsilon))) return false; |
| 3537 |
if (!((fabsf(dy12) < epsilon) || (fabsf(dx12) < epsilon))) return false; |
| 3538 |
if (!((fabsf(dy23) < epsilon) || (fabsf(dx23) < epsilon))) return false; |
| 3539 |
if (!((fabsf(dy30) < epsilon) || (fabsf(dx30) < epsilon))) return false; |
| 3540 |
|
| 3541 |
return true; |
| 3542 |
} |
| 3543 |
|
| 3544 |
static void sw_quad_render(uint32_t state) |
| 3545 |
{ |
| 3546 |
if (RLSW.userState & SW_STATE_CULL_FACE) |
| 3547 |
{ |
| 3548 |
if (!sw_quad_face_culling()) return; |
| 3549 |
} |
| 3550 |
|
| 3551 |
sw_quad_clip_and_project(); |
| 3552 |
if (RLSW.primitive.vertexCount < 3) return; |
| 3553 |
|
| 3554 |
state &= SW_RASTER_QUAD_STATE_MASK; |
| 3555 |
|
| 3556 |
if ((RLSW.primitive.vertexCount == 4) && sw_quad_is_axis_aligned()) |
| 3557 |
{ |
| 3558 |
SW_RASTER_QUAD_TABLE[state]( |
| 3559 |
&RLSW.primitive.buffer[0], |
| 3560 |
&RLSW.primitive.buffer[1], |
| 3561 |
&RLSW.primitive.buffer[2], |
| 3562 |
&RLSW.primitive.buffer[3] |
| 3563 |
); |
| 3564 |
} |
| 3565 |
else |
| 3566 |
{ |
| 3567 |
for (int i = 0; i < RLSW.primitive.vertexCount - 2; i++) |
| 3568 |
{ |
| 3569 |
SW_RASTER_TRIANGLE_TABLE[state]( |
| 3570 |
&RLSW.primitive.buffer[0], |
| 3571 |
&RLSW.primitive.buffer[i + 1], |
| 3572 |
&RLSW.primitive.buffer[i + 2] |
| 3573 |
); |
| 3574 |
} |
| 3575 |
} |
| 3576 |
} |
| 3577 |
|
| 3578 |
|
| 3579 |
|
| 3580 |
|
| 3581 |
static inline bool sw_line_clip_coord(float q, float p, float *t0, float *t1) |
| 3582 |
{ |
| 3583 |
if (fabsf(p) < SW_CLIP_EPSILON) |
| 3584 |
{ |
| 3585 |
|
| 3586 |
if (q < -SW_CLIP_EPSILON) return 0; |
| 3587 |
return 1; |
| 3588 |
} |
| 3589 |
|
| 3590 |
const float r = q/p; |
| 3591 |
|
| 3592 |
if (p < 0) |
| 3593 |
{ |
| 3594 |
if (r > *t1) return 0; |
| 3595 |
if (r > *t0) *t0 = r; |
| 3596 |
} |
| 3597 |
else |
| 3598 |
{ |
| 3599 |
if (r < *t0) return 0; |
| 3600 |
if (r < *t1) *t1 = r; |
| 3601 |
} |
| 3602 |
|
| 3603 |
return 1; |
| 3604 |
} |
| 3605 |
|
| 3606 |
static bool sw_line_clip(sw_vertex_t *v0, sw_vertex_t *v1) |
| 3607 |
{ |
| 3608 |
float t0 = 0.0f, t1 = 1.0f; |
| 3609 |
float dH[4] = { 0 }; |
| 3610 |
float dC[4] = { 0 }; |
| 3611 |
|
| 3612 |
for (int i = 0; i < 4; i++) |
| 3613 |
{ |
| 3614 |
dH[i] = v1->position[i] - v0->position[i]; |
| 3615 |
dC[i] = v1->color[i] - v0->color[i]; |
| 3616 |
} |
| 3617 |
|
| 3618 |
|
| 3619 |
if (!sw_line_clip_coord(v0->position[3] - v0->position[0], -dH[3] + dH[0], &t0, &t1)) return false; |
| 3620 |
if (!sw_line_clip_coord(v0->position[3] + v0->position[0], -dH[3] - dH[0], &t0, &t1)) return false; |
| 3621 |
if (!sw_line_clip_coord(v0->position[3] - v0->position[1], -dH[3] + dH[1], &t0, &t1)) return false; |
| 3622 |
if (!sw_line_clip_coord(v0->position[3] + v0->position[1], -dH[3] - dH[1], &t0, &t1)) return false; |
| 3623 |
if (!sw_line_clip_coord(v0->position[3] - v0->position[2], -dH[3] + dH[2], &t0, &t1)) return false; |
| 3624 |
if (!sw_line_clip_coord(v0->position[3] + v0->position[2], -dH[3] - dH[2], &t0, &t1)) return false; |
| 3625 |
|
| 3626 |
|
| 3627 |
if (RLSW.userState & SW_STATE_SCISSOR_TEST) |
| 3628 |
{ |
| 3629 |
if (!sw_line_clip_coord(v0->position[0] - RLSW.scClipMin[0]*v0->position[3], RLSW.scClipMin[0]*dH[3] - dH[0], &t0, &t1)) return false; |
| 3630 |
if (!sw_line_clip_coord(RLSW.scClipMax[0]*v0->position[3] - v0->position[0], dH[0] - RLSW.scClipMax[0]*dH[3], &t0, &t1)) return false; |
| 3631 |
if (!sw_line_clip_coord(v0->position[1] - RLSW.scClipMin[1]*v0->position[3], RLSW.scClipMin[1]*dH[3] - dH[1], &t0, &t1)) return false; |
| 3632 |
if (!sw_line_clip_coord(RLSW.scClipMax[1]*v0->position[3] - v0->position[1], dH[1] - RLSW.scClipMax[1]*dH[3], &t0, &t1)) return false; |
| 3633 |
} |
| 3634 |
|
| 3635 |
|
| 3636 |
if (t1 < 1.0f) |
| 3637 |
{ |
| 3638 |
for (int i = 0; i < 4; i++) |
| 3639 |
{ |
| 3640 |
v1->position[i] = v0->position[i] + t1*dH[i]; |
| 3641 |
v1->color[i] = v0->color[i] + t1*dC[i]; |
| 3642 |
} |
| 3643 |
} |
| 3644 |
|
| 3645 |
if (t0 > 0.0f) |
| 3646 |
{ |
| 3647 |
for (int i = 0; i < 4; i++) |
| 3648 |
{ |
| 3649 |
v0->position[i] += t0*dH[i]; |
| 3650 |
v0->color[i] += t0*dC[i]; |
| 3651 |
} |
| 3652 |
} |
| 3653 |
|
| 3654 |
return true; |
| 3655 |
} |
| 3656 |
|
| 3657 |
static bool sw_line_clip_and_project(sw_vertex_t *v0, sw_vertex_t *v1) |
| 3658 |
{ |
| 3659 |
if (!sw_line_clip(v0, v1)) return false; |
| 3660 |
|
| 3661 |
|
| 3662 |
v0->position[3] = 1.0f/v0->position[3]; |
| 3663 |
v1->position[3] = 1.0f/v1->position[3]; |
| 3664 |
for (int i = 0; i < 3; i++) |
| 3665 |
{ |
| 3666 |
v0->position[i] *= v0->position[3]; |
| 3667 |
v1->position[i] *= v1->position[3]; |
| 3668 |
} |
| 3669 |
|
| 3670 |
|
| 3671 |
sw_project_ndc_to_screen(v0->position); |
| 3672 |
sw_project_ndc_to_screen(v1->position); |
| 3673 |
|
| 3674 |
|
| 3675 |
|
| 3676 |
v0->position[0] = sw_clamp(v0->position[0], 0.0f, (float)(RLSW.colorBuffer->width - 1) + 0.5f); |
| 3677 |
v0->position[1] = sw_clamp(v0->position[1], 0.0f, (float)(RLSW.colorBuffer->height - 1) + 0.5f); |
| 3678 |
v1->position[0] = sw_clamp(v1->position[0], 0.0f, (float)(RLSW.colorBuffer->width - 1) + 0.5f); |
| 3679 |
v1->position[1] = sw_clamp(v1->position[1], 0.0f, (float)(RLSW.colorBuffer->height - 1) + 0.5f); |
| 3680 |
|
| 3681 |
return true; |
| 3682 |
} |
| 3683 |
|
| 3684 |
static void sw_line_render(uint32_t state, sw_vertex_t *vertices) |
| 3685 |
{ |
| 3686 |
if (!sw_line_clip_and_project(&vertices[0], &vertices[1])) return; |
| 3687 |
|
| 3688 |
state &= SW_RASTER_LINE_STATE_MASK; |
| 3689 |
|
| 3690 |
if (RLSW.lineWidth >= 2.0f) |
| 3691 |
{ |
| 3692 |
SW_RASTER_LINE_THICK_TABLE[state](&vertices[0], &vertices[1]); |
| 3693 |
} |
| 3694 |
else |
| 3695 |
{ |
| 3696 |
SW_RASTER_LINE_TABLE[state](&vertices[0], &vertices[1]); |
| 3697 |
} |
| 3698 |
} |
| 3699 |
|
| 3700 |
|
| 3701 |
|
| 3702 |
|
| 3703 |
static bool sw_point_clip_and_project(sw_vertex_t *v) |
| 3704 |
{ |
| 3705 |
if (v->position[3] != 1.0f) |
| 3706 |
{ |
| 3707 |
for (int_fast8_t i = 0; i < 3; i++) |
| 3708 |
{ |
| 3709 |
if ((v->position[i] < -v->position[3]) || (v->position[i] > v->position[3])) return false; |
| 3710 |
} |
| 3711 |
|
| 3712 |
v->position[3] = 1.0f/v->position[3]; |
| 3713 |
v->position[0] *= v->position[3]; |
| 3714 |
v->position[1] *= v->position[3]; |
| 3715 |
v->position[2] *= v->position[3]; |
| 3716 |
} |
| 3717 |
|
| 3718 |
sw_project_ndc_to_screen(v->position); |
| 3719 |
|
| 3720 |
int min[2] = { 0, 0 }; |
| 3721 |
int max[2] = { RLSW.colorBuffer->width, RLSW.colorBuffer->height }; |
| 3722 |
|
| 3723 |
if (RLSW.userState & SW_STATE_SCISSOR_TEST) |
| 3724 |
{ |
| 3725 |
min[0] = sw_clamp_int(RLSW.scMin[0], 0, RLSW.colorBuffer->width); |
| 3726 |
min[1] = sw_clamp_int(RLSW.scMin[1], 0, RLSW.colorBuffer->height); |
| 3727 |
max[0] = sw_clamp_int(RLSW.scMax[0], 0, RLSW.colorBuffer->width); |
| 3728 |
max[1] = sw_clamp_int(RLSW.scMax[1], 0, RLSW.colorBuffer->height); |
| 3729 |
} |
| 3730 |
|
| 3731 |
bool insideX = (v->position[0] - RLSW.pointRadius < max[0]) && (v->position[0] + RLSW.pointRadius > min[0]); |
| 3732 |
bool insideY = (v->position[1] - RLSW.pointRadius < max[1]) && (v->position[1] + RLSW.pointRadius > min[1]); |
| 3733 |
|
| 3734 |
return (insideX && insideY); |
| 3735 |
} |
| 3736 |
|
| 3737 |
static void sw_point_render(uint32_t state, sw_vertex_t *v) |
| 3738 |
{ |
| 3739 |
if (!sw_point_clip_and_project(v)) return; |
| 3740 |
state &= SW_RASTER_POINT_STATE_MASK; |
| 3741 |
SW_RASTER_POINT_TABLE[state](v); |
| 3742 |
} |
| 3743 |
|
| 3744 |
|
| 3745 |
|
| 3746 |
|
| 3747 |
static inline void sw_poly_point_render(uint32_t state) |
| 3748 |
{ |
| 3749 |
for (int i = 0; i < RLSW.primitive.vertexCount; i++) sw_point_render(state, &RLSW.primitive.buffer[i]); |
| 3750 |
} |
| 3751 |
|
| 3752 |
static inline void sw_poly_line_render(uint32_t state) |
| 3753 |
{ |
| 3754 |
const sw_vertex_t *vertices = RLSW.primitive.buffer; |
| 3755 |
int cm1 = RLSW.primitive.vertexCount - 1; |
| 3756 |
|
| 3757 |
for (int i = 0; i < cm1; i++) |
| 3758 |
{ |
| 3759 |
sw_vertex_t verts[2] = { vertices[i], vertices[i + 1] }; |
| 3760 |
sw_line_render(state, verts); |
| 3761 |
} |
| 3762 |
|
| 3763 |
sw_vertex_t verts[2] = { vertices[cm1], vertices[0] }; |
| 3764 |
sw_line_render(state, verts); |
| 3765 |
} |
| 3766 |
|
| 3767 |
static inline void sw_poly_fill_render(uint32_t state) |
| 3768 |
{ |
| 3769 |
switch (RLSW.drawMode) |
| 3770 |
{ |
| 3771 |
case SW_POINTS: sw_point_render(state, &RLSW.primitive.buffer[0]); break; |
| 3772 |
case SW_LINES: sw_line_render(state, RLSW.primitive.buffer); break; |
| 3773 |
case SW_TRIANGLES: sw_triangle_render(state); break; |
| 3774 |
case SW_QUADS: sw_quad_render(state); break; |
| 3775 |
default: break; |
| 3776 |
} |
| 3777 |
} |
| 3778 |
|
| 3779 |
|
| 3780 |
|
| 3781 |
|
| 3782 |
static void sw_immediate_begin(SWdraw mode) |
| 3783 |
{ |
| 3784 |
|
| 3785 |
|
| 3786 |
|
| 3787 |
|
| 3788 |
if (RLSW.isDirtyMVP) |
| 3789 |
{ |
| 3790 |
sw_matrix_mul_rst(RLSW.matMVP, |
| 3791 |
RLSW.stackModelview[RLSW.stackModelviewCounter - 1], |
| 3792 |
RLSW.stackProjection[RLSW.stackProjectionCounter - 1]); |
| 3793 |
|
| 3794 |
RLSW.isDirtyMVP = false; |
| 3795 |
} |
| 3796 |
|
| 3797 |
|
| 3798 |
uint32_t state = RLSW.userState; |
| 3799 |
if (!sw_is_texture_complete(RLSW.depthBuffer)) state &= ~SW_STATE_DEPTH_TEST; |
| 3800 |
if (!sw_is_texture_complete(RLSW.boundTexture)) state &= ~SW_STATE_TEXTURE_2D; |
| 3801 |
else if (sw_pixel_is_depth_format(RLSW.boundTexture->format)) state &= ~SW_STATE_TEXTURE_2D; |
| 3802 |
|
| 3803 |
|
| 3804 |
RLSW.primitive.hasColorAlpha = false; |
| 3805 |
RLSW.primitive.vertexCount = 0; |
| 3806 |
RLSW.rasterState = state; |
| 3807 |
RLSW.drawMode = mode; |
| 3808 |
} |
| 3809 |
|
| 3810 |
static bool sw_immediate_is_active(void) |
| 3811 |
{ |
| 3812 |
return (RLSW.drawMode != SW_DRAW_INVALID); |
| 3813 |
} |
| 3814 |
|
| 3815 |
static void sw_immediate_set_color(const float color[4]) |
| 3816 |
{ |
| 3817 |
RLSW.primitive.color[0] = color[0]; |
| 3818 |
RLSW.primitive.color[1] = color[1]; |
| 3819 |
RLSW.primitive.color[2] = color[2]; |
| 3820 |
RLSW.primitive.color[3] = color[3]; |
| 3821 |
|
| 3822 |
RLSW.primitive.hasColorAlpha |= (color[3] < 1.0f); |
| 3823 |
} |
| 3824 |
|
| 3825 |
static void sw_immediate_set_texcoord(const float texcoord[2]) |
| 3826 |
{ |
| 3827 |
const float *m = RLSW.stackTexture[RLSW.stackTextureCounter - 1]; |
| 3828 |
RLSW.primitive.texcoord[0] = m[0]*texcoord[0] + m[4]*texcoord[1] + m[12]; |
| 3829 |
RLSW.primitive.texcoord[1] = m[1]*texcoord[0] + m[5]*texcoord[1] + m[13]; |
| 3830 |
} |
| 3831 |
|
| 3832 |
static void sw_immediate_push_vertex(const float position[4]) |
| 3833 |
{ |
| 3834 |
|
| 3835 |
if (!sw_is_draw_mode_valid(RLSW.drawMode)) |
| 3836 |
{ |
| 3837 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 3838 |
return; |
| 3839 |
} |
| 3840 |
|
| 3841 |
|
| 3842 |
sw_vertex_t *vertex = &RLSW.primitive.buffer[RLSW.primitive.vertexCount++]; |
| 3843 |
|
| 3844 |
|
| 3845 |
const float *m = RLSW.matMVP; |
| 3846 |
vertex->position[0] = m[0]*position[0] + m[4]*position[1] + m[8]*position[2] + m[12]*position[3]; |
| 3847 |
vertex->position[1] = m[1]*position[0] + m[5]*position[1] + m[9]*position[2] + m[13]*position[3]; |
| 3848 |
vertex->position[2] = m[2]*position[0] + m[6]*position[1] + m[10]*position[2] + m[14]*position[3]; |
| 3849 |
vertex->position[3] = m[3]*position[0] + m[7]*position[1] + m[11]*position[2] + m[15]*position[3]; |
| 3850 |
|
| 3851 |
|
| 3852 |
for (int i = 0; i < 4; i++) vertex->color[i] = RLSW.primitive.color[i]; |
| 3853 |
for (int i = 0; i < 2; i++) vertex->texcoord[i] = RLSW.primitive.texcoord[i]; |
| 3854 |
|
| 3855 |
|
| 3856 |
if (RLSW.primitive.vertexCount == SW_PRIMITIVE_VERTEX_COUNT[RLSW.drawMode]) |
| 3857 |
{ |
| 3858 |
uint32_t state = RLSW.rasterState; |
| 3859 |
|
| 3860 |
|
| 3861 |
if (state & SW_STATE_BLEND) |
| 3862 |
{ |
| 3863 |
if (RLSW.blendFlags & SW_BLEND_FLAG_NOOP) state &= ~SW_STATE_BLEND; |
| 3864 |
else if ((RLSW.blendFlags & SW_BLEND_FLAG_NEEDS_ALPHA) && (!RLSW.primitive.hasColorAlpha)) |
| 3865 |
{ |
| 3866 |
if (!(state & SW_STATE_TEXTURE_2D) || (RLSW.boundTexture->alpha == SW_PIXEL_ALPHA_NONE)) state &= ~SW_STATE_BLEND; |
| 3867 |
} |
| 3868 |
} |
| 3869 |
|
| 3870 |
switch (RLSW.polyMode) |
| 3871 |
{ |
| 3872 |
case SW_FILL: sw_poly_fill_render(state); break; |
| 3873 |
case SW_LINE: sw_poly_line_render(state); break; |
| 3874 |
case SW_POINT: sw_poly_point_render(state); break; |
| 3875 |
default: break; |
| 3876 |
} |
| 3877 |
|
| 3878 |
RLSW.primitive.hasColorAlpha = false; |
| 3879 |
RLSW.primitive.vertexCount = 0; |
| 3880 |
} |
| 3881 |
} |
| 3882 |
|
| 3883 |
static void sw_immediate_end(void) |
| 3884 |
{ |
| 3885 |
RLSW.drawMode = SW_DRAW_INVALID; |
| 3886 |
} |
| 3887 |
|
| 3888 |
|
| 3889 |
|
| 3890 |
|
| 3891 |
|
| 3892 |
bool swInit(int w, int h) |
| 3893 |
{ |
| 3894 |
if (!sw_default_framebuffer_alloc(&RLSW.framebuffer, w, h)) |
| 3895 |
{ |
| 3896 |
swClose(); |
| 3897 |
return false; |
| 3898 |
} |
| 3899 |
|
| 3900 |
if (!sw_pool_init(&RLSW.framebufferPool, SW_MAX_FRAMEBUFFERS, sizeof(sw_framebuffer_t))) |
| 3901 |
{ |
| 3902 |
swClose(); |
| 3903 |
return false; |
| 3904 |
} |
| 3905 |
|
| 3906 |
if (!sw_pool_init(&RLSW.texturePool, SW_MAX_TEXTURES, sizeof(sw_texture_t))) |
| 3907 |
{ |
| 3908 |
swClose(); |
| 3909 |
return false; |
| 3910 |
} |
| 3911 |
|
| 3912 |
RLSW.colorBuffer = &RLSW.framebuffer.color; |
| 3913 |
RLSW.depthBuffer = &RLSW.framebuffer.depth; |
| 3914 |
|
| 3915 |
swViewport(0, 0, w, h); |
| 3916 |
swScissor(0, 0, w, h); |
| 3917 |
|
| 3918 |
RLSW.clearColor[0] = 0.0f; |
| 3919 |
RLSW.clearColor[1] = 0.0f; |
| 3920 |
RLSW.clearColor[2] = 0.0f; |
| 3921 |
RLSW.clearColor[3] = 1.0f; |
| 3922 |
RLSW.clearDepth = 1.0f; |
| 3923 |
|
| 3924 |
RLSW.currentMatrixMode = SW_MODELVIEW; |
| 3925 |
RLSW.currentMatrix = &RLSW.stackModelview[0]; |
| 3926 |
|
| 3927 |
sw_matrix_id(RLSW.stackProjection[0]); |
| 3928 |
sw_matrix_id(RLSW.stackModelview[0]); |
| 3929 |
sw_matrix_id(RLSW.stackTexture[0]); |
| 3930 |
sw_matrix_id(RLSW.matMVP); |
| 3931 |
|
| 3932 |
RLSW.stackProjectionCounter = 1; |
| 3933 |
RLSW.stackModelviewCounter = 1; |
| 3934 |
RLSW.stackTextureCounter = 1; |
| 3935 |
RLSW.isDirtyMVP = false; |
| 3936 |
|
| 3937 |
RLSW.primitive.texcoord[0] = 0.0f; |
| 3938 |
RLSW.primitive.texcoord[1] = 0.0f; |
| 3939 |
|
| 3940 |
RLSW.primitive.color[0] = 1.0f; |
| 3941 |
RLSW.primitive.color[1] = 1.0f; |
| 3942 |
RLSW.primitive.color[2] = 1.0f; |
| 3943 |
RLSW.primitive.color[3] = 1.0f; |
| 3944 |
|
| 3945 |
RLSW.srcFactor = SW_SRC_ALPHA; |
| 3946 |
RLSW.dstFactor = SW_ONE_MINUS_SRC_ALPHA; |
| 3947 |
RLSW.blendFunc = sw_blend_SRC_ALPHA_ONE_MINUS_SRC_ALPHA; |
| 3948 |
|
| 3949 |
RLSW.drawMode = SW_DRAW_INVALID; |
| 3950 |
RLSW.polyMode = SW_FILL; |
| 3951 |
RLSW.cullFace = SW_BACK; |
| 3952 |
|
| 3953 |
#if SW_USE_COLOR_LUT |
| 3954 |
for (int i = 0; i < 256; i++) SW_LUT_UINT8_TO_FLOAT[i] = (float)i*SW_INV_255; |
| 3955 |
#endif |
| 3956 |
|
| 3957 |
SW_LOG("INFO: RLSW: Software renderer initialized successfully\n"); |
| 3958 |
#if defined(SW_HAS_FMA_AVX) && defined(SW_HAS_FMA_AVX2) |
| 3959 |
SW_LOG("INFO: RLSW: Using SIMD instructions: FMA AVX\n"); |
| 3960 |
#endif |
| 3961 |
#if defined(SW_HAS_AVX) || defined(SW_HAS_AVX2) |
| 3962 |
SW_LOG("INFO: RLSW: Using SIMD instructions: AVX\n"); |
| 3963 |
#endif |
| 3964 |
#if defined(SW_HAS_SSE) || defined(SW_HAS_SSE2) || defined(SW_HAS_SSE3) || defined(SW_HAS_SSSE3) || defined(SW_HAS_SSE41) || defined(SW_HAS_SSE42) |
| 3965 |
SW_LOG("INFO: RLSW: Using SIMD instructions: SSE\n"); |
| 3966 |
#endif |
| 3967 |
#if defined(SW_HAS_NEON_FMA) || defined(SW_HAS_NEON) |
| 3968 |
SW_LOG("INFO: RLSW: Using SIMD instructions: NEON\n"); |
| 3969 |
#endif |
| 3970 |
#if defined(SW_HAS_RVV) |
| 3971 |
SW_LOG("INFO: RLSW: Using SIMD instructions: RVV\n"); |
| 3972 |
#endif |
| 3973 |
|
| 3974 |
return true; |
| 3975 |
} |
| 3976 |
|
| 3977 |
void swClose(void) |
| 3978 |
{ |
| 3979 |
for (int i = 1; i < RLSW.texturePool.watermark; i++) |
| 3980 |
{ |
| 3981 |
if (RLSW.texturePool.gen[i] & SW_POOL_SLOT_LIVE) |
| 3982 |
{ |
| 3983 |
sw_texture_free((sw_texture_t *)RLSW.texturePool.data + i); |
| 3984 |
} |
| 3985 |
} |
| 3986 |
|
| 3987 |
sw_pool_destroy(&RLSW.texturePool); |
| 3988 |
sw_pool_destroy(&RLSW.framebufferPool); |
| 3989 |
sw_default_framebuffer_free(&RLSW.framebuffer); |
| 3990 |
|
| 3991 |
RLSW = SW_CURLY_INIT(sw_context_t) { 0 }; |
| 3992 |
} |
| 3993 |
|
| 3994 |
bool swResize(int w, int h) |
| 3995 |
{ |
| 3996 |
return sw_default_framebuffer_alloc(&RLSW.framebuffer, w, h); |
| 3997 |
} |
| 3998 |
|
| 3999 |
void swReadPixels(int x, int y, int w, int h, SWformat format, SWtype type, void *pixels) |
| 4000 |
{ |
| 4001 |
|
| 4002 |
if (format == SW_DEPTH_COMPONENT) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 4003 |
|
| 4004 |
sw_pixelformat_t pFormat = (sw_pixelformat_t)sw_pixel_get_format(format, type); |
| 4005 |
if (pFormat <= SW_PIXELFORMAT_UNKNOWN) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 4006 |
|
| 4007 |
if (w <= 0) { RLSW.errCode = SW_INVALID_VALUE; return; } |
| 4008 |
if (h <= 0) { RLSW.errCode = SW_INVALID_VALUE; return; } |
| 4009 |
|
| 4010 |
if (w > RLSW.colorBuffer->width) w = RLSW.colorBuffer->width; |
| 4011 |
if (h > RLSW.colorBuffer->height) h = RLSW.colorBuffer->height; |
| 4012 |
|
| 4013 |
x = sw_clamp_int(x, 0, w); |
| 4014 |
y = sw_clamp_int(y, 0, h); |
| 4015 |
|
| 4016 |
if ((x >= w) || (y >= h)) return; |
| 4017 |
|
| 4018 |
if ((pFormat == SW_FRAMEBUFFER_COLOR_FORMAT) && (x == 0) && (y == 0) && (w == RLSW.colorBuffer->width) && (h == RLSW.colorBuffer->height)) |
| 4019 |
{ |
| 4020 |
sw_framebuffer_output_fast(pixels, RLSW.colorBuffer); |
| 4021 |
} |
| 4022 |
else |
| 4023 |
{ |
| 4024 |
sw_framebuffer_output_copy(pixels, RLSW.colorBuffer, x, y, w, h, pFormat); |
| 4025 |
} |
| 4026 |
} |
| 4027 |
|
| 4028 |
void swBlitPixels(int xDst, int yDst, int wDst, int hDst, int xSrc, int ySrc, int wSrc, int hSrc, SWformat format, SWtype type, void *pixels) |
| 4029 |
{ |
| 4030 |
|
| 4031 |
if (format == SW_DEPTH_COMPONENT) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 4032 |
|
| 4033 |
sw_pixelformat_t pFormat = (sw_pixelformat_t)sw_pixel_get_format(format, type); |
| 4034 |
if (pFormat <= SW_PIXELFORMAT_UNKNOWN) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 4035 |
|
| 4036 |
if (wSrc <= 0) { RLSW.errCode = SW_INVALID_VALUE; return; } |
| 4037 |
if (hSrc <= 0) { RLSW.errCode = SW_INVALID_VALUE; return; } |
| 4038 |
|
| 4039 |
if (wSrc > RLSW.colorBuffer->width) wSrc = RLSW.colorBuffer->width; |
| 4040 |
if (hSrc > RLSW.colorBuffer->height) hSrc = RLSW.colorBuffer->height; |
| 4041 |
|
| 4042 |
xSrc = sw_clamp_int(xSrc, 0, wSrc); |
| 4043 |
ySrc = sw_clamp_int(ySrc, 0, hSrc); |
| 4044 |
|
| 4045 |
|
| 4046 |
|
| 4047 |
if ((xDst == xSrc) && (yDst == ySrc) && (wDst == wSrc) && (hDst == hSrc)) |
| 4048 |
{ |
| 4049 |
swReadPixels(xSrc, ySrc, wSrc, hSrc, format, type, pixels); |
| 4050 |
} |
| 4051 |
else |
| 4052 |
{ |
| 4053 |
sw_framebuffer_output_blit(pixels, RLSW.colorBuffer, xDst, yDst, wDst, hDst, xSrc, ySrc, wSrc, hSrc, pFormat); |
| 4054 |
} |
| 4055 |
} |
| 4056 |
|
| 4057 |
|
| 4058 |
void *swGetColorBuffer(int *width, int *height) |
| 4059 |
{ |
| 4060 |
if (width != NULL) *width = RLSW.framebuffer.color.width; |
| 4061 |
if (height != NULL) *height = RLSW.framebuffer.color.height; |
| 4062 |
return RLSW.framebuffer.color.pixels; |
| 4063 |
} |
| 4064 |
|
| 4065 |
void swEnable(SWstate state) |
| 4066 |
{ |
| 4067 |
switch (state) |
| 4068 |
{ |
| 4069 |
case SW_SCISSOR_TEST: RLSW.userState |= SW_STATE_SCISSOR_TEST; break; |
| 4070 |
case SW_TEXTURE_2D: RLSW.userState |= SW_STATE_TEXTURE_2D; break; |
| 4071 |
case SW_DEPTH_TEST: RLSW.userState |= SW_STATE_DEPTH_TEST; break; |
| 4072 |
case SW_CULL_FACE: RLSW.userState |= SW_STATE_CULL_FACE; break; |
| 4073 |
case SW_BLEND: RLSW.userState |= SW_STATE_BLEND; break; |
| 4074 |
default: RLSW.errCode = SW_INVALID_ENUM; break; |
| 4075 |
} |
| 4076 |
} |
| 4077 |
|
| 4078 |
void swDisable(SWstate state) |
| 4079 |
{ |
| 4080 |
switch (state) |
| 4081 |
{ |
| 4082 |
case SW_SCISSOR_TEST: RLSW.userState &= ~SW_STATE_SCISSOR_TEST; break; |
| 4083 |
case SW_TEXTURE_2D: RLSW.userState &= ~SW_STATE_TEXTURE_2D; break; |
| 4084 |
case SW_DEPTH_TEST: RLSW.userState &= ~SW_STATE_DEPTH_TEST; break; |
| 4085 |
case SW_CULL_FACE: RLSW.userState &= ~SW_STATE_CULL_FACE; break; |
| 4086 |
case SW_BLEND: RLSW.userState &= ~SW_STATE_BLEND; break; |
| 4087 |
default: RLSW.errCode = SW_INVALID_ENUM; break; |
| 4088 |
} |
| 4089 |
} |
| 4090 |
|
| 4091 |
void swGetIntegerv(SWget name, int *v) |
| 4092 |
{ |
| 4093 |
switch (name) |
| 4094 |
{ |
| 4095 |
case SW_MODELVIEW_STACK_DEPTH: *v = SW_MODELVIEW_STACK_DEPTH; break; |
| 4096 |
case SW_PROJECTION_STACK_DEPTH: *v = SW_PROJECTION_STACK_DEPTH; break; |
| 4097 |
case SW_TEXTURE_STACK_DEPTH: *v = SW_TEXTURE_STACK_DEPTH; break; |
| 4098 |
case SW_DRAW_FRAMEBUFFER_BINDING: *v = RLSW.boundFramebufferId; break; |
| 4099 |
default: RLSW.errCode = SW_INVALID_ENUM; break; |
| 4100 |
} |
| 4101 |
} |
| 4102 |
|
| 4103 |
void swGetFloatv(SWget name, float *v) |
| 4104 |
{ |
| 4105 |
switch (name) |
| 4106 |
{ |
| 4107 |
case SW_COLOR_CLEAR_VALUE: |
| 4108 |
{ |
| 4109 |
v[0] = RLSW.clearColor[0]; |
| 4110 |
v[1] = RLSW.clearColor[1]; |
| 4111 |
v[2] = RLSW.clearColor[2]; |
| 4112 |
v[3] = RLSW.clearColor[3]; |
| 4113 |
} break; |
| 4114 |
case SW_DEPTH_CLEAR_VALUE: |
| 4115 |
{ |
| 4116 |
v[0] = RLSW.clearDepth; |
| 4117 |
} break; |
| 4118 |
case SW_CURRENT_COLOR: |
| 4119 |
{ |
| 4120 |
v[0] = RLSW.primitive.buffer[RLSW.primitive.vertexCount - 1].color[0]; |
| 4121 |
v[1] = RLSW.primitive.buffer[RLSW.primitive.vertexCount - 1].color[1]; |
| 4122 |
v[2] = RLSW.primitive.buffer[RLSW.primitive.vertexCount - 1].color[2]; |
| 4123 |
v[3] = RLSW.primitive.buffer[RLSW.primitive.vertexCount - 1].color[3]; |
| 4124 |
} break; |
| 4125 |
case SW_CURRENT_TEXTURE_COORDS: |
| 4126 |
{ |
| 4127 |
v[0] = RLSW.primitive.buffer[RLSW.primitive.vertexCount - 1].texcoord[0]; |
| 4128 |
v[1] = RLSW.primitive.buffer[RLSW.primitive.vertexCount - 1].texcoord[1]; |
| 4129 |
} break; |
| 4130 |
case SW_POINT_SIZE: |
| 4131 |
{ |
| 4132 |
v[0] = 2.0f*RLSW.pointRadius; |
| 4133 |
} break; |
| 4134 |
case SW_LINE_WIDTH: |
| 4135 |
{ |
| 4136 |
v[0] = RLSW.lineWidth; |
| 4137 |
} break; |
| 4138 |
case SW_MODELVIEW_MATRIX: |
| 4139 |
{ |
| 4140 |
for (int i = 0; i < 16; i++) v[i] = RLSW.stackModelview[RLSW.stackModelviewCounter - 1][i]; |
| 4141 |
|
| 4142 |
} break; |
| 4143 |
case SW_PROJECTION_MATRIX: |
| 4144 |
{ |
| 4145 |
for (int i = 0; i < 16; i++) v[i] = RLSW.stackProjection[RLSW.stackProjectionCounter - 1][i]; |
| 4146 |
|
| 4147 |
} break; |
| 4148 |
case SW_TEXTURE_MATRIX: |
| 4149 |
{ |
| 4150 |
for (int i = 0; i < 16; i++) v[i] = RLSW.stackTexture[RLSW.stackTextureCounter - 1][i]; |
| 4151 |
|
| 4152 |
} break; |
| 4153 |
default: RLSW.errCode = SW_INVALID_ENUM; break; |
| 4154 |
} |
| 4155 |
} |
| 4156 |
|
| 4157 |
const char *swGetString(SWget name) |
| 4158 |
{ |
| 4159 |
const char *result = NULL; |
| 4160 |
|
| 4161 |
switch (name) |
| 4162 |
{ |
| 4163 |
case SW_VENDOR: result = "RLSW"; break; |
| 4164 |
case SW_RENDERER: result = "RLSW OpenGL Software Renderer"; break; |
| 4165 |
case SW_VERSION: result = RLSW_VERSION; break; |
| 4166 |
case SW_EXTENSIONS: result = "None"; break; |
| 4167 |
default: RLSW.errCode = SW_INVALID_ENUM; break; |
| 4168 |
} |
| 4169 |
|
| 4170 |
return result; |
| 4171 |
} |
| 4172 |
|
| 4173 |
SWerrcode swGetError(void) |
| 4174 |
{ |
| 4175 |
SWerrcode ret = RLSW.errCode; |
| 4176 |
RLSW.errCode = SW_NO_ERROR; |
| 4177 |
return ret; |
| 4178 |
} |
| 4179 |
|
| 4180 |
void swViewport(int x, int y, int width, int height) |
| 4181 |
{ |
| 4182 |
if ((width < 0) || (height < 0)) |
| 4183 |
{ |
| 4184 |
RLSW.errCode = SW_INVALID_VALUE; |
| 4185 |
return; |
| 4186 |
} |
| 4187 |
|
| 4188 |
RLSW.vpSize[0] = width; |
| 4189 |
RLSW.vpSize[1] = height; |
| 4190 |
|
| 4191 |
RLSW.vpHalf[0] = width/2.0f; |
| 4192 |
RLSW.vpHalf[1] = height/2.0f; |
| 4193 |
|
| 4194 |
RLSW.vpCenter[0] = (float)x + RLSW.vpHalf[0]; |
| 4195 |
RLSW.vpCenter[1] = (float)y + RLSW.vpHalf[1]; |
| 4196 |
} |
| 4197 |
|
| 4198 |
void swScissor(int x, int y, int width, int height) |
| 4199 |
{ |
| 4200 |
if ((width < 0) || (height < 0)) |
| 4201 |
{ |
| 4202 |
RLSW.errCode = SW_INVALID_VALUE; |
| 4203 |
return; |
| 4204 |
} |
| 4205 |
|
| 4206 |
RLSW.scMin[0] = x; |
| 4207 |
RLSW.scMin[1] = y; |
| 4208 |
RLSW.scMax[0] = x + width; |
| 4209 |
RLSW.scMax[1] = y + height; |
| 4210 |
|
| 4211 |
RLSW.scClipMin[0] = (2.0f*(float)RLSW.scMin[0]/(float)RLSW.vpSize[0]) - 1.0f; |
| 4212 |
RLSW.scClipMax[0] = (2.0f*(float)RLSW.scMax[0]/(float)RLSW.vpSize[0]) - 1.0f; |
| 4213 |
RLSW.scClipMax[1] = 1.0f - (2.0f*(float)RLSW.scMin[1]/(float)RLSW.vpSize[1]); |
| 4214 |
RLSW.scClipMin[1] = 1.0f - (2.0f*(float)RLSW.scMax[1]/(float)RLSW.vpSize[1]); |
| 4215 |
} |
| 4216 |
|
| 4217 |
void swClearColor(float r, float g, float b, float a) |
| 4218 |
{ |
| 4219 |
RLSW.clearColor[0] = r; |
| 4220 |
RLSW.clearColor[1] = g; |
| 4221 |
RLSW.clearColor[2] = b; |
| 4222 |
RLSW.clearColor[3] = a; |
| 4223 |
} |
| 4224 |
|
| 4225 |
void swClearDepth(float depth) |
| 4226 |
{ |
| 4227 |
RLSW.clearDepth = depth; |
| 4228 |
} |
| 4229 |
|
| 4230 |
void swClear(uint32_t bitmask) |
| 4231 |
{ |
| 4232 |
if (!sw_is_ready_to_render()) return; |
| 4233 |
|
| 4234 |
if ((bitmask & (SW_COLOR_BUFFER_BIT)) && (RLSW.colorBuffer != NULL) && (RLSW.colorBuffer->pixels != NULL)) |
| 4235 |
{ |
| 4236 |
sw_framebuffer_fill_color(RLSW.colorBuffer, RLSW.clearColor); |
| 4237 |
} |
| 4238 |
|
| 4239 |
if ((bitmask & (SW_DEPTH_BUFFER_BIT)) && (RLSW.depthBuffer != NULL) && (RLSW.depthBuffer->pixels != NULL)) |
| 4240 |
{ |
| 4241 |
sw_framebuffer_fill_depth(RLSW.depthBuffer, RLSW.clearDepth); |
| 4242 |
} |
| 4243 |
} |
| 4244 |
|
| 4245 |
void swBlendFunc(SWfactor sfactor, SWfactor dfactor) |
| 4246 |
{ |
| 4247 |
int sIndex = sw_blend_factor_index(sfactor); |
| 4248 |
if (sIndex < 0) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 4249 |
|
| 4250 |
int dIndex = sw_blend_factor_index(dfactor); |
| 4251 |
if (dIndex < 0) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 4252 |
|
| 4253 |
RLSW.srcFactor = sfactor; |
| 4254 |
RLSW.dstFactor = dfactor; |
| 4255 |
RLSW.blendFlags = sw_blend_compute_flags(sfactor, dfactor); |
| 4256 |
RLSW.blendFunc = SW_BLEND_TABLE[sIndex][dIndex]; |
| 4257 |
} |
| 4258 |
|
| 4259 |
void swPolygonMode(SWpoly mode) |
| 4260 |
{ |
| 4261 |
if (!sw_is_poly_mode_valid(mode)) |
| 4262 |
{ |
| 4263 |
RLSW.errCode = SW_INVALID_ENUM; |
| 4264 |
return; |
| 4265 |
} |
| 4266 |
|
| 4267 |
RLSW.polyMode = mode; |
| 4268 |
} |
| 4269 |
|
| 4270 |
void swCullFace(SWface face) |
| 4271 |
{ |
| 4272 |
if (!sw_is_face_valid(face)) |
| 4273 |
{ |
| 4274 |
RLSW.errCode = SW_INVALID_ENUM; |
| 4275 |
return; |
| 4276 |
} |
| 4277 |
|
| 4278 |
RLSW.cullFace = face; |
| 4279 |
} |
| 4280 |
|
| 4281 |
void swPointSize(float size) |
| 4282 |
{ |
| 4283 |
RLSW.pointRadius = floorf(size*0.5f); |
| 4284 |
} |
| 4285 |
|
| 4286 |
void swLineWidth(float width) |
| 4287 |
{ |
| 4288 |
RLSW.lineWidth = roundf(width); |
| 4289 |
} |
| 4290 |
|
| 4291 |
void swMatrixMode(SWmatrix mode) |
| 4292 |
{ |
| 4293 |
switch (mode) |
| 4294 |
{ |
| 4295 |
case SW_PROJECTION: RLSW.currentMatrix = &RLSW.stackProjection[RLSW.stackProjectionCounter - 1]; break; |
| 4296 |
case SW_MODELVIEW: RLSW.currentMatrix = &RLSW.stackModelview[RLSW.stackModelviewCounter - 1]; break; |
| 4297 |
case SW_TEXTURE: RLSW.currentMatrix = &RLSW.stackTexture[RLSW.stackTextureCounter - 1]; break; |
| 4298 |
default: RLSW.errCode = SW_INVALID_ENUM; return; |
| 4299 |
} |
| 4300 |
|
| 4301 |
RLSW.currentMatrixMode = mode; |
| 4302 |
} |
| 4303 |
|
| 4304 |
void swPushMatrix(void) |
| 4305 |
{ |
| 4306 |
switch (RLSW.currentMatrixMode) |
| 4307 |
{ |
| 4308 |
case SW_PROJECTION: |
| 4309 |
{ |
| 4310 |
if (RLSW.stackProjectionCounter >= SW_MAX_PROJECTION_STACK_SIZE) |
| 4311 |
{ |
| 4312 |
RLSW.errCode = SW_STACK_OVERFLOW; |
| 4313 |
return; |
| 4314 |
} |
| 4315 |
|
| 4316 |
int iOld = RLSW.stackProjectionCounter - 1; |
| 4317 |
int iNew = RLSW.stackProjectionCounter++; |
| 4318 |
|
| 4319 |
for (int i = 0; i < 16; i++) |
| 4320 |
{ |
| 4321 |
RLSW.stackProjection[iNew][i] = RLSW.stackProjection[iOld][i]; |
| 4322 |
} |
| 4323 |
|
| 4324 |
RLSW.currentMatrix = &RLSW.stackProjection[iNew]; |
| 4325 |
} break; |
| 4326 |
case SW_MODELVIEW: |
| 4327 |
{ |
| 4328 |
if (RLSW.stackModelviewCounter >= SW_MAX_MODELVIEW_STACK_SIZE) |
| 4329 |
{ |
| 4330 |
RLSW.errCode = SW_STACK_OVERFLOW; |
| 4331 |
return; |
| 4332 |
} |
| 4333 |
|
| 4334 |
int iOld = RLSW.stackModelviewCounter - 1; |
| 4335 |
int iNew = RLSW.stackModelviewCounter++; |
| 4336 |
|
| 4337 |
for (int i = 0; i < 16; i++) |
| 4338 |
{ |
| 4339 |
RLSW.stackModelview[iNew][i] = RLSW.stackModelview[iOld][i]; |
| 4340 |
} |
| 4341 |
|
| 4342 |
RLSW.currentMatrix = &RLSW.stackModelview[iNew]; |
| 4343 |
} break; |
| 4344 |
case SW_TEXTURE: |
| 4345 |
{ |
| 4346 |
if (RLSW.stackTextureCounter >= SW_MAX_TEXTURE_STACK_SIZE) |
| 4347 |
{ |
| 4348 |
RLSW.errCode = SW_STACK_OVERFLOW; |
| 4349 |
return; |
| 4350 |
} |
| 4351 |
|
| 4352 |
int iOld = RLSW.stackTextureCounter - 1; |
| 4353 |
int iNew = RLSW.stackTextureCounter++; |
| 4354 |
|
| 4355 |
for (int i = 0; i < 16; i++) |
| 4356 |
{ |
| 4357 |
RLSW.stackTexture[iNew][i] = RLSW.stackTexture[iOld][i]; |
| 4358 |
} |
| 4359 |
|
| 4360 |
RLSW.currentMatrix = &RLSW.stackTexture[iNew]; |
| 4361 |
} break; |
| 4362 |
default: break; |
| 4363 |
} |
| 4364 |
} |
| 4365 |
|
| 4366 |
void swPopMatrix(void) |
| 4367 |
{ |
| 4368 |
switch (RLSW.currentMatrixMode) |
| 4369 |
{ |
| 4370 |
case SW_PROJECTION: |
| 4371 |
{ |
| 4372 |
if (RLSW.stackProjectionCounter <= 0) |
| 4373 |
{ |
| 4374 |
RLSW.errCode = SW_STACK_UNDERFLOW; |
| 4375 |
return; |
| 4376 |
} |
| 4377 |
|
| 4378 |
RLSW.currentMatrix = &RLSW.stackProjection[(--RLSW.stackProjectionCounter) - 1]; |
| 4379 |
RLSW.isDirtyMVP = true; |
| 4380 |
} break; |
| 4381 |
case SW_MODELVIEW: |
| 4382 |
{ |
| 4383 |
if (RLSW.stackModelviewCounter <= 0) |
| 4384 |
{ |
| 4385 |
RLSW.errCode = SW_STACK_UNDERFLOW; |
| 4386 |
return; |
| 4387 |
} |
| 4388 |
|
| 4389 |
RLSW.currentMatrix = &RLSW.stackModelview[(--RLSW.stackModelviewCounter) - 1]; |
| 4390 |
RLSW.isDirtyMVP = true; |
| 4391 |
} break; |
| 4392 |
case SW_TEXTURE: |
| 4393 |
{ |
| 4394 |
if (RLSW.stackTextureCounter <= 0) |
| 4395 |
{ |
| 4396 |
RLSW.errCode = SW_STACK_UNDERFLOW; |
| 4397 |
return; |
| 4398 |
} |
| 4399 |
|
| 4400 |
RLSW.currentMatrix = &RLSW.stackTexture[(--RLSW.stackTextureCounter) - 1]; |
| 4401 |
} break; |
| 4402 |
default: break; |
| 4403 |
} |
| 4404 |
} |
| 4405 |
|
| 4406 |
void swLoadIdentity(void) |
| 4407 |
{ |
| 4408 |
sw_matrix_id(*RLSW.currentMatrix); |
| 4409 |
if (RLSW.currentMatrixMode != SW_TEXTURE) RLSW.isDirtyMVP = true; |
| 4410 |
} |
| 4411 |
|
| 4412 |
void swTranslatef(float x, float y, float z) |
| 4413 |
{ |
| 4414 |
sw_matrix_t mat = { 0 }; |
| 4415 |
sw_matrix_id(mat); |
| 4416 |
|
| 4417 |
mat[12] = x; |
| 4418 |
mat[13] = y; |
| 4419 |
mat[14] = z; |
| 4420 |
|
| 4421 |
sw_matrix_mul(*RLSW.currentMatrix, mat, *RLSW.currentMatrix); |
| 4422 |
|
| 4423 |
if (RLSW.currentMatrixMode != SW_TEXTURE) RLSW.isDirtyMVP = true; |
| 4424 |
} |
| 4425 |
|
| 4426 |
void swRotatef(float angle, float x, float y, float z) |
| 4427 |
{ |
| 4428 |
angle *= SW_DEG2RAD; |
| 4429 |
|
| 4430 |
float lengthSq = x*x + y*y + z*z; |
| 4431 |
|
| 4432 |
if ((lengthSq != 1.0f) && (lengthSq != 0.0f)) |
| 4433 |
{ |
| 4434 |
float invLength = 1.0f/sqrtf(lengthSq); |
| 4435 |
x *= invLength; |
| 4436 |
y *= invLength; |
| 4437 |
z *= invLength; |
| 4438 |
} |
| 4439 |
|
| 4440 |
float sinres = sinf(angle); |
| 4441 |
float cosres = cosf(angle); |
| 4442 |
float t = 1.0f - cosres; |
| 4443 |
|
| 4444 |
sw_matrix_t mat = { 0 }; |
| 4445 |
|
| 4446 |
mat[0] = x*x*t + cosres; |
| 4447 |
mat[1] = y*x*t + z*sinres; |
| 4448 |
mat[2] = z*x*t - y*sinres; |
| 4449 |
mat[3] = 0.0f; |
| 4450 |
|
| 4451 |
mat[4] = x*y*t - z*sinres; |
| 4452 |
mat[5] = y*y*t + cosres; |
| 4453 |
mat[6] = z*y*t + x*sinres; |
| 4454 |
mat[7] = 0.0f; |
| 4455 |
|
| 4456 |
mat[8] = x*z*t + y*sinres; |
| 4457 |
mat[9] = y*z*t - x*sinres; |
| 4458 |
mat[10] = z*z*t + cosres; |
| 4459 |
mat[11] = 0.0f; |
| 4460 |
|
| 4461 |
mat[12] = 0.0f; |
| 4462 |
mat[13] = 0.0f; |
| 4463 |
mat[14] = 0.0f; |
| 4464 |
mat[15] = 1.0f; |
| 4465 |
|
| 4466 |
sw_matrix_mul(*RLSW.currentMatrix, mat, *RLSW.currentMatrix); |
| 4467 |
|
| 4468 |
if (RLSW.currentMatrixMode != SW_TEXTURE) RLSW.isDirtyMVP = true; |
| 4469 |
} |
| 4470 |
|
| 4471 |
void swScalef(float x, float y, float z) |
| 4472 |
{ |
| 4473 |
sw_matrix_t mat = { 0 }; |
| 4474 |
|
| 4475 |
mat[0] = x, mat[1] = 0, mat[2] = 0, mat[3] = 0; |
| 4476 |
mat[4] = 0, mat[5] = y, mat[6] = 0, mat[7] = 0; |
| 4477 |
mat[8] = 0, mat[9] = 0, mat[10] = z, mat[11] = 0; |
| 4478 |
mat[12] = 0, mat[13] = 0, mat[14] = 0, mat[15] = 1; |
| 4479 |
|
| 4480 |
sw_matrix_mul(*RLSW.currentMatrix, mat, *RLSW.currentMatrix); |
| 4481 |
|
| 4482 |
if (RLSW.currentMatrixMode != SW_TEXTURE) RLSW.isDirtyMVP = true; |
| 4483 |
} |
| 4484 |
|
| 4485 |
void swMultMatrixf(const float *mat) |
| 4486 |
{ |
| 4487 |
sw_matrix_mul(*RLSW.currentMatrix, mat, *RLSW.currentMatrix); |
| 4488 |
|
| 4489 |
if (RLSW.currentMatrixMode != SW_TEXTURE) RLSW.isDirtyMVP = true; |
| 4490 |
} |
| 4491 |
|
| 4492 |
void swFrustum(double left, double right, double bottom, double top, double znear, double zfar) |
| 4493 |
{ |
| 4494 |
sw_matrix_t mat = { 0 }; |
| 4495 |
|
| 4496 |
double rl = right - left; |
| 4497 |
double tb = top - bottom; |
| 4498 |
double fn = zfar - znear; |
| 4499 |
|
| 4500 |
mat[0] = (float)(znear*2.0)/rl; |
| 4501 |
mat[1] = 0.0f; |
| 4502 |
mat[2] = 0.0f; |
| 4503 |
mat[3] = 0.0f; |
| 4504 |
|
| 4505 |
mat[4] = 0.0f; |
| 4506 |
mat[5] = (float)(znear*2.0)/tb; |
| 4507 |
mat[6] = 0.0f; |
| 4508 |
mat[7] = 0.0f; |
| 4509 |
|
| 4510 |
mat[8] = (float)(right + left)/rl; |
| 4511 |
mat[9] = (float)(top + bottom)/tb; |
| 4512 |
mat[10] = -(float)(zfar + znear)/fn; |
| 4513 |
mat[11] = -1.0f; |
| 4514 |
|
| 4515 |
mat[12] = 0.0f; |
| 4516 |
mat[13] = 0.0f; |
| 4517 |
mat[14] = -(zfar*znear*2.0)/fn; |
| 4518 |
mat[15] = 0.0f; |
| 4519 |
|
| 4520 |
sw_matrix_mul(*RLSW.currentMatrix, *RLSW.currentMatrix, mat); |
| 4521 |
|
| 4522 |
if (RLSW.currentMatrixMode != SW_TEXTURE) RLSW.isDirtyMVP = true; |
| 4523 |
} |
| 4524 |
|
| 4525 |
void swOrtho(double left, double right, double bottom, double top, double znear, double zfar) |
| 4526 |
{ |
| 4527 |
sw_matrix_t mat = { 0 }; |
| 4528 |
|
| 4529 |
double rl = right - left; |
| 4530 |
double tb = top - bottom; |
| 4531 |
double fn = zfar - znear; |
| 4532 |
|
| 4533 |
mat[0] = 2.0f/(float)rl; |
| 4534 |
mat[1] = 0.0f; |
| 4535 |
mat[2] = 0.0f; |
| 4536 |
mat[3] = 0.0f; |
| 4537 |
|
| 4538 |
mat[4] = 0.0f; |
| 4539 |
mat[5] = 2.0f/(float)tb; |
| 4540 |
mat[6] = 0.0f; |
| 4541 |
mat[7] = 0.0f; |
| 4542 |
|
| 4543 |
mat[8] = 0.0f; |
| 4544 |
mat[9] = 0.0f; |
| 4545 |
mat[10] = -2.0f/(float)fn; |
| 4546 |
mat[11] = 0.0f; |
| 4547 |
|
| 4548 |
mat[12] = -(float)(left + right)/rl; |
| 4549 |
mat[13] = -(float)(top + bottom)/tb; |
| 4550 |
mat[14] = -(float)(zfar + znear)/fn; |
| 4551 |
mat[15] = 1.0f; |
| 4552 |
|
| 4553 |
sw_matrix_mul(*RLSW.currentMatrix, *RLSW.currentMatrix, mat); |
| 4554 |
|
| 4555 |
if (RLSW.currentMatrixMode != SW_TEXTURE) RLSW.isDirtyMVP = true; |
| 4556 |
} |
| 4557 |
|
| 4558 |
void swBegin(SWdraw mode) |
| 4559 |
{ |
| 4560 |
|
| 4561 |
if (!sw_is_ready_to_render()) |
| 4562 |
{ |
| 4563 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4564 |
return; |
| 4565 |
} |
| 4566 |
|
| 4567 |
|
| 4568 |
if (!sw_is_draw_mode_valid(mode)) |
| 4569 |
{ |
| 4570 |
RLSW.errCode = SW_INVALID_ENUM; |
| 4571 |
return; |
| 4572 |
} |
| 4573 |
|
| 4574 |
|
| 4575 |
if (sw_immediate_is_active()) |
| 4576 |
{ |
| 4577 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4578 |
return; |
| 4579 |
} |
| 4580 |
|
| 4581 |
sw_immediate_begin(mode); |
| 4582 |
} |
| 4583 |
|
| 4584 |
void swEnd(void) |
| 4585 |
{ |
| 4586 |
|
| 4587 |
if (!sw_immediate_is_active()) |
| 4588 |
{ |
| 4589 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4590 |
return; |
| 4591 |
} |
| 4592 |
|
| 4593 |
sw_immediate_end(); |
| 4594 |
} |
| 4595 |
|
| 4596 |
void swVertex2i(int x, int y) |
| 4597 |
{ |
| 4598 |
const float v[4] = { (float)x, (float)y, 0.0f, 1.0f }; |
| 4599 |
sw_immediate_push_vertex(v); |
| 4600 |
} |
| 4601 |
|
| 4602 |
void swVertex2f(float x, float y) |
| 4603 |
{ |
| 4604 |
const float v[4] = { x, y, 0.0f, 1.0f }; |
| 4605 |
sw_immediate_push_vertex(v); |
| 4606 |
} |
| 4607 |
|
| 4608 |
void swVertex2fv(const float *v) |
| 4609 |
{ |
| 4610 |
const float v4[4] = { v[0], v[1], 0.0f, 1.0f }; |
| 4611 |
sw_immediate_push_vertex(v4); |
| 4612 |
} |
| 4613 |
|
| 4614 |
void swVertex3i(int x, int y, int z) |
| 4615 |
{ |
| 4616 |
const float v[4] = { (float)x, (float)y, (float)z, 1.0f }; |
| 4617 |
sw_immediate_push_vertex(v); |
| 4618 |
} |
| 4619 |
|
| 4620 |
void swVertex3f(float x, float y, float z) |
| 4621 |
{ |
| 4622 |
const float v[4] = { x, y, z, 1.0f }; |
| 4623 |
sw_immediate_push_vertex(v); |
| 4624 |
} |
| 4625 |
|
| 4626 |
void swVertex3fv(const float *v) |
| 4627 |
{ |
| 4628 |
const float v4[4] = { v[0], v[1], v[2], 1.0f }; |
| 4629 |
sw_immediate_push_vertex(v4); |
| 4630 |
} |
| 4631 |
|
| 4632 |
void swVertex4i(int x, int y, int z, int w) |
| 4633 |
{ |
| 4634 |
const float v[4] = { (float)x, (float)y, (float)z, (float)w }; |
| 4635 |
sw_immediate_push_vertex(v); |
| 4636 |
} |
| 4637 |
|
| 4638 |
void swVertex4f(float x, float y, float z, float w) |
| 4639 |
{ |
| 4640 |
const float v[4] = { x, y, z, w }; |
| 4641 |
sw_immediate_push_vertex(v); |
| 4642 |
} |
| 4643 |
|
| 4644 |
void swVertex4fv(const float *v) |
| 4645 |
{ |
| 4646 |
sw_immediate_push_vertex(v); |
| 4647 |
} |
| 4648 |
|
| 4649 |
void swColor3ub(uint8_t r, uint8_t g, uint8_t b) |
| 4650 |
{ |
| 4651 |
float cv[4] = { 0 }; |
| 4652 |
cv[0] = (float)r*SW_INV_255; |
| 4653 |
cv[1] = (float)g*SW_INV_255; |
| 4654 |
cv[2] = (float)b*SW_INV_255; |
| 4655 |
cv[3] = 1.0f; |
| 4656 |
|
| 4657 |
sw_immediate_set_color(cv); |
| 4658 |
} |
| 4659 |
|
| 4660 |
void swColor3ubv(const uint8_t *v) |
| 4661 |
{ |
| 4662 |
float cv[4] = { 0 }; |
| 4663 |
cv[0] = (float)v[0]*SW_INV_255; |
| 4664 |
cv[1] = (float)v[1]*SW_INV_255; |
| 4665 |
cv[2] = (float)v[2]*SW_INV_255; |
| 4666 |
cv[3] = 1.0f; |
| 4667 |
|
| 4668 |
sw_immediate_set_color(cv); |
| 4669 |
} |
| 4670 |
|
| 4671 |
void swColor3f(float r, float g, float b) |
| 4672 |
{ |
| 4673 |
float cv[4] = { 0 }; |
| 4674 |
cv[0] = r; |
| 4675 |
cv[1] = g; |
| 4676 |
cv[2] = b; |
| 4677 |
cv[3] = 1.0f; |
| 4678 |
|
| 4679 |
sw_immediate_set_color(cv); |
| 4680 |
} |
| 4681 |
|
| 4682 |
void swColor3fv(const float *v) |
| 4683 |
{ |
| 4684 |
float cv[4] = { 0 }; |
| 4685 |
cv[0] = v[0]; |
| 4686 |
cv[1] = v[1]; |
| 4687 |
cv[2] = v[2]; |
| 4688 |
cv[3] = 1.0f; |
| 4689 |
|
| 4690 |
sw_immediate_set_color(cv); |
| 4691 |
} |
| 4692 |
|
| 4693 |
void swColor4ub(uint8_t r, uint8_t g, uint8_t b, uint8_t a) |
| 4694 |
{ |
| 4695 |
float cv[4] = { 0 }; |
| 4696 |
cv[0] = (float)r*SW_INV_255; |
| 4697 |
cv[1] = (float)g*SW_INV_255; |
| 4698 |
cv[2] = (float)b*SW_INV_255; |
| 4699 |
cv[3] = (float)a*SW_INV_255; |
| 4700 |
|
| 4701 |
sw_immediate_set_color(cv); |
| 4702 |
} |
| 4703 |
|
| 4704 |
void swColor4ubv(const uint8_t *v) |
| 4705 |
{ |
| 4706 |
float cv[4] = { 0 }; |
| 4707 |
cv[0] = (float)v[0]*SW_INV_255; |
| 4708 |
cv[1] = (float)v[1]*SW_INV_255; |
| 4709 |
cv[2] = (float)v[2]*SW_INV_255; |
| 4710 |
cv[3] = (float)v[3]*SW_INV_255; |
| 4711 |
|
| 4712 |
sw_immediate_set_color(cv); |
| 4713 |
} |
| 4714 |
|
| 4715 |
void swColor4f(float r, float g, float b, float a) |
| 4716 |
{ |
| 4717 |
float cv[4] = { 0 }; |
| 4718 |
cv[0] = r; |
| 4719 |
cv[1] = g; |
| 4720 |
cv[2] = b; |
| 4721 |
cv[3] = a; |
| 4722 |
|
| 4723 |
sw_immediate_set_color(cv); |
| 4724 |
} |
| 4725 |
|
| 4726 |
void swColor4fv(const float *v) |
| 4727 |
{ |
| 4728 |
sw_immediate_set_color(v); |
| 4729 |
} |
| 4730 |
|
| 4731 |
void swTexCoord2f(float u, float v) |
| 4732 |
{ |
| 4733 |
const float texcoord[2] = { u, v }; |
| 4734 |
sw_immediate_set_texcoord(texcoord); |
| 4735 |
} |
| 4736 |
|
| 4737 |
void swTexCoord2fv(const float *v) |
| 4738 |
{ |
| 4739 |
sw_immediate_set_texcoord(v); |
| 4740 |
} |
| 4741 |
|
| 4742 |
void swBindArray(SWarray type, void *buffer) |
| 4743 |
{ |
| 4744 |
if (sw_immediate_is_active()) |
| 4745 |
{ |
| 4746 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4747 |
return; |
| 4748 |
} |
| 4749 |
|
| 4750 |
switch (type) |
| 4751 |
{ |
| 4752 |
case SW_VERTEX_ARRAY: RLSW.array.positions = (float *)buffer; break; |
| 4753 |
case SW_TEXTURE_COORD_ARRAY: RLSW.array.texcoords = (float *)buffer; break; |
| 4754 |
case SW_COLOR_ARRAY: RLSW.array.colors = (uint8_t *)buffer; break; |
| 4755 |
default: break; |
| 4756 |
} |
| 4757 |
} |
| 4758 |
|
| 4759 |
void swDrawArrays(SWdraw mode, int offset, int count) |
| 4760 |
{ |
| 4761 |
if ((sw_immediate_is_active()) || (!sw_is_ready_to_render()) || (RLSW.array.positions == NULL)) |
| 4762 |
{ |
| 4763 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4764 |
return; |
| 4765 |
} |
| 4766 |
|
| 4767 |
sw_immediate_begin(mode); |
| 4768 |
{ |
| 4769 |
const float *positions = RLSW.array.positions; |
| 4770 |
const float *texcoords = RLSW.array.texcoords; |
| 4771 |
const uint8_t *colors = RLSW.array.colors; |
| 4772 |
|
| 4773 |
int end = offset + count; |
| 4774 |
for (int i = offset; i < end; i++) |
| 4775 |
{ |
| 4776 |
if (texcoords) sw_immediate_set_texcoord(&texcoords[2*i]); |
| 4777 |
|
| 4778 |
if (colors) |
| 4779 |
{ |
| 4780 |
const uint8_t *c = &colors[4*i]; |
| 4781 |
float color[4] = { |
| 4782 |
(float)c[0]*SW_INV_255, |
| 4783 |
(float)c[1]*SW_INV_255, |
| 4784 |
(float)c[2]*SW_INV_255, |
| 4785 |
(float)c[3]*SW_INV_255, |
| 4786 |
}; |
| 4787 |
sw_immediate_set_color(color); |
| 4788 |
} |
| 4789 |
|
| 4790 |
const float *p = &positions[3*i]; |
| 4791 |
float position[4] = { p[0], p[1], p[2], 1.0f }; |
| 4792 |
sw_immediate_push_vertex(position); |
| 4793 |
} |
| 4794 |
} |
| 4795 |
sw_immediate_end(); |
| 4796 |
} |
| 4797 |
|
| 4798 |
void swDrawElements(SWdraw mode, int count, int type, const void *indices) |
| 4799 |
{ |
| 4800 |
if ((sw_immediate_is_active()) || (!sw_is_ready_to_render()) || (RLSW.array.positions == NULL)) |
| 4801 |
{ |
| 4802 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4803 |
return; |
| 4804 |
} |
| 4805 |
|
| 4806 |
if ((count < 0) || (indices == NULL)) |
| 4807 |
{ |
| 4808 |
RLSW.errCode = SW_INVALID_VALUE; |
| 4809 |
return; |
| 4810 |
} |
| 4811 |
|
| 4812 |
if ((type != SW_UNSIGNED_BYTE) && (type != SW_UNSIGNED_SHORT) && (type != SW_UNSIGNED_INT)) |
| 4813 |
{ |
| 4814 |
RLSW.errCode = SW_INVALID_ENUM; |
| 4815 |
return; |
| 4816 |
} |
| 4817 |
|
| 4818 |
sw_immediate_begin(mode); |
| 4819 |
{ |
| 4820 |
const float *positions = RLSW.array.positions; |
| 4821 |
const float *texcoords = RLSW.array.texcoords; |
| 4822 |
const uint8_t *colors = RLSW.array.colors; |
| 4823 |
|
| 4824 |
const uint8_t *indicesUb = (type == SW_UNSIGNED_BYTE)? indices : NULL; |
| 4825 |
const uint16_t *indicesUs = (type == SW_UNSIGNED_SHORT)? indices : NULL; |
| 4826 |
const uint32_t *indicesUi = (type == SW_UNSIGNED_INT)? indices : NULL; |
| 4827 |
|
| 4828 |
for (int i = 0; i < count; i++) |
| 4829 |
{ |
| 4830 |
uint32_t index = indicesUb? (uint32_t)indicesUb[i] : (indicesUs? (uint32_t)indicesUs[i] : (uint32_t)indicesUi[i]); |
| 4831 |
|
| 4832 |
if (texcoords) sw_immediate_set_texcoord(&texcoords[2*index]); |
| 4833 |
|
| 4834 |
if (colors) |
| 4835 |
{ |
| 4836 |
const uint8_t *c = &colors[4*index]; |
| 4837 |
float color[4] = { |
| 4838 |
(float)c[0]*SW_INV_255, |
| 4839 |
(float)c[1]*SW_INV_255, |
| 4840 |
(float)c[2]*SW_INV_255, |
| 4841 |
(float)c[3]*SW_INV_255, |
| 4842 |
}; |
| 4843 |
sw_immediate_set_color(color); |
| 4844 |
} |
| 4845 |
|
| 4846 |
const float *p = &positions[3*index]; |
| 4847 |
float position[4] = { p[0], p[1], p[2], 1.0f }; |
| 4848 |
sw_immediate_push_vertex(position); |
| 4849 |
} |
| 4850 |
} |
| 4851 |
sw_immediate_end(); |
| 4852 |
} |
| 4853 |
|
| 4854 |
void swGenTextures(int count, sw_handle_t *textures) |
| 4855 |
{ |
| 4856 |
if (sw_immediate_is_active()) |
| 4857 |
{ |
| 4858 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4859 |
return; |
| 4860 |
} |
| 4861 |
|
| 4862 |
if (!count || !textures) return; |
| 4863 |
|
| 4864 |
for (int i = 0; i < count; i++) |
| 4865 |
{ |
| 4866 |
sw_handle_t h = sw_pool_alloc(&RLSW.texturePool); |
| 4867 |
if (h == SW_HANDLE_NULL) { RLSW.errCode = SW_OUT_OF_MEMORY; return; } |
| 4868 |
textures[i] = h; |
| 4869 |
} |
| 4870 |
} |
| 4871 |
|
| 4872 |
void swDeleteTextures(int count, sw_handle_t *textures) |
| 4873 |
{ |
| 4874 |
if (sw_immediate_is_active()) |
| 4875 |
{ |
| 4876 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4877 |
return; |
| 4878 |
} |
| 4879 |
|
| 4880 |
if (!count || !textures) return; |
| 4881 |
|
| 4882 |
for (int i = 0; i < count; i++) |
| 4883 |
{ |
| 4884 |
sw_texture_t *tex = sw_pool_get(&RLSW.texturePool, textures[i]); |
| 4885 |
if (!tex) { RLSW.errCode = SW_INVALID_VALUE; continue; } |
| 4886 |
if (tex == RLSW.boundTexture) RLSW.boundTexture = NULL; |
| 4887 |
if (tex == RLSW.colorBuffer) RLSW.colorBuffer = NULL; |
| 4888 |
if (tex == RLSW.depthBuffer) RLSW.depthBuffer = NULL; |
| 4889 |
|
| 4890 |
sw_texture_free(tex); |
| 4891 |
*tex = (sw_texture_t) { 0 }; |
| 4892 |
|
| 4893 |
sw_pool_free(&RLSW.texturePool, textures[i]); |
| 4894 |
} |
| 4895 |
} |
| 4896 |
|
| 4897 |
void swBindTexture(sw_handle_t id) |
| 4898 |
{ |
| 4899 |
if (sw_immediate_is_active()) |
| 4900 |
{ |
| 4901 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4902 |
return; |
| 4903 |
} |
| 4904 |
|
| 4905 |
if (id == SW_HANDLE_NULL) |
| 4906 |
{ |
| 4907 |
RLSW.boundTexture = NULL; |
| 4908 |
return; |
| 4909 |
} |
| 4910 |
|
| 4911 |
if (!sw_is_texture_valid(id)) |
| 4912 |
{ |
| 4913 |
RLSW.errCode = SW_INVALID_VALUE; |
| 4914 |
return; |
| 4915 |
} |
| 4916 |
|
| 4917 |
RLSW.boundTexture = sw_pool_get(&RLSW.texturePool, id); |
| 4918 |
} |
| 4919 |
|
| 4920 |
void swTexStorage2D(int width, int height, SWinternalformat format) |
| 4921 |
{ |
| 4922 |
if (sw_immediate_is_active()) |
| 4923 |
{ |
| 4924 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4925 |
return; |
| 4926 |
} |
| 4927 |
|
| 4928 |
if (RLSW.boundTexture == NULL) return; |
| 4929 |
|
| 4930 |
int pixelFormat = SW_PIXELFORMAT_UNKNOWN; |
| 4931 |
switch (format) |
| 4932 |
{ |
| 4933 |
case SW_LUMINANCE8: pixelFormat = SW_PIXELFORMAT_COLOR_GRAYSCALE; break; |
| 4934 |
case SW_LUMINANCE8_ALPHA8: pixelFormat = SW_PIXELFORMAT_COLOR_GRAYALPHA; break; |
| 4935 |
case SW_R3_G3_B2: pixelFormat = SW_PIXELFORMAT_COLOR_R3G3B2; break; |
| 4936 |
case SW_RGB8: pixelFormat = SW_PIXELFORMAT_COLOR_R8G8B8; break; |
| 4937 |
case SW_RGBA4: pixelFormat = SW_PIXELFORMAT_COLOR_R4G4B4A4; break; |
| 4938 |
case SW_RGB5_A1: pixelFormat = SW_PIXELFORMAT_COLOR_R5G5B5A1; break; |
| 4939 |
case SW_RGBA8: pixelFormat = SW_PIXELFORMAT_COLOR_R8G8B8A8; break; |
| 4940 |
case SW_R16F: pixelFormat = SW_PIXELFORMAT_COLOR_R16; break; |
| 4941 |
case SW_RGB16F: pixelFormat = SW_PIXELFORMAT_COLOR_R16G16B16; break; |
| 4942 |
case SW_RGBA16F: pixelFormat = SW_PIXELFORMAT_COLOR_R16G16B16A16; break; |
| 4943 |
case SW_R32F: pixelFormat = SW_PIXELFORMAT_COLOR_R32; break; |
| 4944 |
case SW_RGB32F: pixelFormat = SW_PIXELFORMAT_COLOR_R32G32B32; break; |
| 4945 |
case SW_RGBA32F: pixelFormat = SW_PIXELFORMAT_COLOR_R32G32B32A32; break; |
| 4946 |
case SW_DEPTH_COMPONENT16: pixelFormat = SW_PIXELFORMAT_DEPTH_D16; break; |
| 4947 |
case SW_DEPTH_COMPONENT24: pixelFormat = SW_PIXELFORMAT_DEPTH_D32; break; |
| 4948 |
case SW_DEPTH_COMPONENT32: pixelFormat = SW_PIXELFORMAT_DEPTH_D32; break; |
| 4949 |
case SW_DEPTH_COMPONENT32F: pixelFormat = SW_PIXELFORMAT_DEPTH_D32; break; |
| 4950 |
default: RLSW.errCode = SW_INVALID_ENUM; return; |
| 4951 |
} |
| 4952 |
|
| 4953 |
(void)sw_texture_alloc(RLSW.boundTexture, NULL, width, height, pixelFormat); |
| 4954 |
} |
| 4955 |
|
| 4956 |
void swTexImage2D(int width, int height, SWformat format, SWtype type, const void *data) |
| 4957 |
{ |
| 4958 |
if (sw_immediate_is_active()) |
| 4959 |
{ |
| 4960 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4961 |
return; |
| 4962 |
} |
| 4963 |
|
| 4964 |
if (RLSW.boundTexture == NULL) return; |
| 4965 |
|
| 4966 |
int pixelFormat = sw_pixel_get_format(format, type); |
| 4967 |
if (pixelFormat <= SW_PIXELFORMAT_UNKNOWN) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 4968 |
|
| 4969 |
(void)sw_texture_alloc(RLSW.boundTexture, data, width, height, pixelFormat); |
| 4970 |
} |
| 4971 |
|
| 4972 |
void swTexSubImage2D(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels) |
| 4973 |
{ |
| 4974 |
if (sw_immediate_is_active()) |
| 4975 |
{ |
| 4976 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4977 |
return; |
| 4978 |
} |
| 4979 |
|
| 4980 |
if (!sw_is_texture_complete(RLSW.boundTexture) || (!pixels) || (width <= 0) || (height <= 0)) |
| 4981 |
{ |
| 4982 |
RLSW.errCode = SW_INVALID_VALUE; |
| 4983 |
return; |
| 4984 |
} |
| 4985 |
|
| 4986 |
if ((x < 0) || (y < 0) || (x + width > RLSW.boundTexture->width) || (y + height > RLSW.boundTexture->height)) |
| 4987 |
{ |
| 4988 |
RLSW.errCode = SW_INVALID_VALUE; |
| 4989 |
return; |
| 4990 |
} |
| 4991 |
|
| 4992 |
sw_pixel_write_color_f writeColor = sw_pixel_get_write_color_func(RLSW.boundTexture->format); |
| 4993 |
if (writeColor == NULL) |
| 4994 |
{ |
| 4995 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 4996 |
return; |
| 4997 |
} |
| 4998 |
|
| 4999 |
sw_pixelformat_t srcPixelFormat = sw_pixel_get_format(format, type); |
| 5000 |
sw_pixel_read_color_f readColor = sw_pixel_get_read_color_func(srcPixelFormat); |
| 5001 |
if (readColor == NULL) |
| 5002 |
{ |
| 5003 |
RLSW.errCode = SW_INVALID_ENUM; |
| 5004 |
return; |
| 5005 |
} |
| 5006 |
|
| 5007 |
const int srcPixelSize = SW_PIXELFORMAT_SIZE[srcPixelFormat]; |
| 5008 |
const int dstPixelSize = SW_PIXELFORMAT_SIZE[RLSW.boundTexture->format]; |
| 5009 |
|
| 5010 |
const uint8_t *srcBytes = (const uint8_t *)pixels; |
| 5011 |
uint8_t *dstBytes = (uint8_t *)RLSW.boundTexture->pixels; |
| 5012 |
|
| 5013 |
if (srcPixelFormat == RLSW.boundTexture->format) |
| 5014 |
{ |
| 5015 |
const int rowSize = width*dstPixelSize; |
| 5016 |
const int dstStride = RLSW.boundTexture->width*dstPixelSize; |
| 5017 |
const int srcStride = width*srcPixelSize; |
| 5018 |
|
| 5019 |
uint8_t *dstRow = dstBytes + (y*RLSW.boundTexture->width + x)*dstPixelSize; |
| 5020 |
const uint8_t *srcRow = srcBytes; |
| 5021 |
|
| 5022 |
for (int j = 0; j < height; ++j) |
| 5023 |
{ |
| 5024 |
for (int i = 0; i < rowSize; ++i) dstRow[i] = srcRow[i]; |
| 5025 |
dstRow += dstStride; |
| 5026 |
srcRow += srcStride; |
| 5027 |
} |
| 5028 |
return; |
| 5029 |
} |
| 5030 |
|
| 5031 |
bool alphaFound = false; |
| 5032 |
|
| 5033 |
for (int j = 0; j < height; ++j) |
| 5034 |
{ |
| 5035 |
for (int i = 0; i < width; ++i) |
| 5036 |
{ |
| 5037 |
float color[4]; |
| 5038 |
const int srcPixelOffset = ((j*width) + i)*srcPixelSize; |
| 5039 |
const int dstPixelOffset = (((y + j)*RLSW.boundTexture->width) + (x + i))*dstPixelSize; |
| 5040 |
|
| 5041 |
readColor(color, srcBytes, srcPixelOffset); |
| 5042 |
alphaFound |= (color[3] < 1.0f); |
| 5043 |
|
| 5044 |
writeColor(dstBytes, color, dstPixelOffset); |
| 5045 |
} |
| 5046 |
} |
| 5047 |
|
| 5048 |
RLSW.boundTexture->alpha = alphaFound? SW_PIXELFORMAT_ALPHA[srcPixelFormat] : SW_PIXEL_ALPHA_NONE; |
| 5049 |
} |
| 5050 |
|
| 5051 |
void swTexParameteri(int param, int value) |
| 5052 |
{ |
| 5053 |
if (sw_immediate_is_active()) |
| 5054 |
{ |
| 5055 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 5056 |
return; |
| 5057 |
} |
| 5058 |
|
| 5059 |
if (RLSW.boundTexture == NULL) return; |
| 5060 |
|
| 5061 |
switch (param) |
| 5062 |
{ |
| 5063 |
case SW_TEXTURE_MIN_FILTER: |
| 5064 |
{ |
| 5065 |
if (!sw_is_texture_filter_valid(value)) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 5066 |
RLSW.boundTexture->minFilter = (SWfilter)value; |
| 5067 |
} break; |
| 5068 |
case SW_TEXTURE_MAG_FILTER: |
| 5069 |
{ |
| 5070 |
if (!sw_is_texture_filter_valid(value)) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 5071 |
RLSW.boundTexture->magFilter = (SWfilter)value; |
| 5072 |
} break; |
| 5073 |
case SW_TEXTURE_WRAP_S: |
| 5074 |
{ |
| 5075 |
if (!sw_is_texture_wrap_valid(value)) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 5076 |
RLSW.boundTexture->sWrap = (SWwrap)value; |
| 5077 |
} break; |
| 5078 |
case SW_TEXTURE_WRAP_T: |
| 5079 |
{ |
| 5080 |
if (!sw_is_texture_wrap_valid(value)) { RLSW.errCode = SW_INVALID_ENUM; return; } |
| 5081 |
RLSW.boundTexture->tWrap = (SWwrap)value; |
| 5082 |
} break; |
| 5083 |
default: RLSW.errCode = SW_INVALID_ENUM; break; |
| 5084 |
} |
| 5085 |
} |
| 5086 |
|
| 5087 |
void swGenFramebuffers(int count, uint32_t *framebuffers) |
| 5088 |
{ |
| 5089 |
if (sw_immediate_is_active()) |
| 5090 |
{ |
| 5091 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 5092 |
return; |
| 5093 |
} |
| 5094 |
|
| 5095 |
if (!count || !framebuffers) return; |
| 5096 |
|
| 5097 |
for (int i = 0; i < count; i++) |
| 5098 |
{ |
| 5099 |
sw_handle_t h = sw_pool_alloc(&RLSW.framebufferPool); |
| 5100 |
if (h == SW_HANDLE_NULL) { RLSW.errCode = SW_OUT_OF_MEMORY; return; } |
| 5101 |
framebuffers[i] = h; |
| 5102 |
} |
| 5103 |
} |
| 5104 |
|
| 5105 |
void swDeleteFramebuffers(int count, uint32_t *framebuffers) |
| 5106 |
{ |
| 5107 |
if (sw_immediate_is_active()) |
| 5108 |
{ |
| 5109 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 5110 |
return; |
| 5111 |
} |
| 5112 |
|
| 5113 |
if (!count || !framebuffers) return; |
| 5114 |
|
| 5115 |
for (int i = 0; i < count; i++) |
| 5116 |
{ |
| 5117 |
sw_framebuffer_t *fb = sw_pool_get(&RLSW.framebufferPool, framebuffers[i]); |
| 5118 |
if (!fb) { RLSW.errCode = SW_INVALID_VALUE; continue; } |
| 5119 |
|
| 5120 |
if (framebuffers[i] == RLSW.boundFramebufferId) |
| 5121 |
{ |
| 5122 |
RLSW.boundFramebufferId = SW_HANDLE_NULL; |
| 5123 |
RLSW.colorBuffer = &RLSW.framebuffer.color; |
| 5124 |
RLSW.depthBuffer = &RLSW.framebuffer.depth; |
| 5125 |
} |
| 5126 |
|
| 5127 |
sw_pool_free(&RLSW.framebufferPool, framebuffers[i]); |
| 5128 |
} |
| 5129 |
} |
| 5130 |
|
| 5131 |
void swBindFramebuffer(uint32_t id) |
| 5132 |
{ |
| 5133 |
if (sw_immediate_is_active()) |
| 5134 |
{ |
| 5135 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 5136 |
return; |
| 5137 |
} |
| 5138 |
|
| 5139 |
if (id == SW_HANDLE_NULL) |
| 5140 |
{ |
| 5141 |
RLSW.boundFramebufferId = SW_HANDLE_NULL; |
| 5142 |
RLSW.colorBuffer = &RLSW.framebuffer.color; |
| 5143 |
RLSW.depthBuffer = &RLSW.framebuffer.depth; |
| 5144 |
return; |
| 5145 |
} |
| 5146 |
|
| 5147 |
sw_framebuffer_t *fb = sw_pool_get(&RLSW.framebufferPool, id); |
| 5148 |
if (fb == NULL) |
| 5149 |
{ |
| 5150 |
RLSW.errCode = SW_INVALID_VALUE; |
| 5151 |
return; |
| 5152 |
} |
| 5153 |
|
| 5154 |
RLSW.boundFramebufferId = id; |
| 5155 |
RLSW.colorBuffer = sw_pool_get(&RLSW.texturePool, fb->colorAttachment); |
| 5156 |
RLSW.depthBuffer = sw_pool_get(&RLSW.texturePool, fb->depthAttachment); |
| 5157 |
} |
| 5158 |
|
| 5159 |
void swFramebufferTexture2D(SWattachment attach, uint32_t texture) |
| 5160 |
{ |
| 5161 |
if ((sw_immediate_is_active()) || (RLSW.boundFramebufferId == SW_HANDLE_NULL)) |
| 5162 |
{ |
| 5163 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 5164 |
return; |
| 5165 |
} |
| 5166 |
|
| 5167 |
sw_framebuffer_t *fb = sw_pool_get(&RLSW.framebufferPool, RLSW.boundFramebufferId); |
| 5168 |
if (fb == NULL) return; |
| 5169 |
|
| 5170 |
switch (attach) |
| 5171 |
{ |
| 5172 |
case SW_COLOR_ATTACHMENT: |
| 5173 |
{ |
| 5174 |
fb->colorAttachment = texture; |
| 5175 |
RLSW.colorBuffer = sw_pool_get(&RLSW.texturePool, texture); |
| 5176 |
} break; |
| 5177 |
case SW_DEPTH_ATTACHMENT: |
| 5178 |
{ |
| 5179 |
fb->depthAttachment = texture; |
| 5180 |
RLSW.depthBuffer = sw_pool_get(&RLSW.texturePool, texture); |
| 5181 |
} break; |
| 5182 |
default: RLSW.errCode = SW_INVALID_ENUM; break; |
| 5183 |
} |
| 5184 |
} |
| 5185 |
|
| 5186 |
SWfbstatus swCheckFramebufferStatus(void) |
| 5187 |
{ |
| 5188 |
if (RLSW.boundFramebufferId == SW_HANDLE_NULL) return SW_FRAMEBUFFER_COMPLETE; |
| 5189 |
|
| 5190 |
if (RLSW.colorBuffer == NULL) return SW_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT; |
| 5191 |
|
| 5192 |
if (!sw_is_texture_complete(RLSW.colorBuffer)) return SW_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 5193 |
|
| 5194 |
if (RLSW.colorBuffer->format != SW_FRAMEBUFFER_COLOR_FORMAT) return SW_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 5195 |
|
| 5196 |
if (RLSW.depthBuffer) |
| 5197 |
{ |
| 5198 |
if (!sw_is_texture_complete(RLSW.depthBuffer)) return SW_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 5199 |
|
| 5200 |
if (RLSW.depthBuffer->format != SW_FRAMEBUFFER_DEPTH_FORMAT) return SW_FRAMEBUFFER_INCOMPLETE_ATTACHMENT; |
| 5201 |
|
| 5202 |
if ((RLSW.colorBuffer->width != RLSW.depthBuffer->width) || |
| 5203 |
(RLSW.colorBuffer->height != RLSW.depthBuffer->height)) return SW_FRAMEBUFFER_INCOMPLETE_DIMENSIONS; |
| 5204 |
} |
| 5205 |
|
| 5206 |
return SW_FRAMEBUFFER_COMPLETE; |
| 5207 |
} |
| 5208 |
|
| 5209 |
void swGetFramebufferAttachmentParameteriv(SWattachment attachment, SWattachget property, int *v) |
| 5210 |
{ |
| 5211 |
if (RLSW.boundFramebufferId == SW_HANDLE_NULL) |
| 5212 |
{ |
| 5213 |
RLSW.errCode = SW_INVALID_OPERATION; |
| 5214 |
return; |
| 5215 |
} |
| 5216 |
|
| 5217 |
if (v == NULL) |
| 5218 |
{ |
| 5219 |
RLSW.errCode = SW_INVALID_VALUE; |
| 5220 |
return; |
| 5221 |
} |
| 5222 |
|
| 5223 |
sw_framebuffer_t *fb = sw_pool_get(&RLSW.framebufferPool, RLSW.boundFramebufferId); |
| 5224 |
if (fb == NULL) return; |
| 5225 |
|
| 5226 |
switch (property) |
| 5227 |
{ |
| 5228 |
case SW_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME: |
| 5229 |
{ |
| 5230 |
if (attachment == SW_COLOR_ATTACHMENT) *v = fb->colorAttachment; |
| 5231 |
else if (attachment == SW_DEPTH_ATTACHMENT) *v = fb->depthAttachment; |
| 5232 |
} break; |
| 5233 |
case SW_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE: *v = GL_TEXTURE; break; |
| 5234 |
default: break; |
| 5235 |
} |
| 5236 |
} |
| 5237 |
|
| 5238 |
#endif |
| 5239 |
|
| 5240 |
|
| 5241 |
|
| 5242 |
|
| 5243 |
|
| 5244 |
|
| 5245 |
|
| 5246 |
#ifdef RLSW_TEMPLATE_RASTER_TRIANGLE |
| 5247 |
|
| 5248 |
|
| 5249 |
|
| 5250 |
|
| 5251 |
|
| 5252 |
|
| 5253 |
|
| 5254 |
#define SW_RASTER_TRIANGLE_SPAN SW_CONCATX(sw_raster_triangle_span_, RLSW_TEMPLATE_RASTER_TRIANGLE) |
| 5255 |
#define SW_RASTER_TRIANGLE SW_CONCATX(sw_raster_triangle_, RLSW_TEMPLATE_RASTER_TRIANGLE) |
| 5256 |
|
| 5257 |
#ifdef SW_ENABLE_TEXTURE |
| 5258 |
#define SW_GET_GRAD sw_get_vertex_grad_PCT |
| 5259 |
#define SW_ADD_GRAD sw_add_vertex_grad_PCT |
| 5260 |
#define SW_ADD_GRAD_SCALED sw_add_vertex_grad_scaled_PCT |
| 5261 |
#else |
| 5262 |
#define SW_GET_GRAD sw_get_vertex_grad_PC |
| 5263 |
#define SW_ADD_GRAD sw_add_vertex_grad_PC |
| 5264 |
#define SW_ADD_GRAD_SCALED sw_add_vertex_grad_scaled_PC |
| 5265 |
#endif |
| 5266 |
|
| 5267 |
static void SW_RASTER_TRIANGLE_SPAN(const sw_vertex_t *start, const sw_vertex_t *end, float dUdy, float dVdy) |
| 5268 |
{ |
| 5269 |
|
| 5270 |
int xStart = (int)start->position[0]; |
| 5271 |
int xEnd = (int)end->position[0]; |
| 5272 |
if (xStart == xEnd) return; |
| 5273 |
|
| 5274 |
|
| 5275 |
float dxRcp = 1.0f/(end->position[0] - start->position[0]); |
| 5276 |
|
| 5277 |
|
| 5278 |
float dWdx = (end->position[3] - start->position[3])*dxRcp; |
| 5279 |
float dCdx[4] = { |
| 5280 |
(end->color[0] - start->color[0])*dxRcp, |
| 5281 |
(end->color[1] - start->color[1])*dxRcp, |
| 5282 |
(end->color[2] - start->color[2])*dxRcp, |
| 5283 |
(end->color[3] - start->color[3])*dxRcp |
| 5284 |
}; |
| 5285 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5286 |
float dZdx = (end->position[2] - start->position[2])*dxRcp; |
| 5287 |
#endif |
| 5288 |
#ifdef SW_ENABLE_TEXTURE |
| 5289 |
float dUdx = (end->texcoord[0] - start->texcoord[0])*dxRcp; |
| 5290 |
float dVdx = (end->texcoord[1] - start->texcoord[1])*dxRcp; |
| 5291 |
#endif |
| 5292 |
|
| 5293 |
|
| 5294 |
float xSubstep = 1.0f - sw_fract(start->position[0]); |
| 5295 |
|
| 5296 |
|
| 5297 |
float w = start->position[3] + dWdx*xSubstep; |
| 5298 |
float color[4] = { |
| 5299 |
start->color[0] + dCdx[0]*xSubstep, |
| 5300 |
start->color[1] + dCdx[1]*xSubstep, |
| 5301 |
start->color[2] + dCdx[2]*xSubstep, |
| 5302 |
start->color[3] + dCdx[3]*xSubstep |
| 5303 |
}; |
| 5304 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5305 |
float z = start->position[2] + dZdx*xSubstep; |
| 5306 |
#endif |
| 5307 |
#ifdef SW_ENABLE_TEXTURE |
| 5308 |
float u = start->texcoord[0] + dUdx*xSubstep; |
| 5309 |
float v = start->texcoord[1] + dVdx*xSubstep; |
| 5310 |
#endif |
| 5311 |
|
| 5312 |
|
| 5313 |
int y = (int)start->position[1]; |
| 5314 |
int baseOffset = y*RLSW.colorBuffer->width + xStart; |
| 5315 |
uint8_t *cPtr = (uint8_t *)(RLSW.colorBuffer->pixels) + baseOffset*SW_FRAMEBUFFER_COLOR_SIZE; |
| 5316 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5317 |
uint8_t *dPtr = (uint8_t *)(RLSW.depthBuffer->pixels) + baseOffset*SW_FRAMEBUFFER_DEPTH_SIZE; |
| 5318 |
#endif |
| 5319 |
|
| 5320 |
#define SW_AFFINE_BLOCK 16 |
| 5321 |
|
| 5322 |
int x = xStart; |
| 5323 |
while (x < xEnd) |
| 5324 |
{ |
| 5325 |
|
| 5326 |
int blockEnd = x + SW_AFFINE_BLOCK; |
| 5327 |
if (blockEnd > xEnd) blockEnd = xEnd; |
| 5328 |
float blockLenF = (float)(blockEnd - x); |
| 5329 |
float blockLenRcp = 1.0f/blockLenF; |
| 5330 |
|
| 5331 |
|
| 5332 |
float wRcpA = 1.0f/w; |
| 5333 |
float wB = w + dWdx*blockLenF; |
| 5334 |
float wRcpB = 1.0f/wB; |
| 5335 |
|
| 5336 |
|
| 5337 |
float srcColor[4] = { |
| 5338 |
color[0]*wRcpA, |
| 5339 |
color[1]*wRcpA, |
| 5340 |
color[2]*wRcpA, |
| 5341 |
color[3]*wRcpA |
| 5342 |
}; |
| 5343 |
float dSrcColordx[4] = { |
| 5344 |
((color[0] + dCdx[0]*blockLenF)*wRcpB - srcColor[0])*blockLenRcp, |
| 5345 |
((color[1] + dCdx[1]*blockLenF)*wRcpB - srcColor[1])*blockLenRcp, |
| 5346 |
((color[2] + dCdx[2]*blockLenF)*wRcpB - srcColor[2])*blockLenRcp, |
| 5347 |
((color[3] + dCdx[3]*blockLenF)*wRcpB - srcColor[3])*blockLenRcp |
| 5348 |
}; |
| 5349 |
|
| 5350 |
#ifdef SW_ENABLE_TEXTURE |
| 5351 |
|
| 5352 |
float uAffine = u*wRcpA; |
| 5353 |
float vAffine = v*wRcpA; |
| 5354 |
float dUaffine = ((u + dUdx*blockLenF)*wRcpB - uAffine)*blockLenRcp; |
| 5355 |
float dVaffine = ((v + dVdx*blockLenF)*wRcpB - vAffine)*blockLenRcp; |
| 5356 |
#endif |
| 5357 |
|
| 5358 |
|
| 5359 |
for (; x < blockEnd; x++) |
| 5360 |
{ |
| 5361 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5362 |
{ |
| 5363 |
float depth = SW_FRAMEBUFFER_DEPTH_GET(dPtr, 0); |
| 5364 |
if (z > depth) goto discard; |
| 5365 |
SW_FRAMEBUFFER_DEPTH_SET(dPtr, z, 0); |
| 5366 |
} |
| 5367 |
#endif |
| 5368 |
|
| 5369 |
#ifdef SW_ENABLE_TEXTURE |
| 5370 |
{ |
| 5371 |
float texColor[4]; |
| 5372 |
sw_texture_sample(texColor, RLSW.boundTexture, uAffine, vAffine, dUdx, dUdy, dVdx, dVdy); |
| 5373 |
float finalColor[4] = { |
| 5374 |
srcColor[0]*texColor[0], |
| 5375 |
srcColor[1]*texColor[1], |
| 5376 |
srcColor[2]*texColor[2], |
| 5377 |
srcColor[3]*texColor[3] |
| 5378 |
}; |
| 5379 |
#ifdef SW_ENABLE_BLEND |
| 5380 |
{ |
| 5381 |
float dstColor[4]; |
| 5382 |
SW_FRAMEBUFFER_COLOR_GET(dstColor, cPtr, 0); |
| 5383 |
RLSW.blendFunc(dstColor, finalColor); |
| 5384 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, dstColor, 0); |
| 5385 |
} |
| 5386 |
#else |
| 5387 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, finalColor, 0); |
| 5388 |
#endif |
| 5389 |
} |
| 5390 |
#else |
| 5391 |
{ |
| 5392 |
#ifdef SW_ENABLE_BLEND |
| 5393 |
{ |
| 5394 |
float dstColor[4]; |
| 5395 |
SW_FRAMEBUFFER_COLOR_GET(dstColor, cPtr, 0); |
| 5396 |
RLSW.blendFunc(dstColor, srcColor); |
| 5397 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, dstColor, 0); |
| 5398 |
} |
| 5399 |
#else |
| 5400 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, srcColor, 0); |
| 5401 |
#endif |
| 5402 |
} |
| 5403 |
#endif |
| 5404 |
|
| 5405 |
discard: |
| 5406 |
srcColor[0] += dSrcColordx[0]; |
| 5407 |
srcColor[1] += dSrcColordx[1]; |
| 5408 |
srcColor[2] += dSrcColordx[2]; |
| 5409 |
srcColor[3] += dSrcColordx[3]; |
| 5410 |
cPtr += SW_FRAMEBUFFER_COLOR_SIZE; |
| 5411 |
|
| 5412 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5413 |
{ |
| 5414 |
z += dZdx; |
| 5415 |
dPtr += SW_FRAMEBUFFER_DEPTH_SIZE; |
| 5416 |
} |
| 5417 |
#endif |
| 5418 |
|
| 5419 |
#ifdef SW_ENABLE_TEXTURE |
| 5420 |
{ |
| 5421 |
uAffine += dUaffine; |
| 5422 |
vAffine += dVaffine; |
| 5423 |
} |
| 5424 |
#endif |
| 5425 |
} |
| 5426 |
|
| 5427 |
|
| 5428 |
w = wB; |
| 5429 |
color[0] += dCdx[0]*blockLenF; |
| 5430 |
color[1] += dCdx[1]*blockLenF; |
| 5431 |
color[2] += dCdx[2]*blockLenF; |
| 5432 |
color[3] += dCdx[3]*blockLenF; |
| 5433 |
#ifdef SW_ENABLE_TEXTURE |
| 5434 |
u += dUdx*blockLenF; |
| 5435 |
v += dVdx*blockLenF; |
| 5436 |
#endif |
| 5437 |
} |
| 5438 |
|
| 5439 |
#undef SW_AFFINE_BLOCK |
| 5440 |
} |
| 5441 |
|
| 5442 |
static void SW_RASTER_TRIANGLE(const sw_vertex_t *v0, const sw_vertex_t *v1, const sw_vertex_t *v2) |
| 5443 |
{ |
| 5444 |
|
| 5445 |
if (v0->position[1] > v1->position[1]) { const sw_vertex_t *tmp = v0; v0 = v1; v1 = tmp; } |
| 5446 |
if (v1->position[1] > v2->position[1]) { const sw_vertex_t *tmp = v1; v1 = v2; v2 = tmp; } |
| 5447 |
if (v0->position[1] > v1->position[1]) { const sw_vertex_t *tmp = v0; v0 = v1; v1 = tmp; } |
| 5448 |
|
| 5449 |
|
| 5450 |
float x0 = v0->position[0], y0 = v0->position[1]; |
| 5451 |
float x1 = v1->position[0], y1 = v1->position[1]; |
| 5452 |
float x2 = v2->position[0], y2 = v2->position[1]; |
| 5453 |
|
| 5454 |
|
| 5455 |
float h02 = y2 - y0; |
| 5456 |
float h01 = y1 - y0; |
| 5457 |
float h12 = y2 - y1; |
| 5458 |
|
| 5459 |
if (h02 < 1e-6f) return; |
| 5460 |
|
| 5461 |
|
| 5462 |
float h02Rcp = 1.0f/h02; |
| 5463 |
float h01Rcp = (h01 > 1e-6f)? 1.0f/h01 : 0.0f; |
| 5464 |
float h12Rcp = (h12 > 1e-6f)? 1.0f/h12 : 0.0f; |
| 5465 |
|
| 5466 |
|
| 5467 |
sw_vertex_t dVXdy02, dVXdy01, dVXdy12; |
| 5468 |
SW_GET_GRAD(&dVXdy02, v0, v2, h02Rcp); |
| 5469 |
SW_GET_GRAD(&dVXdy01, v0, v1, h01Rcp); |
| 5470 |
SW_GET_GRAD(&dVXdy12, v1, v2, h12Rcp); |
| 5471 |
|
| 5472 |
|
| 5473 |
float y0Substep = 1.0f - sw_fract(y0); |
| 5474 |
float y1Substep = 1.0f - sw_fract(y1); |
| 5475 |
|
| 5476 |
|
| 5477 |
sw_vertex_t lVert = *v0, rVert = *v0; |
| 5478 |
SW_ADD_GRAD_SCALED(&lVert, &dVXdy02, y0Substep); |
| 5479 |
SW_ADD_GRAD_SCALED(&rVert, &dVXdy01, y0Substep); |
| 5480 |
|
| 5481 |
|
| 5482 |
int yTop = (int)y0; |
| 5483 |
int yMid = (int)y1; |
| 5484 |
int yBot = (int)y2; |
| 5485 |
|
| 5486 |
|
| 5487 |
for (int y = yTop; y < yMid; y++) |
| 5488 |
{ |
| 5489 |
lVert.position[1] = rVert.position[1] = y; |
| 5490 |
bool longSideIsLeft = (lVert.position[0] < rVert.position[0]); |
| 5491 |
const sw_vertex_t *a = longSideIsLeft? &lVert : &rVert; |
| 5492 |
const sw_vertex_t *b = longSideIsLeft? &rVert : &lVert; |
| 5493 |
SW_RASTER_TRIANGLE_SPAN(a, b, dVXdy02.texcoord[0], dVXdy02.texcoord[1]); |
| 5494 |
SW_ADD_GRAD(&lVert, &dVXdy02); |
| 5495 |
SW_ADD_GRAD(&rVert, &dVXdy01); |
| 5496 |
} |
| 5497 |
|
| 5498 |
|
| 5499 |
rVert = *v1; |
| 5500 |
SW_ADD_GRAD_SCALED(&rVert, &dVXdy12, y1Substep); |
| 5501 |
|
| 5502 |
|
| 5503 |
for (int y = yMid; y < yBot; y++) |
| 5504 |
{ |
| 5505 |
lVert.position[1] = rVert.position[1] = y; |
| 5506 |
bool longSideIsLeft = (lVert.position[0] < rVert.position[0]); |
| 5507 |
const sw_vertex_t *a = longSideIsLeft? &lVert : &rVert; |
| 5508 |
const sw_vertex_t *b = longSideIsLeft? &rVert : &lVert; |
| 5509 |
SW_RASTER_TRIANGLE_SPAN(a, b, dVXdy02.texcoord[0], dVXdy02.texcoord[1]); |
| 5510 |
SW_ADD_GRAD(&lVert, &dVXdy02); |
| 5511 |
SW_ADD_GRAD(&rVert, &dVXdy12); |
| 5512 |
} |
| 5513 |
} |
| 5514 |
|
| 5515 |
#undef SW_GET_GRAD |
| 5516 |
#undef SW_ADD_GRAD |
| 5517 |
#undef SW_ADD_GRAD_SCALED |
| 5518 |
|
| 5519 |
#endif |
| 5520 |
|
| 5521 |
|
| 5522 |
|
| 5523 |
|
| 5524 |
#ifdef RLSW_TEMPLATE_RASTER_QUAD |
| 5525 |
|
| 5526 |
|
| 5527 |
|
| 5528 |
|
| 5529 |
|
| 5530 |
|
| 5531 |
|
| 5532 |
#define SW_RASTER_QUAD SW_CONCATX(sw_raster_quad_, RLSW_TEMPLATE_RASTER_QUAD) |
| 5533 |
|
| 5534 |
|
| 5535 |
|
| 5536 |
|
| 5537 |
static void SW_RASTER_QUAD(const sw_vertex_t *a, const sw_vertex_t *b, |
| 5538 |
const sw_vertex_t *c, const sw_vertex_t *d) |
| 5539 |
{ |
| 5540 |
|
| 5541 |
|
| 5542 |
const sw_vertex_t *verts[4] = { a, b, c, d }; |
| 5543 |
const sw_vertex_t *tl = verts[0], *tr = verts[0], *br = verts[0], *bl = verts[0]; |
| 5544 |
for (int i = 1; i < 4; i++) |
| 5545 |
{ |
| 5546 |
float sum = verts[i]->position[0] + verts[i]->position[1]; |
| 5547 |
float diff = verts[i]->position[0] - verts[i]->position[1]; |
| 5548 |
if (sum < tl->position[0] + tl->position[1]) tl = verts[i]; |
| 5549 |
if (diff > tr->position[0] - tr->position[1]) tr = verts[i]; |
| 5550 |
if (sum > br->position[0] + br->position[1]) br = verts[i]; |
| 5551 |
if (diff < bl->position[0] - bl->position[1]) bl = verts[i]; |
| 5552 |
} |
| 5553 |
|
| 5554 |
int xMin = (int)tl->position[0]; |
| 5555 |
int yMin = (int)tl->position[1]; |
| 5556 |
int xMax = (int)br->position[0]; |
| 5557 |
int yMax = (int)br->position[1]; |
| 5558 |
|
| 5559 |
float w = (float)(xMax - xMin); |
| 5560 |
float h = (float)(yMax - yMin); |
| 5561 |
if ((w <= 0) || (h <= 0)) return; |
| 5562 |
|
| 5563 |
float wRcp = 1.0f/w; |
| 5564 |
float hRcp = 1.0f/h; |
| 5565 |
|
| 5566 |
|
| 5567 |
float xSubstep = 1.0f - sw_fract(tl->position[0]); |
| 5568 |
float ySubstep = 1.0f - sw_fract(tl->position[1]); |
| 5569 |
|
| 5570 |
|
| 5571 |
float dCdx[4] = { |
| 5572 |
(tr->color[0] - tl->color[0])*wRcp, |
| 5573 |
(tr->color[1] - tl->color[1])*wRcp, |
| 5574 |
(tr->color[2] - tl->color[2])*wRcp, |
| 5575 |
(tr->color[3] - tl->color[3])*wRcp, |
| 5576 |
}; |
| 5577 |
float dCdy[4] = { |
| 5578 |
(bl->color[0] - tl->color[0])*hRcp, |
| 5579 |
(bl->color[1] - tl->color[1])*hRcp, |
| 5580 |
(bl->color[2] - tl->color[2])*hRcp, |
| 5581 |
(bl->color[3] - tl->color[3])*hRcp, |
| 5582 |
}; |
| 5583 |
|
| 5584 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5585 |
float dZdx = (tr->position[2] - tl->position[2])*wRcp; |
| 5586 |
float dZdy = (bl->position[2] - tl->position[2])*hRcp; |
| 5587 |
float zRow = tl->position[2] + dZdx*xSubstep + dZdy*ySubstep; |
| 5588 |
#endif |
| 5589 |
|
| 5590 |
#ifdef SW_ENABLE_TEXTURE |
| 5591 |
float dUdx = (tr->texcoord[0] - tl->texcoord[0])*wRcp; |
| 5592 |
float dVdx = (tr->texcoord[1] - tl->texcoord[1])*wRcp; |
| 5593 |
float dUdy = (bl->texcoord[0] - tl->texcoord[0])*hRcp; |
| 5594 |
float dVdy = (bl->texcoord[1] - tl->texcoord[1])*hRcp; |
| 5595 |
float uRow = tl->texcoord[0] + dUdx*xSubstep + dUdy*ySubstep; |
| 5596 |
float vRow = tl->texcoord[1] + dVdx*xSubstep + dVdy*ySubstep; |
| 5597 |
#endif |
| 5598 |
|
| 5599 |
float cRow[4] = { |
| 5600 |
tl->color[0] + dCdx[0]*xSubstep + dCdy[0]*ySubstep, |
| 5601 |
tl->color[1] + dCdx[1]*xSubstep + dCdy[1]*ySubstep, |
| 5602 |
tl->color[2] + dCdx[2]*xSubstep + dCdy[2]*ySubstep, |
| 5603 |
tl->color[3] + dCdx[3]*xSubstep + dCdy[3]*ySubstep, |
| 5604 |
}; |
| 5605 |
|
| 5606 |
int stride = RLSW.colorBuffer->width; |
| 5607 |
uint8_t *cPixels = RLSW.colorBuffer->pixels; |
| 5608 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5609 |
uint8_t *dPixels = RLSW.depthBuffer->pixels; |
| 5610 |
#endif |
| 5611 |
|
| 5612 |
for (int y = yMin; y < yMax; y++) |
| 5613 |
{ |
| 5614 |
int baseOffset = y*stride + xMin; |
| 5615 |
uint8_t *cPtr = cPixels + baseOffset*SW_FRAMEBUFFER_COLOR_SIZE; |
| 5616 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5617 |
uint8_t *dPtr = dPixels + baseOffset*SW_FRAMEBUFFER_DEPTH_SIZE; |
| 5618 |
float z = zRow; |
| 5619 |
#endif |
| 5620 |
#ifdef SW_ENABLE_TEXTURE |
| 5621 |
float u = uRow; |
| 5622 |
float v = vRow; |
| 5623 |
#endif |
| 5624 |
float color[4] = { cRow[0], cRow[1], cRow[2], cRow[3] }; |
| 5625 |
|
| 5626 |
for (int x = xMin; x < xMax; x++) |
| 5627 |
{ |
| 5628 |
float srcColor[4] = { color[0], color[1], color[2], color[3] }; |
| 5629 |
|
| 5630 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5631 |
{ |
| 5632 |
float depth = SW_FRAMEBUFFER_DEPTH_GET(dPtr, 0); |
| 5633 |
if (z > depth) goto discard; |
| 5634 |
SW_FRAMEBUFFER_DEPTH_SET(dPtr, z, 0); |
| 5635 |
} |
| 5636 |
#endif |
| 5637 |
|
| 5638 |
#ifdef SW_ENABLE_TEXTURE |
| 5639 |
{ |
| 5640 |
float texColor[4]; |
| 5641 |
sw_texture_sample(texColor, RLSW.boundTexture, u, v, dUdx, dUdy, dVdx, dVdy); |
| 5642 |
srcColor[0] *= texColor[0]; |
| 5643 |
srcColor[1] *= texColor[1]; |
| 5644 |
srcColor[2] *= texColor[2]; |
| 5645 |
srcColor[3] *= texColor[3]; |
| 5646 |
} |
| 5647 |
#endif |
| 5648 |
|
| 5649 |
#ifdef SW_ENABLE_BLEND |
| 5650 |
{ |
| 5651 |
float dstColor[4]; |
| 5652 |
SW_FRAMEBUFFER_COLOR_GET(dstColor, cPtr, 0); |
| 5653 |
RLSW.blendFunc(dstColor, srcColor); |
| 5654 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, dstColor, 0); |
| 5655 |
} |
| 5656 |
#else |
| 5657 |
{ |
| 5658 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, srcColor, 0); |
| 5659 |
} |
| 5660 |
#endif |
| 5661 |
|
| 5662 |
discard: |
| 5663 |
color[0] += dCdx[0]; |
| 5664 |
color[1] += dCdx[1]; |
| 5665 |
color[2] += dCdx[2]; |
| 5666 |
color[3] += dCdx[3]; |
| 5667 |
|
| 5668 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5669 |
{ |
| 5670 |
z += dZdx; |
| 5671 |
dPtr += SW_FRAMEBUFFER_DEPTH_SIZE; |
| 5672 |
} |
| 5673 |
#endif |
| 5674 |
|
| 5675 |
#ifdef SW_ENABLE_TEXTURE |
| 5676 |
{ |
| 5677 |
u += dUdx; |
| 5678 |
v += dVdx; |
| 5679 |
} |
| 5680 |
#endif |
| 5681 |
|
| 5682 |
cPtr += SW_FRAMEBUFFER_COLOR_SIZE; |
| 5683 |
} |
| 5684 |
|
| 5685 |
cRow[0] += dCdy[0]; |
| 5686 |
cRow[1] += dCdy[1]; |
| 5687 |
cRow[2] += dCdy[2]; |
| 5688 |
cRow[3] += dCdy[3]; |
| 5689 |
|
| 5690 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5691 |
{ |
| 5692 |
zRow += dZdy; |
| 5693 |
} |
| 5694 |
#endif |
| 5695 |
|
| 5696 |
#ifdef SW_ENABLE_TEXTURE |
| 5697 |
{ |
| 5698 |
uRow += dUdy; |
| 5699 |
vRow += dVdy; |
| 5700 |
} |
| 5701 |
#endif |
| 5702 |
} |
| 5703 |
} |
| 5704 |
|
| 5705 |
#endif |
| 5706 |
|
| 5707 |
|
| 5708 |
|
| 5709 |
|
| 5710 |
#ifdef RLSW_TEMPLATE_RASTER_LINE |
| 5711 |
|
| 5712 |
|
| 5713 |
|
| 5714 |
|
| 5715 |
|
| 5716 |
|
| 5717 |
#define SW_RASTER_LINE SW_CONCATX(sw_raster_line_, RLSW_TEMPLATE_RASTER_LINE) |
| 5718 |
#define SW_RASTER_LINE_THICK SW_CONCATX(sw_raster_line_thick_, RLSW_TEMPLATE_RASTER_LINE) |
| 5719 |
|
| 5720 |
static void SW_RASTER_LINE(const sw_vertex_t *v0, const sw_vertex_t *v1) |
| 5721 |
{ |
| 5722 |
|
| 5723 |
float x0 = v0->position[0] - 0.5f; |
| 5724 |
float y0 = v0->position[1] - 0.5f; |
| 5725 |
float x1 = v1->position[0] - 0.5f; |
| 5726 |
float y1 = v1->position[1] - 0.5f; |
| 5727 |
|
| 5728 |
float dx = x1 - x0; |
| 5729 |
float dy = y1 - y0; |
| 5730 |
|
| 5731 |
|
| 5732 |
float steps, substep; |
| 5733 |
if (fabsf(dx) > fabsf(dy)) |
| 5734 |
{ |
| 5735 |
steps = fabsf(dx); |
| 5736 |
if (steps < 1.0f) return; |
| 5737 |
substep = (dx >= 0.0f)? (1.0f - sw_fract(x0)) : sw_fract(x0); |
| 5738 |
} |
| 5739 |
else |
| 5740 |
{ |
| 5741 |
steps = fabsf(dy); |
| 5742 |
if (steps < 1.0f) return; |
| 5743 |
substep = (dy >= 0.0f)? (1.0f - sw_fract(y0)) : sw_fract(y0); |
| 5744 |
} |
| 5745 |
|
| 5746 |
|
| 5747 |
float xInc = dx/steps; |
| 5748 |
float yInc = dy/steps; |
| 5749 |
float stepRcp = 1.0f/steps; |
| 5750 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5751 |
float zInc = (v1->position[2] - v0->position[2])*stepRcp; |
| 5752 |
#endif |
| 5753 |
float rInc = (v1->color[0] - v0->color[0])*stepRcp; |
| 5754 |
float gInc = (v1->color[1] - v0->color[1])*stepRcp; |
| 5755 |
float bInc = (v1->color[2] - v0->color[2])*stepRcp; |
| 5756 |
float aInc = (v1->color[3] - v0->color[3])*stepRcp; |
| 5757 |
|
| 5758 |
|
| 5759 |
float x = x0 + xInc*substep; |
| 5760 |
float y = y0 + yInc*substep; |
| 5761 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5762 |
float z = v0->position[2] + zInc*substep; |
| 5763 |
#endif |
| 5764 |
float r = v0->color[0] + rInc*substep; |
| 5765 |
float g = v0->color[1] + gInc*substep; |
| 5766 |
float b = v0->color[2] + bInc*substep; |
| 5767 |
float a = v0->color[3] + aInc*substep; |
| 5768 |
|
| 5769 |
|
| 5770 |
const int fbWidth = RLSW.colorBuffer->width; |
| 5771 |
uint8_t *cPixels = RLSW.colorBuffer->pixels; |
| 5772 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5773 |
uint8_t *dPixels = RLSW.depthBuffer->pixels; |
| 5774 |
#endif |
| 5775 |
|
| 5776 |
int numPixels = (int)(steps - substep) + 1; |
| 5777 |
|
| 5778 |
for (int i = 0; i < numPixels; i++) |
| 5779 |
{ |
| 5780 |
int px = x; |
| 5781 |
int py = y; |
| 5782 |
|
| 5783 |
int baseOffset = py*fbWidth + px; |
| 5784 |
uint8_t *cPtr = cPixels + baseOffset*SW_FRAMEBUFFER_COLOR_SIZE; |
| 5785 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5786 |
uint8_t *dPtr = dPixels + baseOffset*SW_FRAMEBUFFER_DEPTH_SIZE; |
| 5787 |
#endif |
| 5788 |
|
| 5789 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5790 |
{ |
| 5791 |
|
| 5792 |
float depth = SW_FRAMEBUFFER_DEPTH_GET(dPtr, 0); |
| 5793 |
if (z > depth) goto discard; |
| 5794 |
|
| 5795 |
|
| 5796 |
SW_FRAMEBUFFER_DEPTH_SET(dPtr, z, 0); |
| 5797 |
} |
| 5798 |
#endif |
| 5799 |
|
| 5800 |
float srcColor[4] = {r, g, b, a}; |
| 5801 |
|
| 5802 |
#ifdef SW_ENABLE_BLEND |
| 5803 |
{ |
| 5804 |
float dstColor[4]; |
| 5805 |
SW_FRAMEBUFFER_COLOR_GET(dstColor, cPtr, 0); |
| 5806 |
RLSW.blendFunc(dstColor, srcColor); |
| 5807 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, dstColor, 0); |
| 5808 |
} |
| 5809 |
#else |
| 5810 |
{ |
| 5811 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, srcColor, 0); |
| 5812 |
} |
| 5813 |
#endif |
| 5814 |
|
| 5815 |
discard: |
| 5816 |
x += xInc; |
| 5817 |
y += yInc; |
| 5818 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5819 |
{ |
| 5820 |
z += zInc; |
| 5821 |
} |
| 5822 |
#endif |
| 5823 |
r += rInc; |
| 5824 |
g += gInc; |
| 5825 |
b += bInc; |
| 5826 |
a += aInc; |
| 5827 |
} |
| 5828 |
} |
| 5829 |
|
| 5830 |
static void SW_RASTER_LINE_THICK(const sw_vertex_t *v0, const sw_vertex_t *v1) |
| 5831 |
{ |
| 5832 |
sw_vertex_t tv0, tv1; |
| 5833 |
|
| 5834 |
int x0 = (int)v0->position[0]; |
| 5835 |
int y0 = (int)v0->position[1]; |
| 5836 |
int x1 = (int)v1->position[0]; |
| 5837 |
int y1 = (int)v1->position[1]; |
| 5838 |
|
| 5839 |
int dx = x1 - x0; |
| 5840 |
int dy = y1 - y0; |
| 5841 |
|
| 5842 |
SW_RASTER_LINE(v0, v1); |
| 5843 |
|
| 5844 |
if ((dx != 0) && (abs(dy/dx) < 1)) |
| 5845 |
{ |
| 5846 |
int wy = (int)((RLSW.lineWidth - 1.0f)*abs(dx)/sqrtf(dx*dx + dy*dy)); |
| 5847 |
wy >>= 1; |
| 5848 |
for (int i = 1; i <= wy; i++) |
| 5849 |
{ |
| 5850 |
tv0 = *v0, tv1 = *v1; |
| 5851 |
tv0.position[1] -= i; |
| 5852 |
tv1.position[1] -= i; |
| 5853 |
SW_RASTER_LINE(&tv0, &tv1); |
| 5854 |
tv0 = *v0, tv1 = *v1; |
| 5855 |
tv0.position[1] += i; |
| 5856 |
tv1.position[1] += i; |
| 5857 |
SW_RASTER_LINE(&tv0, &tv1); |
| 5858 |
} |
| 5859 |
} |
| 5860 |
else if (dy != 0) |
| 5861 |
{ |
| 5862 |
int wx = (int)((RLSW.lineWidth - 1.0f)*abs(dy)/sqrtf(dx*dx + dy*dy)); |
| 5863 |
wx >>= 1; |
| 5864 |
for (int i = 1; i <= wx; i++) |
| 5865 |
{ |
| 5866 |
tv0 = *v0, tv1 = *v1; |
| 5867 |
tv0.position[0] -= i; |
| 5868 |
tv1.position[0] -= i; |
| 5869 |
SW_RASTER_LINE(&tv0, &tv1); |
| 5870 |
tv0 = *v0, tv1 = *v1; |
| 5871 |
tv0.position[0] += i; |
| 5872 |
tv1.position[0] += i; |
| 5873 |
SW_RASTER_LINE(&tv0, &tv1); |
| 5874 |
} |
| 5875 |
} |
| 5876 |
} |
| 5877 |
|
| 5878 |
#endif |
| 5879 |
|
| 5880 |
|
| 5881 |
|
| 5882 |
|
| 5883 |
#ifdef RLSW_TEMPLATE_RASTER_POINT |
| 5884 |
|
| 5885 |
|
| 5886 |
|
| 5887 |
|
| 5888 |
|
| 5889 |
|
| 5890 |
#define SW_RASTER_POINT_PIXEL SW_CONCATX(sw_raster_point_pixel_, RLSW_TEMPLATE_RASTER_POINT) |
| 5891 |
#define SW_RASTER_POINT SW_CONCATX(sw_raster_point_, RLSW_TEMPLATE_RASTER_POINT) |
| 5892 |
|
| 5893 |
static void SW_RASTER_POINT_PIXEL(int x, int y, float z, const float color[4]) |
| 5894 |
{ |
| 5895 |
int offset = y*RLSW.colorBuffer->width + x; |
| 5896 |
|
| 5897 |
#ifdef SW_ENABLE_DEPTH_TEST |
| 5898 |
{ |
| 5899 |
uint8_t *dPtr = (uint8_t *)(RLSW.depthBuffer->pixels) + offset*SW_FRAMEBUFFER_DEPTH_SIZE; |
| 5900 |
|
| 5901 |
|
| 5902 |
float depth = SW_FRAMEBUFFER_DEPTH_GET(dPtr, 0); |
| 5903 |
if (z > depth) return; |
| 5904 |
|
| 5905 |
|
| 5906 |
SW_FRAMEBUFFER_DEPTH_SET(dPtr, z, 0); |
| 5907 |
} |
| 5908 |
#endif |
| 5909 |
|
| 5910 |
uint8_t *cPtr = (uint8_t *)(RLSW.colorBuffer->pixels) + offset*SW_FRAMEBUFFER_COLOR_SIZE; |
| 5911 |
|
| 5912 |
#ifdef SW_ENABLE_BLEND |
| 5913 |
{ |
| 5914 |
float dstColor[4]; |
| 5915 |
SW_FRAMEBUFFER_COLOR_GET(dstColor, cPtr, 0); |
| 5916 |
RLSW.blendFunc(dstColor, color); |
| 5917 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, dstColor, 0); |
| 5918 |
} |
| 5919 |
#else |
| 5920 |
{ |
| 5921 |
SW_FRAMEBUFFER_COLOR_SET(cPtr, color, 0); |
| 5922 |
} |
| 5923 |
#endif |
| 5924 |
} |
| 5925 |
|
| 5926 |
static void SW_RASTER_POINT(const sw_vertex_t *v) |
| 5927 |
{ |
| 5928 |
int cx = v->position[0]; |
| 5929 |
int cy = v->position[1]; |
| 5930 |
float cz = v->position[2]; |
| 5931 |
int radius = RLSW.pointRadius; |
| 5932 |
const float *color = v->color; |
| 5933 |
|
| 5934 |
for (int dy = -radius; dy <= radius; dy++) |
| 5935 |
{ |
| 5936 |
for (int dx = -radius; dx <= radius; dx++) |
| 5937 |
{ |
| 5938 |
SW_RASTER_POINT_PIXEL(cx + dx, cy + dy, cz, color); |
| 5939 |
} |
| 5940 |
} |
| 5941 |
} |
| 5942 |
|
| 5943 |
#endif |
| 5944 |
|