/* * Copyright (C) 2009 Francisco Jerez. * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice (including the * next paragraph) shall be included in all copies or substantial * portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #include "nouveau_driver.h" #include "nouveau_context.h" #include "nouveau_texture.h" #include "nouveau_fbo.h" #include "nouveau_util.h" #include "main/pbo.h" #include "main/texobj.h" #include "main/texstore.h" #include "main/texformat.h" #include "main/texcompress.h" #include "main/texgetimage.h" #include "main/mipmap.h" #include "main/teximage.h" #include "drivers/common/meta.h" #include "swrast/s_texfetch.h" static struct gl_texture_object * nouveau_texture_new(struct gl_context *ctx, GLuint name, GLenum target) { struct nouveau_texture *nt = CALLOC_STRUCT(nouveau_texture); _mesa_initialize_texture_object(ctx, &nt->base, name, target); return &nt->base; } static void nouveau_texture_free(struct gl_context *ctx, struct gl_texture_object *t) { struct nouveau_texture *nt = to_nouveau_texture(t); int i; for (i = 0; i < MAX_TEXTURE_LEVELS; i++) nouveau_surface_ref(NULL, &nt->surfaces[i]); _mesa_delete_texture_object(ctx, t); } static struct gl_texture_image * nouveau_teximage_new(struct gl_context *ctx) { struct nouveau_teximage *nti = CALLOC_STRUCT(nouveau_teximage); return &nti->base.Base; } static void nouveau_teximage_free(struct gl_context *ctx, struct gl_texture_image *ti) { struct nouveau_teximage *nti = to_nouveau_teximage(ti); nouveau_surface_ref(NULL, &nti->surface); } static void nouveau_map_texture_image(struct gl_context *ctx, struct gl_texture_image *ti, GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h, GLbitfield mode, GLubyte **map, GLint *stride) { struct nouveau_teximage *nti = to_nouveau_teximage(ti); struct nouveau_surface *s = &nti->surface; struct nouveau_surface *st = &nti->transfer.surface; struct nouveau_client *client = context_client(ctx); /* Nouveau has no support for 3D or cubemap textures. */ assert(slice == 0); if (s->bo) { if (!(mode & GL_MAP_READ_BIT) && nouveau_pushbuf_refd(context_push(ctx), s->bo)) { unsigned size; /* * Heuristic: use a bounce buffer to pipeline * teximage transfers. */ st->layout = LINEAR; st->format = s->format; st->cpp = s->cpp; st->width = w; st->height = h; st->pitch = s->pitch; nti->transfer.x = x; nti->transfer.y = y; size = get_format_blocksy(st->format, h) * st->pitch; *map = nouveau_get_scratch(ctx, size, &st->bo, &st->offset); *stride = st->pitch; } else { int ret, flags = 0; if (mode & GL_MAP_READ_BIT) flags |= NOUVEAU_BO_RD; if (mode & GL_MAP_WRITE_BIT) flags |= NOUVEAU_BO_WR; if (!s->bo->map) { ret = nouveau_bo_map(s->bo, flags, client); assert(!ret); } *map = s->bo->map + get_format_blocksy(s->format, y) * s->pitch + get_format_blocksx(s->format, x) * s->cpp; *stride = s->pitch; } } else { *map = nti->base.Buffer + get_format_blocksy(s->format, y) * s->pitch + get_format_blocksx(s->format, x) * s->cpp; *stride = s->pitch; } } static void nouveau_unmap_texture_image(struct gl_context *ctx, struct gl_texture_image *ti, GLuint slice) { struct nouveau_teximage *nti = to_nouveau_teximage(ti); struct nouveau_surface *s = &nti->surface; struct nouveau_surface *st = &nti->transfer.surface; if (st->bo) { context_drv(ctx)->surface_copy(ctx, s, st, nti->transfer.x, nti->transfer.y, 0, 0, st->width, st->height); nouveau_surface_ref(NULL, st); } } static mesa_format nouveau_choose_tex_format(struct gl_context *ctx, GLenum target, GLint internalFormat, GLenum srcFormat, GLenum srcType) { switch (internalFormat) { case 4: case GL_RGBA: case GL_RGBA2: case GL_RGBA4: case GL_RGBA8: case GL_RGBA12: case GL_RGBA16: case GL_RGB10_A2: case GL_COMPRESSED_RGBA: return MESA_FORMAT_B8G8R8A8_UNORM; case GL_RGB5_A1: return MESA_FORMAT_B5G5R5A1_UNORM; case GL_RGB: case GL_RGB8: case GL_RGB10: case GL_RGB12: case GL_RGB16: case GL_COMPRESSED_RGB: return MESA_FORMAT_B8G8R8X8_UNORM; case 3: case GL_R3_G3_B2: case GL_RGB4: case GL_RGB5: return MESA_FORMAT_B5G6R5_UNORM; case 2: case GL_LUMINANCE_ALPHA: case GL_LUMINANCE4_ALPHA4: case GL_LUMINANCE6_ALPHA2: case GL_LUMINANCE12_ALPHA4: case GL_LUMINANCE12_ALPHA12: case GL_LUMINANCE16_ALPHA16: case GL_LUMINANCE8_ALPHA8: case GL_COMPRESSED_LUMINANCE_ALPHA: return MESA_FORMAT_B8G8R8A8_UNORM; case 1: case GL_LUMINANCE: case GL_LUMINANCE4: case GL_LUMINANCE12: case GL_LUMINANCE16: case GL_LUMINANCE8: case GL_COMPRESSED_LUMINANCE: return MESA_FORMAT_L_UNORM8; case GL_ALPHA: case GL_ALPHA4: case GL_ALPHA12: case GL_ALPHA16: case GL_ALPHA8: case GL_COMPRESSED_ALPHA: return MESA_FORMAT_A_UNORM8; case GL_INTENSITY: case GL_INTENSITY4: case GL_INTENSITY12: case GL_INTENSITY16: case GL_INTENSITY8: case GL_COMPRESSED_INTENSITY: return MESA_FORMAT_I_UNORM8; case GL_RGB_S3TC: case GL_RGB4_S3TC: case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return MESA_FORMAT_RGB_DXT1; case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return MESA_FORMAT_RGBA_DXT1; case GL_RGBA_S3TC: case GL_RGBA4_S3TC: case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: return MESA_FORMAT_RGBA_DXT3; case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: return MESA_FORMAT_RGBA_DXT5; default: assert(0); } } static GLboolean teximage_fits(struct gl_texture_object *t, int level) { struct nouveau_surface *s = &to_nouveau_texture(t)->surfaces[level]; struct gl_texture_image *ti = t->Image[0][level]; if (!ti || !to_nouveau_teximage(ti)->surface.bo) return GL_FALSE; if (level == t->BaseLevel && (s->offset & 0x7f)) return GL_FALSE; return t->Target == GL_TEXTURE_RECTANGLE || (s->bo && s->format == ti->TexFormat && s->width == ti->Width && s->height == ti->Height); } static GLboolean validate_teximage(struct gl_context *ctx, struct gl_texture_object *t, int level, int x, int y, int z, int width, int height, int depth) { struct gl_texture_image *ti = t->Image[0][level]; if (teximage_fits(t, level)) { struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces; struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface; if (t->Target == GL_TEXTURE_RECTANGLE) nouveau_surface_ref(s, &ss[level]); else context_drv(ctx)->surface_copy(ctx, &ss[level], s, x, y, x, y, width, height); return GL_TRUE; } return GL_FALSE; } static int get_last_level(struct gl_texture_object *t) { struct gl_texture_image *base = t->Image[0][t->BaseLevel]; if (t->Sampler.MinFilter == GL_NEAREST || t->Sampler.MinFilter == GL_LINEAR || !base) return t->BaseLevel; else return MIN2(t->BaseLevel + base->MaxNumLevels - 1, t->MaxLevel); } static void relayout_texture(struct gl_context *ctx, struct gl_texture_object *t) { struct gl_texture_image *base = t->Image[0][t->BaseLevel]; if (base && t->Target != GL_TEXTURE_RECTANGLE) { struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces; struct nouveau_surface *s = &to_nouveau_teximage(base)->surface; int i, ret, last = get_last_level(t); enum nouveau_surface_layout layout = (_mesa_is_format_compressed(s->format) ? LINEAR : SWIZZLED); unsigned size, pitch, offset = 0, width = s->width, height = s->height; /* Deallocate the old storage. */ for (i = 0; i < MAX_TEXTURE_LEVELS; i++) nouveau_bo_ref(NULL, &ss[i].bo); /* Relayout the mipmap tree. */ for (i = t->BaseLevel; i <= last; i++) { pitch = _mesa_format_row_stride(s->format, width); size = get_format_blocksy(s->format, height) * pitch; /* Images larger than 16B have to be aligned. */ if (size > 16) offset = align(offset, 64); ss[i] = (struct nouveau_surface) { .offset = offset, .layout = layout, .format = s->format, .width = width, .height = height, .cpp = s->cpp, .pitch = pitch, }; offset += size; width = minify(width, 1); height = minify(height, 1); } if (t->BaseLevel <= last) { /* Get new storage. */ size = align(offset, 64); assert(size); ret = nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_MAP | NOUVEAU_BO_GART | NOUVEAU_BO_VRAM, 0, size, NULL, &ss[last].bo); assert(!ret); for (i = t->BaseLevel; i < last; i++) nouveau_bo_ref(ss[last].bo, &ss[i].bo); } } } GLboolean nouveau_texture_validate(struct gl_context *ctx, struct gl_texture_object *t) { struct nouveau_texture *nt = to_nouveau_texture(t); int i, last = get_last_level(t); if (!teximage_fits(t, t->BaseLevel) || !teximage_fits(t, last)) return GL_FALSE; if (nt->dirty) { nt->dirty = GL_FALSE; /* Copy the teximages to the actual miptree. */ for (i = t->BaseLevel; i <= last; i++) { struct nouveau_surface *s = &nt->surfaces[i]; validate_teximage(ctx, t, i, 0, 0, 0, s->width, s->height, 1); } PUSH_KICK(context_push(ctx)); } return GL_TRUE; } void nouveau_texture_reallocate(struct gl_context *ctx, struct gl_texture_object *t) { if (!teximage_fits(t, t->BaseLevel) || !teximage_fits(t, get_last_level(t))) { texture_dirty(t); relayout_texture(ctx, t); nouveau_texture_validate(ctx, t); } } static unsigned get_teximage_placement(struct gl_texture_image *ti) { if (ti->TexFormat == MESA_FORMAT_A_UNORM8 || ti->TexFormat == MESA_FORMAT_L_UNORM8 || ti->TexFormat == MESA_FORMAT_I_UNORM8) /* 1 cpp formats will have to be swizzled by the CPU, * so leave them in system RAM for now. */ return NOUVEAU_BO_MAP; else return NOUVEAU_BO_GART | NOUVEAU_BO_MAP; } static void nouveau_compressed_copy(struct gl_context *ctx, GLint dims, struct gl_texture_image *ti, GLsizei width, GLsizei height, GLsizei depth, const GLvoid *src, GLvoid *dst, int row_stride) { struct compressed_pixelstore store; int i; _mesa_compute_compressed_pixelstore(dims, ti->TexFormat, width, height, depth, &ctx->Unpack, &store); src += store.SkipBytes; assert(store.CopySlices == 1); /* copy rows of blocks */ for (i = 0; i < store.CopyRowsPerSlice; i++) { memcpy(dst, src, store.CopyBytesPerRow); dst += row_stride; src += store.TotalBytesPerRow; } } static void nouveau_teximage(struct gl_context *ctx, GLint dims, struct gl_texture_image *ti, GLsizei imageSize, GLenum format, GLenum type, const GLvoid *pixels, const struct gl_pixelstore_attrib *packing, GLboolean compressed) { struct gl_texture_object *t = ti->TexObject; const GLuint level = ti->Level; struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface; struct nouveau_teximage *nti = to_nouveau_teximage(ti); int ret; GLuint depth = compressed ? 1 : ti->Depth; /* Allocate a new bo for the image. */ nouveau_surface_alloc(ctx, s, LINEAR, get_teximage_placement(ti), ti->TexFormat, ti->Width, ti->Height); nti->base.RowStride = s->pitch / s->cpp; if (compressed) pixels = _mesa_validate_pbo_compressed_teximage(ctx, dims, imageSize, pixels, packing, "glCompressedTexImage"); else pixels = _mesa_validate_pbo_teximage(ctx, dims, ti->Width, ti->Height, depth, format, type, pixels, packing, "glTexImage"); if (pixels) { GLubyte *map; int row_stride; /* Store the pixel data. */ nouveau_map_texture_image(ctx, ti, 0, 0, 0, ti->Width, ti->Height, GL_MAP_WRITE_BIT, &map, &row_stride); if (compressed) { nouveau_compressed_copy(ctx, dims, ti, ti->Width, ti->Height, depth, pixels, map, row_stride); } else { ret = _mesa_texstore(ctx, dims, ti->_BaseFormat, ti->TexFormat, row_stride, &map, ti->Width, ti->Height, depth, format, type, pixels, packing); assert(ret); } nouveau_unmap_texture_image(ctx, ti, 0); _mesa_unmap_teximage_pbo(ctx, packing); if (!validate_teximage(ctx, t, level, 0, 0, 0, ti->Width, ti->Height, depth)) /* It doesn't fit, mark it as dirty. */ texture_dirty(t); } if (level == t->BaseLevel) { if (!teximage_fits(t, level)) relayout_texture(ctx, t); nouveau_texture_validate(ctx, t); } context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit); context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit); } static void nouveau_teximage_123d(struct gl_context *ctx, GLuint dims, struct gl_texture_image *ti, GLenum format, GLenum type, const GLvoid *pixels, const struct gl_pixelstore_attrib *packing) { nouveau_teximage(ctx, dims, ti, 0, format, type, pixels, packing, GL_FALSE); } static void nouveau_compressed_teximage(struct gl_context *ctx, GLuint dims, struct gl_texture_image *ti, GLsizei imageSize, const GLvoid *data) { nouveau_teximage(ctx, 2, ti, imageSize, 0, 0, data, &ctx->Unpack, GL_TRUE); } static GLboolean nouveau_teximage_alloc(struct gl_context *ctx, struct gl_texture_image *ti) { nouveau_teximage(ctx, 3, ti, 0, 0, 0, NULL, &ctx->DefaultPacking, _mesa_is_format_compressed(ti->TexFormat)); return GL_TRUE; } static void nouveau_texsubimage(struct gl_context *ctx, GLint dims, struct gl_texture_image *ti, GLint xoffset, GLint yoffset, GLint zoffset, GLint width, GLint height, GLint depth, GLsizei imageSize, GLenum format, GLenum type, const void *pixels, const struct gl_pixelstore_attrib *packing, GLboolean compressed) { int ret; if (compressed) pixels = _mesa_validate_pbo_compressed_teximage(ctx, dims, imageSize, pixels, packing, "glCompressedTexSubImage"); else pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format, type, pixels, packing, "glTexSubImage"); if (pixels) { GLubyte *map; int row_stride; nouveau_map_texture_image(ctx, ti, 0, xoffset, yoffset, width, height, GL_MAP_WRITE_BIT, &map, &row_stride); if (compressed) { nouveau_compressed_copy(ctx, dims, ti, width, height, depth, pixels, map, row_stride); } else { ret = _mesa_texstore(ctx, dims, ti->_BaseFormat, ti->TexFormat, row_stride, &map, width, height, depth, format, type, pixels, packing); assert(ret); } nouveau_unmap_texture_image(ctx, ti, 0); _mesa_unmap_teximage_pbo(ctx, packing); } if (!to_nouveau_texture(ti->TexObject)->dirty) validate_teximage(ctx, ti->TexObject, ti->Level, xoffset, yoffset, zoffset, width, height, depth); } static void nouveau_texsubimage_123d(struct gl_context *ctx, GLuint dims, struct gl_texture_image *ti, GLint xoffset, GLint yoffset, GLint zoffset, GLint width, GLint height, GLint depth, GLenum format, GLenum type, const void *pixels, const struct gl_pixelstore_attrib *packing) { nouveau_texsubimage(ctx, dims, ti, xoffset, yoffset, zoffset, width, height, depth, 0, format, type, pixels, packing, GL_FALSE); } static void nouveau_compressed_texsubimage(struct gl_context *ctx, GLuint dims, struct gl_texture_image *ti, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLint height, GLint depth, GLenum format, GLint imageSize, const void *data) { nouveau_texsubimage(ctx, dims, ti, xoffset, yoffset, zoffset, width, height, depth, imageSize, format, 0, data, &ctx->Unpack, GL_TRUE); } static void nouveau_bind_texture(struct gl_context *ctx, GLuint texUnit, GLenum target, struct gl_texture_object *t) { context_dirty_i(ctx, TEX_OBJ, texUnit); context_dirty_i(ctx, TEX_ENV, texUnit); } static mesa_format get_texbuffer_format(struct gl_renderbuffer *rb, GLint format) { struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface; if (s->cpp < 4) return s->format; else if (format == __DRI_TEXTURE_FORMAT_RGBA) return MESA_FORMAT_B8G8R8A8_UNORM; else return MESA_FORMAT_B8G8R8X8_UNORM; } void nouveau_set_texbuffer(__DRIcontext *dri_ctx, GLint target, GLint format, __DRIdrawable *draw) { struct nouveau_context *nctx = dri_ctx->driverPrivate; struct gl_context *ctx = &nctx->base; struct gl_framebuffer *fb = draw->driverPrivate; struct gl_renderbuffer *rb = fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer; struct gl_texture_object *t = _mesa_get_current_tex_object(ctx, target); struct gl_texture_image *ti; struct nouveau_teximage *nti; struct nouveau_surface *s; _mesa_lock_texture(ctx, t); ti = _mesa_get_tex_image(ctx, t, target, 0); nti = to_nouveau_teximage(ti); s = &to_nouveau_teximage(ti)->surface; /* Update the texture surface with the given drawable. */ nouveau_update_renderbuffers(dri_ctx, draw); nouveau_surface_ref(&to_nouveau_renderbuffer(rb)->surface, s); s->format = get_texbuffer_format(rb, format); /* Update the image fields. */ _mesa_init_teximage_fields(ctx, ti, s->width, s->height, 1, 0, s->cpp, s->format); nti->base.RowStride = s->pitch / s->cpp; /* Try to validate it. */ if (!validate_teximage(ctx, t, 0, 0, 0, 0, s->width, s->height, 1)) nouveau_texture_reallocate(ctx, t); context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit); context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit); _mesa_unlock_texture(ctx, t); } void nouveau_texture_functions_init(struct dd_function_table *functions) { functions->NewTextureObject = nouveau_texture_new; functions->DeleteTexture = nouveau_texture_free; functions->NewTextureImage = nouveau_teximage_new; functions->FreeTextureImageBuffer = nouveau_teximage_free; functions->AllocTextureImageBuffer = nouveau_teximage_alloc; functions->ChooseTextureFormat = nouveau_choose_tex_format; functions->TexImage = nouveau_teximage_123d; functions->TexSubImage = nouveau_texsubimage_123d; functions->CompressedTexImage = nouveau_compressed_teximage; functions->CompressedTexSubImage = nouveau_compressed_texsubimage; functions->BindTexture = nouveau_bind_texture; functions->MapTextureImage = nouveau_map_texture_image; functions->UnmapTextureImage = nouveau_unmap_texture_image; } ='#n462'>462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
/****************************************************************************
*
*                        Mesa 3-D graphics library
*                        Direct3D Driver Interface
*
*  ========================================================================
*
*   Copyright (C) 1991-2004 SciTech Software, Inc. All rights reserved.
*
*   Permission is hereby granted, free of charge, to any person obtaining a
*   copy of this software and associated documentation files (the "Software"),
*   to deal in the Software without restriction, including without limitation
*   the rights to use, copy, modify, merge, publish, distribute, sublicense,
*   and/or sell copies of the Software, and to permit persons to whom the
*   Software is furnished to do so, subject to the following conditions:
*
*   The above copyright notice and this permission notice shall be included
*   in all copies or substantial portions of the Software.
*
*   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
*   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
*   SCITECH SOFTWARE INC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
*   OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*   SOFTWARE.
*
*  ======================================================================
*
* Language:     ANSI C
* Environment:  Windows 9x/2000/XP/XBox (Win32)
*
* Description:  Primitive (points/lines/tris/quads) rendering
*
****************************************************************************/

