forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Strange pspgu behaviour

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
lego



Joined: 17 Oct 2008
Posts: 43

PostPosted: Tue Sep 29, 2009 11:30 pm    Post subject: Strange pspgu behaviour Reply with quote

Hi.

I'm experimenting with GU. Now I have a simple code, that get my some strange result. First triangle is drawn nice, but a second one, which is dynamically allocated, isn't drawn. If I change an array from dynamically allocatable to a statically allocatable one, then all triangles are drawn well.

Code:

#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
/*
#include <GL/gl.h>
#include <GL/glu.h>
*/
#include <math.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspthreadman.h>
#include <pspdisplay.h>
#include <pspnet_inet.h>
#include <pspnet.h>
#include <psputility.h>
#include <pspgu.h>
#include <pspgum.h>
#include <psputils.h>


PSP_MODULE_INFO("ogl", 0, 3, 5);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | PSP_THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(-1024);
PSP_MAIN_THREAD_STACK_SIZE_KB(1024);

#define printf pspDebugScreenPrintf

#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)

void *dList;// display List, used by sceGUStart
void *fbp0;// frame buffer


typedef struct {
  float x, y, z;
} Vertex_t;

Vertex_t __attribute__((aligned(16))) triangle[3] =
{
  { 100, 100, 0 }, { 200, 200, 0}, { 100, 200, 0}
}  ;

Vertex_t *vv;


/*************************************************************
 * EXIT CALLBACK STUFF
 *************************************************************/
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
  sceKernelExitGame();
  return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
  int cbid;

  cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  sceKernelRegisterExitCallback(cbid);

  sceKernelSleepThreadCB();

  return 0;

}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
  int thid = 0;

  thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
  if(thid >= 0) {
    sceKernelStartThread(thid, 0, 0);
  }

  return thid;
}

void initGU( void )
{
  // Init GU
  sceGuInit();
  sceGuStart( GU_DIRECT, dList );

  // Set Buffers
  sceGuDrawBuffer( GU_PSM_8888, fbp0, BUF_WIDTH );
  sceGuDispBuffer( SCR_WIDTH, SCR_HEIGHT, (void*)0x88000, BUF_WIDTH);
  sceGuDepthBuffer( (void*)0x110000, BUF_WIDTH);

  sceGuOffset( 2048 - (SCR_WIDTH/2), 2048 - (SCR_HEIGHT/2));
  sceGuViewport( 2048, 2048, SCR_WIDTH, SCR_HEIGHT);
  sceGuDepthRange( 65535, 0);

  // Set Render States
  sceGuScissor( 0, 0, SCR_WIDTH, SCR_HEIGHT);
  sceGuEnable( GU_SCISSOR_TEST );
  sceGuDepthFunc( GU_GEQUAL );
  sceGuEnable( GU_DEPTH_TEST );
  sceGuFrontFace( GU_CW );
  sceGuShadeModel( GU_SMOOTH );
  sceGuEnable( GU_CULL_FACE );
  sceGuEnable( GU_CLIP_PLANES );
  sceGuFinish();
  sceGuSync(0,0);

  sceDisplayWaitVblankStart();
  sceGuDisplay(GU_TRUE);
  // finish
}

void setupProjection( void )
{
  // setup matrices for the triangle

  sceGumMatrixMode(GU_PROJECTION);
  sceGumLoadIdentity();
  sceGumPerspective( 75.0f, 16.0f/9.0f, 0.5f, 1000.0f);

  sceGumMatrixMode(GU_VIEW);
  sceGumLoadIdentity();

  sceGuClearColor( GU_COLOR( 0.0f, 0.0f, 0.0f, 1.0f ) );
  sceGuClearDepth(0);

  sceGumMatrixMode(GU_MODEL);
  sceGumLoadIdentity();
}

int main(void)
{
  SetupCallbacks();
  pspDebugScreenInit();

  if ( TTF_Init() ) {
    fprintf(stderr, "TTF Error: %s\n", TTF_GetError());
    exit(1);
  }
  atexit(TTF_Quit);



  dList = memalign(16, 524288);
  initGU();
  setupProjection();

  vv = (Vertex_t*)memalign(16, 3 * sizeof(Vertex_t));
  vv[0].x = 100;
  vv[0].y = 100;
  vv[0].z = 0;
  vv[1].x = 200;
  vv[1].y = 100;
  vv[1].z = 0;
  vv[2].x = 200;
  vv[2].y = 200;
  vv[2].z = 0;

  sceGuStart(GU_DIRECT, dList);

    sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
    sceGuColor(GU_COLOR(1, 1, 1, 0));

    sceGumDrawArray(GU_TRIANGLES, GU_VERTEX_32BITF | GU_TRANSFORM_2D,
                    3, 0, triangle);
    sceGuColor(GU_COLOR(0, 0, 1, 0));
    sceGumDrawArray(GU_TRIANGLES, GU_VERTEX_32BITF | GU_TRANSFORM_2D,
                    3, 0, tr);
  sceGuFinish();
  sceGuSync(0, 0);

  sceDisplayWaitVblankStart();
  sceGuSwapBuffers();

  sceKernelSleepThreadCB();

  return 0;
}


