Fixes for rendering and new samples to test

This commit is contained in:
Hayden K 2019-03-08 15:32:29 -05:00
parent 4e29678866
commit 5eea65c31f
9 changed files with 221 additions and 103 deletions

View File

@ -1070,7 +1070,7 @@ static void push(PVRHeader* header, ClipVertex* output, const GLsizei count, Pol
cxt.list_type = activePolyList->list_type;
_glUpdatePVRTextureContext(&cxt, textureUnit);
pvr_poly_compile(&header->hdr, &cxt);
/* Post-process the vertex list */

View File

@ -179,6 +179,9 @@ void _glUpdatePVRTextureContext(pvr_poly_cxt_t* context, GLshort textureUnit) {
if(tx1->isCompressed && _glIsMipmapComplete(tx1)) {
enableMipmaps = GL_TRUE;
}
if(tx1->isPaletted){
enableMipmaps = GL_FALSE;
}
if(enableMipmaps) {
if(tx1->minFilter == GL_LINEAR_MIPMAP_NEAREST) {
@ -194,6 +197,9 @@ void _glUpdatePVRTextureContext(pvr_poly_cxt_t* context, GLshort textureUnit) {
if(tx1->minFilter == GL_LINEAR && tx1->magFilter == GL_LINEAR) {
filter = PVR_FILTER_BILINEAR;
}
if(tx1->isPaletted){
filter = PVR_FILTER_NONE;
}
}
/* If we don't have complete mipmaps, and yet mipmapping was enabled, we disable texturing.
@ -303,7 +309,7 @@ GLAPI void APIENTRY glEnable(GLenum cap) {
SHARED_PALETTE_ENABLED = GL_TRUE;
/* Apply the texture palette if necessary */
_glApplyColorTable();
//_glApplyColorTable(); //@Todo: Actually Dont.
}
break;
case GL_LIGHT0:
@ -357,7 +363,7 @@ GLAPI void APIENTRY glDisable(GLenum cap) {
SHARED_PALETTE_ENABLED = GL_FALSE;
/* Restore whatever palette may exist on a bound texture */
_glApplyColorTable();
//_glApplyColorTable(); //@Todo: Actually Dont.
}
break;
case GL_LIGHT0:

View File

@ -19,6 +19,7 @@ static NamedArray TEXTURE_OBJECTS;
static GLubyte ACTIVE_TEXTURE = 0;
static TexturePalette* SHARED_PALETTE = NULL;
static GLbyte CURRENT_PALETTE = 0;
static GLuint _determinePVRFormat(GLint internalFormat, GLenum type);
@ -27,6 +28,7 @@ static GLuint _determinePVRFormat(GLint internalFormat, GLenum type);
static TexturePalette* last_bound_palette = NULL;
void _glApplyColorTable() {
/*
* FIXME:
*
@ -278,7 +280,7 @@ void APIENTRY glBindTexture(GLenum target, GLuint texture) {
TEXTURE_UNITS[ACTIVE_TEXTURE] = (TextureObject*) named_array_get(&TEXTURE_OBJECTS, texture);
/* Apply the texture palette if necessary */
_glApplyColorTable();
//_glApplyColorTable(); //@Todo: Actually Dont.
} else {
TEXTURE_UNITS[ACTIVE_TEXTURE] = NULL;
}
@ -551,11 +553,7 @@ static GLuint _determinePVRFormat(GLint internalFormat, GLenum type) {
case GL_COMPRESSED_ARGB_1555_VQ_MIPMAP_TWID_KOS:
return PVR_TXRFMT_ARGB1555 | PVR_TXRFMT_TWIDDLED | PVR_TXRFMT_VQ_ENABLE;
case GL_COLOR_INDEX8_EXT:
if(type == GL_UNSIGNED_BYTE_TWID_KOS) {
return PVR_TXRFMT_PAL8BPP | PVR_TXRFMT_TWIDDLED;
} else {
return PVR_TXRFMT_PAL8BPP | PVR_TXRFMT_NONTWIDDLED;
}
return PVR_TXRFMT_PAL8BPP | PVR_TXRFMT_8BPP_PAL(CURRENT_PALETTE);
default:
return 0;
}
@ -570,11 +568,11 @@ static inline void _rgba8888_to_argb4444(const GLubyte* source, GLubyte* dest) {
static inline void _rgba8888_to_rgba8888(const GLubyte* source, GLubyte* dest) {
/* Noop */
GLubyte* dst = (GLubyte*) dest;
dst[0] = source[0];
dst[1] = source[1];
dst[2] = source[2];
dst[3] = source[3];
//GLubyte* dst = (GLubyte*) dest;
dest[0] = source[0];
dest[1] = source[1];
dest[2] = source[2];
dest[3] = source[3];
}
static inline void _rgba8888_to_rgb565(const GLubyte* source, GLubyte* dest) {
@ -583,11 +581,11 @@ static inline void _rgba8888_to_rgb565(const GLubyte* source, GLubyte* dest) {
static inline void _rgb888_to_rgba8888(const GLubyte* source, GLubyte* dest) {
/* Noop */
GLubyte* dst = (GLubyte*) dest;
dst[0] = source[0];
dst[1] = source[1];
dst[2] = source[2];
dst[3] = 255;
//GLubyte* dst = (GLubyte*) dest;
dest[0] = source[0];
dest[1] = source[1];
dest[2] = source[2];
dest[3] = 255;
}
static inline void _rgb888_to_rgb565(const GLubyte* source, GLubyte* dest) {
@ -841,12 +839,50 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
/* Let's assume we need to convert */
GLboolean needsConversion = GL_TRUE;
GLubyte* targetData = _glGetMipmapLocation(active, level);
assert(targetData);
/*
* 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
*/
if(format == GL_COLOR_INDEX) {
/* Don't convert color indexes */
if(type == GL_UNSIGNED_BYTE_TWID_KOS){
} else {
/* Don't convert color indexes */
/* 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) )
#define MIN(a, b) ( (a)<(b)? (a):(b) )
uint32 x, y, yout, min, mask, invert;
min = MIN(w, h);
mask = min - 1;
invert = 0;
uint8 * pixels;
uint16 * vtex;
pixels = (uint8 *) data;
vtex = (uint16*)targetData;
for(y = 0; y < h; y += 2) {
if(!invert)
yout = y;
else
yout = ((h - 1) - 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);
}
}
data = NULL;
}
needsConversion = GL_FALSE;
} else if(format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && internalFormat == GL_RGBA) {
needsConversion = GL_FALSE;
@ -862,9 +898,6 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
needsConversion = GL_FALSE;
}
GLubyte* targetData = _glGetMipmapLocation(active, level);
assert(targetData);
if(!data) {
/* No data? Do nothing! */
return;
@ -873,40 +906,8 @@ void APIENTRY glTexImage2D(GLenum target, GLint level, GLint internalFormat,
assert(data);
assert(bytes);
/* 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) )
#define MIN(a, b) ( (a)<(b)? (a):(b) )
uint32 x, y, yout, min, mask, invert;
min = MIN(w, h);
mask = min - 1;
invert = 0;
uint8 * pixels;
uint16 * vtex;
pixels = (uint8 *) data;
vtex = (uint16*)targetData;
for(y = 0; y < h; y += 2) {
if(!invert)
yout = y;
else
yout = ((h - 1) - 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);
}
}
active->color = PVR_TXRFMT_PAL8BPP | PVR_TXRFMT_TWIDDLED;
/* No conversion? Just copy the data, and the pvr_format is correct */
//sq_cpy(targetData, data, bytes);
sq_cpy(targetData, data, bytes);
return;
} else {
TextureConversionFunc convert = _determineConversion(
@ -1081,11 +1082,13 @@ GLAPI void APIENTRY glColorTableEXT(GLenum target, GLenum internalFormat, GLsize
assert(palette);
if(target) {
pvr_mem_free(palette->data);
//pvr_mem_free(palette->data);
free(palette->data);
palette->data = NULL;
}
palette->data = (GLubyte*) pvr_mem_malloc(width * 4);
//palette->data = (GLubyte*) pvr_mem_malloc(width * 4);
palette->data = (GLubyte*) malloc(width * 4);
palette->format = format;
palette->width = width;
@ -1130,3 +1133,23 @@ GLAPI void APIENTRY glGetColorTableParameterfvEXT(GLenum target, GLenum pname, G
_glKosPrintError();
}
void APIENTRY glKosSetPalette( GLenum palette ) {
switch(palette) {
case GL_EXT_PALLETE_0_8BPP:
CURRENT_PALETTE = 0;
break;
case GL_EXT_PALLETE_1_8BPP:
CURRENT_PALETTE = 1;
break;
case GL_EXT_PALLETE_2_8BPP:
CURRENT_PALETTE = 2;
break;
case GL_EXT_PALLETE_3_8BPP:
CURRENT_PALETTE = 3;
break;
}
}
GLbyte APIENTRY glKosGetPalette() {
return CURRENT_PALETTE;
}

View File

@ -34,9 +34,13 @@ __BEGIN_DECLS
#define GL_UNSIGNED_BYTE_TWID_KOS 0xEEFB
#define GL_EXT_PALLETE_0_8BPP 0xEFA0
#define GL_EXT_PALLETE_1_8BPP 0xEFA1
#define GL_EXT_PALLETE_2_8BPP 0xEFA2
#define GL_EXT_PALLETE_3_8BPP 0xEFA3
GLAPI void APIENTRY glKosSwapBuffers();
GLAPI void APIENTRY glKosSetPalette( GLenum palette );
__END_DECLS

View File

@ -0,0 +1,32 @@
TARGET = paletted.elf
OBJS = main.o
LIBS = -lGLdc
all: rm-elf $(TARGET)
include $(KOS_BASE)/Makefile.rules
clean:
-rm -f $(TARGET) $(OBJS) romdisk.* 1ST_READ.BIN paletted.bin
rm-elf:
-rm -f $(TARGET) romdisk.* 1ST_READ.BIN paletted.bin
$(TARGET): $(OBJS) romdisk.o
$(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \
$(OBJS) romdisk.o $(OBJEXTRA) $(LIBS) -lm -lkosutils $(KOS_LIBS)
romdisk.img:
$(KOS_GENROMFS) -f romdisk.img -d romdisk -v
romdisk.o: romdisk.img
$(KOS_BASE)/utils/bin2o/bin2o romdisk.img romdisk romdisk.o
run: $(TARGET)
$(KOS_LOADER) $(TARGET)
dist: $(TARGET)
rm -f $(OBJS) romdisk.o romdisk.img
$(KOS_STRIP) $(TARGET)
strip-bin paletted
scramble paletted.bin 1ST_READ.BIN

81
samples/gl_polygon/main.c Normal file
View File

@ -0,0 +1,81 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
#include <GL/glkos.h>
extern uint8 romdisk[];
KOS_INIT_ROMDISK(romdisk);
/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
}
/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
if (Height == 0) // Prevent A Divide By Zero If The Window Is Too Small
Height = 1;
glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}
/* The main drawing function. */
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-5.0f); // move 5 units into the screen.
glColor3f(1.0,1.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.1, 0.1,0.0);
glVertex3f(0.6, 0.1,0.0);
glVertex3f(0.8,0.3,0.0);
glVertex3f(0.6,0.6,0.0);
glVertex3f(0.1,0.6,0.0);
glVertex3f(0.0,0.3,0.0);
glEnd(); // done with the polygon.
// swap buffers to display, since we're double buffered.
glKosSwapBuffers();
}
int main(int argc, char **argv)
{
glKosInit();
InitGL(640, 480);
ReSizeGLScene(640, 480);
while(1) {
DrawGLScene();
}
return 0;
}

View File

@ -339,6 +339,8 @@ void DrawGLScene()
int main(int argc, char **argv)
{
vid_set_mode(DM_640x480, PM_RGB565);
glKosInit();
InitGL(640, 480);

View File

@ -1,5 +1,6 @@
TARGET = paletted.elf
OBJS = main.o
LIBS = -lGLdc
all: rm-elf $(TARGET)
@ -13,7 +14,7 @@ rm-elf:
$(TARGET): $(OBJS) romdisk.o
$(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \
$(OBJS) romdisk.o $(OBJEXTRA) -lm -lkosutils $(KOS_LIBS)
$(OBJS) romdisk.o $(OBJEXTRA) $(LIBS) -lm -lkosutils $(KOS_LIBS)
romdisk.img:
$(KOS_GENROMFS) -f romdisk.img -d romdisk -v
@ -24,6 +25,8 @@ romdisk.o: romdisk.img
run: $(TARGET)
$(KOS_LOADER) $(TARGET)
dist:
dist: $(TARGET)
rm -f $(OBJS) romdisk.o romdisk.img
$(KOS_STRIP) $(TARGET)
strip-bin paletted
scramble paletted.bin 1ST_READ.BIN

View File

@ -1,17 +1,21 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if 0
#include "gl.h"
#include "glext.h"
#include "glu.h"
#include "glkos.h"
#else
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
#include <GL/glkos.h>
#endif
extern uint8 romdisk[];
KOS_INIT_ROMDISK(romdisk);
/* floats for x rotation, y rotation, z rotation */
float xrot, yrot, zrot;
/* storage for one texture */
int texture[1];
@ -168,55 +172,18 @@ void DrawGLScene()
glTranslatef(0.0f,0.0f,-5.0f); // move 5 units into the screen.
glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis
glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis
glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate On The Z Axis
glBindTexture(GL_TEXTURE_2D, texture[0]); // choose the texture to use.
glBegin(GL_QUADS); // begin drawing a cube
// Front Face (note that the texture's corners have to match the quad's corners)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd(); // done with the polygon.
xrot+=1.5f; // X Axis Rotation
yrot+=1.5f; // Y Axis Rotation
zrot+=1.5f; // Z Axis Rotation
//
// swap buffers to display, since we're double buffered.
glKosSwapBuffers();