//#include "../GLDirect.h"

//#include "gld_dx8.h"

#include "dglcontext.h"
#include "ddlog.h"
#include "gld_dx7.h"

#include "glheader.h"
#include "context.h"
#include "colormac.h"
#include "depth.h"
#include "extensions.h"
#include "macros.h"
#include "matrix.h"
// #include "mem.h"
//#include "mmath.h"
#include "mtypes.h"
#include "texformat.h"
#include "texstore.h"
#include "vbo/vbo.h"
#include "swrast/swrast.h"
#include "swrast_setup/swrast_setup.h"
#include "swrast_setup/ss_context.h"
#include "swrast/s_context.h"
#include "swrast/s_depth.h"
#include "swrast/s_lines.h"
#include "swrast/s_triangle.h"
#include "swrast/s_trispan.h"
#include "tnl/tnl.h"
#include "tnl/t_context.h"
#include "tnl/t_pipeline.h"

// Disable compiler complaints about unreferenced local variables
#pragma warning (disable:4101)

//---------------------------------------------------------------------------
// Helper defines for primitives
//---------------------------------------------------------------------------

//static const float ooZ		= 1.0f / 65536.0f; // One over Z

#define GLD_COLOUR (D3DCOLOR_RGBA(swv->color[0], swv->color[1], swv->color[2], swv->color[3]))
#define GLD_SPECULAR (D3DCOLOR_RGBA(swv->specular[0], swv->specular[1], swv->specular[2], swv->specular[3]))
#define GLD_FLIP_Y(y) (gldCtx->dwHeight - (y))

