Problem with gu cube sample

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Sarkins
Posts: 2
Joined: Fri Jan 25, 2008 7:11 pm

Problem with gu cube sample

Post by Sarkins »

I try to modify the cube code to make my own mesh code .
For the moment i try to copy the vertices tab in to an other tab and render it. With a normal tab it doesn't work may be the data is not aligned, how to work the code whith this solution.

I try with a dynamic tab (with memalign) and it doesn't work to, one face is not render correctly, i don't understand why ?

Thank for your help.

Code: Select all

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * Copyright (c) 2005 Jesper Svennevid
 */

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <string.h>

#include <pspgu.h>
#include <pspgum.h>

#include "../common/callbacks.h"
#include "../common/vram.h"

PSP_MODULE_INFO&#40;"Cube Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
extern unsigned char logo_start&#91;&#93;;

typedef struct Vertex
&#123;
	float u, v;
	unsigned int color;
	float x,y,z;
&#125;Vertex;

Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; vertices&#91;12*3&#93; =
&#123;
	&#123;0, 0, 0xff7f0000,-1,-1, 1&#125;, // 0
	&#123;1, 0, 0xff7f0000,-1, 1, 1&#125;, // 4
	&#123;1, 1, 0xff7f0000, 1, 1, 1&#125;, // 5

	&#123;0, 0, 0xff7f0000,-1,-1, 1&#125;, // 0
	&#123;1, 1, 0xff7f0000, 1, 1, 1&#125;, // 5
	&#123;0, 1, 0xff7f0000, 1,-1, 1&#125;, // 1

	&#123;0, 0, 0xff7f0000,-1,-1,-1&#125;, // 3
	&#123;1, 0, 0xff7f0000, 1,-1,-1&#125;, // 2
	&#123;1, 1, 0xff7f0000, 1, 1,-1&#125;, // 6

	&#123;0, 0, 0xff7f0000,-1,-1,-1&#125;, // 3
	&#123;1, 1, 0xff7f0000, 1, 1,-1&#125;, // 6
	&#123;0, 1, 0xff7f0000,-1, 1,-1&#125;, // 7

	&#123;0, 0, 0xff007f00, 1,-1,-1&#125;, // 0
	&#123;1, 0, 0xff007f00, 1,-1, 1&#125;, // 3
	&#123;1, 1, 0xff007f00, 1, 1, 1&#125;, // 7

	&#123;0, 0, 0xff007f00, 1,-1,-1&#125;, // 0
	&#123;1, 1, 0xff007f00, 1, 1, 1&#125;, // 7
	&#123;0, 1, 0xff007f00, 1, 1,-1&#125;, // 4

	&#123;0, 0, 0xff007f00,-1,-1,-1&#125;, // 0
	&#123;1, 0, 0xff007f00,-1, 1,-1&#125;, // 3
	&#123;1, 1, 0xff007f00,-1, 1, 1&#125;, // 7

	&#123;0, 0, 0xff007f00,-1,-1,-1&#125;, // 0
	&#123;1, 1, 0xff007f00,-1, 1, 1&#125;, // 7
	&#123;0, 1, 0xff007f00,-1,-1, 1&#125;, // 4

	&#123;0, 0, 0xff00007f,-1, 1,-1&#125;, // 0
	&#123;1, 0, 0xff00007f, 1, 1,-1&#125;, // 1
	&#123;1, 1, 0xff00007f, 1, 1, 1&#125;, // 2

	&#123;0, 0, 0xff00007f,-1, 1,-1&#125;, // 0
	&#123;1, 1, 0xff00007f, 1, 1, 1&#125;, // 2
	&#123;0, 1, 0xff00007f,-1, 1, 1&#125;, // 3

	&#123;0, 0, 0xff00007f,-1,-1,-1&#125;, // 4
	&#123;1, 0, 0xff00007f,-1,-1, 1&#125;, // 7
	&#123;1, 1, 0xff00007f, 1,-1, 1&#125;, // 6

	&#123;0, 0, 0xff00007f,-1,-1,-1&#125;, // 4
	&#123;1, 1, 0xff00007f, 1,-1, 1&#125;, // 6
	&#123;0, 1, 0xff00007f, 1,-1,-1&#125;, // 5
&#125;;

