src/platform/3ds/ctr-gpu.c (view raw)
1/* Copyright (c) 2015 Yuri Kunde Schlesner
2 * Copyright (c) 2016 Jeffrey Pfau
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8#include <3ds.h>
9#include <3ds/gpu/gpu.h>
10#include <3ds/gpu/gx.h>
11#include <stdlib.h>
12#include <string.h>
13#include <stdio.h>
14
15#include "ctr-gpu.h"
16
17#include "uishader.h"
18#include "uishader.shbin.h"
19
20struct ctrUIVertex {
21 short x, y;
22 short w, h;
23 short u, v;
24 short uw, vh;
25 u32 abgr;
26 float rotate[2];
27};
28
29#define MAX_NUM_QUADS 4096
30#define VERTEX_BUFFER_SIZE MAX_NUM_QUADS * sizeof(struct ctrUIVertex)
31
32static struct ctrUIVertex* ctrVertexBuffer = NULL;
33static int ctrNumVerts = 0;
34static int ctrVertStart = 0;
35
36static const C3D_Tex* activeTexture = NULL;
37
38static shaderProgram_s uiProgram;
39static DVLB_s* uiShader = NULL;
40static int GSH_FVEC_projectionMtx;
41static int GSH_FVEC_textureMtx;
42
43bool ctrInitGpu(void) {
44 // Load vertex shader binary
45 uiShader = DVLB_ParseFile((u32*) uishader, uishader_size);
46 if (uiShader == NULL) {
47 return false;
48 }
49
50 // Create shader
51 shaderProgramInit(&uiProgram);
52 Result res = shaderProgramSetVsh(&uiProgram, &uiShader->DVLE[0]);
53 if (res < 0) {
54 return false;
55 }
56 res = shaderProgramSetGsh(&uiProgram, &uiShader->DVLE[1], 4);
57 if (res < 0) {
58 return false;
59 }
60 C3D_BindProgram(&uiProgram);
61 GSH_FVEC_projectionMtx = shaderInstanceGetUniformLocation(uiProgram.geometryShader, "projectionMtx");
62 GSH_FVEC_textureMtx = shaderInstanceGetUniformLocation(uiProgram.geometryShader, "textureMtx");
63
64 // Allocate buffers
65 ctrVertexBuffer = linearAlloc(VERTEX_BUFFER_SIZE);
66 if (ctrVertexBuffer == NULL) {
67 return false;
68 }
69
70 C3D_CullFace(GPU_CULL_NONE);
71 C3D_DepthTest(false, GPU_ALWAYS, GPU_WRITE_ALL);
72 C3D_AlphaBlend(GPU_BLEND_ADD, GPU_BLEND_ADD, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA);
73 C3D_AlphaTest(false, GPU_ALWAYS, 0);
74 C3D_BlendingColor(0);
75
76 C3D_AttrInfo* attrInfo = C3D_GetAttrInfo();
77 AttrInfo_Init(attrInfo);
78 AttrInfo_AddLoader(attrInfo, 0, GPU_SHORT, 4); // in_pos
79 AttrInfo_AddLoader(attrInfo, 1, GPU_SHORT, 4); // in_tc0
80 AttrInfo_AddLoader(attrInfo, 2, GPU_UNSIGNED_BYTE, 4); // in_col
81 AttrInfo_AddLoader(attrInfo, 3, GPU_FLOAT, 2); // in_rot
82
83 C3D_BufInfo* bufInfo = C3D_GetBufInfo();
84 BufInfo_Init(bufInfo);
85 BufInfo_Add(bufInfo, ctrVertexBuffer, sizeof(struct ctrUIVertex), 4, 0x3210);
86
87 return true;
88}
89
90void ctrDeinitGpu(void) {
91 if (ctrVertexBuffer) {
92 linearFree(ctrVertexBuffer);
93 ctrVertexBuffer = NULL;
94 }
95
96 shaderProgramFree(&uiProgram);
97
98 if (uiShader) {
99 DVLB_Free(uiShader);
100 uiShader = NULL;
101 }
102}
103
104void ctrSetViewportSize(s16 w, s16 h, bool tilt) {
105 C3D_SetViewport(0, 0, h, w);
106 C3D_Mtx projectionMtx;
107 if (tilt) {
108 Mtx_OrthoTilt(&projectionMtx, 0.0, w, h, 0.0, 0.0, 1.0, true);
109 } else {
110 Mtx_Ortho(&projectionMtx, 0.0, w, 0.0, h, 0.0, 1.0, true);
111 }
112 C3D_FVUnifMtx4x4(GPU_GEOMETRY_SHADER, GSH_FVEC_projectionMtx, &projectionMtx);
113}
114
115void ctrFlushBatch(void) {
116 int thisBatch = ctrNumVerts - ctrVertStart;
117 if (!thisBatch) {
118 return;
119 }
120 if (thisBatch < 0) {
121 svcBreak(USERBREAK_PANIC);
122 }
123 C3D_DrawArrays(GPU_GEOMETRY_PRIM, ctrVertStart, thisBatch);
124 ctrVertStart = ctrNumVerts;
125}
126
127void ctrActivateTexture(const C3D_Tex* texture) {
128 if (texture == activeTexture) {
129 return;
130 }
131
132 if (activeTexture) {
133 ctrFlushBatch();
134 }
135
136 activeTexture = texture;
137 C3D_TexBind(0, activeTexture);
138
139 C3D_TexEnv* env = C3D_GetTexEnv(0);
140 C3D_TexEnvInit(env);
141 if (texture->fmt < GPU_LA8) {
142 C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, 0);
143 C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE);
144 } else {
145 C3D_TexEnvSrc(env, C3D_RGB, GPU_PRIMARY_COLOR, 0, 0);
146 C3D_TexEnvSrc(env, C3D_Alpha, GPU_TEXTURE0, GPU_PRIMARY_COLOR, 0);
147 C3D_TexEnvFunc(env, C3D_RGB, GPU_REPLACE);
148 C3D_TexEnvFunc(env, C3D_Alpha, GPU_MODULATE);
149 }
150 env = C3D_GetTexEnv(1);
151 C3D_TexEnvInit(env);
152 C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, 0, 0);
153 C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE);
154 env = C3D_GetTexEnv(2);
155 C3D_TexEnvInit(env);
156 C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, 0, 0);
157 C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE);
158
159 C3D_Mtx textureMtx = {
160 .m = {
161 // Rows are in the order w z y x, because ctrulib
162 0.0f, 0.0f, 0.0f, 1.0f / activeTexture->width,
163 0.0f, 0.0f, 1.0f / activeTexture->height, 0.0f
164 }
165 };
166 C3D_FVUnifMtx2x4(GPU_GEOMETRY_SHADER, GSH_FVEC_textureMtx, &textureMtx);
167}
168
169void ctrTextureMultiply(void) {
170 C3D_TexEnv* env = C3D_GetTexEnv(1);
171 C3D_TexEnvInit(env);
172 C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, GPU_TEXTURE0, 0);
173 C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE);
174}
175
176void ctrTextureBias(u32 color) {
177 C3D_TexEnv* env = C3D_GetTexEnv(2);
178 C3D_TexEnvInit(env);
179 C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, GPU_CONSTANT, 0);
180 C3D_TexEnvFunc(env, C3D_Both, GPU_ADD);
181 C3D_TexEnvColor(env, color);
182}
183
184void ctrAddRectEx(u32 color, s16 x, s16 y, s16 w, s16 h, s16 u, s16 v, s16 uw, s16 vh, float rotate) {
185 if (x >= 400 && w >= 0) {
186 return;
187 }
188 if (y >= 240 && h >= 0) {
189 return;
190 }
191
192 if (ctrNumVerts == MAX_NUM_QUADS) {
193 abort();
194 }
195
196 struct ctrUIVertex* vtx = &ctrVertexBuffer[ctrNumVerts];
197 vtx->x = x;
198 vtx->y = y;
199 vtx->w = w;
200 vtx->h = h;
201 vtx->u = u;
202 vtx->v = v;
203 vtx->uw = uw;
204 vtx->vh = vh;
205 vtx->abgr = color;
206 vtx->rotate[0] = cosf(rotate);
207 vtx->rotate[1] = sinf(rotate);
208
209 ++ctrNumVerts;
210}
211
212void ctrAddRect(u32 color, s16 x, s16 y, s16 u, s16 v, s16 w, s16 h) {
213 ctrAddRectEx(color, x, y, w, h, u, v, w, h, 0);
214}
215
216void ctrStartFrame(void) {
217 ctrNumVerts = 0;
218 ctrVertStart = 0;
219 activeTexture = NULL;
220}
221
222void ctrEndFrame(void) {
223 ctrFlushBatch();
224}