//---------------------------------------------------------------------------
// 2D vertex setup
//---------------------------------------------------------------------------

#define GLD_SETUP_2D_VARS_POINTS							\
	GLD_context		*gldCtx	= GLD_GET_CONTEXT(ctx);			\
	GLD_driver_dx7	*gld	= GLD_GET_DX7_DRIVER(gldCtx);	\
	GLD_2D_VERTEX	*pV		= (GLD_2D_VERTEX*)gld->PB2d.pPoints;	\
	SScontext		*ss		= SWSETUP_CONTEXT(ctx);			\
	SWvertex		*swv;									\
	DWORD			dwSpecularColour;						\
	DWORD			dwFlatColour

#define GLD_SETUP_2D_VARS_LINES								\
	GLD_context		*gldCtx	= GLD_GET_CONTEXT(ctx);			\
	GLD_driver_dx7	*gld	= GLD_GET_DX7_DRIVER(gldCtx);	\
	GLD_2D_VERTEX	*pV		= (GLD_2D_VERTEX*)gld->PB2d.pLines;	\
	SScontext		*ss		= SWSETUP_CONTEXT(ctx);			\
	SWvertex		*swv;									\
	DWORD			dwSpecularColour;						\
	DWORD			dwFlatColour

#define GLD_SETUP_2D_VARS_TRIANGLES							\
	BOOL			bFog = ctx->Fog.Enabled;				\
	GLD_context		*gldCtx	= GLD_GET_CONTEXT(ctx);			\
	GLD_driver_dx7	*gld	= GLD_GET_DX7_DRIVER(gldCtx);	\
	GLD_2D_VERTEX	*pV		= (GLD_2D_VERTEX*)gld->PB2d.pTriangles;	\
	SScontext		*ss		= SWSETUP_CONTEXT(ctx);			\
	SWvertex		*swv;									\
	DWORD			dwSpecularColour;						\
	DWORD			dwFlatColour;							\
	GLuint					facing = 0;						\
	struct vertex_buffer	*VB;							\
	GLchan					(*vbcolor)[4];					\
	GLchan					(*vbspec)[4]

