GU troubles #1

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

Moderators: cheriff, TyRaNiD

Post Reply
pegasus2000
Posts: 160
Joined: Wed Jul 12, 2006 7:09 am

GU troubles #1

Post by pegasus2000 »

For all users that are experts of PSP GU. Please see this code:

Code: Select all

#define ND_NO_INITIAL_MINWINDOWSBAR
#include <nanodesktop.h>

void *fbp0, *fbp1, *zbp;
extern unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; ndlist&#91;262144&#93;;

char ndGFXRenderingInProgress = 0;


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

static unsigned int staticOffset = 0;

static unsigned int getMemorySize&#40;unsigned int width, unsigned int height, unsigned int psm&#41;
&#123;
	switch &#40;psm&#41;
	&#123;
		case GU_PSM_T4&#58;
			return &#40;width * height&#41; >> 1;

		case GU_PSM_T8&#58;
			return width * height;

		case GU_PSM_5650&#58;
		case GU_PSM_5551&#58;
		case GU_PSM_4444&#58;
		case GU_PSM_T16&#58;
			return 2 * width * height;

		case GU_PSM_8888&#58;
		case GU_PSM_T32&#58;
			return 4 * width * height;

		default&#58;
			return 0;
	&#125;
&#125;

void* getStaticVramBuffer&#40;unsigned int width, unsigned int height, unsigned int psm&#41;
&#123;
	unsigned int memSize = getMemorySize&#40;width,height,psm&#41;;
	void* result = &#40;void*&#41;staticOffset;
	staticOffset += memSize;

	return result;
&#125;

void* getStaticVramTexture&#40;unsigned int width, unsigned int height, unsigned int psm&#41;
&#123;
	void* result = getStaticVramBuffer&#40;width,height,psm&#41;;
	return &#40;void*&#41;&#40;&#40;&#40;unsigned int&#41;result&#41; + &#40;&#40;unsigned int&#41;sceGeEdramGetAddr&#40;&#41;&#41;&#41;;
&#125;


struct Vertex
&#123;
	float x,y,z;
&#125;;

struct Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; vertices &#91;1&#93;;


int ndHAL_EnableGFXEngine &#40;&#41;
&#123;
    // setup GU

	fbp0 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_5551&#41;;
	fbp1 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_5551&#41;;
	zbp = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444&#41;;

	sceGuInit&#40;&#41;;

	sceGuStart&#40;GU_DIRECT,ndlist&#41;;
	sceGuDrawBuffer&#40;GU_PSM_5551,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;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;

	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuDisplay&#40;GU_TRUE&#41;;
	
	ndGFXRenderingInProgress=0;
&#125;

int ndHAL_TerminateGFXEngine &#40;&#41;
&#123;
    sceGuTerm&#40;&#41;;
&#125;

int ndGFX_ClearScreen &#40;&#41;
&#123;
    sceGuClearColor&#40;0&#41;;
    sceGuClear&#40;GU_COLOR_BUFFER_BIT&#41;;
&#125;

inline void ndGFX_BeginRender &#40;&#41;
&#123;
    if &#40;!ndGFXRenderingInProgress&#41;
    &#123;
       sceGuStart&#40;GU_DIRECT,ndlist&#41;;   
       ndGFXRenderingInProgress=1;
    &#125;
&#125;

inline void ndGFX_CompleteRender &#40;&#41;
&#123;
    if &#40;ndGFXRenderingInProgress&#41;
    &#123;
        sceGuFinish&#40;&#41;;
        sceGuSync&#40;0,0&#41;;
    
        sceDisplayWaitVblankStart&#40;&#41;;
        sceGuSwapBuffers&#40;&#41;;
        
        ndGFXRenderingInProgress=0;   
    &#125;
&#125;



