PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
texture.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# (c) 2009 Lion
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include <errno.h>
12#include <stdio.h>
13#include <kernel.h>
14#include <libgs.h>
15
16#include "internal.h"
17
18extern QWORD GsPrimWorkArea[];
19
20int GsLoadImage(const void *source_addr, GS_IMAGE *dest)
21{
22 int i;
23 const unsigned char *pTexSrc;
24 unsigned int current, max, remainder, img_qwc;
25 QWORD *p;
26
27 switch(dest->psm)
28 {
29 case GS_TEX_32: //32 bit image
30 img_qwc = ((dest->width * dest->height)*4)/16;
31 break;
32 case GS_TEX_24: //24 bit image
33 img_qwc = ((dest->width * dest->height)*3)/16;
34 break;
35 case GS_TEX_16: //16 bit image
36 img_qwc = ((dest->width * dest->height)*2)/16;
37 break;
38 case GS_TEX_8: //8 bit image
39 img_qwc = ((dest->width * dest->height)*1)/16;
40 break;
41 case GS_TEX_4: //4 bit image
42 img_qwc = ((dest->width * dest->height)/2)/16;
43 break;
44 default:
45 //printf("unable to load unsupported image(%02x)",dest->psm);
46 return -1;
47 }
48
49 p=UNCACHED_SEG(GsPrimWorkArea);
50 gs_setGIF_TAG(((GS_GIF_TAG*)&p[0]), 4,1,0,0,GS_GIF_PACKED,1,gif_rd_ad);
51 gs_setR_BITBLTBUF(((GS_R_BITBLTBUF*)&p[1]),0,0,0,dest->vram_addr,dest->vram_width,dest->psm);
52 gs_setR_TRXPOS(((GS_R_TRXPOS*)&p[2]), 0,0,dest->x,dest->y,0);
53 gs_setR_TRXREG(((GS_R_TRXREG*)&p[3]), dest->width,dest->height);
54 gs_setR_TRXDIR(((GS_R_TRXDIR*)&p[4]), 0);
55
56 GsDmaSend(GsPrimWorkArea, 5);
57 GsDmaWait();
58
59 // Ok , We Send Image Now
60 max = img_qwc / 16384;
61 remainder = img_qwc % 16384;
62 current = 16384;
63 pTexSrc = (const unsigned char *)source_addr;
64 for(i=0;(unsigned int)i<max;i++)
65 {
66 //1st we signal gs we are about to send
67 //16384 qwords
68
69 gs_setGIF_TAG(((GS_GIF_TAG *)&p[0]), current,1,0,0,GS_GIF_IMAGE,0,0x00);
70
71 GsDmaSend(GsPrimWorkArea, 1);
72 GsDmaWait();
73
74 //we now send 16384 more qwords
75 GsDmaSend(pTexSrc, current);
76 GsDmaWait();
77
78 pTexSrc += current*16;
79 }
80
81 //transfer the rest if we have left overs
82 current = remainder;
83 //or exit if none is left
84 if(current)
85 {
86 // we signal are about to send whats left
87 gs_setGIF_TAG(((GS_GIF_TAG *)&p[0]), current,1,0,0,GS_GIF_IMAGE,0,0x00);
88
89 GsDmaSend(GsPrimWorkArea, 1);
90 GsDmaWait();
91
92 //send data leftover
93 GsDmaSend(pTexSrc, current);
94 GsDmaWait();
95 }
96
97 return 1;
98}
99
100/* VRAM */
101static unsigned int vr_addr=0;
102static unsigned int vr_tex_start=0;
103static unsigned int vr_2ndtolast_alloc=0; //address before last alloc so we can Free the last alloc
104
105int GsVramAllocFrameBuffer(s16 w, s16 h, s16 psm)
106{
107 int size, remainder, ret, byte_pp; // byte per pixel
108
109 switch(psm)
110 {
111 case GS_PIXMODE_32:
112 case GS_TEX_4HH: // these are 32bit
113 case GS_TEX_4HL: // ..
114 case GS_TEX_8H: // ..
115 case GS_ZBUFF_32: //
116 case GS_ZBUFF_24: //
117 case GS_PIXMODE_24: // also 24bit takes up 4 bytes
118 byte_pp = 4;
119 break;
120 case GS_PIXMODE_16:
121 case GS_PIXMODE_16S:
122 case GS_ZBUFF_16:
123 byte_pp = 2;
124 break;
125 case GS_TEX_8:
126 byte_pp = 1;
127 break;
128 case GS_TEX_4:
129 byte_pp = 0;
130 break;
131 default:
132 return -EINVAL;
133 }
134
135 if(byte_pp > 0) // 8 to 32 bit
136 {
137 size = ((w*h)*byte_pp)/4;
138 }
139 else // 4 bit
140 {
141 size = (w*h)/2;
142 }
143
144 remainder = (vr_addr % (2048));
145
146 if(remainder)
147 vr_addr += ((2048)-remainder);
148
149 ret = vr_addr/(2048);
150 vr_addr += size;
151 vr_tex_start = vr_addr;
152
153 return ret;
154}
155
156int GsVramAllocTextureBuffer(s16 w, s16 h, s16 psm)
157{
158 int size, remainder, ret, byte_pp; // byte per pixel
159
160 switch(psm)
161 {
162 case GS_PIXMODE_32:
163 case GS_PIXMODE_24: // also 24bit takes up 4 bytes
164 case GS_TEX_8H: // ..
165 case GS_TEX_4HH: // these are 32bit
166 case GS_TEX_4HL: // ..
167 byte_pp = 4;
168 break;
169 case GS_PIXMODE_16:
170 case GS_PIXMODE_16S:
171 byte_pp = 2;
172 break;
173 case GS_TEX_8:
174 byte_pp = 1;
175 break;
176 case GS_TEX_4:
177 byte_pp = 0;
178 break;
179 default:
180 return -EINVAL;
181 }
182
183 if(byte_pp > 0) // 8 to 32 bit
184 {
185 size = ((w*h)*byte_pp)/4;
186 }
187 else // 4 bit
188 {
189 size = (w*h)/2;
190 }
191
192 remainder = (vr_addr % (64));
193
194 //---
195 vr_2ndtolast_alloc = vr_addr;
196
197 if(remainder)
198 vr_addr += ((64)-remainder);
199
200 ret = vr_addr/(64);
201 vr_addr += size;
202
203 return ret;
204}
205
207{
208 vr_addr = vr_tex_start;
209}
210
211#if 0
212int VramFreeLastTextureBuffer()
213{
214 if(vr_2ndtolast_alloc)
215 {
216 vr_addr = vr_2ndtolast_alloc;
217 vr_2ndtolast_alloc =0;
218 }
219}
220#endif
221
222void GsVramFreeAll(void)
223{
224 vr_addr = 0;
225 vr_tex_start = 0;
226}
#define EINVAL
Definition errno.h:63
#define GS_TEX_32
Definition libgs.h:88
u16 width
Definition libgs.h:2302
u16 vram_addr
Definition libgs.h:2306
#define gif_rd_ad
Definition libgs.h:326
#define GS_PIXMODE_32
Definition libgs.h:77
u8 psm
Definition libgs.h:2310
void GsVramFreeAllTextureBuffer(void)
Definition texture.c:206
#define GS_ZBUFF_32
Definition libgs.h:99
u16 y
Definition libgs.h:2300
u16 x
Definition libgs.h:2298
u8 vram_width
Definition libgs.h:2308
u16 height
Definition libgs.h:2304
Definition libgs.h:333