view TaskManager/Test/simple_render/viewer.cpp @ 94:588ab5f0e659

*** empty log message ***
author gongo
date Thu, 28 Feb 2008 17:41:08 +0900
parents 663bc2a213f5
children 57bac9e07662
line wrap: on
line source

#include <iostream>
#include <SDL.h>
#include "polygon.h"
#include "viewer.h"
#include "sys.h"
#include "spu_span.h"
using namespace std;

#define redMask   0x00ff0000
#define greenMask 0x0000ff00
#define blueMask  0x000000ff
#define alphaMask 0

extern int create_sgp(Polygon *sg, SceneGraphPack *sgp);
extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp);
extern int create_pp(SceneGraphPack *sgp, PolygonPack *pp);

Viewer::Viewer(int b, int w, int h)
{
    bpp = b;
    width = w;
    height = h;
}


void Viewer::sdl_init()
{
    if (SDL_Init( SDL_INIT_VIDEO ) < 0) {
	fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
	exit( 1 );
    }
    
#ifdef _DEBUG
    screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE);
    if (screen == NULL) {
	fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
	SDL_Quit();
	exit(1);
    }
#else
    void *_pixels = new Uint32[width*height*32/8];
    //posix_memalign(&pixels, width*heigh/8, DEFAULT_ALIGNMENT);
    screen = SDL_CreateRGBSurfaceFrom(_pixels, width, height, 32,
				      width*4, redMask, greenMask,
				      blueMask, alphaMask);
#endif
}


int Viewer::get_ticks()
{
    int time;
    time = SDL_GetTicks();
    return time;
}

bool Viewer::quit_check()
{
    bool quit = false;
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
	if(event.type==SDL_QUIT)
	{
	    quit = true;
	    return quit;
	}
    }
    return quit;
}

void Viewer::quit()
{
    SDL_Quit();
}


void Viewer::swap_buffers()
{
    SDL_GL_SwapBuffers();
}


void
Viewer::write_pixel(int x, int y,float z, Uint32 rgb)
{
    SDL_PixelFormat *pf;
    pf = screen->format;
    x += width/2;
    y += height/2;

    static int diffz,diffz1;

    diffz1 = diffz;
    diffz = (zRow[x][y]>z);
    if (z < zRow[x][y]) { 
	if (x < width && x > 0 && y > 0 && y < height) {
	    zRow[x][y] = z;
	    y = height - y;
	    pixels[width*y + x] = rgb;
	}
    }
}

void
Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
{
    if (x1 > x2) {
	float x=0;
	float y=0;
	x=x1;
	y=y1;
	x1 = x2;
	y1 = y2;
	x2 = x;
	y2 = y;
    }

    float s = y1;

    if ((int)x1 == (int)x2) {
	if (y1 > y2) {
	    float y=0;
	    y = y1;
	    y1 = y2;
	    y2 = y;
	}

	for (float i=y1; i<y2; i++) {
	    //write_pixel((int)x1,(int)i);
	    write_pixel((int)x1,(int)i,0,rgb);
	}
    } else {
	float t = (y2 - y1)/(x2 - x1);
	if (t < -1) {
	    float f = 0;
	    for (float i=x1; i<x2; i++) {
		for (float a=(int)t; a<0; a++) {
		    write_pixel((int)i,(int)s,0,rgb);
		    s--;
		}

		f += t-(int)t;

		if (f <= -1) {
		    write_pixel((int)i,(int)s,0,rgb);
		    f = 0;
		    s--;
		}
	    }
	} else if (t <= 1) {
	    for(float i=x1; i<x2; i++) {
		//write_pixel((int)i,(int)s);
		write_pixel((int)i,(int)s,0,rgb);
		s += t;
	    }
	} else {
	    float f = 0;
	    for (float i=x1; i<x2; i++) {
		for (float a=0; a<(int)t; a++) {
		    write_pixel((int)i,(int)s,0,rgb);
		    s++;
		}

		f += t-(int)t;

		if (f >= 1) {
		    write_pixel((int)i,(int)s,0,rgb);
		    f = 0;
		    s++;
		}
	    }
	}
    }
}

void Viewer::write_triangle(float x1, float y1, float x2, float y2, float x3, float y3, Uint32 rgb)
{
    write_line(x1,y1,x2,y2,rgb);
    write_line(x2,y2,x3,y3,rgb);
    write_line(x3,y3,x1,y1,rgb);
}