#define BUF_WIDTH &#40;512&#41;
#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	int i;
	//Vertex dataVertex&#91;12*3&#93;;
	Vertex *dataVertex = &#40;Vertex *&#41;memalign&#40;16, 12*3 * sizeof&#40;Vertex&#41;&#41;;
	setupCallbacks&#40;&#41;;

	// setup GU

	void* fbp0 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888&#41;;
	void* fbp1 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888&#41;;
	void* zbp = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444&#41;;

	sceGuInit&#40;&#41;;

	sceGuStart&#40;GU_DIRECT,list&#41;;
	sceGuDrawBuffer&#40;GU_PSM_8888,fbp0,BUF_WIDTH&#41;;
	sceGuDispBuffer&#40;SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH&#41;;
	sceGuDepthBuffer&#40;zbp,BUF_WIDTH&#41;;
	sceGuOffset&#40;2048 - &#40;SCR_WIDTH/2&#41;,2048 - &#40;SCR_HEIGHT/2&#41;&#41;;
	sceGuViewport&#40;2048,2048,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuDepthRange&#40;65535,0&#41;;
	sceGuScissor&#40;0,0,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuEnable&#40;GU_SCISSOR_TEST&#41;;
	sceGuDepthFunc&#40;GU_GEQUAL&#41;;
	sceGuEnable&#40;GU_DEPTH_TEST&#41;;
	sceGuFrontFace&#40;GU_CW&#41;;
	sceGuShadeModel&#40;GU_SMOOTH&#41;;
	sceGuEnable&#40;GU_CULL_FACE&#41;;
	sceGuEnable&#40;GU_TEXTURE_2D&#41;;
	sceGuEnable&#40;GU_CLIP_PLANES&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;

	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuDisplay&#40;GU_TRUE&#41;;

	// run sample

	int val = 0;
	
	for&#40;i=0;i<12*3;i++&#41;&#123;
	
		dataVertex&#91;i&#93;.u = vertices&#91;i&#93;.u;
		dataVertex&#91;i&#93;.v = vertices&#91;i&#93;.v;
		dataVertex&#91;i&#93;.color = vertices&#91;i&#93;.color;
		dataVertex&#91;i&#93;.x = vertices&#91;i&#93;.x;
		dataVertex&#91;i&#93;.y = vertices&#91;i&#93;.y;
		dataVertex&#91;i&#93;.z = vertices&#91;i&#93;.z;
	
	&#125;
	
	while&#40;running&#40;&#41;&#41;
	&#123;
		sceGuStart&#40;GU_DIRECT,list&#41;;

		// clear screen

		sceGuClearColor&#40;0xff554433&#41;;
		sceGuClearDepth&#40;0&#41;;
		sceGuClear&#40;GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT&#41;;

		// setup matrices for cube

		sceGumMatrixMode&#40;GU_PROJECTION&#41;;
		sceGumLoadIdentity&#40;&#41;;
		sceGumPerspective&#40;75.0f,16.0f/9.0f,0.5f,1000.0f&#41;;

		sceGumMatrixMode&#40;GU_VIEW&#41;;
		sceGumLoadIdentity&#40;&#41;;

		sceGumMatrixMode&#40;GU_MODEL&#41;;
		sceGumLoadIdentity&#40;&#41;;
		&#123;
			ScePspFVector3 pos = &#123; 0, 0, -2.5f &#125;;
			ScePspFVector3 rot = &#123; val * 0.79f * &#40;GU_PI/180.0f&#41;, val * 0.98f * &#40;GU_PI/180.0f&#41;, val * 1.32f * &#40;GU_PI/180.0f&#41; &#125;;
			sceGumTranslate&#40;&pos&#41;;
			sceGumRotateXYZ&#40;&rot&#41;;
		&#125;

		// setup texture

		sceGuTexMode&#40;GU_PSM_4444,0,0,0&#41;;
		sceGuTexImage&#40;0,64,64,64,logo_start&#41;;
		sceGuTexFunc&#40;GU_TFX_ADD,GU_TCC_RGB&#41;;
		sceGuTexEnvColor&#40;0xffff00&#41;;
		sceGuTexFilter&#40;GU_LINEAR,GU_LINEAR&#41;;
		sceGuTexScale&#40;1.0f,1.0f&#41;;
		sceGuTexOffset&#40;0.0f,0.0f&#41;;
		sceGuAmbientColor&#40;0xffffffff&#41;;

		// draw cube

		//sceGumDrawArray&#40;GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices&#41;;
		sceGumDrawArray&#40;GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,dataVertex&#41;;

		sceGuFinish&#40;&#41;;
		sceGuSync&#40;0,0&#41;;

		sceDisplayWaitVblankStart&#40;&#41;;
		sceGuSwapBuffers&#40;&#41;;

		val++;
	&#125;
	
	free&#40;dataVertex&#41;;
	sceGuTerm&#40;&#41;;

	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

makefile

Code: Select all

TARGET = cube
OBJS = cube.o logo.o ../common/callbacks.o ../common/vram.o

INCDIR =
CFLAGS = -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LDFLAGS =
LIBS= -lpspgum -lpspgu -lm

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Cube Sample

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak

logo.o&#58; logo.raw
	bin2o -i logo.raw logo.o logo

shadow_gg
Posts: 3
Joined: Fri Oct 07, 2005 6:14 am
Location: Paris, France
Contact:

Post by shadow_gg »

Bcs the vertices of the face you don't see are not yet physically in the RAM, but still in the cache memory, so the GU didn't have the right data to draw.
You should invalidate the cache each time after you fill your dynamic array.
Me I'm using sceKernelDcacheWritebackAll();

Hope it helps
Cordially
Sarkins
Posts: 2
Joined: Fri Jan 25, 2008 7:11 pm

Post by Sarkins »

Thank for your help with sceKernelDcacheWritebackAll(); my code work.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Has anyone tried only flushing a range of the cache? I know on some processors, flushing the whole cache when you don't need to makes the program half the speed or worse. If you only need to flush 256 bytes (for example), why flush all the rest of the cache?
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

J.F. wrote:Has anyone tried only flushing a range of the cache? I know on some processors, flushing the whole cache when you don't need to makes the program half the speed or worse. If you only need to flush 256 bytes (for example), why flush all the rest of the cache?
I guess sceKernelDcacheWritebackRange(buffer, size) is the candidate to test, especially if you don't want to flush the stack for nothing
shadow_gg
Posts: 3
Joined: Fri Oct 07, 2005 6:14 am
Location: Paris, France
Contact:

Post by shadow_gg »

Sarkins: you welcome

oh I didn't know there was also a function to flush a range of memory.
You definitely right, I'll try it :p

Thx
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

shadow_gg wrote:Sarkins: you welcome

oh I didn't know there was also a function to flush a range of memory.
You definitely right, I'll try it :p

Thx
I don't know how much difference it would make on a simple cube test (probably none). It would need to be more substantial before you saw a difference... something that was pushing the PSP toward its limits.
Post Reply