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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/*
Copyright 2016-2023 melonDS team
This file is part of melonDS.
melonDS is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with melonDS. If not, see http://www.gnu.org/licenses/.
*/
#include "GPU_OpenGL.h"
#include <assert.h>
#include <cstdio>
#include <cstring>
#include "NDS.h"
#include "GPU.h"
#include "GPU3D_OpenGL.h"
#include "OpenGLSupport.h"
#include "GPU_OpenGL_shaders.h"
namespace melonDS
{
using namespace OpenGL;
std::optional<GLCompositor> GLCompositor::New() noexcept
{
assert(glBindAttribLocation != nullptr);
std::array<GLuint, 3> CompShader {};
if (!OpenGL::BuildShaderProgram(kCompositorVS, kCompositorFS_Nearest, &CompShader[0], "CompositorShader"))
return std::nullopt;
glBindAttribLocation(CompShader[2], 0, "vPosition");
glBindAttribLocation(CompShader[2], 1, "vTexcoord");
glBindFragDataLocation(CompShader[2], 0, "oColor");
if (!OpenGL::LinkShaderProgram(CompShader.data()))
// OpenGL::LinkShaderProgram already deletes the shader program object
// if linking the shaders together failed.
return std::nullopt;
return { GLCompositor(CompShader) };
}
GLCompositor::GLCompositor(std::array<GLuint, 3> compShader) noexcept : CompShader(compShader)
{
CompScaleLoc = glGetUniformLocation(CompShader[2], "u3DScale");
Comp3DXPosLoc = glGetUniformLocation(CompShader[2], "u3DXPos");
glUseProgram(CompShader[2]);
GLuint screenTextureUniform = glGetUniformLocation(CompShader[2], "ScreenTex");
glUniform1i(screenTextureUniform, 0);
GLuint _3dTextureUniform = glGetUniformLocation(CompShader[2], "_3DTex");
glUniform1i(_3dTextureUniform, 1);
// all this mess is to prevent bleeding
#define SETVERTEX(i, x, y, offset) \
CompVertices[i].Position[0] = x; \
CompVertices[i].Position[1] = y + offset; \
CompVertices[i].Texcoord[0] = (x + 1.f) * (256.f / 2.f); \
CompVertices[i].Texcoord[1] = (y + 1.f) * (384.f / 2.f)
const float padOffset = 1.f/(192*2.f+2.f)*2.f;
// top screen
SETVERTEX(0, -1, 1, 0);
SETVERTEX(1, 1, 0, padOffset);
SETVERTEX(2, 1, 1, 0);
SETVERTEX(3, -1, 1, 0);
SETVERTEX(4, -1, 0, padOffset);
SETVERTEX(5, 1, 0, padOffset);
// bottom screen
SETVERTEX(6, -1, 0, -padOffset);
SETVERTEX(7, 1, -1, 0);
SETVERTEX(8, 1, 0, -padOffset);
SETVERTEX(9, -1, 0, -padOffset);
SETVERTEX(10, -1, -1, 0);
SETVERTEX(11, 1, -1, 0);
#undef SETVERTEX
glGenBuffers(1, &CompVertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, CompVertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(CompVertices), &CompVertices[0], GL_STATIC_DRAW);
glGenVertexArrays(1, &CompVertexArrayID);
glBindVertexArray(CompVertexArrayID);
glEnableVertexAttribArray(0); // position
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(CompVertex), (void*)(offsetof(CompVertex, Position)));
glEnableVertexAttribArray(1); // texcoord
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(CompVertex), (void*)(offsetof(CompVertex, Texcoord)));
glGenFramebuffers(CompScreenOutputFB.size(), &CompScreenOutputFB[0]);
glGenTextures(1, &CompScreenInputTex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, CompScreenInputTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, 256*3 + 1, 192*2, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, NULL);
glGenTextures(CompScreenOutputTex.size(), &CompScreenOutputTex[0]);
for (GLuint i : CompScreenOutputTex)
{
glBindTexture(GL_TEXTURE_2D, i);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
GLCompositor::~GLCompositor()
{
assert(glDeleteFramebuffers != nullptr);
glDeleteFramebuffers(CompScreenOutputFB.size(), &CompScreenOutputFB[0]);
glDeleteTextures(1, &CompScreenInputTex);
glDeleteTextures(CompScreenOutputTex.size(), &CompScreenOutputTex[0]);
glDeleteVertexArrays(1, &CompVertexArrayID);
glDeleteBuffers(1, &CompVertexBufferID);
OpenGL::DeleteShaderProgram(CompShader.data());
}
GLCompositor::GLCompositor(GLCompositor&& other) noexcept :
Scale(other.Scale),
ScreenH(other.ScreenH),
ScreenW(other.ScreenW),
CompScaleLoc(other.CompScaleLoc),
Comp3DXPosLoc(other.Comp3DXPosLoc),
CompVertices(other.CompVertices),
CompShader(other.CompShader),
CompVertexBufferID(other.CompVertexBufferID),
CompVertexArrayID(other.CompVertexArrayID),
CompScreenInputTex(other.CompScreenInputTex),
CompScreenOutputTex(other.CompScreenOutputTex),
CompScreenOutputFB(other.CompScreenOutputFB)
{
other.CompScreenOutputFB = {};
other.CompScreenInputTex = {};
other.CompScreenOutputTex = {};
other.CompVertexArrayID = {};
other.CompVertexBufferID = {};
other.CompShader = {};
}
GLCompositor& GLCompositor::operator=(GLCompositor&& other) noexcept
{
if (this != &other)
{
Scale = other.Scale;
ScreenH = other.ScreenH;
ScreenW = other.ScreenW;
CompScaleLoc = other.CompScaleLoc;
Comp3DXPosLoc = other.Comp3DXPosLoc;
CompVertices = other.CompVertices;
// Clean up these resources before overwriting them
OpenGL::DeleteShaderProgram(CompShader.data());
CompShader = other.CompShader;
glDeleteBuffers(1, &CompVertexBufferID);
CompVertexBufferID = other.CompVertexBufferID;
glDeleteVertexArrays(1, &CompVertexArrayID);
CompVertexArrayID = other.CompVertexArrayID;
glDeleteTextures(1, &CompScreenInputTex);
CompScreenInputTex = other.CompScreenInputTex;
glDeleteTextures(CompScreenOutputTex.size(), &CompScreenOutputTex[0]);
CompScreenOutputTex = other.CompScreenOutputTex;
glDeleteFramebuffers(CompScreenOutputFB.size(), &CompScreenOutputFB[0]);
CompScreenOutputFB = other.CompScreenOutputFB;
other.CompScreenOutputFB = {};
other.CompScreenInputTex = {};
other.CompScreenOutputTex = {};
other.CompVertexArrayID = {};
other.CompVertexBufferID = {};
other.CompShader = {};
}
return *this;
}
void GLCompositor::SetScaleFactor(int scale) noexcept
{
if (scale == Scale)
return;
Scale = scale;
ScreenW = 256 * scale;
ScreenH = (384+2) * scale;
for (int i = 0; i < 2; i++)
{
glBindTexture(GL_TEXTURE_2D, CompScreenOutputTex[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ScreenW, ScreenH, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// fill the padding
u8* zeroPixels = (u8*) calloc(1, ScreenW*2*scale*4);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 192*scale, ScreenW, 2*scale, GL_RGBA, GL_UNSIGNED_BYTE, zeroPixels);
GLenum fbassign[] = {GL_COLOR_ATTACHMENT0};
glBindFramebuffer(GL_FRAMEBUFFER, CompScreenOutputFB[i]);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, CompScreenOutputTex[i], 0);
glDrawBuffers(1, fbassign);
free(zeroPixels);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void GLCompositor::Stop(const GPU& gpu) noexcept
{
for (int i = 0; i < 2; i++)
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, CompScreenOutputFB[gpu.FrontBuffer]);
glClear(GL_COLOR_BUFFER_BIT);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void GLCompositor::RenderFrame(const GPU& gpu, GLRenderer& renderer) noexcept
{
int frontbuf = gpu.FrontBuffer;
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, CompScreenOutputFB[frontbuf]);
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
glColorMaski(0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glViewport(0, 0, ScreenW, ScreenH);
glClear(GL_COLOR_BUFFER_BIT);
// TODO: select more shaders (filtering, etc)
OpenGL::UseShaderProgram(CompShader.data());
glUniform1ui(CompScaleLoc, Scale);
// TODO: support setting this midframe, if ever needed
glUniform1i(Comp3DXPosLoc, ((int)gpu.GPU3D.GetRenderXPos() << 23) >> 23);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, CompScreenInputTex);
if (gpu.Framebuffer[frontbuf][0] && gpu.Framebuffer[frontbuf][1])
{
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256*3 + 1, 192, GL_RGBA_INTEGER,
GL_UNSIGNED_BYTE, gpu.Framebuffer[frontbuf][0].get());
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 192, 256*3 + 1, 192, GL_RGBA_INTEGER,
GL_UNSIGNED_BYTE, gpu.Framebuffer[frontbuf][1].get());
}
glActiveTexture(GL_TEXTURE1);
renderer.SetupAccelFrame();
glBindBuffer(GL_ARRAY_BUFFER, CompVertexBufferID);
glBindVertexArray(CompVertexArrayID);
glDrawArrays(GL_TRIANGLES, 0, 4*3);
}
void GLCompositor::BindOutputTexture(int buf)
{
glBindTexture(GL_TEXTURE_2D, CompScreenOutputTex[buf]);
}
}
|