all repos — mgba @ 448bc200c36c68a1250a2eb0394f5b90a2f75bed

mGBA Game Boy Advance Emulator

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 256
 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 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() {
 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	return true;
 84}
 85
 86void ctrDeinitGpu() {
 87	if (ctrVertexBuffer) {
 88		linearFree(ctrVertexBuffer);
 89		ctrVertexBuffer = NULL;
 90	}
 91
 92	shaderProgramFree(&uiProgram);
 93
 94	if (uiShader) {
 95		DVLB_Free(uiShader);
 96		uiShader = NULL;
 97	}
 98}
 99
100void ctrSetViewportSize(s16 w, s16 h, bool tilt) {
101	C3D_SetViewport(0, 0, h, w);
102	C3D_Mtx projectionMtx;
103	if (tilt) {
104		Mtx_OrthoTilt(&projectionMtx, 0.0, w, h, 0.0, 0.0, 1.0, true);
105	} else {
106		Mtx_Ortho(&projectionMtx, 0.0, w, 0.0, h, 0.0, 1.0, true);
107	}
108	C3D_FVUnifMtx4x4(GPU_GEOMETRY_SHADER, GSH_FVEC_projectionMtx, &projectionMtx);
109}
110
111void ctrActivateTexture(C3D_Tex* texture) {
112	if (texture == activeTexture) {
113		return;
114	}
115	if (activeTexture) {
116		ctrFlushBatch();
117	}
118
119	activeTexture = texture;
120	C3D_TexBind(0, activeTexture);
121
122	C3D_TexEnv* env = C3D_GetTexEnv(0);
123	C3D_TexEnvOp(env, C3D_Both, 0, 0, 0);
124	if (texture->fmt < GPU_LA8) {
125		C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, 0);
126		C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE);
127	} else {
128		C3D_TexEnvSrc(env, C3D_RGB, GPU_PRIMARY_COLOR, 0, 0);
129		C3D_TexEnvSrc(env, C3D_Alpha, GPU_TEXTURE0, GPU_PRIMARY_COLOR, 0);
130		C3D_TexEnvFunc(env, C3D_RGB, GPU_REPLACE);
131		C3D_TexEnvFunc(env, C3D_Alpha, GPU_MODULATE);
132	}
133	env = C3D_GetTexEnv(1);
134	C3D_TexEnvOp(env, C3D_Both, 0, 0, 0);
135	C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, 0, 0);
136	C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE);
137	env = C3D_GetTexEnv(2);
138	C3D_TexEnvOp(env, C3D_Both, 0, 0, 0);
139	C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, 0, 0);
140	C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE);
141
142	C3D_Mtx textureMtx = {
143		.m = {
144			// Rows are in the order w z y x, because ctrulib
145			0.0f, 0.0f, 0.0f, 1.0f / activeTexture->width,
146			0.0f, 0.0f, 1.0f / activeTexture->height, 0.0f 
147		}
148	};
149	C3D_FVUnifMtx2x4(GPU_GEOMETRY_SHADER, GSH_FVEC_textureMtx, &textureMtx);
150}
151
152void ctrTextureMultiply(void) {
153	C3D_TexEnv* env = C3D_GetTexEnv(1);
154	C3D_TexEnvOp(env, C3D_Both, 0, 0, 0);
155	C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, GPU_TEXTURE0, 0);
156	C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE);
157}
158
159void ctrTextureBias(u32 color) {
160	C3D_TexEnv* env = C3D_GetTexEnv(2);
161	C3D_TexEnvOp(env, C3D_Both, 0, 0, 0);
162	C3D_TexEnvSrc(env, C3D_Both, GPU_PREVIOUS, GPU_CONSTANT, 0);
163	C3D_TexEnvFunc(env, C3D_Both, GPU_ADD);
164	C3D_TexEnvColor(env, color);
165}
166
167void ctrAddRectEx(u32 color, s16 x, s16 y, s16 w, s16 h, s16 u, s16 v, s16 uw, s16 vh, float rotate) {
168	if (x >= 400 && w >= 0) {
169		return;
170	}
171	if (y >= 240 && h >= 0) {
172		return;
173	}
174
175	if (ctrNumVerts + ctrVertStart == MAX_NUM_QUADS) {
176		ctrFlushBatch();
177		C3D_Flush();
178		ctrNumVerts = 0;
179		ctrVertStart = 0;
180	}
181
182	struct ctrUIVertex* vtx = &ctrVertexBuffer[ctrVertStart + ctrNumVerts];
183	vtx->x = x;
184	vtx->y = y;
185	vtx->w = w;
186	vtx->h = h;
187	vtx->u = u;
188	vtx->v = v;
189	vtx->uw = uw;
190	vtx->vh = vh;
191	vtx->abgr = color;
192	vtx->rotate[0] = cosf(rotate);
193	vtx->rotate[1] = sinf(rotate);
194
195	++ctrNumVerts;
196}
197
198void ctrAddRect(u32 color, s16 x, s16 y, s16 u, s16 v, s16 w, s16 h) {
199	ctrAddRectEx(color, x, y, w, h, u, v, w, h, 0);
200}
201
202void ctrFlushBatch(void) {
203	if (ctrNumVerts == 0) {
204		return;
205	}
206
207	C3D_BufInfo* bufInfo = C3D_GetBufInfo();
208	BufInfo_Init(bufInfo);
209	BufInfo_Add(bufInfo, &ctrVertexBuffer[ctrVertStart], sizeof(struct ctrUIVertex), 4, 0x3210);
210
211	GSPGPU_FlushDataCache(&ctrVertexBuffer[ctrVertStart], sizeof(struct ctrUIVertex) * ctrNumVerts);
212	C3D_DrawArrays(GPU_GEOMETRY_PRIM, 0, ctrNumVerts);
213
214	ctrVertStart += ctrNumVerts;
215	ctrNumVerts = 0;
216}
217
218void ctrFinalize(void) {
219	ctrFlushBatch();
220	C3D_Flush();
221	ctrNumVerts = 0;
222	ctrVertStart = 0;
223}