int ndGFX_DrawPixel &#40;short int PosX, short int PosY, short int PosZ, TypeColor Color, char RenderNow&#41;
&#123;
    if &#40;RenderNow&#41; ndGFX_BeginRender&#40;&#41;;
    
    sceGuColor&#40;0xFFFFFFFF&#41;;

    vertices &#91;0&#93;.x = PosX;
    vertices &#91;0&#93;.y = PosY;
    vertices &#91;0&#93;.z = PosZ;

    sceGuDrawArray&#40;GU_POINTS,GU_VERTEX_32BITF|GU_TRANSFORM_2D,1,0,vertices&#41;;
    
    if &#40;RenderNow&#41; ndGFX_CompleteRender &#40;&#41;;
&#125; 


int ndMain &#40;&#41;
&#123;
    ndInitSystem &#40;&#41;;
    ndHAL_EnableGFXEngine &#40;&#41;;
    ndGFX_ClearScreen &#40;&#41;;
    
    int Counter;
    
    for &#40;Counter=0; Counter<100; Counter++&#41;
    &#123;
        ndGFX_DrawPixel &#40;Counter, 50, 0, COLOR_WHITE, 1&#41;;
    &#125;
&#125;

I'm expecting a single orizzontal line of the screen.
Instead, the system gives me a set of **separated** points.

Why ?


A second thing. It seems that GU is a 3d engine only, so all pixel
has always 3 dimensions-coordinates (x,y,z). Am I wrong ?

There isn't a 2D mode in PSP-GU ?
a_noob
Posts: 97
Joined: Sun Sep 17, 2006 8:33 am
Location: _start: jr 0xDEADBEEF

Post by a_noob »

the GU is plenty capable of 2D, but yes draw array must always contain z. (this is due to how the GE works)

For drawing a line you should create 2 points, and use the GU_LINES primitive, as it is much faster than drawing each pixel. Also I may be wrong but the if(RenderNow) may be causing the problem.

Also it is a big mistake to open and close a GU display list every time you render, this causes alot of unneccasary over head. Rather open a display list call all renderings, then close it.

also in the ndGFX_BeginRender the if (ndGFXRenderingInPRogress) is more than likely what is causing the pixel skips as the GU/GE isnt completely in sync with the CPU, so the overhead caused by opening and closing , along with the CPU telling to render things while it is still trying to complete another list is what is causing your pixel drop.

heres the fixed code

Code: Select all

#define ND_NO_INITIAL_MINWINDOWSBAR 
#include <nanodesktop.h> 

void *fbp0, *fbp1, *zbp; 
extern unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; ndlist&#91;262144&#93;; 

char ndGFXRenderingInProgress = 0; 


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

static unsigned int staticOffset = 0; 

static unsigned int getMemorySize&#40;unsigned int width, unsigned int height, unsigned int psm&#41;
&#123; 
   switch &#40;psm&#41; 
   &#123; 
      case GU_PSM_T4&#58; 
         return &#40;width * height&#41; >> 1; 

      case GU_PSM_T8&#58; 
         return width * height; 

      case GU_PSM_5650&#58; 
      case GU_PSM_5551&#58; 
      case GU_PSM_4444&#58; 
      case GU_PSM_T16&#58; 
         return 2 * width * height; 

      case GU_PSM_8888&#58; 
      case GU_PSM_T32&#58; 
         return 4 * width * height; 

      default&#58; 
         return 0; 
   &#125; 
&#125; 

void* getStaticVramBuffer&#40;unsigned int width, unsigned int height, unsigned int psm&#41; 
&#123; 
   unsigned int memSize = getMemorySize&#40;width,height,psm&#41;; 
   void* result = &#40;void*&#41;staticOffset; 
   staticOffset += memSize; 

   return result; 
&#125; 

void* getStaticVramTexture&#40;unsigned int width, unsigned int height, unsigned int psm&#41; 
&#123; 
   void* result = getStaticVramBuffer&#40;width,height,psm&#41;; 
   return &#40;void*&#41;&#40;&#40;&#40;unsigned int&#41;result&#41; + &#40;&#40;unsigned int&#41;sceGeEdramGetAddr&#40;&#41;&#41;&#41;; 
