Skip to content

Commit 3d94c15

Browse files
committed
Render to texture
1 parent 87a823f commit 3d94c15

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ add_object_library_macros(GS_CORE_OBJS ee/gs/src/gsCore.c
111111
gsKit_vram_clear
112112
gsKit_sync_flip
113113
gsKit_setactive
114+
gsKit_renderToScreen
114115
gsKit_finish
115116
gsKit_lock_buffer
116117
gsKit_unlock_buffer
@@ -216,6 +217,7 @@ add_object_library_macros(GS_TEXTURE_OBJS ee/gs/src/gsTexture.c
216217
gsKit_texture_send
217218
gsKit_texture_send_inline
218219
gsKit_texture_upload
220+
gsKit_renderToTexture
219221
gsKit_prim_sprite_texture_3d
220222
gsKit_prim_sprite_striped_texture_3d
221223
gsKit_prim_triangle_texture_3d
@@ -361,6 +363,7 @@ if(NOT SKIP_BUILD_EXAMPLES)
361363
modetest
362364
modetesthires
363365
pixelperfect
366+
rendertexture
364367
texstream
365368
textures
366369
vsync

ee/gs/include/gsCore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ void gsKit_switch_context(GSGLOBAL *gsGlobal);
128128
/// Sets Your Active Framebuffer
129129
void gsKit_setactive(GSGLOBAL *gsGlobal);
130130

131+
/// Set the render target to the current screen buffer
132+
void gsKit_renderToScreen(GSGLOBAL *gsGlobal);
133+
131134
/// Blocks until FINISH is triggered
132135
void gsKit_finish(void);
133136

ee/gs/include/gsTexture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ void gsKit_texture_to_psm16(GSTEXTURE *Texture);
117117
void gsKit_texture_send(u32 *mem, int width, int height, u32 tbp, u32 psm, u32 tbw, u8 clut);
118118
void gsKit_texture_send_inline(GSGLOBAL *gsGlobal, u32 *mem, int width, int height, u32 tbp, u32 psm, u32 tbw, u8 clut);
119119
void gsKit_texture_upload(GSGLOBAL *gsGlobal, GSTEXTURE *Texture);
120+
void gsKit_renderToTexture(GSGLOBAL *gsGlobal, GSTEXTURE *texture);
120121

121122
void gsKit_prim_sprite_texture_3d(GSGLOBAL *gsGlobal, const GSTEXTURE *Texture, float x1, float y1, int iz1, float u1, float v1,
122123
float x2, float y2, int iz2, float u2, float v2,

ee/gs/src/gsCore.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ void gsKit_setactive(GSGLOBAL *gsGlobal)
120120
}
121121
#endif
122122

123+
#if F_gsKit_renderToScreen
124+
void gsKit_renderToScreen(GSGLOBAL *gsGlobal)
125+
{
126+
u64 *p_data;
127+
u64 *p_store;
128+
int qsize = 1;
129+
130+
p_store = p_data = gsKit_heap_alloc(gsGlobal, qsize, (qsize*16), GIF_AD);
131+
132+
if(p_store == gsGlobal->CurQueue->last_tag)
133+
{
134+
*p_data++ = GIF_TAG_AD(qsize);
135+
*p_data++ = GIF_AD;
136+
}
137+
138+
*p_data++ = GS_SETREG_FRAME_1( gsGlobal->ScreenBuffer[gsGlobal->ActiveBuffer & 1] / 8192,
139+
gsGlobal->Width / 64, gsGlobal->PSM, 0 );
140+
*p_data++ = GS_FRAME_1;
141+
}
142+
#endif
143+
123144
#if F_gsKit_finish
124145
void gsKit_finish(void)
125146
{

ee/gs/src/gsTexture.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,27 @@ void gsKit_prim_sprite_texture_3d(GSGLOBAL *gsGlobal, const GSTEXTURE *Texture,
522522
}
523523
#endif
524524

525+
#if F_gsKit_renderToTexture
526+
void gsKit_renderToTexture(GSGLOBAL *gsGlobal, GSTEXTURE *texture)
527+
{
528+
u64 *p_data;
529+
u64 *p_store;
530+
int qsize = 1;
531+
532+
p_store = p_data = gsKit_heap_alloc(gsGlobal, qsize, (qsize*16), GIF_AD);
533+
534+
if(p_store == gsGlobal->CurQueue->last_tag)
535+
{
536+
*p_data++ = GIF_TAG_AD(qsize);
537+
*p_data++ = GIF_AD;
538+
}
539+
540+
*p_data++ = GS_SETREG_FRAME_1(texture->Vram / 8192,
541+
texture->Width / 64, texture->PSM, 0 );
542+
*p_data++ = GS_FRAME_1;
543+
}
544+
#endif
545+
525546
#if F_gsKit_prim_sprite_striped_texture_3d
526547
void gsKit_prim_sprite_striped_texture_3d(GSGLOBAL *gsGlobal, const GSTEXTURE *Texture,
527548
float x1, float y1, int iz1, float u1, float v1,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// ____ ___ | / _____ _____
2+
// | __ | |___/ | |
3+
// |___| ___| | \ __|__ | gsKit Open Source Project.
4+
// ----------------------------------------------------------------------
5+
// Copyright 2004 - Chris "Neovanglist" Gilbert <[email protected]>
6+
// Licenced under Academic Free License version 2.0
7+
// Review gsKit README & LICENSE files for further details.
8+
//
9+
// renderTexture.c - Example demonstrating render to texture operation.
10+
// First we are rendering a triangle to a texture,
11+
// then we are rendering the texture to the screen.
12+
//
13+
14+
#include <stdio.h>
15+
#include <malloc.h>
16+
17+
#include <gsKit.h>
18+
#include <dmaKit.h>
19+
#include <gsToolkit.h>
20+
#include <gsInline.h>
21+
#include <gsTexture.h>
22+
23+
#define TEXTURE_WIDTH 256
24+
#define TEXTURE_HEIGHT 256
25+
26+
int main(int argc, char *argv[])
27+
{
28+
GSGLOBAL *gsGlobal;
29+
GSTEXTURE renderTexture;
30+
uint32_t i, j, offset;
31+
uint32_t totalVertices = 3;
32+
uint32_t textureTotalVertices = 2;
33+
uint64_t White = GS_SETREG_RGBAQ(0xFF, 0xFF, 0xFF, 0x00, 0x00);
34+
gs_rgbaq color = color_to_RGBAQ(0x80, 0x80, 0x80, 0x80, 0);
35+
u32 render_target_ptr;
36+
37+
gsGlobal = gsKit_init_global();
38+
39+
gsGlobal->PSM = GS_PSM_CT24;
40+
gsGlobal->PSMZ = GS_PSMZ_16S;
41+
42+
dmaKit_init(D_CTRL_RELE_OFF, D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC,
43+
D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF);
44+
45+
// Initialize the DMAC
46+
dmaKit_chan_init(DMA_CHANNEL_GIF);
47+
48+
gsKit_init_screen(gsGlobal);
49+
gsKit_mode_switch(gsGlobal, GS_ONESHOT);
50+
51+
renderTexture.Width = TEXTURE_WIDTH;
52+
renderTexture.Height = TEXTURE_HEIGHT;
53+
renderTexture.PSM = GS_PSM_CT16;
54+
renderTexture.Mem = 0; // NOT NEEDED
55+
renderTexture.Vram = gsKit_vram_alloc(gsGlobal, gsKit_texture_size(renderTexture.Width, renderTexture.Height, renderTexture.PSM), GSKIT_ALLOC_USERBUFFER);
56+
57+
gsKit_set_clamp(gsGlobal, GS_CMODE_CLAMP);
58+
59+
GSPRIMPOINT *verts = (GSPRIMPOINT *)malloc(sizeof(GSPRIMPOINT) * totalVertices);
60+
verts[0].xyz2 = vertex_to_XYZ2(gsGlobal, 0, 0, 0);
61+
verts[0].rgbaq = color_to_RGBAQ(0xFF, 0x00, 0x00, 0xFF, 0);
62+
63+
verts[1].xyz2 = vertex_to_XYZ2(gsGlobal, renderTexture.Width, 0, 0);
64+
verts[1].rgbaq = color_to_RGBAQ(0x00, 0xFF, 0x00, 0xFF, 0);
65+
66+
verts[2].xyz2 = vertex_to_XYZ2(gsGlobal, renderTexture.Width / 2, renderTexture.Height, 0);
67+
verts[2].rgbaq = color_to_RGBAQ(0x00, 0x00, 0xFF, 0xFF, 0);
68+
69+
GSPRIMUVPOINTFLAT *textureVertex = (GSPRIMUVPOINTFLAT *)malloc(sizeof(GSPRIMUVPOINTFLAT) * textureTotalVertices);
70+
textureVertex[0].xyz2 = vertex_to_XYZ2(gsGlobal, 0, 0, 0);
71+
textureVertex[0].uv = vertex_to_UV(&renderTexture, 0, 0);
72+
73+
textureVertex[1].xyz2 = vertex_to_XYZ2(gsGlobal, gsGlobal->Width, gsGlobal->Height, 0);
74+
textureVertex[1].uv = vertex_to_UV(&renderTexture, renderTexture.Width, renderTexture.Height);
75+
76+
while (1)
77+
{
78+
gsKit_clear(gsGlobal, White);
79+
80+
// set render target to the texture
81+
gsKit_renderToTexture(gsGlobal, &renderTexture);
82+
83+
gsKit_prim_list_triangle_gouraud_3d(gsGlobal, totalVertices, verts);
84+
85+
// set render target back to the screen
86+
gsKit_renderToScreen(gsGlobal);
87+
88+
gskit_prim_list_sprite_texture_uv_flat_color(gsGlobal, &renderTexture, color, textureTotalVertices, textureVertex);
89+
90+
gsKit_queue_exec(gsGlobal);
91+
gsKit_sync_flip(gsGlobal);
92+
}
93+
94+
free(verts);
95+
96+
return 0;
97+
}

0 commit comments

Comments
 (0)