-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxbuffer.c
138 lines (125 loc) · 3.14 KB
/
xbuffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* This file belongs to the 'xlab' game engine.
* Copyright 2009 xfacter
* Copyright 2016 wickles
* This work is licensed under the LGPLv3
* subject to all terms as reproduced in the included LICENSE file.
*/
#include <pspgu.h>
#include "xmem.h"
#include "xmath.h"
#include "xgraphics.h"
#include "xbuffer.h"
xBuffer* xBufferConstruct(int psm, int width, int height)
{
xBuffer* b = (xBuffer*)x_malloc(sizeof(xBuffer));
if (!b) return 0;
int Bpp = 0;
switch (psm)
{
case GU_PSM_5650:
case GU_PSM_5551:
case GU_PSM_4444:
Bpp = 2;
break;
default:
Bpp = 4;
psm = GU_PSM_8888;
break;
}
b->psm = psm;
b->width = width;
b->height = height;
b->buf_width = x_next_pow2(width);
b->pow2_height = x_next_pow2(height);
b->u_scale = (float)b->width/b->buf_width;
b->v_scale = (float)b->height/b->pow2_height;
b->data = x_valloc(Bpp*b->buf_width*b->height);
if (!b->data)
{
return 0;
}
return b;
}
void xBufferDestroy(xBuffer* b)
{
if (!b) return;
if (b->data) x_free(b->data);
x_free(b);
}
static xBuffer frame_buf = {
X_SCREEN_PSM,
X_SCREEN_WIDTH, X_SCREEN_HEIGHT,
X_SCREEN_STRIDE, 512,
(float)X_SCREEN_WIDTH/X_SCREEN_STRIDE, (float)X_SCREEN_HEIGHT/512,
0
};
xBuffer* xBufferFrameBuffer()
{
frame_buf.data = xGuDrawPtr(0, 1);
return &frame_buf;
}
void xBufferSetImage(xBuffer* b)
{
if (!b)
{
xGuSetDebugTex();
return;
}
if (!b->data)
{
xGuSetDebugTex();
return;
}
sceGuTexScale(b->u_scale, b->v_scale);
sceGuTexMode(b->psm, 0, 0, 0);
sceGuTexImage(0, b->buf_width, b->pow2_height, b->buf_width, b->data);
}
int xBufferSetRenderTarget(xBuffer* b)
{
if (!b) return 1;
if (!b->data) return 1;
xGuRenderToTarget(b->psm, b->width, b->height, b->buf_width, X_VREL(b->data));
return 0;
}
void xBufferDrawA2B(xBuffer* a, xBuffer* b)
{
if (!a || !b) return;
if (xBufferSetRenderTarget(b) != 0) return;
xGuTexMode(GU_TFX_REPLACE, 0);
xBufferSetImage(a);
xGuSaveStates();
sceGuEnable(GU_TEXTURE_2D);
sceGuDisable(GU_LIGHTING);
sceGuDisable(GU_DEPTH_TEST);
sceGuDisable(GU_DITHER);
sceGuDepthMask(GU_TRUE);
xGuDrawTex(0, 0, b->width, b->height, 0, 0, a->width, a->height);
sceGuDepthMask(GU_FALSE);
xGuLoadStates();
xGuRenderToScreen();
}
void xBuffer4x4Pcf(xBuffer* a, xBuffer* b)
{
if (!a || !b) return;
if (xBufferSetRenderTarget(b) != 0) return;
xGuTexMode(GU_TFX_REPLACE, 0);
xBufferSetImage(a);
xGuSaveStates();
xGuTexFilter(X_BILINEAR);
sceGuEnable(GU_TEXTURE_2D);
sceGuEnable(GU_BLEND);
sceGuDisable(GU_LIGHTING);
sceGuDisable(GU_DEPTH_TEST);
sceGuDisable(GU_DITHER);
sceGuDepthMask(GU_TRUE);
sceGuBlendFunc(GU_ADD, GU_FIX, GU_FIX, 0x3f3f3f3f, 0x000000);
xGuDrawTexf(-1.5f, 1.5f, b->width, b->height, 0, 0, a->width, a->height);
sceGuBlendFunc(GU_ADD, GU_FIX, GU_FIX, 0x40404040, 0xffffffff);
xGuDrawTexf(0.5f, -1.5f, b->width, b->height, 0, 0, a->width, a->height);
xGuDrawTexf(-1.5f, 0.5f, b->width, b->height, 0, 0, a->width, a->height);
xGuDrawTexf(0.5f, 0.5f, b->width, b->height, 0, 0, a->width, a->height);
sceGuDepthMask(GU_FALSE);
xGuLoadStates();
xGuRenderToScreen();
}