&#125; 


struct Vertex 
&#123; 
   float x,y,z; 
&#125;; 

struct Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; vertices &#91;2&#93;; 


int ndHAL_EnableGFXEngine &#40;&#41; 
&#123; 
    // setup GU 

   fbp0 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_5551&#41;; 
   fbp1 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_5551&#41;; 
   zbp = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444&#41;; 

   sceGuInit&#40;&#41;; 

   sceGuStart&#40;GU_DIRECT,ndlist&#41;; 
   sceGuDrawBuffer&#40;GU_PSM_5551,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;; 
   sceGuFinish&#40;&#41;; 
   sceGuSync&#40;0,0&#41;; 

   sceDisplayWaitVblankStart&#40;&#41;; 
   sceGuDisplay&#40;GU_TRUE&#41;; 
    
   ndGFXRenderingInProgress=0; 
&#125; 

int ndHAL_TerminateGFXEngine &#40;&#41; 
&#123; 
    sceGuTerm&#40;&#41;; 
&#125; 

int ndGFX_ClearScreen &#40;&#41; 
&#123; 
    sceGuClearColor&#40;0&#41;; 
    sceGuClear&#40;GU_COLOR_BUFFER_BIT&#41;; 
&#125; 

inline void ndGFX_BeginRender &#40;&#41; 
&#123; 
       sceGuStart&#40;GU_DIRECT,ndlist&#41;;    
&#125; 

inline void ndGFX_CompleteRender &#40;&#41; 
&#123; 
    sceGuFinish&#40;&#41;; 
        sceGuSync&#40;0,0&#41;; 
    
        sceDisplayWaitVblankStart&#40;&#41;; 
        sceGuSwapBuffers&#40;&#41;; 

&#125; 



int ndGFX_DrawPixel &#40;short int PosX, short int PosY, short int PosZ, TypeColor Color, char RenderNow&#41; 
&#123; 
    
    sceGuColor&#40;0xFFFFFFFF&#41;; 

    vertices &#91;0&#93;.x = PosX; 
    vertices &#91;0&#93;.y = PosY; 
    vertices &#91;0&#93;.z = PosZ; 

    sceGuDrawArray&#40;GU_POINTS,GU_VERTEX_32BITF|GU_TRANSFORM_2D,1,0,vertices&#41;; 
&#125; 

int ndGFX_DrawLine &#40;short int PosX, short int PosY, short int PosZ, short int Pos2X, short int Pos2Y, short int Pos2Z, TypeColor Color, char RenderNow&#41; 
&#123; 
    
    sceGuColor&#40;0xFFFFFFFF&#41;; 

    vertices &#91;0&#93;.x = PosX; 
    vertices &#91;0&#93;.y = PosY; 
    vertices &#91;0&#93;.z = PosZ;

 	vertices &#91;1&#93;.x = Pos2X; 
    vertices &#91;1&#93;.y = Pos2Y; 
    vertices &#91;1&#93;.z = Pos2Z; 

    sceGuDrawArray&#40;GU_LINES,GU_VERTEX_32BITF|GU_TRANSFORM_2D,2,0,vertices&#41;; 
&#125;

int ndMain &#40;&#41; 
&#123; 
    ndInitSystem &#40;&#41;; 
    ndHAL_EnableGFXEngine &#40;&#41;; 
    ndGFX_ClearScreen &#40;&#41;; 
    
    int Counter; 
	ndGFX_BeginRender&#40;&#41;;
	// per pixel render
    for &#40;Counter=0; Counter<100; Counter++&#41; 
    &#123; 
        ndGFX_DrawPixel &#40;Counter, 50, 0, COLOR_WHITE, 1&#41;; 
    &#125; 
	//line render
	ndGFX_DrawLine &#40;0,126,0, 480,126,0, COLOR_WHITE, 1&#41;;
	ndGFX_CompleteRender&#40;&#41;;
&#125;

Code: Select all

.øOº'ºOø.
'ºOo.oOº'
Post Reply