Skip to content
This repository
Browse code

Add an adjustment to each matrix. This allows a scale/translate to be

applied to each matrix before it is loaded into hardware.  This is useful
for implementing flipped textures, and also to deal with the difference
between OpenGL and PSP in interpreting integer vertex and texture coords
(OpenGL treats them as plain integers, PSP treats them as -1 - 1).
  • Loading branch information...
commit 6720419c973c79c7d3fc062d900e256e0d7ae9a9 1 parent 7fe2a0d
authored February 18, 2006
5  eglCreateContext.c
@@ -20,12 +20,15 @@ static GLboolean init_matrix_stack(struct pspgl_matrix_stack *mstk, int limit, u
20 20
 	mstk->depth = 0;
21 21
 	mstk->flags = flags | MF_DIRTY;
22 22
 
  23
+	mstk->scale[0] = mstk->scale[1] = mstk->scale[2] = mstk->scale[3] = 1.0f;
  24
+	mstk->trans[0] = mstk->trans[1] = mstk->trans[2] = mstk->trans[3] = 0.0f;
  25
+
23 26
 	return GL_TRUE;
24 27
 }
25 28
 
26 29
 EGLContext eglCreateContext (EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
27 30
 {
28  
-	struct pspgl_context *ctx = malloc(sizeof(struct pspgl_context));
  31
+	struct pspgl_context *ctx = memalign(VFPU_ALIGNMENT, sizeof(struct pspgl_context));
29 32
 
30 33
 	if (!ctx)
31 34
 		goto out_error;
6  glTexCoordPointer.c
@@ -28,6 +28,12 @@ void glTexCoordPointer (GLint size, GLenum type, GLsizei stride, const GLvoid *p
28 28
 		pointer, __pspgl_bufferobj_deref(pspgl_curctx->vertex_array.arraybuffer, (void *)pointer),
29 29
 		size, type, stride, native);
30 30
 
  31
+	/* If we're changed vertex formats, then mark the texture
  32
+	   matrix as dirty, so that any adjustment for this format can
  33
+	   be applied. */
  34
+	if (va->type != type)
  35
+		pspgl_curctx->texture_stack.flags |= MF_DIRTY;
  36
+
31 37
 	va->size = size;
32 38
 	va->type = type;
33 39
 	va->stride = stride;
6  glVertexPointer.c
@@ -27,6 +27,12 @@ void glVertexPointer (GLint size, GLenum type, GLsizei stride, const GLvoid *poi
27 27
 		pointer, __pspgl_bufferobj_deref(pspgl_curctx->vertex_array.arraybuffer, (void *)pointer),
28 28
 		size, type, stride, native);
29 29
 
  30
+	/* If we're changed vertex formats, then mark the modelview matrix
  31
+	   as dirty, so that any adjustment for this format can be
  32
+	   applied. */
  33
+	if (va->type != type)
  34
+		pspgl_curctx->modelview_stack.flags |= MF_DIRTY;
  35
+
30 36
 	va->size = size;
31 37
 	va->type = type;
32 38
 	va->stride = stride;
112  pspgl_context.c
@@ -17,7 +17,6 @@ void __pspgl_context_writereg (struct pspgl_context *c, uint32_t cmd,
17 17
 	}
18 18
 }
19 19
 
20  
-
21 20
 void __pspgl_context_writereg_masked (struct pspgl_context *c, uint32_t cmd,
22 21
 				      uint32_t argi, uint32_t mask)
23 22
 {
@@ -85,6 +84,7 @@ static void flush_matrix(struct pspgl_context *c, unsigned opcode, int index,
85 84
 			 struct pspgl_matrix_stack *stk)
86 85
 {
87 86
 	const GLfloat *m;
  87
+	GLfloat adjusted[16];
88 88
 
89 89
 	if (!(stk->flags & MF_DIRTY))
90 90
 		return;
@@ -98,6 +98,45 @@ static void flush_matrix(struct pspgl_context *c, unsigned opcode, int index,
98 98
 		m = stk->stack[stk->depth].mat;
99 99
 	}
100 100
 
  101
+	if (stk->flags & MF_ADJUST) {
  102
+		/* Apply adjustment to the matrix.  This is used for
  103
+		   scaling OpenGL integral vertex and texcoord data to
  104
+		   PSP integral vertex/texture coords (OpenGL
  105
+		   interprets them as literal integers, but the PSP
  106
+		   treats them as being in the range [-1, 1]).  Also
  107
+		   used for flipping inverted textures. */
  108
+		pspvfpu_use_matrices(c->vfpu_context, 0, VMAT4 | VMAT5 | VMAT6);
  109
+
  110
+		asm volatile("vmzero.t	m500\n"
  111
+
  112
+			     /* original matrix */
  113
+			     "lv.q	c600,  0 + %3\n"
  114
+			     "lv.q	c610, 16 + %3\n"
  115
+			     "lv.q	c620, 32 + %3\n"
  116
+			     "lv.q	c630, 48 + %3\n"
  117
+
  118
+			     /* translate */
  119
+			     "lv.q	c530, 0 + %2\n"
  120
+
  121
+			     /* scale - doesn't seem to be a way of
  122
+				loading a diagonal in one go... */
  123
+			     "lv.s	s500, 0 + %1\n"
  124
+			     "lv.s	s511, 4 + %1\n"
  125
+			     "lv.s	s522, 8 + %1\n"
  126
+			     
  127
+			     "vidt.q	r503\n"
  128
+
  129
+			     "vmmul.q	m400, m600, m500\n"
  130
+
  131
+			     "usv.q	c400,  0 + %0\n"
  132
+			     "usv.q	c410, 16 + %0\n"
  133
+			     "usv.q	c420, 32 + %0\n"
  134
+			     "usv.q	c430, 48 + %0\n"
  135
+			     : "=m" (adjusted) : "m" (stk->scale[0]), "m" (stk->trans[0]), "m" (m[0]));
  136
+
  137
+		m = adjusted;
  138
+	}
  139
+
101 140
 	/*
102 141
 	  Different matrices skip different rows:
103 142
 	  PROJ is a full 4x4 projective matrix
@@ -206,11 +245,74 @@ void __pspgl_context_render_setup(struct pspgl_context *c, unsigned vtxfmt,
206 245
 			}
207 246
 		}
208 247
 
209  
-		if (c->texture_stack.stack[c->texture_stack.depth].flags & MF_IDENTITY)
210  
-			__pspgl_context_writereg(c, CMD_TEXMAPMODE, (GE_UV << 8) | GE_TEXTURE_COORDS);
211  
-		else
212  
-			__pspgl_context_writereg(c, CMD_TEXMAPMODE, (GE_UV << 8) | GE_TEXTURE_MATRIX);
  248
+		__pspgl_context_writereg(c, CMD_TEXMAPMODE, (GE_UV << 8) | GE_TEXTURE_MATRIX);
  249
+
  250
+		/* If we're using an integral texcoord format (byte or
  251
+		   short), then we need to add a scaling factor.  GL
  252
+		   interprets the texcoord as a simple integer,
  253
+		   whereas the PSP treats it as number scaled (-1, 1).
  254
+
  255
+		   This is combined with flipping for flipped textures.
  256
+		*/
  257
+		{
  258
+			struct pspgl_matrix_stack *tm = &c->texture_stack;
  259
+			float su = 1.f, sv = 1.f;
  260
+			float tu = 0.f, tv = 0.f;
  261
+
  262
+			switch(vtxfmt & GE_TEXTURE_SHIFT(3)) {
  263
+			case GE_TEXTURE_8BIT:
  264
+				su = 127.f;
  265
+				sv = 127.f;
  266
+				break;
  267
+
  268
+			case GE_TEXTURE_16BIT:
  269
+				su = 32767.f;
  270
+				sv = 32767.f;
  271
+				break;
  272
+			}
  273
+
  274
+			if (tobj->flags & TOF_FLIPPED) {
  275
+				sv = -sv;
  276
+				tv = 1.f;
  277
+			}
  278
+
  279
+			tm->flags &= ~MF_ADJUST;
  280
+			if (su != 1 || sv != 1 || tu != 0 || tv != 0) {
  281
+				tm->flags |= MF_ADJUST;
  282
+				tm->scale[0] = su;
  283
+				tm->scale[1] = sv;
  284
+				tm->scale[2] = 1.f;
  285
+				tm->trans[0] = tu;
  286
+				tm->trans[1] = tv;
  287
+				tm->trans[2] = 0.f;
  288
+			}
  289
+		}
  290
+	}
213 291
 
  292
+	/* If the program is used 8 or 16 bit vertex coords, we need
  293
+	   to scale them to match what the PSP hardware wants.  OpenGL
  294
+	   defines integral coords as just signed integers, but the
  295
+	   PSP interprets them as scaled floats in the range -1 - 1.
  296
+	   Therefore, we need to apply an appropriate scale factor as
  297
+	   a backend adjustment.  The modelview matrix is the first one to
  298
+	   be applied to a vertex, so put the scaling there (XXX
  299
+	   except for bones when skinning is enabled...) */
  300
+	c->modelview_stack.flags &= ~MF_ADJUST;
  301
+	
  302
+	switch(vtxfmt & GE_VERTEX_SHIFT(3)) {
  303
+	case GE_VERTEX_8BIT:
  304
+		c->modelview_stack.scale[0] = 127.f;
  305
+		c->modelview_stack.scale[1] = 127.f;
  306
+		c->modelview_stack.scale[2] = 127.f;
  307
+		c->modelview_stack.flags |= MF_ADJUST;
  308
+		break;
  309
+
  310
+	case GE_VERTEX_16BIT:
  311
+		c->modelview_stack.scale[0] = 32767.f;
  312
+		c->modelview_stack.scale[1] = 32767.f;
  313
+		c->modelview_stack.scale[2] = 32767.f;
  314
+		c->modelview_stack.flags |= MF_ADJUST;
  315
+		break;
214 316
 	}
215 317
 
216 318
 	__pspgl_context_writereg(c, CMD_VERTEXTYPE, vtxfmt);
6  pspgl_internal.h
@@ -39,6 +39,7 @@ struct pspgl_shared_context {
39 39
 #define MF_DISABLED	(1<<1)	/* always load with identity (stack) */
40 40
 #define MF_IDENTITY	(1<<2)	/* is identity matrix (matrix) */
41 41
 #define MF_VFPU		(1<<3)	/* matrix stack top is in VFPU (stack) */
  42
+#define MF_ADJUST	(1<<4)	/* some pre-use adjustments to apply */
42 43
 
43 44
 #define VFPU_STACKTOP	VMAT7	/* use matrix 7 for top-of-stack */
44 45
 
@@ -51,7 +52,12 @@ struct pspgl_matrix {
51 52
 };
52 53
 
53 54
 struct pspgl_matrix_stack {
  55
+	/* adjustments to be applied before use */
  56
+	float scale[4] __attribute__((aligned(VFPU_ALIGNMENT)));
  57
+	float trans[4] __attribute__((aligned(VFPU_ALIGNMENT)));
  58
+
54 59
 	struct pspgl_matrix *stack;
  60
+
55 61
 	unsigned limit;
56 62
 	unsigned depth;
57 63
 	unsigned flags;
11  pspgl_texobj.c
@@ -551,11 +551,8 @@ void __pspgl_update_texenv(struct pspgl_texobj *tobj)
551 551
 		__pspgl_context_writereg_masked(pspgl_curctx, CMD_TEXENV_FUNC,
552 552
 						(fmt->flags & TF_ALPHA) ? GE_TEXENV_RGBA : GE_TEXENV_RGB, 0x100);
553 553
 
554  
-	if (tobj->flags & TOF_FLIPPED) {
555  
-		sendCommandf(CMD_TEXTURE_SV, -1.0f);
556  
-		sendCommandf(CMD_TEXTURE_TV, 1.0f);
557  
-	} else {
558  
-		sendCommandf(CMD_TEXTURE_SV, 1.0f);
559  
-		sendCommandf(CMD_TEXTURE_TV, 0.0f);
560  
-	}
  554
+	/* If we've switched textures or changed the flip status, mark
  555
+	   the texture matrix as dirty so that any adjustment can be
  556
+	   applied. */
  557
+	pspgl_curctx->texture_stack.flags |= MF_DIRTY;
561 558
 }

0 notes on commit 6720419

Please sign in to comment.
Something went wrong with that request. Please try again.