2018-05-11 14:39:28 +00:00
|
|
|
#include "private.h"
|
2018-08-07 19:22:44 +00:00
|
|
|
|
2018-08-14 08:49:31 +00:00
|
|
|
#include <stddef.h>
|
2018-05-11 14:39:28 +00:00
|
|
|
#include <stdio.h>
|
2018-09-20 14:01:13 +00:00
|
|
|
#include <stdlib.h>
|
2019-02-21 20:35:18 +00:00
|
|
|
#include <assert.h>
|
2019-03-13 12:05:06 +00:00
|
|
|
#include <string.h>
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2019-02-25 08:30:02 +00:00
|
|
|
#include "config.h"
|
2021-04-09 15:24:47 +00:00
|
|
|
#include "platform.h"
|
2021-02-19 07:08:58 +00:00
|
|
|
|
|
|
|
#include "yalloc/yalloc.h"
|
|
|
|
|
|
|
|
/* We always leave this amount of vram unallocated to prevent
|
|
|
|
* issues with the allocator */
|
|
|
|
#define PVR_MEM_BUFFER_SIZE (64 * 1024)
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
#define CLAMP_U (1<<1)
|
|
|
|
#define CLAMP_V (1<<0)
|
|
|
|
|
|
|
|
static TextureObject* TEXTURE_UNITS[MAX_TEXTURE_UNITS] = {NULL, NULL};
|
|
|
|
static NamedArray TEXTURE_OBJECTS;
|
2021-09-12 14:04:52 +00:00
|
|
|
GLubyte ACTIVE_TEXTURE = 0;
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
static TexturePalette* SHARED_PALETTES[4] = {NULL, NULL, NULL, NULL};
|
2019-02-21 21:58:31 +00:00
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
static GLuint _determinePVRFormat(GLint internalFormat, GLenum type);
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2019-03-09 14:32:50 +00:00
|
|
|
static GLboolean BANKS_USED[4]; // Each time a 256 colour bank is used, this is set to true
|
|
|
|
static GLboolean SUBBANKS_USED[4][16]; // 4 counts of the used 16 colour banks within the 256 ones
|
2019-03-11 19:07:59 +00:00
|
|
|
static GLenum INTERNAL_PALETTE_FORMAT = GL_RGBA4;
|
2019-03-09 14:32:50 +00:00
|
|
|
|
2021-02-19 07:08:58 +00:00
|
|
|
static void* YALLOC_BASE = NULL;
|
|
|
|
static size_t YALLOC_SIZE = 0;
|
|
|
|
|
2021-02-20 15:04:40 +00:00
|
|
|
static void* yalloc_alloc_and_defrag(size_t size) {
|
|
|
|
void* ret = yalloc_alloc(YALLOC_BASE, size);
|
|
|
|
|
|
|
|
if(!ret) {
|
|
|
|
/* Tried to allocate, but out of room, let's try defragging
|
|
|
|
* and repeating the alloc */
|
|
|
|
glDefragmentTextureMemory_KOS();
|
|
|
|
ret = yalloc_alloc(YALLOC_BASE, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ret && "Out of PVR memory!");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-09 14:32:50 +00:00
|
|
|
static TexturePalette* _initTexturePalette() {
|
|
|
|
TexturePalette* palette = (TexturePalette*) malloc(sizeof(TexturePalette));
|
|
|
|
assert(palette);
|
|
|
|
|
2021-04-09 15:24:47 +00:00
|
|
|
MEMSET4(palette, 0x0, sizeof(TexturePalette));
|
2019-03-09 14:32:50 +00:00
|
|
|
palette->bank = -1;
|
|
|
|
return palette;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLshort _glGenPaletteSlot(GLushort size) {
|
|
|
|
GLushort i, j;
|
|
|
|
|
|
|
|
assert(size == 16 || size == 256);
|
|
|
|
|
|
|
|
if(size == 16) {
|
|
|
|
for(i = 0; i < 4; ++i) {
|
|
|
|
for(j = 0; j < 16; ++j) {
|
|
|
|
if(!SUBBANKS_USED[i][j]) {
|
|
|
|
BANKS_USED[i] = GL_TRUE;
|
|
|
|
SUBBANKS_USED[i][j] = GL_TRUE;
|
|
|
|
return (i * 16) + j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(i = 0; i < 4; ++i) {
|
|
|
|
if(!BANKS_USED[i]) {
|
|
|
|
BANKS_USED[i] = GL_TRUE;
|
|
|
|
for(j = 0; j < 16; ++j) {
|
|
|
|
SUBBANKS_USED[i][j] = GL_TRUE;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "GL ERROR: No palette slots remain\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _glReleasePaletteSlot(GLshort slot, GLushort size) {
|
|
|
|
GLushort i;
|
|
|
|
|
|
|
|
assert(size == 16 || size == 256);
|
|
|
|
if(size == 16) {
|
|
|
|
GLushort bank = slot / 4;
|
|
|
|
GLushort subbank = slot % 4;
|
|
|
|
|
|
|
|
SUBBANKS_USED[bank][subbank] = GL_FALSE;
|
|
|
|
for(i = 0; i < 16; ++i) {
|
|
|
|
if(SUBBANKS_USED[bank][i]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BANKS_USED[bank] = GL_FALSE;
|
|
|
|
} else {
|
|
|
|
BANKS_USED[slot] = GL_FALSE;
|
|
|
|
for(i = 0; i < 16; ++i) {
|
|
|
|
SUBBANKS_USED[slot][i] = GL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:24:47 +00:00
|
|
|
/* Linear/iterative twiddling algorithm from Marcus' tatest */
|
|
|
|
#define TWIDTAB(x) ( (x&1)|((x&2)<<1)|((x&4)<<2)|((x&8)<<3)|((x&16)<<4)| \
|
|
|
|
((x&32)<<5)|((x&64)<<6)|((x&128)<<7)|((x&256)<<8)|((x&512)<<9) )
|
|
|
|
#define TWIDOUT(x, y) ( TWIDTAB((y)) | (TWIDTAB((x)) << 1) )
|
|
|
|
|
|
|
|
|
|
|
|
static void GPUTextureTwiddle8PPP(void* src, void* dst, uint32_t w, uint32_t h) {
|
|
|
|
uint32_t x, y, yout, min, mask;
|
|
|
|
|
|
|
|
min = MIN(w, h);
|
|
|
|
mask = min - 1;
|
|
|
|
|
|
|
|
uint8_t* pixels;
|
|
|
|
uint16_t* vtex;
|
|
|
|
pixels = (uint8_t*) src;
|
|
|
|
vtex = (uint16_t*) dst;
|
|
|
|
|
|
|
|
for(y = 0; y < h; y += 2) {
|
|
|
|
yout = y;
|
|
|
|
for(x = 0; x < w; x++) {
|
|
|
|
vtex[TWIDOUT((yout & mask) / 2, x & mask) +
|
|
|
|
(x / min + yout / min)*min * min / 2] =
|
|
|
|
pixels[y * w + x] | (pixels[(y + 1) * w + x] << 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GPUTextureTwiddle16BPP(void * src, void* dst, uint32_t w, uint32_t h) {
|
|
|
|
uint32_t x, y, yout, min, mask;
|
|
|
|
|
|
|
|
min = MIN(w, h);
|
|
|
|
mask = min - 1;
|
|
|
|
|
|
|
|
uint16_t* pixels;
|
|
|
|
uint16_t* vtex;
|
|
|
|
pixels = (uint16_t*) src;
|
|
|
|
vtex = (uint16_t*) dst;
|
|
|
|
|
|
|
|
for(y = 0; y < h; y++) {
|
|
|
|
yout = y;
|
|
|
|
|
|
|
|
for(x = 0; x < w; x++) {
|
|
|
|
vtex[TWIDOUT(x & mask, yout & mask) +
|
|
|
|
(x / min + yout / min)*min * min] = pixels[y * w + x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
TexturePalette* _glGetSharedPalette(GLshort bank) {
|
|
|
|
assert(bank >= 0 && bank < 4);
|
|
|
|
return SHARED_PALETTES[bank];
|
|
|
|
}
|
2019-03-09 14:32:50 +00:00
|
|
|
|
2019-03-11 19:07:59 +00:00
|
|
|
void _glSetInternalPaletteFormat(GLenum val) {
|
|
|
|
INTERNAL_PALETTE_FORMAT = val;
|
|
|
|
|
|
|
|
if(INTERNAL_PALETTE_FORMAT == GL_RGBA4) {
|
2021-04-09 15:24:47 +00:00
|
|
|
GPUSetPaletteFormat(GPU_PAL_ARGB4444);
|
2019-03-11 19:07:59 +00:00
|
|
|
} else {
|
|
|
|
assert(INTERNAL_PALETTE_FORMAT == GL_RGBA8);
|
2021-04-09 15:24:47 +00:00
|
|
|
GPUSetPaletteFormat(GPU_PAL_ARGB8888);
|
2019-03-11 19:07:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
void _glApplyColorTable(TexturePalette* src) {
|
2019-02-21 21:58:31 +00:00
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
*
|
|
|
|
* - Different palette formats (GL_RGB -> PVR_PAL_RGB565)
|
|
|
|
*/
|
2019-03-10 11:18:56 +00:00
|
|
|
if(!src || !src->data) {
|
2019-03-03 18:56:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
GLushort i;
|
|
|
|
GLushort offset = src->size * src->bank;
|
|
|
|
for(i = 0; i < src->width; ++i) {
|
2019-02-21 21:58:31 +00:00
|
|
|
GLubyte* entry = &src->data[i * 4];
|
2019-03-11 19:07:59 +00:00
|
|
|
if(INTERNAL_PALETTE_FORMAT == GL_RGBA8) {
|
2021-04-09 15:24:47 +00:00
|
|
|
GPUSetPaletteEntry(offset + i, PACK_ARGB8888(entry[3], entry[0], entry[1], entry[2]));
|
2019-03-11 19:07:59 +00:00
|
|
|
} else {
|
2021-04-09 15:24:47 +00:00
|
|
|
GPUSetPaletteEntry(offset + i, PACK_ARGB4444(entry[3], entry[0], entry[1], entry[2]));
|
2019-03-11 19:07:59 +00:00
|
|
|
}
|
2019-02-21 20:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 08:49:31 +00:00
|
|
|
GLubyte _glGetActiveTexture() {
|
|
|
|
return ACTIVE_TEXTURE;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
static GLint _determineStride(GLenum format, GLenum type) {
|
2018-08-08 15:50:09 +00:00
|
|
|
switch(type) {
|
|
|
|
case GL_BYTE:
|
|
|
|
case GL_UNSIGNED_BYTE:
|
2019-09-17 01:48:06 +00:00
|
|
|
return (format == GL_RED || format == GL_ALPHA) ? 1 : (format == GL_RGB) ? 3 : 4;
|
2018-08-08 15:50:09 +00:00
|
|
|
case GL_UNSIGNED_SHORT:
|
2019-09-17 01:48:06 +00:00
|
|
|
return (format == GL_RED || format == GL_ALPHA) ? 2 : (format == GL_RGB) ? 6 : 8;
|
2018-08-13 08:18:59 +00:00
|
|
|
case GL_UNSIGNED_SHORT_5_6_5:
|
2018-08-08 15:50:09 +00:00
|
|
|
case GL_UNSIGNED_SHORT_5_6_5_REV:
|
|
|
|
case GL_UNSIGNED_SHORT_5_6_5_TWID_KOS:
|
|
|
|
case GL_UNSIGNED_SHORT_5_5_5_1:
|
|
|
|
case GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS:
|
|
|
|
case GL_UNSIGNED_SHORT_1_5_5_5_REV:
|
|
|
|
case GL_UNSIGNED_SHORT_4_4_4_4:
|
|
|
|
case GL_UNSIGNED_SHORT_4_4_4_4_REV_TWID_KOS:
|
|
|
|
case GL_UNSIGNED_SHORT_4_4_4_4_REV:
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
static GLuint _glGetMipmapDataOffset(const TextureObject* obj, GLuint level) {
|
2018-08-08 15:50:09 +00:00
|
|
|
GLuint offset = 0;
|
2019-09-24 14:47:23 +00:00
|
|
|
GLuint size = obj->height;
|
2018-08-08 15:50:09 +00:00
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
if(obj->width != obj->height) {
|
|
|
|
fprintf(stderr, "ERROR: Accessing memory location of mipmaps on non-square texture\n");
|
|
|
|
return obj->baseDataOffset;
|
|
|
|
}
|
2020-05-19 20:27:34 +00:00
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
if(obj->isPaletted){
|
2019-09-26 08:17:07 +00:00
|
|
|
switch(size >> level){
|
2019-09-24 20:26:17 +00:00
|
|
|
case 1024:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x55558;
|
2019-09-24 20:26:17 +00:00
|
|
|
break;
|
|
|
|
case 512:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x15558;
|
2019-09-24 20:26:17 +00:00
|
|
|
break;
|
2019-09-24 14:47:23 +00:00
|
|
|
case 256:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x05558;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
|
|
|
case 128:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x01558;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
|
|
|
case 64:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x00558;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
|
|
|
case 32:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x00158;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
|
|
|
case 16:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x00058;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x00018;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x00008;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x00004;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2019-09-26 08:17:07 +00:00
|
|
|
offset = 0x00003;
|
2019-09-24 14:47:23 +00:00
|
|
|
break;
|
2018-08-08 15:50:09 +00:00
|
|
|
}
|
2020-03-05 20:09:19 +00:00
|
|
|
} else if(obj->isCompressed) {
|
|
|
|
switch(size >> level){
|
|
|
|
case 1024:
|
|
|
|
offset = 0x15556;
|
|
|
|
break;
|
|
|
|
case 512:
|
|
|
|
offset = 0x05556;
|
2020-05-19 20:27:34 +00:00
|
|
|
break;
|
2020-03-05 20:09:19 +00:00
|
|
|
case 256:
|
|
|
|
offset = 0x01556;
|
|
|
|
break;
|
|
|
|
case 128:
|
|
|
|
offset = 0x00556;
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
offset = 0x00156;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
offset = 0x00056;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
offset = 0x00016;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
offset = 0x00006;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
offset = 0x00002;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
offset = 0x00001;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
offset = 0x00000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
switch(size >> level){
|
2019-09-24 20:26:17 +00:00
|
|
|
case 1024:
|
|
|
|
offset = 0xAAAB0;
|
|
|
|
break;
|
|
|
|
case 512:
|
|
|
|
offset = 0x2AAB0;
|
|
|
|
break;
|
2019-09-24 14:47:23 +00:00
|
|
|
case 256:
|
|
|
|
offset = 0x0AAB0;
|
|
|
|
break;
|
|
|
|
case 128:
|
|
|
|
offset = 0x02AB0;
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
offset = 0x00AB0;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
offset = 0x002B0;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
offset = 0x000B0;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
offset = 0x00030;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
offset = 0x00010;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
offset = 0x00008;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
offset = 0x00006;
|
|
|
|
break;
|
2018-08-08 15:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GLubyte* _glGetMipmapLocation(const TextureObject* obj, GLuint level) {
|
2019-09-24 14:47:23 +00:00
|
|
|
return ((GLubyte*) obj->data) + _glGetMipmapDataOffset(obj, level);
|
2018-08-08 15:50:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GLuint _glGetMipmapLevelCount(const TextureObject* obj) {
|
|
|
|
return 1 + floorf(log2f(MAX(obj->width, obj->height)));
|
2018-08-08 15:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GLuint _glGetMipmapDataSize(TextureObject* obj) {
|
2019-09-24 14:47:23 +00:00
|
|
|
/* The mipmap data size is the offset + the size of the
|
|
|
|
* image */
|
2018-08-08 15:50:09 +00:00
|
|
|
|
2019-09-24 20:26:17 +00:00
|
|
|
GLuint imageSize = obj->baseDataSize;
|
2019-09-24 14:47:23 +00:00
|
|
|
GLuint offset = _glGetMipmapDataOffset(obj, 0);
|
2018-08-08 15:50:09 +00:00
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
return imageSize + offset;
|
2018-08-08 15:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 19:02:25 +00:00
|
|
|
GLubyte _glInitTextures() {
|
2019-02-25 08:30:02 +00:00
|
|
|
named_array_init(&TEXTURE_OBJECTS, sizeof(TextureObject), MAX_TEXTURE_COUNT);
|
2019-02-21 21:58:31 +00:00
|
|
|
|
2019-02-28 18:33:37 +00:00
|
|
|
// Reserve zero so that it is never given to anyone as an ID!
|
|
|
|
named_array_reserve(&TEXTURE_OBJECTS, 0);
|
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
SHARED_PALETTES[0] = _initTexturePalette();
|
|
|
|
SHARED_PALETTES[1] = _initTexturePalette();
|
|
|
|
SHARED_PALETTES[2] = _initTexturePalette();
|
|
|
|
SHARED_PALETTES[3] = _initTexturePalette();
|
|
|
|
|
2019-03-13 12:05:06 +00:00
|
|
|
memset((void*) BANKS_USED, 0x0, sizeof(BANKS_USED));
|
|
|
|
memset((void*) SUBBANKS_USED, 0x0, sizeof(SUBBANKS_USED));
|
2021-02-19 07:08:58 +00:00
|
|
|
|
2021-04-09 15:24:47 +00:00
|
|
|
size_t vram_free = GPUMemoryAvailable();
|
2021-02-19 07:08:58 +00:00
|
|
|
YALLOC_SIZE = vram_free - PVR_MEM_BUFFER_SIZE; /* Take all but 64kb VRAM */
|
2021-04-09 15:24:47 +00:00
|
|
|
YALLOC_BASE = GPUMemoryAlloc(YALLOC_SIZE);
|
2021-04-18 12:57:00 +00:00
|
|
|
|
2021-04-18 20:33:23 +00:00
|
|
|
#ifdef __DREAMCAST__
|
2021-04-18 12:57:00 +00:00
|
|
|
/* Ensure memory is aligned */
|
|
|
|
assert((uintptr_t) YALLOC_BASE % 32 == 0);
|
2021-04-18 20:33:23 +00:00
|
|
|
#endif
|
2021-04-18 12:57:00 +00:00
|
|
|
|
2021-02-19 07:08:58 +00:00
|
|
|
yalloc_init(YALLOC_BASE, YALLOC_SIZE);
|
2018-05-11 14:39:28 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-03 19:06:01 +00:00
|
|
|
TextureObject* _glGetTexture0() {
|
2018-05-11 14:39:28 +00:00
|
|
|
return TEXTURE_UNITS[0];
|
|
|
|
}
|
|
|
|
|
2019-03-03 19:06:01 +00:00
|
|
|
TextureObject* _glGetTexture1() {
|
2018-05-11 14:39:28 +00:00
|
|
|
return TEXTURE_UNITS[1];
|
|
|
|
}
|
|
|
|
|
2019-03-03 19:06:01 +00:00
|
|
|
TextureObject* _glGetBoundTexture() {
|
2018-05-11 14:39:28 +00:00
|
|
|
return TEXTURE_UNITS[ACTIVE_TEXTURE];
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glActiveTextureARB(GLenum texture) {
|
|
|
|
TRACE();
|
|
|
|
|
2021-09-13 09:29:04 +00:00
|
|
|
if(texture < GL_TEXTURE0_ARB || texture > GL_TEXTURE0_ARB + MAX_TEXTURE_UNITS) {
|
2018-05-11 14:39:28 +00:00
|
|
|
_glKosThrowError(GL_INVALID_ENUM, "glActiveTextureARB");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTIVE_TEXTURE = texture & 0xF;
|
|
|
|
}
|
|
|
|
|
2018-05-20 20:28:48 +00:00
|
|
|
GLboolean APIENTRY glIsTexture(GLuint texture) {
|
|
|
|
return (named_array_used(&TEXTURE_OBJECTS, texture)) ? GL_TRUE : GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2019-02-27 20:45:53 +00:00
|
|
|
static void _glInitializeTextureObject(TextureObject* txr, unsigned int id) {
|
|
|
|
txr->index = id;
|
|
|
|
txr->width = txr->height = 0;
|
|
|
|
txr->mipmap = 0;
|
|
|
|
txr->uv_clamp = 0;
|
2021-04-09 15:24:47 +00:00
|
|
|
txr->env = GPU_TXRENV_MODULATEALPHA;
|
2019-02-27 20:45:53 +00:00
|
|
|
txr->data = NULL;
|
|
|
|
txr->mipmapCount = 0;
|
|
|
|
txr->minFilter = GL_NEAREST;
|
|
|
|
txr->magFilter = GL_NEAREST;
|
|
|
|
txr->palette = NULL;
|
|
|
|
txr->isCompressed = GL_FALSE;
|
|
|
|
txr->isPaletted = GL_FALSE;
|
2021-10-25 18:04:58 +00:00
|
|
|
txr->mipmap_bias = GL_KOS_INTERNAL_DEFAULT_MIPMAP_LOD_BIAS;
|
2019-03-10 11:18:56 +00:00
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
/* Not mipmapped by default */
|
|
|
|
txr->baseDataOffset = 0;
|
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
/* Always default to the first shared bank */
|
|
|
|
txr->shared_bank = 0;
|
2019-02-27 20:45:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
void APIENTRY glGenTextures(GLsizei n, GLuint *textures) {
|
|
|
|
TRACE();
|
|
|
|
|
|
|
|
while(n--) {
|
|
|
|
GLuint id = 0;
|
|
|
|
TextureObject* txr = (TextureObject*) named_array_alloc(&TEXTURE_OBJECTS, &id);
|
2019-02-28 18:33:37 +00:00
|
|
|
|
|
|
|
assert(id); // Generated IDs must never be zero
|
|
|
|
|
2019-02-27 20:45:53 +00:00
|
|
|
_glInitializeTextureObject(txr, id);
|
2018-05-11 14:39:28 +00:00
|
|
|
|
|
|
|
*textures = id;
|
|
|
|
|
|
|
|
textures++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glDeleteTextures(GLsizei n, GLuint *textures) {
|
|
|
|
TRACE();
|
|
|
|
|
|
|
|
while(n--) {
|
|
|
|
TextureObject* txr = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, *textures);
|
|
|
|
|
2018-05-20 20:28:48 +00:00
|
|
|
/* Make sure we update framebuffer objects that have this texture attached */
|
2019-03-03 19:06:01 +00:00
|
|
|
_glWipeTextureOnFramebuffers(*textures);
|
2018-05-20 20:28:48 +00:00
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
if(txr == TEXTURE_UNITS[ACTIVE_TEXTURE]) {
|
|
|
|
TEXTURE_UNITS[ACTIVE_TEXTURE] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(txr->data) {
|
2021-02-19 07:08:58 +00:00
|
|
|
yalloc_free(YALLOC_BASE, txr->data);
|
2018-08-14 08:49:31 +00:00
|
|
|
txr->data = NULL;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:45:17 +00:00
|
|
|
if(txr->palette && txr->palette->data) {
|
2019-03-08 21:36:48 +00:00
|
|
|
free(txr->palette->data);
|
2018-09-22 19:45:17 +00:00
|
|
|
txr->palette->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(txr->palette) {
|
|
|
|
free(txr->palette);
|
|
|
|
txr->palette = NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-29 09:47:58 +00:00
|
|
|
named_array_release(&TEXTURE_OBJECTS, *textures);
|
|
|
|
*textures = 0;
|
|
|
|
textures++;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glBindTexture(GLenum target, GLuint texture) {
|
|
|
|
TRACE();
|
|
|
|
|
2019-03-13 15:14:09 +00:00
|
|
|
GLint target_values [] = {GL_TEXTURE_2D, 0};
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2018-08-22 08:24:49 +00:00
|
|
|
if(_glCheckValidEnum(target, target_values, __func__) != 0) {
|
2018-05-11 14:39:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(texture) {
|
2019-02-28 18:28:43 +00:00
|
|
|
/* If this didn't come from glGenTextures, then we should initialize the
|
|
|
|
* texture the first time it's bound */
|
|
|
|
if(!named_array_used(&TEXTURE_OBJECTS, texture)) {
|
|
|
|
TextureObject* txr = named_array_reserve(&TEXTURE_OBJECTS, texture);
|
|
|
|
_glInitializeTextureObject(txr, texture);
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
TEXTURE_UNITS[ACTIVE_TEXTURE] = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, texture);
|
|
|
|
} else {
|
|
|
|
TEXTURE_UNITS[ACTIVE_TEXTURE] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glTexEnvi(GLenum target, GLenum pname, GLint param) {
|
|
|
|
TRACE();
|
|
|
|
|
|
|
|
GLubyte failures = 0;
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GLint target_values [] = {GL_TEXTURE_ENV, GL_TEXTURE_FILTER_CONTROL_EXT, 0};
|
2018-08-22 08:24:49 +00:00
|
|
|
failures += _glCheckValidEnum(target, target_values, __func__);
|
2018-05-11 14:39:28 +00:00
|
|
|
|
|
|
|
TextureObject* active = TEXTURE_UNITS[ACTIVE_TEXTURE];
|
2020-05-19 20:27:34 +00:00
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
if(!active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(failures) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-05 20:09:19 +00:00
|
|
|
switch(target){
|
|
|
|
case GL_TEXTURE_ENV:
|
|
|
|
{
|
|
|
|
GLint pname_values [] = {GL_TEXTURE_ENV_MODE, 0};
|
|
|
|
GLint param_values [] = {GL_MODULATE, GL_DECAL, GL_REPLACE, 0};
|
|
|
|
failures += _glCheckValidEnum(pname, pname_values, __func__);
|
|
|
|
failures += _glCheckValidEnum(param, param_values, __func__);
|
2020-05-19 20:27:34 +00:00
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
if(failures) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
switch(param) {
|
|
|
|
case GL_MODULATE:
|
2021-04-09 15:24:47 +00:00
|
|
|
active->env = GPU_TXRENV_MODULATEALPHA;
|
2020-03-05 20:09:19 +00:00
|
|
|
break;
|
|
|
|
case GL_DECAL:
|
2021-04-09 15:24:47 +00:00
|
|
|
active->env = GPU_TXRENV_DECAL;
|
2020-03-05 20:09:19 +00:00
|
|
|
break;
|
|
|
|
case GL_REPLACE:
|
2021-04-09 15:24:47 +00:00
|
|
|
active->env = GPU_TXRENV_REPLACE;
|
2020-03-05 20:09:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_TEXTURE_FILTER_CONTROL_EXT:
|
|
|
|
{
|
|
|
|
GLint pname_values [] = {GL_TEXTURE_LOD_BIAS_EXT, 0};
|
|
|
|
failures += _glCheckValidEnum(pname, pname_values, __func__);
|
2020-05-19 20:27:34 +00:00
|
|
|
failures += (param > GL_MAX_TEXTURE_LOD_BIAS_DEFAULT || param < -GL_MAX_TEXTURE_LOD_BIAS_DEFAULT);
|
2020-03-05 20:09:19 +00:00
|
|
|
if(failures) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
active->mipmap_bias = (GL_MAX_TEXTURE_LOD_BIAS_DEFAULT+1)+param; // bring to 1-15 inclusive
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glTexEnvf(GLenum target, GLenum pname, GLfloat param) {
|
|
|
|
glTexEnvi(target, pname, param);
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:49:10 +00:00
|
|
|
void APIENTRY glCompressedTexImage2DARB(GLenum target,
|
2018-05-11 14:39:28 +00:00
|
|
|
GLint level,
|
2018-08-07 19:22:44 +00:00
|
|
|
GLenum internalFormat,
|
2018-05-11 14:39:28 +00:00
|
|
|
GLsizei width,
|
|
|
|
GLsizei height,
|
|
|
|
GLint border,
|
|
|
|
GLsizei imageSize,
|
|
|
|
const GLvoid *data) {
|
|
|
|
TRACE();
|
|
|
|
|
2018-08-13 08:18:59 +00:00
|
|
|
if(target != GL_TEXTURE_2D) {
|
2018-08-07 19:49:10 +00:00
|
|
|
_glKosThrowError(GL_INVALID_ENUM, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2018-08-13 08:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLint w = width;
|
|
|
|
if(w < 8 || (w & -w) != w) {
|
|
|
|
/* Width is not a power of two. Must be!*/
|
2018-09-22 19:45:17 +00:00
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2018-08-13 08:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLint h = height;
|
|
|
|
if(h < 8 || (h & -h) != h) {
|
2018-08-14 14:55:03 +00:00
|
|
|
/* Height is not a power of two. Must be!*/
|
2018-09-22 19:45:17 +00:00
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2018-08-13 08:18:59 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2018-08-08 08:50:57 +00:00
|
|
|
if(level || border) {
|
|
|
|
/* We don't support setting mipmap levels manually with compressed textures
|
|
|
|
maybe one day */
|
2018-08-07 19:49:10 +00:00
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2018-08-08 08:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLboolean mipmapped = GL_FALSE;
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2018-08-07 19:22:44 +00:00
|
|
|
switch(internalFormat) {
|
|
|
|
case GL_COMPRESSED_ARGB_1555_VQ_KOS:
|
|
|
|
case GL_COMPRESSED_ARGB_1555_VQ_TWID_KOS:
|
|
|
|
case GL_COMPRESSED_ARGB_4444_VQ_KOS:
|
|
|
|
case GL_COMPRESSED_ARGB_4444_VQ_TWID_KOS:
|
|
|
|
case GL_COMPRESSED_RGB_565_VQ_KOS:
|
|
|
|
case GL_COMPRESSED_RGB_565_VQ_TWID_KOS:
|
|
|
|
break;
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_KOS:
|
|
|
|
case GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_TWID_KOS:
|
|
|
|
case GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_KOS:
|
|
|
|
case GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_TWID_KOS:
|
|
|
|
case GL_COMPRESSED_RGB_565_VQ_MIPMAP_KOS:
|
|
|
|
case GL_COMPRESSED_RGB_565_VQ_MIPMAP_TWID_KOS:
|
|
|
|
mipmapped = GL_TRUE;
|
|
|
|
break;
|
2021-09-13 09:29:04 +00:00
|
|
|
default: {
|
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-07 19:22:44 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2018-08-07 19:22:44 +00:00
|
|
|
if(TEXTURE_UNITS[ACTIVE_TEXTURE] == NULL) {
|
2018-08-07 19:49:10 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2018-05-11 14:39:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureObject* active = TEXTURE_UNITS[ACTIVE_TEXTURE];
|
|
|
|
|
2018-08-08 08:50:57 +00:00
|
|
|
/* Set the required mipmap count */
|
2018-05-11 14:39:28 +00:00
|
|
|
active->width = width;
|
|
|
|
active->height = height;
|
|
|
|
active->color = _determinePVRFormat(
|
2018-08-07 19:22:44 +00:00
|
|
|
internalFormat,
|
2019-03-10 11:18:56 +00:00
|
|
|
internalFormat /* Doesn't matter (see determinePVRFormat) */
|
2018-05-11 14:39:28 +00:00
|
|
|
);
|
2018-08-20 07:34:54 +00:00
|
|
|
active->mipmapCount = _glGetMipmapLevelCount(active);
|
|
|
|
active->mipmap = (mipmapped) ? ~0 : (1 << level); /* Set only a single bit if this wasn't mipmapped otherwise set all */
|
|
|
|
active->isCompressed = GL_TRUE;
|
2018-05-11 14:39:28 +00:00
|
|
|
|
|
|
|
/* Odds are slim new data is same size as old, so free always */
|
2021-02-19 07:08:58 +00:00
|
|
|
if(active->data) {
|
|
|
|
yalloc_free(YALLOC_BASE, active->data);
|
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2021-02-20 15:04:40 +00:00
|
|
|
active->data = yalloc_alloc_and_defrag(imageSize);
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2021-09-04 11:13:59 +00:00
|
|
|
assert(active->data); // Debug assert
|
|
|
|
|
|
|
|
if(!active->data) { // Release, bail out "gracefully"
|
|
|
|
_glKosThrowError(GL_OUT_OF_MEMORY, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-19 07:08:58 +00:00
|
|
|
if(data) {
|
2021-04-09 15:24:47 +00:00
|
|
|
FASTCPY(active->data, data, imageSize);
|
2021-02-19 07:08:58 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GLint _cleanInternalFormat(GLint internalFormat) {
|
|
|
|
switch (internalFormat) {
|
2020-03-05 20:09:19 +00:00
|
|
|
case GL_COLOR_INDEX4_EXT:
|
|
|
|
return GL_COLOR_INDEX4_EXT;
|
2018-09-22 19:45:17 +00:00
|
|
|
case GL_COLOR_INDEX8_EXT:
|
|
|
|
return GL_COLOR_INDEX8_EXT;
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_ALPHA:
|
|
|
|
/* case GL_ALPHA4:
|
|
|
|
case GL_ALPHA8:
|
|
|
|
case GL_ALPHA12:
|
|
|
|
case GL_ALPHA16:*/
|
|
|
|
return GL_ALPHA;
|
|
|
|
case 1:
|
|
|
|
case GL_LUMINANCE:
|
|
|
|
/* case GL_LUMINANCE4:
|
|
|
|
case GL_LUMINANCE8:
|
|
|
|
case GL_LUMINANCE12:
|
|
|
|
case GL_LUMINANCE16:*/
|
|
|
|
return GL_LUMINANCE;
|
|
|
|
case 2:
|
|
|
|
case GL_LUMINANCE_ALPHA:
|
|
|
|
/* case GL_LUMINANCE4_ALPHA4:
|
|
|
|
case GL_LUMINANCE6_ALPHA2:
|
|
|
|
case GL_LUMINANCE8_ALPHA8:
|
|
|
|
case GL_LUMINANCE12_ALPHA4:
|
|
|
|
case GL_LUMINANCE12_ALPHA12:
|
|
|
|
case GL_LUMINANCE16_ALPHA16: */
|
|
|
|
return GL_LUMINANCE_ALPHA;
|
|
|
|
/* case GL_INTENSITY:
|
|
|
|
case GL_INTENSITY4:
|
|
|
|
case GL_INTENSITY8:
|
|
|
|
case GL_INTENSITY12:
|
|
|
|
case GL_INTENSITY16:
|
|
|
|
return GL_INTENSITY; */
|
|
|
|
case 3:
|
|
|
|
return GL_RGB;
|
|
|
|
case GL_RGB:
|
2018-09-22 19:45:17 +00:00
|
|
|
/* case GL_R3_G3_B2: */
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_RGB4:
|
|
|
|
case GL_RGB5:
|
|
|
|
case GL_RGB8:
|
|
|
|
case GL_RGB10:
|
|
|
|
case GL_RGB12:
|
2018-09-22 19:45:17 +00:00
|
|
|
case GL_RGB16:
|
2018-05-11 14:39:28 +00:00
|
|
|
return GL_RGB;
|
|
|
|
case 4:
|
|
|
|
return GL_RGBA;
|
|
|
|
case GL_RGBA:
|
2018-09-22 19:45:17 +00:00
|
|
|
case GL_RGBA2:
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_RGBA4:
|
|
|
|
case GL_RGB5_A1:
|
|
|
|
case GL_RGBA8:
|
|
|
|
case GL_RGB10_A2:
|
|
|
|
case GL_RGBA12:
|
2018-09-22 19:45:17 +00:00
|
|
|
case GL_RGBA16:
|
2018-05-11 14:39:28 +00:00
|
|
|
return GL_RGBA;
|
|
|
|
|
|
|
|
/* Support ARB_texture_rg */
|
|
|
|
case GL_RED:
|
|
|
|
/* case GL_R8:
|
|
|
|
case GL_R16:
|
|
|
|
case GL_RED:
|
|
|
|
case GL_COMPRESSED_RED: */
|
|
|
|
return GL_RED;
|
|
|
|
/* case GL_RG:
|
|
|
|
case GL_RG8:
|
|
|
|
case GL_RG16:
|
|
|
|
case GL_COMPRESSED_RG:
|
|
|
|
return GL_RG;*/
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
static GLuint _determinePVRFormat(GLint internalFormat, GLenum type) {
|
2018-05-11 14:39:28 +00:00
|
|
|
/* Given a cleaned internalFormat, return the Dreamcast format
|
|
|
|
* that can hold it
|
|
|
|
*/
|
|
|
|
switch(internalFormat) {
|
|
|
|
case GL_ALPHA:
|
|
|
|
case GL_LUMINANCE:
|
|
|
|
case GL_LUMINANCE_ALPHA:
|
|
|
|
case GL_RGBA:
|
|
|
|
/* OK so if we have something that requires alpha, we return 4444 unless
|
|
|
|
* the type was already 1555 (1-bit alpha) in which case we return that
|
|
|
|
*/
|
2018-08-28 16:51:33 +00:00
|
|
|
if(type == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB1555 | GPU_TXRFMT_NONTWIDDLED;
|
2018-08-28 16:51:33 +00:00
|
|
|
} else if(type == GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS) {
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB1555 | GPU_TXRFMT_TWIDDLED;
|
2018-08-28 16:51:33 +00:00
|
|
|
} else if(type == GL_UNSIGNED_SHORT_4_4_4_4_REV_TWID_KOS) {
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB4444 | GPU_TXRFMT_TWIDDLED;
|
2018-08-28 16:51:33 +00:00
|
|
|
} else {
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB4444 | GPU_TXRFMT_NONTWIDDLED;
|
2018-08-28 16:51:33 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_RED:
|
|
|
|
case GL_RGB:
|
2021-04-13 20:28:59 +00:00
|
|
|
switch(type) {
|
|
|
|
case GL_UNSIGNED_SHORT_5_6_5_TWID_KOS:
|
|
|
|
return GPU_TXRFMT_RGB565 | GPU_TXRFMT_TWIDDLED;
|
|
|
|
case GL_COMPRESSED_RGB_565_VQ_KOS:
|
|
|
|
case GL_COMPRESSED_RGB_565_VQ_MIPMAP_KOS:
|
|
|
|
return GPU_TXRFMT_RGB565 | GPU_TXRFMT_NONTWIDDLED | GPU_TXRFMT_VQ_ENABLE;
|
|
|
|
case GL_COMPRESSED_RGB_565_VQ_TWID_KOS:
|
|
|
|
case GL_COMPRESSED_RGB_565_VQ_MIPMAP_TWID_KOS:
|
|
|
|
return GPU_TXRFMT_RGB565 | GPU_TXRFMT_TWIDDLED | GPU_TXRFMT_VQ_ENABLE;
|
|
|
|
default:
|
|
|
|
return GPU_TXRFMT_RGB565 | GPU_TXRFMT_NONTWIDDLED;
|
|
|
|
}
|
|
|
|
break;
|
2018-05-11 14:39:28 +00:00
|
|
|
/* Compressed and twiddled versions */
|
|
|
|
case GL_UNSIGNED_SHORT_5_6_5_TWID_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_RGB565 | GPU_TXRFMT_TWIDDLED;
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_UNSIGNED_SHORT_4_4_4_4_REV_TWID_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB4444 | GPU_TXRFMT_TWIDDLED;
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB1555 | GPU_TXRFMT_TWIDDLED;
|
2018-08-07 19:22:44 +00:00
|
|
|
case GL_COMPRESSED_RGB_565_VQ_KOS:
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_COMPRESSED_RGB_565_VQ_MIPMAP_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_RGB565 | GPU_TXRFMT_NONTWIDDLED | GPU_TXRFMT_VQ_ENABLE;
|
2018-08-07 19:22:44 +00:00
|
|
|
case GL_COMPRESSED_RGB_565_VQ_TWID_KOS:
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_COMPRESSED_RGB_565_VQ_MIPMAP_TWID_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_RGB565 | GPU_TXRFMT_TWIDDLED | GPU_TXRFMT_VQ_ENABLE;
|
2018-08-07 19:22:44 +00:00
|
|
|
case GL_COMPRESSED_ARGB_4444_VQ_TWID_KOS:
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_TWID_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB4444 | GPU_TXRFMT_TWIDDLED | GPU_TXRFMT_VQ_ENABLE;
|
2018-08-07 19:22:44 +00:00
|
|
|
case GL_COMPRESSED_ARGB_4444_VQ_KOS:
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_COMPRESSED_ARGB_4444_VQ_MIPMAP_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB4444 | GPU_TXRFMT_NONTWIDDLED | GPU_TXRFMT_VQ_ENABLE;
|
2018-08-07 19:22:44 +00:00
|
|
|
case GL_COMPRESSED_ARGB_1555_VQ_KOS:
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB1555 | GPU_TXRFMT_NONTWIDDLED | GPU_TXRFMT_VQ_ENABLE;
|
2018-08-07 19:22:44 +00:00
|
|
|
case GL_COMPRESSED_ARGB_1555_VQ_TWID_KOS:
|
2018-08-08 08:50:57 +00:00
|
|
|
case GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_TWID_KOS:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_ARGB1555 | GPU_TXRFMT_TWIDDLED | GPU_TXRFMT_VQ_ENABLE;
|
2018-09-22 19:45:17 +00:00
|
|
|
case GL_COLOR_INDEX8_EXT:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_PAL8BPP | GPU_TXRFMT_TWIDDLED;
|
2020-03-05 20:09:19 +00:00
|
|
|
case GL_COLOR_INDEX4_EXT:
|
2021-04-09 15:24:47 +00:00
|
|
|
return GPU_TXRFMT_PAL4BPP | GPU_TXRFMT_TWIDDLED;
|
2018-05-11 14:39:28 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-22 19:45:17 +00:00
|
|
|
typedef void (*TextureConversionFunc)(const GLubyte*, GLubyte*);
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _rgba8888_to_argb4444(const GLubyte* source, GLubyte* dest) {
|
2018-09-22 19:45:17 +00:00
|
|
|
*((GLushort*) dest) = (source[3] & 0xF0) << 8 | (source[0] & 0xF0) << 4 | (source[1] & 0xF0) | (source[2] & 0xF0) >> 4;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _rgba8888_to_rgba8888(const GLubyte* source, GLubyte* dest) {
|
2018-09-20 14:08:06 +00:00
|
|
|
/* Noop */
|
|
|
|
GLubyte* dst = (GLubyte*) dest;
|
|
|
|
dst[0] = source[0];
|
|
|
|
dst[1] = source[1];
|
|
|
|
dst[2] = source[2];
|
|
|
|
dst[3] = source[3];
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _rgba8888_to_rgb565(const GLubyte* source, GLubyte* dest) {
|
2019-02-27 20:11:14 +00:00
|
|
|
*((GLushort*) dest) = ((source[0] & 0b11111000) << 8) | ((source[1] & 0b11111100) << 3) | (source[2] >> 3);
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _rgb888_to_rgba8888(const GLubyte* source, GLubyte* dest) {
|
2018-09-20 14:10:08 +00:00
|
|
|
/* Noop */
|
|
|
|
GLubyte* dst = (GLubyte*) dest;
|
|
|
|
dst[0] = source[0];
|
|
|
|
dst[1] = source[1];
|
|
|
|
dst[2] = source[2];
|
|
|
|
dst[3] = 255;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _rgb888_to_rgb565(const GLubyte* source, GLubyte* dest) {
|
2018-09-22 19:45:17 +00:00
|
|
|
*((GLushort*) dest) = ((source[0] & 0b11111000) << 8) | ((source[1] & 0b11111100) << 3) | (source[2] >> 3);
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _rgba8888_to_a000(const GLubyte* source, GLubyte* dest) {
|
2018-09-22 19:45:17 +00:00
|
|
|
*((GLushort*) dest) = ((source[3] & 0b11111000) << 8);
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _r8_to_rgb565(const GLubyte* source, GLubyte* dest) {
|
2018-09-22 19:45:17 +00:00
|
|
|
*((GLushort*) dest) = (source[0] & 0b11111000) << 8;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _rgba4444_to_argb4444(const GLubyte* source, GLubyte* dest) {
|
2018-05-11 14:39:28 +00:00
|
|
|
GLushort* src = (GLushort*) source;
|
2018-09-22 19:45:17 +00:00
|
|
|
*((GLushort*) dest) = ((*src & 0x000F) << 12) | *src >> 4;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _rgba4444_to_rgba8888(const GLubyte* source, GLubyte* dest) {
|
2018-09-20 14:08:06 +00:00
|
|
|
GLushort src = *((GLushort*) source);
|
|
|
|
GLubyte* dst = (GLubyte*) dest;
|
|
|
|
|
2018-09-22 19:45:17 +00:00
|
|
|
dst[0] = ((src & 0xF000) >> 12) * 2;
|
|
|
|
dst[1] = ((src & 0x0F00) >> 8) * 2;
|
|
|
|
dst[2] = ((src & 0x00F0) >> 4) * 2;
|
|
|
|
dst[3] = ((src & 0x000F)) * 2;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GL_FORCE_INLINE void _i8_to_i8(const GLubyte* source, GLubyte* dest) {
|
2018-09-22 19:45:17 +00:00
|
|
|
/* For indexes */
|
|
|
|
GLubyte* dst = (GLubyte*) dest;
|
|
|
|
*dst = *source;
|
2018-09-20 14:08:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 01:48:06 +00:00
|
|
|
static inline void _alpha8_to_argb4444(const GLubyte* source, GLubyte* dest) {
|
|
|
|
*((GLushort*) dest) = (*source & 0xF0) << 8 | (0xFF & 0xF0) << 4 | (0xFF & 0xF0) | (0xFF & 0xF0) >> 4;
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
static TextureConversionFunc _determineConversion(GLint internalFormat, GLenum format, GLenum type) {
|
|
|
|
switch(internalFormat) {
|
|
|
|
case GL_ALPHA: {
|
2019-09-17 01:48:06 +00:00
|
|
|
if(format == GL_ALPHA) {
|
2019-09-17 13:28:43 +00:00
|
|
|
/* Dreamcast doesn't really support GL_ALPHA internally, so store as argb with each rgb value as white */
|
2019-09-17 01:48:06 +00:00
|
|
|
return _alpha8_to_argb4444;
|
|
|
|
} else if(type == GL_UNSIGNED_BYTE && format == GL_RGBA) {
|
2018-05-11 14:39:28 +00:00
|
|
|
return _rgba8888_to_a000;
|
|
|
|
} else if(type == GL_BYTE && format == GL_RGBA) {
|
|
|
|
return _rgba8888_to_a000;
|
|
|
|
}
|
|
|
|
} break;
|
2018-08-14 14:55:03 +00:00
|
|
|
case GL_RED: {
|
|
|
|
if(type == GL_UNSIGNED_BYTE && format == GL_RED) {
|
|
|
|
/* Dreamcast doesn't really support GL_RED internally, so store as rgb */
|
|
|
|
return _r8_to_rgb565;
|
|
|
|
}
|
|
|
|
} break;
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_RGB: {
|
|
|
|
if(type == GL_UNSIGNED_BYTE && format == GL_RGB) {
|
|
|
|
return _rgb888_to_rgb565;
|
2019-02-27 20:11:14 +00:00
|
|
|
} else if(type == GL_UNSIGNED_BYTE && format == GL_RGBA) {
|
|
|
|
return _rgba8888_to_rgb565;
|
2018-05-11 14:39:28 +00:00
|
|
|
} else if(type == GL_BYTE && format == GL_RGB) {
|
|
|
|
return _rgb888_to_rgb565;
|
|
|
|
} else if(type == GL_UNSIGNED_BYTE && format == GL_RED) {
|
|
|
|
return _r8_to_rgb565;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case GL_RGBA: {
|
|
|
|
if(type == GL_UNSIGNED_BYTE && format == GL_RGBA) {
|
|
|
|
return _rgba8888_to_argb4444;
|
|
|
|
} else if (type == GL_BYTE && format == GL_RGBA) {
|
|
|
|
return _rgba8888_to_argb4444;
|
|
|
|
} else if(type == GL_UNSIGNED_SHORT_4_4_4_4 && format == GL_RGBA) {
|
|
|
|
return _rgba4444_to_argb4444;
|
|
|
|
}
|
|
|
|
} break;
|
2018-09-20 14:08:06 +00:00
|
|
|
case GL_RGBA8: {
|
|
|
|
if(type == GL_UNSIGNED_BYTE && format == GL_RGBA) {
|
|
|
|
return _rgba8888_to_rgba8888;
|
|
|
|
} else if (type == GL_BYTE && format == GL_RGBA) {
|
|
|
|
return _rgba8888_to_rgba8888;
|
2018-09-20 14:10:08 +00:00
|
|
|
} else if(type == GL_UNSIGNED_BYTE && format == GL_RGB) {
|
|
|
|
return _rgb888_to_rgba8888;
|
|
|
|
} else if (type == GL_BYTE && format == GL_RGB) {
|
|
|
|
return _rgb888_to_rgba8888;
|
2018-09-20 14:08:06 +00:00
|
|
|
} else if(type == GL_UNSIGNED_SHORT_4_4_4_4 && format == GL_RGBA) {
|
|
|
|
return _rgba4444_to_rgba8888;
|
|
|
|
}
|
|
|
|
} break;
|
2018-09-22 19:45:17 +00:00
|
|
|
case GL_COLOR_INDEX8_EXT:
|
|
|
|
if(format == GL_COLOR_INDEX) {
|
|
|
|
switch(type) {
|
|
|
|
case GL_BYTE:
|
|
|
|
case GL_UNSIGNED_BYTE:
|
|
|
|
return _i8_to_i8;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-05-11 14:39:28 +00:00
|
|
|
default:
|
2018-08-14 14:55:03 +00:00
|
|
|
fprintf(stderr, "Unsupported conversion: %x -> %x, %x\n", internalFormat, format, type);
|
2018-05-11 14:39:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLboolean _isSupportedFormat(GLenum format) {
|
|
|
|
switch(format) {
|
2019-09-17 01:48:06 +00:00
|
|
|
case GL_ALPHA:
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_RED:
|
|
|
|
case GL_RGB:
|
|
|
|
case GL_RGBA:
|
|
|
|
case GL_BGRA:
|
2018-09-22 19:45:17 +00:00
|
|
|
case GL_COLOR_INDEX:
|
2021-04-13 20:28:59 +00:00
|
|
|
case GL_UNSIGNED_SHORT_4_4_4_4_REV_TWID_KOS:
|
2018-05-11 14:39:28 +00:00
|
|
|
return GL_TRUE;
|
|
|
|
default:
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 08:49:31 +00:00
|
|
|
GLboolean _glIsMipmapComplete(const TextureObject* obj) {
|
2019-09-24 14:47:23 +00:00
|
|
|
|
|
|
|
// Non-square textures can't have mipmaps
|
|
|
|
if(obj->width != obj->height) {
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2018-08-08 08:50:57 +00:00
|
|
|
if(!obj->mipmap || !obj->mipmapCount) {
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLsizei i = 0;
|
2021-02-19 07:08:58 +00:00
|
|
|
for(; i < (GLubyte) obj->mipmapCount; ++i) {
|
2018-08-08 08:50:57 +00:00
|
|
|
if((obj->mipmap & (1 << i)) == 0) {
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2019-04-17 13:55:30 +00:00
|
|
|
void _glAllocateSpaceForMipmaps(TextureObject* active) {
|
2019-09-24 14:47:23 +00:00
|
|
|
if(active->data && active->baseDataOffset > 0) {
|
|
|
|
/* Already done - mipmaps have a dataOffset */
|
2019-04-17 13:55:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've allocated level 0 before, but now we're allocating
|
|
|
|
* a level beyond that, we need to reallocate the data, copy level 0
|
|
|
|
* then free the original
|
|
|
|
*/
|
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
GLuint size = active->baseDataSize;
|
2019-04-17 13:55:30 +00:00
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
/* Copy the data out of the pvr and back to ram */
|
|
|
|
GLubyte* temp = (GLubyte*) malloc(size);
|
|
|
|
memcpy(temp, active->data, size);
|
2019-04-17 13:55:30 +00:00
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
/* Free the PVR data */
|
2021-02-19 07:08:58 +00:00
|
|
|
yalloc_free(YALLOC_BASE, active->data);
|
2019-09-24 14:47:23 +00:00
|
|
|
active->data = NULL;
|
|
|
|
|
|
|
|
/* Figure out how much room to allocate for mipmaps */
|
|
|
|
GLuint bytes = _glGetMipmapDataSize(active);
|
|
|
|
|
2021-02-20 15:04:40 +00:00
|
|
|
active->data = yalloc_alloc_and_defrag(bytes);
|
2019-09-24 14:47:23 +00:00
|
|
|
|
2021-09-04 11:13:59 +00:00
|
|
|
assert(active->data);
|
|
|
|
if(!active->data) {
|
|
|
|
free(temp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
/* If there was existing data, then copy it where it should go */
|
|
|
|
memcpy(_glGetMipmapLocation(active, 0), temp, size);
|
|
|
|
|
2020-03-02 19:20:42 +00:00
|
|
|
/* We no longer need this */
|
|
|
|
free(temp);
|
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
/* Set the data offset depending on whether or not this is a
|
|
|
|
* paletted texure */
|
|
|
|
active->baseDataOffset = _glGetMipmapDataOffset(active, 0);
|
2019-04-17 13:55:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
2021-04-13 20:28:59 +00:00
|
|
|
#define INFO_MSG(x) fprintf(stderr, "%s:%s > %s\n", __FILE__, TOSTRING(__LINE__), x)
|
2020-03-05 20:09:19 +00:00
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
|
|
|
|
GLsizei width, GLsizei height, GLint border,
|
|
|
|
GLenum format, GLenum type, const GLvoid *data) {
|
|
|
|
|
|
|
|
TRACE();
|
|
|
|
|
|
|
|
if(target != GL_TEXTURE_2D) {
|
2020-03-05 20:09:19 +00:00
|
|
|
INFO_MSG("");
|
|
|
|
_glKosThrowError(GL_INVALID_ENUM, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:35:18 +00:00
|
|
|
if(format != GL_COLOR_INDEX) {
|
|
|
|
if(!_isSupportedFormat(format)) {
|
2021-04-13 20:28:59 +00:00
|
|
|
INFO_MSG("Unsupported format");
|
2020-03-05 20:09:19 +00:00
|
|
|
_glKosThrowError(GL_INVALID_ENUM, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2019-02-21 20:35:18 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2019-02-21 20:35:18 +00:00
|
|
|
/* Abuse determineStride to see if type is valid */
|
|
|
|
if(_determineStride(GL_RGBA, type) == -1) {
|
2020-03-05 20:09:19 +00:00
|
|
|
INFO_MSG("");
|
|
|
|
_glKosThrowError(GL_INVALID_ENUM, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2019-02-21 20:35:18 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2019-02-21 20:35:18 +00:00
|
|
|
internalFormat = _cleanInternalFormat(internalFormat);
|
|
|
|
if(internalFormat == -1) {
|
2020-03-05 20:09:19 +00:00
|
|
|
INFO_MSG("");
|
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2019-02-21 20:35:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(internalFormat != GL_COLOR_INDEX8_EXT) {
|
2020-03-05 20:09:19 +00:00
|
|
|
INFO_MSG("");
|
2019-02-21 20:35:18 +00:00
|
|
|
_glKosThrowError(GL_INVALID_ENUM, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2019-02-21 20:35:18 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
GLuint w = width;
|
|
|
|
GLuint h = height;
|
|
|
|
if(level == 0){
|
|
|
|
if((w < 8 || (w & -w) != w)) {
|
|
|
|
/* Width is not a power of two. Must be!*/
|
|
|
|
INFO_MSG("");
|
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2020-03-05 20:09:19 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2020-05-19 20:27:34 +00:00
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
if((h < 8 || (h & -h) != h)) {
|
|
|
|
/* height is not a power of two. Must be!*/
|
|
|
|
INFO_MSG("");
|
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2020-03-05 20:09:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Mipmap Errors, kos crashes if 1x1 */
|
|
|
|
if((h < 2) || (w < 2)){
|
|
|
|
assert(TEXTURE_UNITS[ACTIVE_TEXTURE]);
|
|
|
|
TEXTURE_UNITS[ACTIVE_TEXTURE]->mipmap |= (1 << level);
|
|
|
|
return;
|
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 08:18:59 +00:00
|
|
|
if(level < 0) {
|
2020-03-05 20:09:19 +00:00
|
|
|
INFO_MSG("");
|
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 15:19:03 +00:00
|
|
|
if(level > 0 && width != height) {
|
2021-04-13 20:28:59 +00:00
|
|
|
INFO_MSG("Tried to set non-square texture as a mipmap level");
|
|
|
|
printf("[GL ERROR] Mipmaps cannot be supported on non-square textures\n");
|
2019-04-17 15:19:03 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2019-04-17 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
if(border) {
|
2020-03-05 20:09:19 +00:00
|
|
|
INFO_MSG("");
|
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!TEXTURE_UNITS[ACTIVE_TEXTURE]) {
|
2021-04-13 20:28:59 +00:00
|
|
|
INFO_MSG("Called glTexImage2D on unbound texture");
|
2019-03-10 19:38:47 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2021-09-13 09:29:04 +00:00
|
|
|
return;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:35:18 +00:00
|
|
|
GLboolean isPaletted = (internalFormat == GL_COLOR_INDEX8_EXT) ? GL_TRUE : GL_FALSE;
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
/* Calculate the format that we need to convert the data to */
|
2019-03-10 11:18:56 +00:00
|
|
|
GLuint pvr_format = _determinePVRFormat(internalFormat, type);
|
2018-05-11 14:39:28 +00:00
|
|
|
|
|
|
|
TextureObject* active = TEXTURE_UNITS[ACTIVE_TEXTURE];
|
|
|
|
|
2019-03-03 19:11:53 +00:00
|
|
|
assert(active);
|
|
|
|
|
2020-03-05 20:09:19 +00:00
|
|
|
if(active->data && (level == 0)) {
|
2018-05-11 14:39:28 +00:00
|
|
|
/* pre-existing texture - check if changed */
|
|
|
|
if(active->width != width ||
|
|
|
|
active->height != height ||
|
|
|
|
active->color != pvr_format) {
|
|
|
|
/* changed - free old texture memory */
|
2021-02-19 21:28:31 +00:00
|
|
|
yalloc_free(YALLOC_BASE, active->data);
|
2018-05-11 14:39:28 +00:00
|
|
|
active->data = NULL;
|
2018-08-08 08:50:57 +00:00
|
|
|
active->mipmap = 0;
|
2018-08-08 15:50:09 +00:00
|
|
|
active->mipmapCount = 0;
|
2020-03-05 20:09:19 +00:00
|
|
|
active->mipmap_bias = GL_KOS_INTERNAL_DEFAULT_MIPMAP_LOD_BIAS;
|
2018-08-08 15:50:09 +00:00
|
|
|
active->dataStride = 0;
|
2019-09-24 14:47:23 +00:00
|
|
|
active->baseDataOffset = 0;
|
|
|
|
active->baseDataSize = 0;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
2018-08-07 19:22:44 +00:00
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2018-09-22 19:45:17 +00:00
|
|
|
/* All colour formats are represented as shorts internally. Paletted textures
|
|
|
|
* are represented by byte indexes (which look up into a color table)
|
|
|
|
*/
|
|
|
|
GLint destStride = isPaletted ? 1 : 2;
|
|
|
|
GLuint bytes = (width * height * destStride);
|
2018-05-11 14:39:28 +00:00
|
|
|
|
|
|
|
if(!active->data) {
|
2019-02-21 20:35:18 +00:00
|
|
|
assert(active);
|
|
|
|
assert(width);
|
|
|
|
assert(height);
|
|
|
|
assert(destStride);
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
/* need texture memory */
|
|
|
|
active->width = width;
|
|
|
|
active->height = height;
|
|
|
|
active->color = pvr_format;
|
2018-08-09 07:56:43 +00:00
|
|
|
/* Set the required mipmap count */
|
|
|
|
active->mipmapCount = _glGetMipmapLevelCount(active);
|
2020-03-05 20:09:19 +00:00
|
|
|
active->mipmap_bias = GL_KOS_INTERNAL_DEFAULT_MIPMAP_LOD_BIAS;
|
2018-09-22 19:45:17 +00:00
|
|
|
active->dataStride = destStride;
|
2019-09-24 14:47:23 +00:00
|
|
|
active->baseDataSize = bytes;
|
2018-08-14 08:49:31 +00:00
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
assert(bytes);
|
2019-04-17 13:55:30 +00:00
|
|
|
|
|
|
|
if(level > 0) {
|
2019-09-24 14:47:23 +00:00
|
|
|
/* If we're uploading a mipmap level, we need to allocate the full amount of space */
|
|
|
|
_glAllocateSpaceForMipmaps(active);
|
|
|
|
} else {
|
2021-02-20 15:04:40 +00:00
|
|
|
active->data = yalloc_alloc_and_defrag(active->baseDataSize);
|
2019-04-17 13:55:30 +00:00
|
|
|
}
|
2019-02-21 20:35:18 +00:00
|
|
|
|
2018-08-20 07:34:54 +00:00
|
|
|
active->isCompressed = GL_FALSE;
|
2018-09-22 19:45:17 +00:00
|
|
|
active->isPaletted = isPaletted;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:55:30 +00:00
|
|
|
/* We're supplying a mipmap level, but previously we only had
|
2019-09-24 14:47:23 +00:00
|
|
|
* data for the first level (level 0) */
|
|
|
|
if(level > 0 && active->baseDataOffset == 0) {
|
2019-04-17 13:55:30 +00:00
|
|
|
_glAllocateSpaceForMipmaps(active);
|
|
|
|
}
|
|
|
|
|
2021-09-04 11:13:59 +00:00
|
|
|
assert(active->data);
|
|
|
|
|
|
|
|
/* If we run out of PVR memory just return */
|
|
|
|
if(!active->data) {
|
|
|
|
_glKosThrowError(GL_OUT_OF_MEMORY, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-09 07:56:43 +00:00
|
|
|
/* Mark this level as set in the mipmap bitmask */
|
|
|
|
active->mipmap |= (1 << level);
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
/* Let's assume we need to convert */
|
|
|
|
GLboolean needsConversion = GL_TRUE;
|
2019-09-24 14:47:23 +00:00
|
|
|
|
|
|
|
/* Let's assume we need twiddling - we always store things twiddled! */
|
|
|
|
GLboolean needsTwiddling = GL_TRUE;
|
2018-05-11 14:39:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These are the only formats where the source format passed in matches the pvr format.
|
|
|
|
* Note the REV formats + GL_BGRA will reverse to ARGB which is what the PVR supports
|
|
|
|
*/
|
2019-02-21 20:35:18 +00:00
|
|
|
if(format == GL_COLOR_INDEX) {
|
|
|
|
/* Don't convert color indexes */
|
|
|
|
needsConversion = GL_FALSE;
|
2019-09-26 08:17:07 +00:00
|
|
|
|
|
|
|
if(type == GL_UNSIGNED_BYTE_TWID_KOS) {
|
|
|
|
needsTwiddling = GL_FALSE;
|
|
|
|
}
|
2019-02-21 20:35:18 +00:00
|
|
|
} else if(format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && internalFormat == GL_RGBA) {
|
2018-05-11 14:39:28 +00:00
|
|
|
needsConversion = GL_FALSE;
|
|
|
|
} else if(format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV && internalFormat == GL_RGBA) {
|
|
|
|
needsConversion = GL_FALSE;
|
|
|
|
} else if(format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5 && internalFormat == GL_RGB) {
|
|
|
|
needsConversion = GL_FALSE;
|
|
|
|
} else if(format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5_TWID_KOS && internalFormat == GL_RGB) {
|
|
|
|
needsConversion = GL_FALSE;
|
2019-09-24 14:47:23 +00:00
|
|
|
needsTwiddling = GL_FALSE;
|
2018-05-11 14:39:28 +00:00
|
|
|
} else if(format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV_TWID_KOS && internalFormat == GL_RGBA) {
|
|
|
|
needsConversion = GL_FALSE;
|
2019-09-24 14:47:23 +00:00
|
|
|
needsTwiddling = GL_FALSE;
|
2018-05-11 14:39:28 +00:00
|
|
|
} else if(format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV_TWID_KOS && internalFormat == GL_RGBA) {
|
|
|
|
needsConversion = GL_FALSE;
|
2019-09-24 14:47:23 +00:00
|
|
|
needsTwiddling = GL_FALSE;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
GLubyte* targetData = (active->baseDataOffset == 0) ? active->data : _glGetMipmapLocation(active, level);
|
2019-02-21 20:35:18 +00:00
|
|
|
assert(targetData);
|
2018-08-08 15:50:09 +00:00
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
GLubyte* conversionBuffer = NULL;
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
if(!data) {
|
|
|
|
/* No data? Do nothing! */
|
|
|
|
return;
|
2019-09-24 14:47:23 +00:00
|
|
|
} else if(!needsConversion && !needsTwiddling) {
|
2019-03-03 19:11:53 +00:00
|
|
|
assert(targetData);
|
|
|
|
assert(data);
|
|
|
|
assert(bytes);
|
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
/* No conversion? Just copy the data, and the pvr_format is correct */
|
2019-09-24 18:39:23 +00:00
|
|
|
FASTCPY(targetData, data, bytes);
|
2018-05-11 14:39:28 +00:00
|
|
|
return;
|
2019-09-24 14:47:23 +00:00
|
|
|
} else if(needsConversion) {
|
2018-05-11 14:39:28 +00:00
|
|
|
TextureConversionFunc convert = _determineConversion(
|
|
|
|
internalFormat,
|
|
|
|
format,
|
|
|
|
type
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!convert) {
|
2021-04-13 20:28:59 +00:00
|
|
|
INFO_MSG("Couldn't find conversion\n");
|
2018-08-08 15:50:09 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2018-05-11 14:39:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-09 07:56:43 +00:00
|
|
|
GLint stride = _determineStride(format, type);
|
2019-03-03 19:11:53 +00:00
|
|
|
assert(stride > -1);
|
2018-08-09 07:56:43 +00:00
|
|
|
|
|
|
|
if(stride == -1) {
|
2021-04-13 20:28:59 +00:00
|
|
|
INFO_MSG("Stride was not detected\n");
|
2018-08-08 15:50:09 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2018-05-11 14:39:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-24 14:47:23 +00:00
|
|
|
conversionBuffer = malloc(bytes);
|
|
|
|
|
|
|
|
GLubyte* dest = conversionBuffer;
|
|
|
|
const GLubyte* source = data;
|
|
|
|
|
|
|
|
assert(conversionBuffer);
|
|
|
|
assert(source);
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
/* Perform the conversion */
|
2018-05-14 16:10:53 +00:00
|
|
|
GLuint i;
|
2018-09-22 19:45:17 +00:00
|
|
|
for(i = 0; i < bytes; i += destStride) {
|
2018-05-11 14:39:28 +00:00
|
|
|
convert(source, dest);
|
|
|
|
|
2018-09-22 19:45:17 +00:00
|
|
|
dest += destStride;
|
2018-08-09 07:56:43 +00:00
|
|
|
source += stride;
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 14:47:23 +00:00
|
|
|
|
|
|
|
if(needsTwiddling) {
|
|
|
|
const GLubyte *pixels = (GLubyte*) (conversionBuffer) ? conversionBuffer : data;
|
|
|
|
|
|
|
|
if(internalFormat == GL_COLOR_INDEX8_EXT) {
|
2021-04-09 15:24:47 +00:00
|
|
|
GPUTextureTwiddle8PPP((void*) pixels, targetData, width, height);
|
2019-09-24 14:47:23 +00:00
|
|
|
} else {
|
2021-04-09 15:24:47 +00:00
|
|
|
GPUTextureTwiddle16BPP((void*) pixels, targetData, width, height);
|
2019-09-24 14:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We make sure we remove nontwiddled and add twiddled. We could always
|
|
|
|
* make it twiddled when determining the format but I worry that would make the
|
|
|
|
* code less flexible to change in the future */
|
2019-09-25 12:45:08 +00:00
|
|
|
active->color &= ~(1 << 26);
|
2019-09-24 14:47:23 +00:00
|
|
|
} else {
|
|
|
|
/* We should only get here if we converted twiddled data... which is never currently */
|
|
|
|
assert(conversionBuffer);
|
|
|
|
|
|
|
|
// We've already converted the data and we
|
|
|
|
// don't need to twiddle it!
|
2019-09-24 18:39:23 +00:00
|
|
|
FASTCPY(targetData, conversionBuffer, bytes);
|
2019-09-24 14:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(conversionBuffer) {
|
|
|
|
free(conversionBuffer);
|
|
|
|
conversionBuffer = NULL;
|
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void APIENTRY glTexParameteri(GLenum target, GLenum pname, GLint param) {
|
|
|
|
TRACE();
|
|
|
|
|
2019-03-03 19:06:01 +00:00
|
|
|
TextureObject* active = _glGetBoundTexture();
|
2018-05-11 14:39:28 +00:00
|
|
|
|
2018-05-28 06:16:40 +00:00
|
|
|
if(!active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:39:28 +00:00
|
|
|
if(target == GL_TEXTURE_2D) {
|
|
|
|
switch(pname) {
|
|
|
|
case GL_TEXTURE_MAG_FILTER:
|
|
|
|
switch(param) {
|
2018-08-09 08:32:29 +00:00
|
|
|
case GL_NEAREST:
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_LINEAR:
|
2018-08-09 08:32:29 +00:00
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
active->magFilter = param;
|
|
|
|
break;
|
|
|
|
case GL_TEXTURE_MIN_FILTER:
|
|
|
|
switch(param) {
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_NEAREST:
|
2018-08-09 08:32:29 +00:00
|
|
|
case GL_LINEAR:
|
|
|
|
case GL_NEAREST_MIPMAP_LINEAR:
|
|
|
|
case GL_NEAREST_MIPMAP_NEAREST:
|
|
|
|
case GL_LINEAR_MIPMAP_LINEAR:
|
|
|
|
case GL_LINEAR_MIPMAP_NEAREST:
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
|
|
|
return;
|
|
|
|
}
|
2018-05-11 14:39:28 +00:00
|
|
|
}
|
2018-08-09 08:32:29 +00:00
|
|
|
active->minFilter = param;
|
|
|
|
break;
|
2018-05-11 14:39:28 +00:00
|
|
|
case GL_TEXTURE_WRAP_S:
|
|
|
|
switch(param) {
|
|
|
|
case GL_CLAMP:
|
|
|
|
active->uv_clamp |= CLAMP_U;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_REPEAT:
|
|
|
|
active->uv_clamp &= ~CLAMP_U;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_TEXTURE_WRAP_T:
|
|
|
|
switch(param) {
|
|
|
|
case GL_CLAMP:
|
|
|
|
active->uv_clamp |= CLAMP_V;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_REPEAT:
|
|
|
|
active->uv_clamp &= ~CLAMP_V;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
break;
|
|
|
|
case GL_SHARED_TEXTURE_BANK_KOS:
|
|
|
|
active->shared_bank = param;
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-11 14:39:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 14:01:13 +00:00
|
|
|
|
2022-01-18 17:23:30 +00:00
|
|
|
void APIENTRY glTexParameterf(GLenum target, GLenum pname, GLfloat param) {
|
2019-02-28 19:21:16 +00:00
|
|
|
glTexParameteri(target, pname, (GLint) param);
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:01:13 +00:00
|
|
|
GLAPI void APIENTRY glColorTableEXT(GLenum target, GLenum internalFormat, GLsizei width, GLenum format, GLenum type, const GLvoid *data) {
|
2019-03-13 15:14:09 +00:00
|
|
|
GLint validTargets[] = {
|
2019-03-10 11:18:56 +00:00
|
|
|
GL_TEXTURE_2D,
|
|
|
|
GL_SHARED_TEXTURE_PALETTE_EXT,
|
|
|
|
GL_SHARED_TEXTURE_PALETTE_0_KOS,
|
|
|
|
GL_SHARED_TEXTURE_PALETTE_1_KOS,
|
|
|
|
GL_SHARED_TEXTURE_PALETTE_2_KOS,
|
|
|
|
GL_SHARED_TEXTURE_PALETTE_3_KOS,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2019-03-13 15:14:09 +00:00
|
|
|
GLint validInternalFormats[] = {GL_RGB8, GL_RGBA8, 0};
|
|
|
|
GLint validFormats[] = {GL_RGB, GL_RGBA, 0};
|
|
|
|
GLint validTypes[] = {GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, 0};
|
2018-09-20 14:01:13 +00:00
|
|
|
|
|
|
|
if(_glCheckValidEnum(target, validTargets, __func__) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_glCheckValidEnum(internalFormat, validInternalFormats, __func__) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_glCheckValidEnum(format, validFormats, __func__) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_glCheckValidEnum(type, validTypes, __func__) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:48:26 +00:00
|
|
|
/* Only allow up to 256 colours in a palette */
|
2019-02-21 20:35:18 +00:00
|
|
|
if(width > 256 || width == 0) {
|
2018-09-20 15:48:26 +00:00
|
|
|
_glKosThrowError(GL_INVALID_VALUE, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 19:49:28 +00:00
|
|
|
GLint sourceStride = _determineStride(format, type);
|
|
|
|
|
|
|
|
assert(sourceStride > -1);
|
2018-09-20 14:01:13 +00:00
|
|
|
|
|
|
|
TextureConversionFunc convert = _determineConversion(
|
|
|
|
GL_RGBA8, /* We always store palettes in this format */
|
|
|
|
format,
|
|
|
|
type
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!convert) {
|
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-21 21:58:31 +00:00
|
|
|
TexturePalette* palette = NULL;
|
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
/* Custom extension - allow uploading to one of 4 custom palettes */
|
|
|
|
if(target == GL_SHARED_TEXTURE_PALETTE_EXT || target == GL_SHARED_TEXTURE_PALETTE_0_KOS) {
|
|
|
|
palette = SHARED_PALETTES[0];
|
|
|
|
} else if(target == GL_SHARED_TEXTURE_PALETTE_1_KOS) {
|
|
|
|
palette = SHARED_PALETTES[1];
|
|
|
|
} else if(target == GL_SHARED_TEXTURE_PALETTE_2_KOS) {
|
|
|
|
palette = SHARED_PALETTES[2];
|
|
|
|
} else if(target == GL_SHARED_TEXTURE_PALETTE_3_KOS) {
|
|
|
|
palette = SHARED_PALETTES[3];
|
2018-09-20 14:01:13 +00:00
|
|
|
} else {
|
2019-03-03 19:06:01 +00:00
|
|
|
TextureObject* active = _glGetBoundTexture();
|
2019-02-21 21:58:31 +00:00
|
|
|
if(!active->palette) {
|
2019-03-09 14:32:50 +00:00
|
|
|
active->palette = _initTexturePalette();
|
2019-02-21 21:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
palette = active->palette;
|
2018-09-20 14:01:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 19:49:28 +00:00
|
|
|
assert(palette);
|
|
|
|
|
2019-02-21 21:58:31 +00:00
|
|
|
if(target) {
|
2019-03-08 21:36:48 +00:00
|
|
|
free(palette->data);
|
2019-02-21 21:58:31 +00:00
|
|
|
palette->data = NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-09 14:32:50 +00:00
|
|
|
if(palette->bank > -1) {
|
|
|
|
_glReleasePaletteSlot(palette->bank, palette->size);
|
|
|
|
palette->bank = -1;
|
|
|
|
}
|
|
|
|
|
2019-03-08 21:36:48 +00:00
|
|
|
palette->data = (GLubyte*) malloc(width * 4);
|
2019-02-21 21:58:31 +00:00
|
|
|
palette->format = format;
|
|
|
|
palette->width = width;
|
2019-03-09 14:32:50 +00:00
|
|
|
palette->size = (width > 16) ? 256 : 16;
|
|
|
|
assert(palette->size == 16 || palette->size == 256);
|
|
|
|
|
|
|
|
palette->bank = _glGenPaletteSlot(palette->size);
|
|
|
|
|
|
|
|
if(palette->bank < 0) {
|
|
|
|
/* We ran out of slots! */
|
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
|
|
|
|
|
|
|
free(palette->data);
|
|
|
|
palette->format = palette->width = palette->size = 0;
|
|
|
|
return;
|
|
|
|
}
|
2018-09-20 14:01:13 +00:00
|
|
|
|
|
|
|
GLubyte* src = (GLubyte*) data;
|
2019-02-21 21:58:31 +00:00
|
|
|
GLubyte* dst = (GLubyte*) palette->data;
|
2019-03-03 19:49:28 +00:00
|
|
|
|
|
|
|
assert(src);
|
|
|
|
assert(dst);
|
2018-09-20 14:01:13 +00:00
|
|
|
|
|
|
|
/* Transform and copy the source palette to the texture */
|
|
|
|
GLushort i = 0;
|
|
|
|
for(; i < width; ++i) {
|
2018-09-22 19:45:17 +00:00
|
|
|
convert(src, dst);
|
2018-09-20 14:01:13 +00:00
|
|
|
|
|
|
|
src += sourceStride;
|
|
|
|
dst += 4;
|
|
|
|
}
|
2019-03-03 18:54:25 +00:00
|
|
|
|
2019-03-10 11:18:56 +00:00
|
|
|
_glApplyColorTable(palette);
|
2018-09-20 14:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glColorSubTableEXT(GLenum target, GLsizei start, GLsizei count, GLenum format, GLenum type, const GLvoid *data) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(start);
|
|
|
|
_GL_UNUSED(count);
|
|
|
|
_GL_UNUSED(format);
|
|
|
|
_GL_UNUSED(type);
|
|
|
|
_GL_UNUSED(data);
|
2018-09-20 14:02:22 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2018-09-20 14:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glGetColorTableEXT(GLenum target, GLenum format, GLenum type, GLvoid *data) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(format);
|
|
|
|
_GL_UNUSED(type);
|
|
|
|
_GL_UNUSED(data);
|
2018-09-20 14:02:22 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2018-09-20 14:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glGetColorTableParameterivEXT(GLenum target, GLenum pname, GLint *params) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(pname);
|
|
|
|
_GL_UNUSED(params);
|
2018-09-20 14:02:22 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2018-09-20 14:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glGetColorTableParameterfvEXT(GLenum target, GLenum pname, GLfloat *params) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(pname);
|
|
|
|
_GL_UNUSED(params);
|
2018-09-20 14:02:22 +00:00
|
|
|
_glKosThrowError(GL_INVALID_OPERATION, __func__);
|
2018-09-20 14:01:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 12:19:41 +00:00
|
|
|
GLAPI void APIENTRY glTexSubImage2D(
|
|
|
|
GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
|
|
|
GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(level);
|
|
|
|
_GL_UNUSED(xoffset);
|
|
|
|
_GL_UNUSED(yoffset);
|
2020-05-19 20:27:34 +00:00
|
|
|
_GL_UNUSED(width);
|
|
|
|
_GL_UNUSED(height);
|
|
|
|
_GL_UNUSED(format);
|
|
|
|
_GL_UNUSED(type);
|
|
|
|
_GL_UNUSED(pixels);
|
2020-03-05 20:09:19 +00:00
|
|
|
assert(0 && "Not Implemented");
|
2019-03-10 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glCopyTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(level);
|
|
|
|
_GL_UNUSED(xoffset);
|
|
|
|
_GL_UNUSED(yoffset);
|
2020-05-19 20:27:34 +00:00
|
|
|
_GL_UNUSED(x);
|
|
|
|
_GL_UNUSED(y);
|
|
|
|
_GL_UNUSED(width);
|
|
|
|
_GL_UNUSED(height);
|
2020-03-05 20:09:19 +00:00
|
|
|
assert(0 && "Not Implemented");
|
2019-03-10 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glCopyTexSubImage1D(GLenum target, GLint level, GLint xoffset, GLint x, GLint y, GLsizei width) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(level);
|
|
|
|
_GL_UNUSED(xoffset);
|
|
|
|
_GL_UNUSED(x);
|
|
|
|
_GL_UNUSED(y);
|
2020-05-19 20:27:34 +00:00
|
|
|
_GL_UNUSED(width);
|
2020-03-05 20:09:19 +00:00
|
|
|
assert(0 && "Not Implemented");
|
2019-03-10 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glCopyTexImage2D(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(level);
|
|
|
|
_GL_UNUSED(internalformat);
|
2020-05-19 20:27:34 +00:00
|
|
|
_GL_UNUSED(x);
|
|
|
|
_GL_UNUSED(y);
|
|
|
|
_GL_UNUSED(width);
|
|
|
|
_GL_UNUSED(height);
|
|
|
|
_GL_UNUSED(border);
|
2020-03-05 20:09:19 +00:00
|
|
|
assert(0 && "Not Implemented");
|
2019-03-10 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glCopyTexImage1D(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLint border) {
|
2020-03-05 20:09:19 +00:00
|
|
|
_GL_UNUSED(target);
|
|
|
|
_GL_UNUSED(level);
|
|
|
|
_GL_UNUSED(internalformat);
|
2020-05-19 20:27:34 +00:00
|
|
|
_GL_UNUSED(x);
|
|
|
|
_GL_UNUSED(y);
|
|
|
|
_GL_UNUSED(width);
|
|
|
|
_GL_UNUSED(border);
|
2020-03-05 20:09:19 +00:00
|
|
|
assert(0 && "Not Implemented");
|
2019-03-10 12:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI void APIENTRY glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels) {
|
2020-05-19 20:27:34 +00:00
|
|
|
_GL_UNUSED(x);
|
|
|
|
_GL_UNUSED(y);
|
|
|
|
_GL_UNUSED(width);
|
|
|
|
_GL_UNUSED(height);
|
|
|
|
_GL_UNUSED(format);
|
|
|
|
_GL_UNUSED(type);
|
|
|
|
_GL_UNUSED(pixels);
|
2020-03-05 20:09:19 +00:00
|
|
|
assert(0 && "Not Implemented");
|
2019-03-10 12:19:41 +00:00
|
|
|
}
|
2021-02-19 21:28:31 +00:00
|
|
|
|
|
|
|
GLuint _glFreeTextureMemory() {
|
|
|
|
return yalloc_count_free(YALLOC_BASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint _glUsedTextureMemory() {
|
|
|
|
return YALLOC_SIZE - _glFreeTextureMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint _glFreeContiguousTextureMemory() {
|
|
|
|
return yalloc_count_continuous(YALLOC_BASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLAPI GLvoid APIENTRY glDefragmentTextureMemory_KOS(void) {
|
|
|
|
yalloc_defrag_start(YALLOC_BASE);
|
|
|
|
|
|
|
|
GLuint id;
|
|
|
|
|
|
|
|
/* Replace all texture pointers */
|
|
|
|
for(id = 0; id < MAX_TEXTURE_COUNT; id++){
|
|
|
|
if(glIsTexture(id)){
|
|
|
|
TextureObject* txr = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, id);
|
|
|
|
txr->data = yalloc_defrag_address(YALLOC_BASE, txr->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
yalloc_defrag_commit(YALLOC_BASE);
|
|
|
|
}
|
2022-01-31 08:29:00 +00:00
|
|
|
|
|
|
|
GLAPI void APIENTRY glGetTexImage(GLenum tex, GLint lod, GLenum format, GLenum type, GLvoid* img) {
|
|
|
|
_GL_UNUSED(tex);
|
|
|
|
_GL_UNUSED(lod);
|
|
|
|
_GL_UNUSED(format);
|
|
|
|
_GL_UNUSED(type);
|
|
|
|
_GL_UNUSED(img);
|
|
|
|
}
|