Blit 320x240 image to VRAM

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

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
ChaosKnight
Posts: 142
Joined: Thu Apr 14, 2005 2:08 am
Location: Florida, USA

Blit 320x240 image to VRAM

Post by ChaosKnight »

I seem to be having some extraordinary difficulty doing something routinely simple. I need to take a 320*240*4 (ARGB but color is not important to me right now) and put it in the PSP's framebuffer. I've tried a number of differant ways (including nested for loops with 2d->1d conversion to make up for the 320 vs 480 thing.) Anyway, I'll stop babbling and show what i have that is not working. VideoInterrupt is called at 60Hz. Most of the code should be fairly straight-forward.

Code: Select all

#include <pspgu.h>

#define DEBUG 0
#include "debug.h"

// Basilisk display configuration
#define BII_SCREEN_WIDTH 320
#define BII_SCREEN_HEIGHT 240
int Temp;
static uint32 PspFrameBuffer&#91;BII_SCREEN_WIDTH * BII_SCREEN_HEIGHT&#93;;

/// Things from LUAPlayer's graphics.c
//#define SCREEN_WIDTH 480
//#define SCREEN_HEIGHT 272
//#define PSP_LINE_SIZE 512
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define PSP_LINE_SIZE 512
#define FRAMEBUFFER_SIZE &#40;PSP_LINE_SIZE*SCREEN_HEIGHT*2&#41;
u16* g_vram_base = &#40;u16*&#41; &#40;0x40000000 | 0x04000000&#41;;
typedef struct
&#123;
        unsigned short u, v;
        unsigned short color;
        short x, y, z;
&#125; Vertex;
static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;256&#93;;
static int dispBufferNumber;
typedef u16 Color;
Color* getVramDrawBuffer&#40;&#41;
&#123;
        Color* vram = &#40;Color*&#41; g_vram_base;
        if &#40;dispBufferNumber == 0&#41; vram += FRAMEBUFFER_SIZE / 2;
        return vram;
&#125;
/// Things from LUAPlayer's graphics.c

/*
 *  Initialization
 */

bool VideoInit&#40;bool classic&#41;
&#123;
	// Buffer
//	PspFrameBuffer = &#40;uint32 *&#41;malloc&#40;BII_SCREEN_WIDTH * BII_SCREEN_HEIGHT * 4&#41;;
	
	// guInit - Taken from initGraphics&#40;&#41; in graphics.c from LUAPlayer
        sceDisplaySetMode&#40;0, SCREEN_WIDTH, SCREEN_HEIGHT&#41;;

        dispBufferNumber = 0;
        sceDisplayWaitVblankStart&#40;&#41;;
        sceDisplaySetFrameBuf&#40;&#40;void*&#41; g_vram_base, PSP_LINE_SIZE, 1, 1&#41;;

        sceGuInit&#40;&#41;;

        sceGuStart&#40;GU_DIRECT, list&#41;;
        sceGuDrawBuffer&#40;GU_PSM_8888, &#40;void*&#41;FRAMEBUFFER_SIZE, PSP_LINE_SIZE&#41;;
        sceGuDispBuffer&#40;SCREEN_WIDTH, SCREEN_HEIGHT, &#40;void*&#41;0, PSP_LINE_SIZE&#41;;
        sceGuClear&#40;GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT&#41;;
        sceGuDepthBuffer&#40;&#40;void*&#41; 0x110000, PSP_LINE_SIZE&#41;;
        sceGuOffset&#40;2048 - &#40;SCREEN_WIDTH / 2&#41;, 2048 - &#40;SCREEN_HEIGHT / 2&#41;&#41;;
        sceGuViewport&#40;2048, 2048, SCREEN_WIDTH, SCREEN_HEIGHT&#41;;
        sceGuDepthRange&#40;0xc350, 0x2710&#41;;
        sceGuScissor&#40;0, 0, SCREEN_WIDTH, SCREEN_HEIGHT&#41;;
        sceGuEnable&#40;GU_SCISSOR_TEST&#41;;
        sceGuAlphaFunc&#40;GU_GREATER, 0, 0xff&#41;;
        sceGuEnable&#40;GU_ALPHA_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;;
        sceGuTexMode&#40;GU_PSM_8888, 0, 0, 0&#41;;
        sceGuTexFunc&#40;GU_TFX_REPLACE, GU_TCC_RGBA&#41;;
        sceGuTexFilter&#40;GU_NEAREST, GU_NEAREST&#41;;
        sceGuAmbientColor&#40;0xffffffff&#41;;
        sceGuFinish&#40;&#41;;
        sceGuSync&#40;0, 0&#41;;

        sceDisplayWaitVblankStart&#40;&#41;;
        sceGuDisplay&#40;1&#41;;

	// Configure the Basilisk II framebuffer.
	VideoMonitor.x = BII_SCREEN_WIDTH;
	VideoMonitor.y = BII_SCREEN_HEIGHT;
	VideoMonitor.mode = VMODE_32BIT;
	VideoMonitor.bytes_per_row = SCREEN_WIDTH * 4;
	VideoMonitor.mac_frame_base = Host2MacAddr&#40;&#40;uint8 *&#41;PspFrameBuffer&#41;;

	// Done.
	D&#40;bug&#40;"video "&#41;&#41;;
	return true;