#define GLD_SETUP_GET_SWVERT(s)					\
	swv = &ss->verts[##s]

#define GLD_SETUP_2D_VERTEX						\
	pV->x			= swv->win[0];				\
	pV->y			= GLD_FLIP_Y(swv->win[1]);	\
	pV->rhw			= swv->win[3]

#define GLD_SETUP_SMOOTH_COLOUR					\
	pV->diffuse		= GLD_COLOUR

#define GLD_SETUP_GET_FLAT_COLOUR				\
	dwFlatColour	= GLD_COLOUR
#define GLD_SETUP_GET_FLAT_FOG_COLOUR			\
	dwFlatColour	= _gldComputeFog(ctx, swv)

#define GLD_SETUP_USE_FLAT_COLOUR				\
	pV->diffuse		= dwFlatColour

#define GLD_SETUP_GET_FLAT_SPECULAR				\
	dwSpecularColour= GLD_SPECULAR

#define GLD_SETUP_USE_FLAT_SPECULAR				\
	pV->specular	= dwSpecularColour

#define GLD_SETUP_DEPTH							\
	pV->sz			= swv->win[2] / ctx->DepthMaxF
//	pV->z			= swv->win[2] * ooZ;

#define GLD_SETUP_SPECULAR						\
	pV->specular	= GLD_SPECULAR

#define GLD_SETUP_FOG							\
	pV->diffuse		= _gldComputeFog(ctx, swv)

#define GLD_SETUP_TEX0							\
	pV->t0_u		= swv->texcoord[0][0];		\
	pV->t0_v		= swv->texcoord[0][1]

#define GLD_SETUP_TEX1							\
	pV->t1_u		= swv->texcoord[1][0];		\
	pV->t1_v		= swv->texcoord[1][1]

#define GLD_SETUP_LIGHTING(v)			\
	if (facing == 1) {					\
		pV->diffuse	= D3DCOLOR_RGBA(vbcolor[##v][0], vbcolor[##v][1], vbcolor[##v][2], vbcolor[##v][3]);	\
		if (vbspec) {																					\
			pV->specular = D3DCOLOR_RGBA(vbspec[##v][0], vbspec[##v][1], vbspec[##v][2], vbspec[##v][3]);	\
		}	\
	} else {	\
		if (bFog)						\
			GLD_SETUP_FOG;				\
		else							\
			GLD_SETUP_SMOOTH_COLOUR;	\
		GLD_SETUP_SPECULAR;				\
	}

#define GLD_SETUP_GET_FLAT_LIGHTING(v)	\
	if (facing == 1) {					\
		dwFlatColour = D3DCOLOR_RGBA(vbcolor[##v][0], vbcolor[##v][1], vbcolor[##v][2], vbcolor[##v][3]);	\
		if (vbspec) {																					\
			dwSpecularColour = D3DCOLOR_RGBA(vbspec[##v][0], vbspec[##v][1], vbspec[##v][2], vbspec[##v][3]);	\
		}	\
	}

#define GLD_SETUP_TWOSIDED_LIGHTING		\
	/* Two-sided lighting */				\
	if (ctx->_TriangleCaps & DD_TRI_LIGHT_TWOSIDE) {	\
		SWvertex	*verts = SWSETUP_CONTEXT(ctx)->verts;	\
		SWvertex	*v[3];									\
		GLfloat		ex,ey,fx,fy,cc;							\
		/* Get vars for later */							\
		VB		= &TNL_CONTEXT(ctx)->vb;					\
		vbcolor	= (GLchan (*)[4])VB->ColorPtr[1]->data;		\
		if (VB->SecondaryColorPtr[1]) {						\
			vbspec = (GLchan (*)[4])VB->SecondaryColorPtr[1]->data;	\
		} else {													\
			vbspec = NULL;											\
		}															\
		v[0] = &verts[v0];											\
		v[1] = &verts[v1];											\
		v[2] = &verts[v2];											\
		ex = v[0]->win[0] - v[2]->win[0];	\
		ey = v[0]->win[1] - v[2]->win[1];	\
		fx = v[1]->win[0] - v[2]->win[0];	\
		fy = v[1]->win[1] - v[2]->win[1];	\
		cc  = ex*fy - ey*fx;				\
		facing = (cc < 0.0) ^ ctx->Polygon._FrontBit;	\
	}

//---------------------------------------------------------------------------
// 3D vertex setup
//---------------------------------------------------------------------------

#define GLD_SETUP_3D_VARS_POINTS											\
	GLD_context		*gldCtx	= GLD_GET_CONTEXT(ctx);			\
	GLD_driver_dx7	*gld	= GLD_GET_DX7_DRIVER(gldCtx);	\
	GLD_3D_VERTEX			*pV		= (GLD_3D_VERTEX*)gld->PB3d.pPoints;	\
	TNLcontext				*tnl	= TNL_CONTEXT(ctx);				\
	struct vertex_buffer	*VB		= &tnl->vb;						\
	GLfloat					(*p4f)[4];								\
	GLfloat					(*tc)[4];								\
	DWORD					dwColor;

#define GLD_SETUP_3D_VARS_LINES											\
	GLD_context		*gldCtx	= GLD_GET_CONTEXT(ctx);			\
	GLD_driver_dx7	*gld	= GLD_GET_DX7_DRIVER(gldCtx);	\
	GLD_3D_VERTEX			*pV		= (GLD_3D_VERTEX*)gld->PB3d.pLines;	\
	TNLcontext				*tnl	= TNL_CONTEXT(ctx);				\
	struct vertex_buffer	*VB		= &tnl->vb;						\
	GLfloat					(*p4f)[4];								\
	GLfloat					(*tc)[4];								\
	DWORD					dwColor;

#define GLD_SETUP_3D_VARS_TRIANGLES											\
	GLD_context		*gldCtx	= GLD_GET_CONTEXT(ctx);			\
	GLD_driver_dx7	*gld	= GLD_GET_DX7_DRIVER(gldCtx);	\
	GLD_3D_VERTEX			*pV		= (GLD_3D_VERTEX*)gld->PB3d.pTriangles;	\
	TNLcontext				*tnl	= TNL_CONTEXT(ctx);				\
	struct vertex_buffer	*VB		= &tnl->vb;						\
	GLfloat					(*p4f)[4];								\
	GLfloat					(*tc)[4];								\
	DWORD					dwColor;

#define GLD_SETUP_3D_VERTEX(v)					\
	p4f				= VB->ObjPtr->data;			\
	pV->Position.x	= p4f[##v][0];				\
	pV->Position.y	= p4f[##v][1];				\
	pV->Position.z	= p4f[##v][2];

#define GLD_SETUP_SMOOTH_COLOUR_3D(v)															\
	p4f			= (GLfloat (*)[4])VB->ColorPtr[0]->data;										\
	pV->Diffuse	= D3DCOLOR_COLORVALUE(p4f[##v][0], p4f[##v][1], p4f[##v][2], p4f[##v][3]);


#define GLD_SETUP_GET_FLAT_COLOUR_3D(v)													\
	p4f		= (GLfloat (*)[4])VB->ColorPtr[0]->data;										\
	dwColor	= D3DCOLOR_COLORVALUE(p4f[##v][0], p4f[##v][1], p4f[##v][2], p4f[##v][3]);

#define GLD_SETUP_USE_FLAT_COLOUR_3D			\
	pV->Diffuse = dwColor;

#define GLD_SETUP_TEX0_3D(v)						\
	if (VB->TexCoordPtr[0]) {						\
		tc				= VB->TexCoordPtr[0]->data;	\
		pV->TexUnit0.x	= tc[##v][0];				\
		pV->TexUnit0.y	= tc[##v][1];				\
	}

#define GLD_SETUP_TEX1_3D(v)						\
	if (VB->TexCoordPtr[1]) {						\
		tc				= VB->TexCoordPtr[1]->data;	\
		pV->TexUnit1.x	= tc[##v][0];				\
		pV->TexUnit1.y	= tc[##v][1];				\
	}

//---------------------------------------------------------------------------
// Helper functions
//---------------------------------------------------------------------------

__inline DWORD _gldComputeFog(
	GLcontext *ctx,
	SWvertex *swv)
{
	// Full fog calculation.
	// Based on Mesa code.

	GLchan			rFog, gFog, bFog;
	GLchan			fR, fG, fB;
	const GLfloat	f = swv->fog;
	const GLfloat	g = 1.0 - f;
	
	UNCLAMPED_FLOAT_TO_CHAN(rFog, ctx->Fog.Color[RCOMP]);
	UNCLAMPED_FLOAT_TO_CHAN(gFog, ctx->Fog.Color[GCOMP]);
	UNCLAMPED_FLOAT_TO_CHAN(bFog, ctx->Fog.Color[BCOMP]);
	fR = f * swv->color[0] + g * rFog;
	fG = f * swv->color[1] + g * gFog;
	fB = f * swv->color[2] + g * bFog;
	return D3DCOLOR_RGBA(fR, fG, fB, swv->color[3]);
}

//---------------------------------------------------------------------------

void gld_ResetLineStipple_DX7(
	GLcontext *ctx)
{
	// TODO: Fake stipple with a 32x32 texture.
}

//---------------------------------------------------------------------------
// 2D (post-transformed) primitives
//---------------------------------------------------------------------------

void gld_Points2D_DX7(
	GLcontext *ctx,
	GLuint first,
	GLuint last)
{
	GLD_SETUP_2D_VARS_POINTS;

	unsigned				i;
	struct vertex_buffer	*VB = &TNL_CONTEXT(ctx)->vb;

	// _Size is already clamped to MaxPointSize and MinPointSize
	// Not supported by DX7
//	IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_POINTSIZE, *((DWORD*)&ctx->Point._Size));

	if (VB->Elts) {
		for (i=first; i<last; i++, pV++) {
			if (VB->ClipMask[VB->Elts[i]] == 0) {
//				_swrast_Point( ctx, &verts[VB->Elts[i]] );
				GLD_SETUP_GET_SWVERT(VB->Elts[i]);
				GLD_SETUP_2D_VERTEX;
				GLD_SETUP_SMOOTH_COLOUR;
				GLD_SETUP_DEPTH;
				GLD_SETUP_SPECULAR;
				GLD_SETUP_TEX0;
				GLD_SETUP_TEX1;
			}
		}
	} else {
		GLD_SETUP_GET_SWVERT(first);
		for (i=first; i<last; i++, swv++, pV++) {
			if (VB->ClipMask[i] == 0) {
//				_swrast_Point( ctx, &verts[i] );
				GLD_SETUP_2D_VERTEX;
				GLD_SETUP_SMOOTH_COLOUR;
				GLD_SETUP_DEPTH;
				GLD_SETUP_SPECULAR;
				GLD_SETUP_TEX0;
				GLD_SETUP_TEX1;
			}
		}
	}

	gld->PB2d.pPoints = (BYTE*)pV;
	gld->PB2d.nPoints += (last-first);
}

//---------------------------------------------------------------------------

void gld_Line2DFlat_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1)
{
	GLD_SETUP_2D_VARS_LINES;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_GET_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_GET_FLAT_SPECULAR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	gld->PB2d.pLines = (BYTE*)pV;
	gld->PB2d.nLines++;
}

//---------------------------------------------------------------------------

void gld_Line2DSmooth_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1)
{
	GLD_SETUP_2D_VARS_LINES;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_SPECULAR;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_SPECULAR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	gld->PB2d.pLines = (BYTE*)pV;
	gld->PB2d.nLines++;
}

//---------------------------------------------------------------------------

void gld_Triangle2DFlat_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2)
{
	GLD_SETUP_2D_VARS_TRIANGLES;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_GET_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;

	gld->PB2d.pTriangles = (BYTE*)pV;
	gld->PB2d.nTriangles++;
}

//---------------------------------------------------------------------------

void gld_Triangle2DSmooth_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2)
{

	GLD_SETUP_2D_VARS_TRIANGLES;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	gld->PB2d.pTriangles = (BYTE*)pV;
	gld->PB2d.nTriangles++;
}

//---------------------------------------------------------------------------

void gld_Triangle2DFlatExtras_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2)
{
	GLD_SETUP_2D_VARS_TRIANGLES;

	GLD_SETUP_TWOSIDED_LIGHTING(v2);

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	if (bFog)
		GLD_SETUP_GET_FLAT_FOG_COLOUR;
	else
		GLD_SETUP_GET_FLAT_COLOUR;
	GLD_SETUP_GET_FLAT_SPECULAR;
	GLD_SETUP_GET_FLAT_LIGHTING(v2);
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	gld->PB2d.pTriangles = (BYTE*)pV;
	gld->PB2d.nTriangles++;
}

//---------------------------------------------------------------------------

void gld_Triangle2DSmoothExtras_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2)
{
	GLD_SETUP_2D_VARS_TRIANGLES;

	GLD_SETUP_TWOSIDED_LIGHTING(v0);

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v0);
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v1);
	pV++;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v2);
	pV++;

	gld->PB2d.pTriangles = (BYTE*)pV;
	gld->PB2d.nTriangles++;
}

//---------------------------------------------------------------------------

void gld_Quad2DFlat_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2,
	GLuint v3)
{
	GLD_SETUP_2D_VARS_TRIANGLES;

	GLD_SETUP_GET_SWVERT(v3);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_GET_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;

	GLD_SETUP_GET_SWVERT(v3);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	pV++;

	gld->PB2d.pTriangles = (BYTE*)pV;
	gld->PB2d.nTriangles += 2;
}

//---------------------------------------------------------------------------

void gld_Quad2DSmooth_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2,
	GLuint v3)
{
	GLD_SETUP_2D_VARS_TRIANGLES;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	GLD_SETUP_GET_SWVERT(v3);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_SMOOTH_COLOUR;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	pV++;

	gld->PB2d.pTriangles = (BYTE*)pV;
	gld->PB2d.nTriangles += 2;
}

//---------------------------------------------------------------------------

void gld_Quad2DFlatExtras_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2,
	GLuint v3)
{
	GLD_SETUP_2D_VARS_TRIANGLES;

	GLD_SETUP_TWOSIDED_LIGHTING(v3);

	GLD_SETUP_GET_SWVERT(v3);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	if (bFog)
		GLD_SETUP_GET_FLAT_FOG_COLOUR;
	else
		GLD_SETUP_GET_FLAT_COLOUR;
	GLD_SETUP_GET_FLAT_SPECULAR;
	GLD_SETUP_GET_FLAT_LIGHTING(v3);
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	GLD_SETUP_GET_SWVERT(v3);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_USE_FLAT_COLOUR;
	GLD_SETUP_USE_FLAT_SPECULAR;
	pV++;

	gld->PB2d.pTriangles = (BYTE*)pV;
	gld->PB2d.nTriangles += 2;
}

//---------------------------------------------------------------------------

void gld_Quad2DSmoothExtras_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2,
	GLuint v3)
{
	GLD_SETUP_2D_VARS_TRIANGLES;

	GLD_SETUP_TWOSIDED_LIGHTING(v0);

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v0);
	pV++;

	GLD_SETUP_GET_SWVERT(v1);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v1);
	pV++;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v2);
	pV++;

	GLD_SETUP_GET_SWVERT(v2);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v2);
	pV++;

	GLD_SETUP_GET_SWVERT(v3);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v3);
	pV++;

	GLD_SETUP_GET_SWVERT(v0);
	GLD_SETUP_2D_VERTEX;
	GLD_SETUP_DEPTH;
	GLD_SETUP_TEX0;
	GLD_SETUP_TEX1;
	GLD_SETUP_LIGHTING(v0);
	pV++;

	gld->PB2d.pTriangles = (BYTE*)pV;
	gld->PB2d.nTriangles += 2;
}

//---------------------------------------------------------------------------
// 3D (pre-transformed) primitives
//---------------------------------------------------------------------------

void gld_Points3D_DX7(
	GLcontext *ctx,
	GLuint first,
	GLuint last)
{
	GLD_SETUP_3D_VARS_POINTS

	unsigned				i;
//	struct vertex_buffer	*VB = &TNL_CONTEXT(ctx)->vb;

	// _Size is already clamped to MaxPointSize and MinPointSize
	// Not supported by DX7
//	IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_POINTSIZE, *((DWORD*)&ctx->Point._Size));

	if (VB->Elts) {
		for (i=first; i<last; i++, pV++) {
			if (VB->ClipMask[VB->Elts[i]] == 0) {
//				_swrast_Point( ctx, &verts[VB->Elts[i]] );
//				GLD_SETUP_GET_SWVERT(VB->Elts[i]);
				GLD_SETUP_3D_VERTEX(VB->Elts[i])
				GLD_SETUP_SMOOTH_COLOUR_3D(i)
				GLD_SETUP_TEX0_3D(i)
				GLD_SETUP_TEX1_3D(i)
			}
		}
	} else {
//		GLD_SETUP_GET_SWVERT(first);
		for (i=first; i<last; i++, pV++) {
			if (VB->ClipMask[i] == 0) {
//				_swrast_Point( ctx, &verts[i] );
				GLD_SETUP_3D_VERTEX(i)
				GLD_SETUP_SMOOTH_COLOUR_3D(i)
				GLD_SETUP_TEX0_3D(i)
				GLD_SETUP_TEX1_3D(i)
			}
		}
	}
/*
	for (i=first; i<last; i++, pV++) {
		GLD_SETUP_3D_VERTEX(i)
		GLD_SETUP_SMOOTH_COLOUR_3D(i)
		GLD_SETUP_TEX0_3D(i)
		GLD_SETUP_TEX1_3D(i)
	}
*/
	gld->PB3d.pPoints = (BYTE*)pV;
	gld->PB3d.nPoints += (last-first);
}

//---------------------------------------------------------------------------
// Line functions
//---------------------------------------------------------------------------

void gld_Line3DFlat_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1)
{
	GLD_SETUP_3D_VARS_LINES

	GLD_SETUP_3D_VERTEX(v1)
	GLD_SETUP_GET_FLAT_COLOUR_3D(v1)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	GLD_SETUP_TEX0_3D(v1)
	GLD_SETUP_TEX1_3D(v1)
	pV++;

	GLD_SETUP_3D_VERTEX(v0)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	GLD_SETUP_TEX0_3D(v0)
	GLD_SETUP_TEX1_3D(v0)
	pV++;

	gld->PB3d.pLines = (BYTE*)pV;
	gld->PB3d.nLines++;
}

//---------------------------------------------------------------------------

void gld_Line3DSmooth_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1)
{
	GLD_SETUP_3D_VARS_LINES

	GLD_SETUP_3D_VERTEX(v1)
	GLD_SETUP_SMOOTH_COLOUR_3D(v1)
	GLD_SETUP_TEX0_3D(v1)
	GLD_SETUP_TEX1_3D(v1)
	pV++;

	GLD_SETUP_3D_VERTEX(v0)
	GLD_SETUP_SMOOTH_COLOUR_3D(v0)
	GLD_SETUP_TEX0_3D(v0)
	GLD_SETUP_TEX1_3D(v0)
	pV++;

	gld->PB3d.pLines = (BYTE*)pV;
	gld->PB3d.nLines++;
}