What do I wrong :-)?
Back to top
View user's profile Send private message
psPea



Joined: 01 Sep 2007
Posts: 64

PostPosted: Tue Sep 29, 2009 11:51 pm    Post subject: Reply with quote

It's probably a cache issue,
try sceKernelDCacheWritebackInvaidateRange on the vertices.
_________________
Click ME!
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
a_noob



Joined: 17 Sep 2006
Posts: 97
Location: _start: jr 0xDEADBEEF

PostPosted: Wed Sep 30, 2009 1:07 am    Post subject: Reply with quote

you can avoid the cache problem by using local GU memory (from the display list). sceGuGetMemory(int size); (works just like malloc) You dont need to free this memory or write it back. However it is invalid at the end of the frame. Also if you choose you use malloc. It is 16bit aligned so no need to use memalign.
_________________
Code:
.øOº'ºOø.
'ºOo.oOº'
Back to top
View user's profile Send private message AIM Address MSN Messenger
lego



Joined: 17 Oct 2008
Posts: 43

PostPosted: Wed Sep 30, 2009 1:28 am    Post subject: Reply with quote

a_noob wrote:
you can avoid the cache problem by using local GU memory (from the display list). sceGuGetMemory(int size); (works just like malloc) You dont need to free this memory or write it back. However it is invalid at the end of the frame. Also if you choose you use malloc. It is 16bit aligned so no need to use memalign.


sceGuGetMemory() don't work for me. With or without sceKernelDcacheWritebackInvalidateRage() it give me nothing...

My changed code part:

Code:

  vv = (Vertex_t*)sceGuGetMemory(3 * sizeof(Vertex_t));


instead of old:

Code:

  vv = (Vertex_t*)memalign(16, 3 * sizeof(Vertex_t));
Back to top
View user's profile Send private message
lego



Joined: 17 Oct 2008
Posts: 43

PostPosted: Wed Sep 30, 2009 1:36 am    Post subject: Reply with quote

psPea wrote:
It's probably a cache issue,
try sceKernelDCacheWritebackInvaidateRange on the vertices.


Thanks!
It's work. sceKernelDCacheWritebackInvaidateRange(vv, 3 * sizeof(Vertex_t)) help me.

Could you help me to understand a cache work?
May i call this function after malloc() or i need to call it after i filled up an array with right data? How it works?

What a difference in work between this functions:
- sceKernelDcacheWritebackAll
- sceKernelDcacheWritebackInvalidateAll
- sceKernelDcacheWritebackRange
- sceKernelDcacheWritebackInvalidateRange
- sceKernelDcacheInvalidateRange

Where do I call it?
Back to top
View user's profile Send private message
lego



Joined: 17 Oct 2008
Posts: 43

PostPosted: Wed Sep 30, 2009 2:14 am    Post subject: Reply with quote

lego wrote:

May i call this function after malloc() or i need to call it after i filled up an array with right data?


Before an array data is filled, sceKernelDcacheWritebackInvalidateRange() don't give a needed result :-). So, this question has droped off.
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Thu Oct 01, 2009 7:24 am    Post subject: Reply with quote

For the cache problem, see the first paragraph here: http://wiki.ps2dev.org/psp:ge_faq

sceGuGetMemory() should work fine. I would address this issue instead of allocating vertex memory and clearing the cache.
Back to top
View user's profile Send private message
a_noob



Joined: 17 Sep 2006
Posts: 97
Location: _start: jr 0xDEADBEEF

PostPosted: Thu Oct 01, 2009 8:23 am    Post subject: Reply with quote

you need to call sceGuGetMemory within your GU render context between sceGuStart and sceGuFinish
_________________
Code:
.øOº'ºOø.
'ºOo.oOº'
Back to top
View user's profile Send private message AIM Address MSN Messenger
lego



Joined: 17 Oct 2008
Posts: 43

PostPosted: Fri Oct 02, 2009 12:03 am    Post subject: Reply with quote

Insert_witty_name wrote:
For the cache problem, see the first paragraph here: http://wiki.ps2dev.org/psp:ge_faq

sceGuGetMemory() should work fine. I would address this issue instead of allocating vertex memory and clearing the cache.


Many thanks.

Very useful link.
Back to top
View user's profile Send private message
lego



Joined: 17 Oct 2008
Posts: 43

PostPosted: Fri Oct 02, 2009 1:16 am    Post subject: Reply with quote

a_noob wrote:
you need to call sceGuGetMemory within your GU render context between sceGuStart and sceGuFinish


Thank you for reply.
Now it works, but I have some strange result - http://forums.ps2dev.org/viewtopic.php?t=12480 .
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group