&#125;

void video_set_palette&#40;unsigned char *p&#41;
&#123;
	D&#40;bug&#40;"video_set_palette called\n"&#41;&#41;;
&#125;

/*
 *  Deinitialization
 */

void VideoExit&#40;void&#41;
&#123;
//	free&#40;PspFrameBuffer&#41;;
	sceGuTerm&#40;&#41;;
&#125;


/*
 *  Video message handling
 */

void VideoInterrupt&#40;void&#41;
&#123;
	// blitImageToScreen&#40;...&#41; with some modifications.
        Color* vram = getVramDrawBuffer&#40;&#41;;
        sceKernelDcacheWritebackInvalidateAll&#40;&#41;;
        sceGuStart&#40;GU_DIRECT,list&#41;;
        sceGuCopyImage&#40;GU_PSM_8888, 0, 0, BII_SCREEN_WIDTH, BII_SCREEN_HEIGHT,
		SCREEN_WIDTH, PspFrameBuffer, 0, 0, PSP_LINE_SIZE, vram&#41;;
        sceGuFinish&#40;&#41;;
        sceGuSync&#40;0,0&#41;;
	
	// flipScreen&#40;&#41;
        sceGuSwapBuffers&#40;&#41;;
        sceDisplaySetFrameBuf&#40;vram, PSP_LINE_SIZE, 1, 1&#41;;
        dispBufferNumber ^= 1;
&#125;
w00t
Arwin
Posts: 426
Joined: Tue Jul 12, 2005 7:00 pm

Post by Arwin »

I'm an idiot at video on this level, I'm only just learning the basics and I barely understand them. But a guess still is still better than nothing ...

As far as I can tell, you're treating the PSP memory as 16bit (which is fine if that's what you want to use) but you're setting this for Basilisk:

VideoMonitor.mode = VMODE_32BIT;

Might that cause problems?
User avatar
ChaosKnight
Posts: 142
Joined: Thu Apr 14, 2005 2:08 am
Location: Florida, USA

Post by ChaosKnight »

Arwin wrote:VideoMonitor.mode = VMODE_32BIT;

Might that cause problems?
Yes. It does, I noticed that and changed everything to 16 bit mode which is now crashing. Fun fun. It's weird how you can't just write these things to video memory and how you need the buffer to be 512 wide when only 480 of it is going to be used even though 480 is divisible by 16 just like 512.

-- EDIT --
Fixed it. I was telling basilisk i had a lot more memory than i was allocating.
Things seem to be working now ok. Still not perfect but then again what is?
w00t
Post Reply