//---------------------------------------------------------------------------
// Triangle functions
//---------------------------------------------------------------------------

void gld_Triangle3DFlat_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2)
{
	GLD_SETUP_3D_VARS_TRIANGLES

	GLD_SETUP_3D_VERTEX(v2)
	GLD_SETUP_TEX0_3D(v2)
	GLD_SETUP_TEX1_3D(v2)
	GLD_SETUP_GET_FLAT_COLOUR_3D(v2)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	pV++;

	GLD_SETUP_3D_VERTEX(v0)
	GLD_SETUP_TEX0_3D(v0)
	GLD_SETUP_TEX1_3D(v0)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	pV++;

	GLD_SETUP_3D_VERTEX(v1)
	GLD_SETUP_TEX0_3D(v1)
	GLD_SETUP_TEX1_3D(v1)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	pV++;

	gld->PB3d.pTriangles = (BYTE*)pV;
	gld->PB3d.nTriangles++;
}

//---------------------------------------------------------------------------

void gld_Triangle3DSmooth_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2)
{
	GLD_SETUP_3D_VARS_TRIANGLES

	GLD_SETUP_3D_VERTEX(v0)
	GLD_SETUP_SMOOTH_COLOUR_3D(v0)
	GLD_SETUP_TEX0_3D(v0)
	GLD_SETUP_TEX1_3D(v0)
	pV++;

	GLD_SETUP_3D_VERTEX(v1)
	GLD_SETUP_SMOOTH_COLOUR_3D(v1)
	GLD_SETUP_TEX0_3D(v1)
	GLD_SETUP_TEX1_3D(v1)
	pV++;

	GLD_SETUP_3D_VERTEX(v2)
	GLD_SETUP_SMOOTH_COLOUR_3D(v2)
	GLD_SETUP_TEX0_3D(v2)
	GLD_SETUP_TEX1_3D(v2)
	pV++;

	gld->PB3d.pTriangles = (BYTE*)pV;
	gld->PB3d.nTriangles++;
}