void Viewer::clean_pixels()
{
    for(int i=0; i<width*height; i++)
    {
	pixels[i] = 0x00;
    }
}

void Viewer::graph_line()
{
    int xl = width*height/2;
    int yl = width/2;
    for(int i=0; i<width; i++)
    {
	for(int t=0; t<height; t+=20)
	{
	    pixels[width*t+i] = 0x5a;
	}
	pixels[xl +i] = 0xff;
    }
    for(int i=0; i<height; i++)
    {
	for(int t=0; t<width; t+=20)
	{
	    pixels[i*width+t] = 0x5a;
	}
	pixels[i*width+yl] = 0xff;
    }
}

int start_time;
int this_time;
int frames;
SDL_Surface *bitmap;
SDL_PixelFormat *pixelFormat;
Uint32 background;
Polygon *polygon;
SceneGraphPack *sgp;
PolygonPack *pp;    
SPUSPANLIST *ssl;
DmaBuffer *sgp_buff;
DmaBuffer *pp_buff;
DmaBuffer *ssl_buff;

SPANPACK send_pack[10] __attribute__((aligned(16)));
unsigned int fbdev_addr;

extern int get_fbdev_addr(void);

void
Viewer::run_init()
{
    HTaskPtr task;
    int fd;

    //SceneGraphPack *sgp;
    //PolygonPack *pp;
    //SPUSPANLIST *ssl;

    start_time = get_ticks();
    this_time  = 0;
    frames     = 0;

    pixelFormat = screen->format;
    background  = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
    polygon     = new Polygon;
    polygon->set_data("cube.xml");
    polygon->viewer  = this;

#if 0 // USE DOUBLE BUFFER
    sgp_buff = manager->allocate(sizeof(SceneGraphPack));
    pp_buff  = manager->allocate(sizeof(PolygonPack));
    ssl_buff = manager->allocate(sizeof(SPUSPANLIST));

    // DmaBuffer の初期化、かっこいい方法ないか
    for (int i = 0; i < 2; i ++) {
	sgp_buff->get_buffer(&sgp);
	ssl_buff->get_buffer(&ssl);

	create_sgp(polygon, sgp);
	sgp->ssl = ssl;

	sgp_buff->swap_buffer();
	ssl_buff->swap_buffer();
    }
#else
    posix_memalign((void**)&sgp, 16, sizeof(SceneGraphPack));
    posix_memalign((void**)&pp, 16, sizeof(SceneGraphPack));
    posix_memalign((void**)&ssl, 16, sizeof(SceneGraphPack));
    //sgp = new SceneGraphPack;
    //pp  = new PolygonPack;
    //ssl = new SPUSPANLIST;
    create_sgp(polygon, sgp);
    sgp->ssl = ssl;
#endif

    pixels = new Uint32[width*height];
    
    graph_line();

    bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
				      width*4, redMask, greenMask,
				      blueMask, alphaMask);

    fd = manager->open("ViewerRunLoop");
    task = manager->create_task(fd, 0, 0, 0, NULL);      
    task->spawn();

    fbdev_addr = get_fbdev_addr();
}

void
Viewer::run_loop(void)
{
    HTaskPtr task_update_sgp = NULL;
    HTaskPtr task_create_pp  = NULL;
    HTaskPtr task_create_sp  = NULL;
    HTaskPtr task_finish     = NULL;
    int fd_update_sgp;
    int fd_create_pp;
    int fd_create_sp;
    int fd_finish;

    HTaskPtr task;
    int fd;
    bool quit_flg;

    quit_flg = quit_check();

    if (quit_flg == true) {
	this_time = get_ticks();
	fd_finish = manager->open("ViewerRunFinish");
	task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
	task_finish->spawn();
	return;
    }

    // clean_pixels や zRow_init は、
    // spe で fb に draw する時は必要ない。
    // ppe 側で draw する時にだけ呼ぶべき。
    //clean_pixels();
    //zRow_init();

    // これ自身、一つのタスクとして回す方がよいか
    graph_line();

    fd_update_sgp = manager->open("UpdateSGP");
    fd_create_pp  = manager->open("CreatePP");
    fd_create_sp  = manager->open("CreateSP");
    fd = manager->open("ViewerRunDraw");

#if 0 // USE DOUBLE BUFFER
    task_update_sgp
	= manager->create_task(fd_update_sgp, sizeof(SceneGraphPack),
			       sgp_buff, sgp_buff, NULL);
    task_create_pp
	= manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
			       sgp_buff, pp_buff, NULL);
    task_create_sp 
 	= manager->create_task(fd_create_sp, sizeof(SceneGraphPack),
			       pp_buff, ssl_buff, NULL);
