Skip to content

Commit 478a76d

Browse files
committed
Render to texture
1 parent 87a823f commit 478a76d

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// ____ ___ | / _____ _____
2+
// | __ | |___/ | |
3+
// |___| ___| | \ __|__ | gsKit Open Source Project.
4+
// ----------------------------------------------------------------------
5+
// Copyright 2004 - Chris "Neovanglist" Gilbert <Neovanglist@LainOS.org>
6+
// Licenced under Academic Free License version 2.0
7+
// Review gsKit README & LICENSE files for further details.
8+
//
9+
// textures.c - Example demonstrating how the 2 different CLUT storage modes work.
10+
// CSMT1 and CSM1 are the two different CLUT storage modes. CSM1 is the default
11+
// mode, and is the mode that is used which requires to have the content swizzled
12+
// before uploading it to the GS. However, CSM2 is a mode that allows you to
13+
// upload the CLUT data directly to the GS, without having to swizzle it first.
14+
// Additionally it allows you to upload a list of CLUTs, and then switch between
15+
// them using the GS_SETCLUT command.
16+
//
17+
//
18+
19+
#include <stdio.h>
20+
#include <malloc.h>
21+
22+
#include <gsKit.h>
23+
#include <dmaKit.h>
24+
#include <gsToolkit.h>
25+
#include <gsInline.h>
26+
#include <gsTexture.h>
27+
28+
#define TEXTURE_WIDTH 256
29+
#define TEXTURE_HEIGHT 256
30+
31+
int main(int argc, char *argv[])
32+
{
33+
GSGLOBAL *gsGlobal;
34+
GSTEXTURE renderTexture;
35+
uint32_t i, j, offset;
36+
uint32_t totalVertices = 3;
37+
uint32_t textureTotalVertices = 2;
38+
uint64_t White = GS_SETREG_RGBAQ(0xFF, 0xFF, 0xFF, 0x00, 0x00);
39+
gs_rgbaq color = color_to_RGBAQ(0x80, 0x80, 0x80, 0x80, 0);
40+
u32 render_target_ptr;
41+
42+
gsGlobal = gsKit_init_global();
43+
44+
gsGlobal->PSM = GS_PSM_CT24;
45+
gsGlobal->PSMZ = GS_PSMZ_16S;
46+
47+
dmaKit_init(D_CTRL_RELE_OFF, D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC,
48+
D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF);
49+
50+
// Initialize the DMAC
51+
dmaKit_chan_init(DMA_CHANNEL_GIF);
52+
53+
gsKit_init_screen(gsGlobal);
54+
gsKit_mode_switch(gsGlobal, GS_ONESHOT);
55+
56+
renderTexture.Width = TEXTURE_WIDTH;
57+
renderTexture.Height = TEXTURE_HEIGHT;
58+
renderTexture.PSM = GS_PSM_CT16;
59+
renderTexture.Mem = 0; // NOT NEEDED
60+
renderTexture.Vram = gsKit_vram_alloc(gsGlobal, gsKit_texture_size(renderTexture.Width, renderTexture.Height, renderTexture.PSM), GSKIT_ALLOC_USERBUFFER);
61+
62+
gsKit_set_clamp(gsGlobal, GS_CMODE_CLAMP);
63+
64+
GSPRIMPOINT *verts = (GSPRIMPOINT *)malloc(sizeof(GSPRIMPOINT) * totalVertices);
65+
verts[0].xyz2 = vertex_to_XYZ2(gsGlobal, 0, 0, 0);
66+
verts[0].rgbaq = color_to_RGBAQ(0xFF, 0x00, 0x00, 0xFF, 0);
67+
68+
verts[1].xyz2 = vertex_to_XYZ2(gsGlobal, renderTexture.Width, 0, 0);
69+
verts[1].rgbaq = color_to_RGBAQ(0x00, 0xFF, 0x00, 0xFF, 0);
70+
71+
verts[2].xyz2 = vertex_to_XYZ2(gsGlobal, renderTexture.Width / 2, renderTexture.Height, 0);
72+
verts[2].rgbaq = color_to_RGBAQ(0x00, 0x00, 0xFF, 0xFF, 0);
73+
74+
GSPRIMUVPOINTFLAT *textureVertex = (GSPRIMUVPOINTFLAT *)malloc(sizeof(GSPRIMUVPOINTFLAT) * textureTotalVertices);
75+
textureVertex[0].xyz2 = vertex_to_XYZ2(gsGlobal, 0, 0, 0);
76+
textureVertex[0].uv = vertex_to_UV(&renderTexture, 0, 0);
77+
78+
textureVertex[1].xyz2 = vertex_to_XYZ2(gsGlobal, gsGlobal->Width, gsGlobal->Height, 0);
79+
textureVertex[1].uv = vertex_to_UV(&renderTexture, renderTexture.Width, renderTexture.Height);
80+
81+
while (1)
82+
{
83+
gsKit_clear(gsGlobal, White);
84+
85+
// set render target to the texture
86+
gsKit_renderToTexture(gsGlobal, &renderTexture);
87+
88+
gsKit_prim_list_triangle_gouraud_3d(gsGlobal, totalVertices, verts);
89+
90+
// set render target back to the screen
91+
gsKit_renderToScreen(gsGlobal);
92+
93+
gskit_prim_list_sprite_texture_uv_flat_color(gsGlobal, &renderTexture, color, textureTotalVertices, textureVertex);
94+
95+
gsKit_queue_exec(gsGlobal);
96+
gsKit_sync_flip(gsGlobal);
97+
}
98+
99+
free(verts);
100+
101+
return 0;
102+
}

0 commit comments

Comments
 (0)