//---------------------------------------------------------------------------
// Quad functions
//---------------------------------------------------------------------------

void gld_Quad3DFlat_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2,
	GLuint v3)
{
	GLD_SETUP_3D_VARS_TRIANGLES

	GLD_SETUP_3D_VERTEX(v3)
	GLD_SETUP_GET_FLAT_COLOUR_3D(v3)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	GLD_SETUP_TEX0_3D(v3)
	GLD_SETUP_TEX1_3D(v3)
	pV++;

	GLD_SETUP_3D_VERTEX(v0)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	GLD_SETUP_TEX0_3D(v0)
	GLD_SETUP_TEX1_3D(v0)
	pV++;

	GLD_SETUP_3D_VERTEX(v1)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	GLD_SETUP_TEX0_3D(v1)
	GLD_SETUP_TEX1_3D(v1)
	pV++;

	GLD_SETUP_3D_VERTEX(v1)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	GLD_SETUP_TEX0_3D(v1)
	GLD_SETUP_TEX1_3D(v1)
	pV++;

	GLD_SETUP_3D_VERTEX(v2)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	GLD_SETUP_TEX0_3D(v2)
	GLD_SETUP_TEX1_3D(v2)
	pV++;

	GLD_SETUP_3D_VERTEX(v3)
	GLD_SETUP_USE_FLAT_COLOUR_3D
	GLD_SETUP_TEX0_3D(v3)
	GLD_SETUP_TEX1_3D(v3)
	pV++;

	gld->PB3d.pTriangles = (BYTE*)pV;
	gld->PB3d.nTriangles += 2;
}