#else
    task_update_sgp
	= manager->create_task(fd_update_sgp, sizeof(SceneGraphPack),
			       (uint32)sgp, (uint32)sgp, NULL);
    task_create_pp
	= manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
			       (uint32)sgp, (uint32)pp, NULL);
    task_create_sp 
 	= manager->create_task(fd_create_sp, sizeof(PolygonPack),
			       (uint32)pp, 0, NULL);
#endif

#if 0

    task_draw_finish =  manager->create();
    for (int i = 0; i < draw_spe_num; i++) {
	     task_spu_draw[i]
	     = manager->create_task(fd_create_pp, sizeof(SceneGraphPack),
				    spuspan_buff, pp_buff, NULL);
	     task_spu_draw[i]->set_depend(task_spu_span);
	     task_draw_finish->set_depend(tas_spu_draw[i]);
    }
#endif
    task = manager->create_task(fd, 0, 0, 0, NULL);

    task->set_depend(task_update_sgp);
    task->set_depend(task_create_pp);
    task->set_depend(task_create_sp);
    task_create_sp->set_depend(task_create_pp);
   
    //task_update_sgp->set_cpu(CPU_SPE);
    //task_create_pp->set_cpu(CPU_SPE);

    task_update_sgp->spawn();
    task_create_pp->spawn();
    task_create_sp->spawn();
    task->spawn();

    //sgp_buff->swap_buffer();
}

/**
 * 本当はタスクとして TestDraw を選ぶ
 */

//#define DRAW_POLYGON
#define DRAW_SPANPACK
//#define DRAW_SPUSPAN
void
Viewer::run_draw(void)
{
    HTaskPtr task;
    HTaskPtr task_draw[10];
    int fd;

    fd = manager->open("ViewerRunLoop");
    task = manager->create_task(fd, 0, 0, 0, NULL);

    for (int i = 0; i < 10; i++) {
	memcpy(&send_pack[i], &ssl->ss[0].spp[i], sizeof(SPANPACK));
	task_draw[i]
	    = manager->create_task(0, sizeof(SPANPACK),
				   (uint32)&send_pack[i], fbdev_addr, NULL);
	task_draw[i]->set_cpu(CPU_SPE);
	task->set_depend(task_draw[i]);
	task_draw[i]->spawn();
    }

    task->spawn();

    SDL_BlitSurface(bitmap, NULL, screen, NULL);
    SDL_UpdateRect(screen, 0, 0, 0, 0);    

    frames++;

    return;

#if 0 // USE DOUBLE BUFFER
    PolygonPack *pp;
    SPUSPANLIST *ssl;

    pp_buff->get_buffer(&pp);
    ssl_buff->get_buffer(&ssl);
#endif

#ifdef DRAW_POLYGON
    polygon->draw(pp);    // test draw of PolygonPack

#else
#  ifdef DRAW_SPANPACK // test draw of SpanPack
    for (int i = 0; i < 1; i++) {
	polygon->draw(&ssl->ss[0].spp[i]);
	polygon->draw(&ssl->ss[1].spp[i]);
	polygon->draw(&ssl->ss[2].spp[i]);
	polygon->draw(&ssl->ss[3].spp[i]);
	polygon->draw(&ssl->ss[4].spp[i]);
	polygon->draw(&ssl->ss[5].spp[i]);
    }
#  else
    polygon->draw(&ssl->ss[0]);
    polygon->draw(&ssl->ss[1]);
    polygon->draw(&ssl->ss[2]);
    polygon->draw(&ssl->ss[3]);
    polygon->draw(&ssl->ss[4]);
    polygon->draw(&ssl->ss[5]);
#  endif
#endif
}

#if 0
void
Viewer::draw_finish(void)
{
    //

    HTaskPtr task = manager->create(task_r);
    
}
#endif

void
Viewer::run_finish(void)
{
    if (this_time != start_time) {
	cout<< (((float)frames)/(this_time-start_time))*1000.0 << " FPS\n";
    }

    SDL_FreeSurface(bitmap);
    delete [] pixels;
    polygon->delete_data();
    delete polygon;
    //delete sgp_buff;
    //delete pp_buff;
    quit();
}

void
Viewer::zRow_init()
{
    for (int i = 0; i < width; i++) {
	for (int j = 0; j < height; j++) {
	    zRow[i][j] = 65535;
	}
    }
}