diff TaskManager/Test/test_render/spe/DrawSpan.cpp @ 109:5c194c71eca8

Cerium cvs version
author gongo@gendarme.local
date Wed, 12 Nov 2008 17:39:33 +0900
parents
children a52e193f9a42
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/spe/DrawSpan.cpp	Wed Nov 12 17:39:33 2008 +0900
@@ -0,0 +1,179 @@
+#include <stdlib.h>
+#include <string.h>
+#include "DrawSpan.h"
+#include "polygon_pack.h"
+#include "SpanPack.h"
+#include "texture.h"
+#include "viewer_types.h"
+
+#define SPAN_PACK_LOAD 0
+
+SchedDefineTask(DrawSpan);
+
+void
+DrawSpan::linebuf_init(int *buf, int x, int rgb)
+{
+    for (int i = 0; i < x; i++) {
+	buf[i] = rgb;
+    }
+}
+
+float*
+DrawSpan::zRow_init(int w, int h)
+{
+    float *zRow = NULL;
+    float z = 65535.0f;
+    int length = w*h;
+
+    zRow = (float*)smanager->allocate(sizeof(float)*length);
+
+    for (int i = 0; i < length; i++) {
+	zRow[i] = z;
+    }
+
+    return zRow;
+}
+
+
+char*
+DrawSpan::get_pixel(int tx, int ty, void *texture_image)
+{
+    return (char*)texture_image+(3*((128)*ty+tx));
+}
+
+Uint32
+DrawSpan::get_rgb(int tx, int ty, void *texture)
+{
+    Uint8 red, green, blue, alpha;
+
+    if (tx<0) tx = 0;
+    if (128-1< tx) tx = 128-1 ;
+    if (ty<0) ty = 0;
+    if (128-1< ty) ty = 128-1 ;
+
+    char *p = get_pixel(tx,ty,texture);
+
+    blue  = (Uint8) p[0];
+    green = (Uint8) p[1];
+    red   = (Uint8) p[2];
+    alpha = 255;
+
+    return (red & 0xff) * 0x10000 + (green & 0xff) * 0x100
+	+ (blue & 0xff) + (alpha << 24);
+}
+
+int
+DrawSpan::run(void *rbuf, void *wbuf)
+{
+    SpanPack *sp = (SpanPack*)smanager->get_input(0);
+    SpanPack *next_sp =
+	(SpanPack*)smanager->allocate(sizeof(SpanPack));
+    SpanPack *free_sp = next_sp; // next_sp の free() 用
+    SpanPack *tmp_sp = NULL;
+    Span *span;
+
+    int render_y = sp->info.y_top;
+    void *texture_image = global_get(TEXTURE_ID);
+
+    int rangex_start  = get_param(0); // このタスクが担当する x の範囲の始点
+    int rangex_end    = get_param(1); // 終点 (start <= x <= end)
+    int rangey        = get_param(2); // y の範囲 (render_y + rangey - 1)
+    int rangex        = rangex_end - rangex_start + 1;
+
+    float *zRow = zRow_init(rangex, rangey);
+
+    int **linebuf = (int**)smanager->allocate(sizeof(int*)*rangey);
+
+    for (int i = 0; i < rangey; i++) {
+	linebuf[i] = (int*)smanager->get_output(i);
+	linebuf_init(linebuf[i], rangex, 0x00ff00ff);
+    }
+
+    do {
+	/**
+	 * SpanPack->next が存在する場合、
+	 * 現在の SpanPack を処理してる間に
+	 * 次の SpanPack の DMA 転送を行う
+	 */
+	if (sp->next != NULL) {
+	    smanager->dma_load(next_sp, (uint32)sp->next,
+			       sizeof(SpanPack), SPAN_PACK_LOAD);
+	} else {
+	    next_sp = NULL;
+	}
+
+	for (int t = 0; t < sp->info.size; t++) {	  
+	    span = &sp->span[t];
+
+	    int end = span->length_x;
+	    Uint32 rgb = 0x00ff00;
+	    float tex1 = span->tex_x1;
+	    float tex2 = span->tex_x2;
+	    float tey1 = span->tex_y1;
+	    float tey2 = span->tex_y2;
+	    int tex_xpos;
+	    int tex_ypos;
+	    int tex_zpos;
+	    int x = span->x;
+	    int y = span->y;
+	    float z = span->start_z;
+	    float zpos = span->end_z;
+
+	    // 座標が [0 .. split_screen_w-1] に入るように x,y を -1
+	    int localx = getLocalX(x-1);
+	    int localy = getLocalY(y-1);
+	    
+	    if (end == 1) {
+		if (x < rangex_start || rangex_end < x) {
+		    continue;
+		}
+
+		tex_xpos = (int)((span->tex_height-1) * tex1);
+		tex_ypos = (int)((span->tex_width-1) * tey1);
+		tex_zpos = (int)z;
+
+		if (zpos < zRow[localx + (rangex * localy)]) {
+		    rgb = get_rgb(tex_xpos, tex_ypos, texture_image);
+		    zRow[localx + (rangex * localy)] = zpos;
+		    linebuf[localy][localx] = rgb;
+		}
+	    } else {
+		float tex_x, tex_y, tex_z;
+		int js = (x < rangex_start) ? rangex_start - x : 0;
+		int je = (x + end > rangex_end) ? rangex_end - x : end;
+
+		for (int j = js; j <= je; j++) {
+		    localx = getLocalX(x-1+j);
+
+		    tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
+		    tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
+		    tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
+		    if (tex_x > 1) tex_x = 1;
+		    if (tex_y > 1) tex_y = 1;
+		    tex_xpos = (int)((span->tex_height-1) * tex_x);
+		    tex_ypos = (int)((span->tex_width-1) * tex_y);
+
+		    if (tex_z < zRow[localx + (rangex*localy)]) {
+			rgb = get_rgb(tex_xpos, tex_ypos, texture_image);
+			zRow[localx + (rangex*localy)] = tex_z;
+			linebuf[localy][localx] = rgb;
+		    }
+		}
+	    }
+	}
+ 
+	smanager->dma_wait(SPAN_PACK_LOAD);
+
+	tmp_sp = sp;
+	sp = next_sp;
+	next_sp = tmp_sp;
+    } while (sp);
+
+
+FINISH:
+    free(free_sp);
+    free(linebuf);
+    free(zRow);
+
+    return 0;
+}