//---------------------------------------------------------------------------

void gld_Quad3DSmooth_DX7(
	GLcontext *ctx,
	GLuint v0,
	GLuint v1,
	GLuint v2,
	GLuint v3)
{
	GLD_SETUP_3D_VARS_TRIANGLES

	GLD_SETUP_3D_VERTEX(v0)
	GLD_SETUP_SMOOTH_COLOUR_3D(v0)
	GLD_SETUP_TEX0_3D(v0)
	GLD_SETUP_TEX1_3D(v0)
	pV++;

	GLD_SETUP_3D_VERTEX(v1)
	GLD_SETUP_SMOOTH_COLOUR_3D(v1)
	GLD_SETUP_TEX0_3D(v1)
	GLD_SETUP_TEX1_3D(v1)
	pV++;

	GLD_SETUP_3D_VERTEX(v2)
	GLD_SETUP_SMOOTH_COLOUR_3D(v2)
	GLD_SETUP_TEX0_3D(v2)
	GLD_SETUP_TEX1_3D(v2)
	pV++;

	GLD_SETUP_3D_VERTEX(v2)
	GLD_SETUP_SMOOTH_COLOUR_3D(v2)
	GLD_SETUP_TEX0_3D(v2)
	GLD_SETUP_TEX1_3D(v2)
	pV++;

	GLD_SETUP_3D_VERTEX(v3)
	GLD_SETUP_SMOOTH_COLOUR_3D(v3)
	GLD_SETUP_TEX0_3D(v3)
	GLD_SETUP_TEX1_3D(v3)
	pV++;

	GLD_SETUP_3D_VERTEX(v0)
	GLD_SETUP_SMOOTH_COLOUR_3D(v0)
	GLD_SETUP_TEX0_3D(v0)
	GLD_SETUP_TEX1_3D(v0)
	pV++;

	gld->PB3d.pTriangles = (BYTE*)pV;
	gld->PB3d.nTriangles += 2;
}

//---------------------------------------------------------------------------
// Vertex setup for two-sided-lighting vertex shader
//---------------------------------------------------------------------------

/*

void gld_Points2DTwoside_DX8(GLcontext *ctx, GLuint first, GLuint last)
{
	// NOTE: Two-sided lighting does not apply to Points
}

//---------------------------------------------------------------------------

void gld_Line2DFlatTwoside_DX8(GLcontext *ctx, GLuint v0, GLuint v1)
{
	// NOTE: Two-sided lighting does not apply to Lines
}

//---------------------------------------------------------------------------

void gld_Line2DSmoothTwoside_DX8(GLcontext *ctx, GLuint v0, GLuint v1)
{
	// NOTE: Two-sided lighting does not apply to Lines
}

//---------------------------------------------------------------------------

void gld_Triangle2DFlatTwoside_DX8(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2)
{
}

//---------------------------------------------------------------------------

void gld_Triangle2DSmoothTwoside_DX8(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2)
{
	GLD_context			*gldCtx	= GLD_GET_CONTEXT(ctx);
	GLD_driver_dx8		*gld	= GLD_GET_DX8_DRIVER(gldCtx);
	GLD_TWOSIDED_VERTEX	*pV		= (GLD_TWOSIDED_VERTEX*)gld->PBtwosidelight.pTriangles;
	SScontext			*ss		= SWSETUP_CONTEXT(ctx);
	SWvertex			*swv;
	DWORD				dwSpecularColour;
	DWORD				dwFlatColour;
	GLuint					facing = 0;
	struct vertex_buffer	*VB;
	GLchan					(*vbcolor)[4];
	GLchan					(*vbspec)[4];

	// Reciprocal of DepthMax
	const float ooDepthMax = 1.0f / ctx->DepthMaxF; 

	// 1st vert
	swv = &ss->verts[v0];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 2nd vert
	swv = &ss->verts[v1];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 3rd vert
	swv = &ss->verts[v2];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	gld->PBtwosidelight.pTriangles = (BYTE*)pV;
	gld->PBtwosidelight.nTriangles++;
}

//---------------------------------------------------------------------------

void gld_Quad2DFlatTwoside_DX8(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3)
{
	GLD_context			*gldCtx	= GLD_GET_CONTEXT(ctx);
	GLD_driver_dx8		*gld	= GLD_GET_DX8_DRIVER(gldCtx);
	GLD_TWOSIDED_VERTEX	*pV		= (GLD_TWOSIDED_VERTEX*)gld->PBtwosidelight.pTriangles;
	SScontext			*ss		= SWSETUP_CONTEXT(ctx);
	SWvertex			*swv;
	DWORD				dwSpecularColour;
	DWORD				dwFlatColour;
	GLuint					facing = 0;
	struct vertex_buffer	*VB;
	GLchan					(*vbcolor)[4];
	GLchan					(*vbspec)[4];

	// Reciprocal of DepthMax
	const float ooDepthMax = 1.0f / ctx->DepthMaxF; 

	// 1st vert
	swv = &ss->verts[v0];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 2nd vert
	swv = &ss->verts[v1];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 3rd vert
	swv = &ss->verts[v2];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 4th vert
	swv = &ss->verts[v2];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 5th vert
	swv = &ss->verts[v3];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 6th vert
	swv = &ss->verts[v0];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	gld->PBtwosidelight.pTriangles = (BYTE*)pV;
	gld->PBtwosidelight.nTriangles += 2;
}

//---------------------------------------------------------------------------

void gld_Quad2DSmoothTwoside_DX8(GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint v3)
{
	GLD_context			*gldCtx	= GLD_GET_CONTEXT(ctx);
	GLD_driver_dx8		*gld	= GLD_GET_DX8_DRIVER(gldCtx);
	GLD_TWOSIDED_VERTEX	*pV		= (GLD_TWOSIDED_VERTEX*)gld->PBtwosidelight.pTriangles;
	SScontext			*ss		= SWSETUP_CONTEXT(ctx);
	SWvertex			*swv;
	DWORD				dwSpecularColour;
	DWORD				dwFlatColour;
	GLuint					facing = 0;
	struct vertex_buffer	*VB;
	GLchan					(*vbcolor)[4];
	GLchan					(*vbspec)[4];

	// Reciprocal of DepthMax
	const float ooDepthMax = 1.0f / ctx->DepthMaxF; 

	// 1st vert
	swv = &ss->verts[v0];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 2nd vert
	swv = &ss->verts[v1];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 3rd vert
	swv = &ss->verts[v2];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 4th vert
	swv = &ss->verts[v2];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 5th vert
	swv = &ss->verts[v3];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	// 6th vert
	swv = &ss->verts[v0];
	pV->Position.x = swv->win[0];
	pV->Position.y = GLD_FLIP_Y(swv->win[1]);
	pV->Position.z = swv->win[2] * ooDepthMax;
	pV->Position.w = swv->win[3];
	pV->TexUnit0.x = swv->texcoord[0][0];
	pV->TexUnit0.y = swv->texcoord[0][1];
	pV->TexUnit1.x = swv->texcoord[1][0];
	pV->TexUnit1.y = swv->texcoord[1][1];
	pV->FrontDiffuse = GLD_COLOUR;
	pV->FrontSpecular = GLD_SPECULAR;
	pV++;

	gld->PBtwosidelight.pTriangles = (BYTE*)pV;
	gld->PBtwosidelight.nTriangles += 2;
}

//---------------------------------------------------------------------------

*/