Skip to content

Commit b74a20e

Browse files
committed
opengl: use horizontal and vertical display ranges
Clip and move display
1 parent 9f37bdf commit b74a20e

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

data/shader/blit.shader

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
uniform sampler2D renderBuffer;
2-
uniform vec2 clipLeftTop;
3-
uniform vec2 clipRightBottom;
2+
uniform vec2 iResolution;
3+
uniform vec2 displayHorizontal;
4+
uniform vec2 displayVertical;
5+
uniform bool displayEnabled;
46

57
SHARED vec2 fragTexcoord;
68

@@ -17,10 +19,12 @@ void main() {
1719

1820
#ifdef FRAGMENT_SHADER
1921
void main() {
20-
vec2 pos = vec2(fragTexcoord.x, fragTexcoord.y);
22+
vec2 pos = gl_FragCoord.xy / iResolution;
23+
pos.y = 1. - pos.y;
2124

22-
if (pos.x < clipLeftTop.x || pos.y < clipLeftTop.y ||
23-
pos.x >= clipRightBottom.x || pos.y >= clipRightBottom.y) {
25+
if (!displayEnabled ||
26+
pos.x < displayHorizontal.x || pos.x >= displayHorizontal.y ||
27+
pos.y < displayVertical.x || pos.y >= displayVertical.y) {
2428
outColor = vec4(0.0, 0.0, 0.0, 1.0);
2529
return;
2630
}

src/renderer/opengl/opengl.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,36 @@ void OpenGL::renderBlit(gpu::GPU* gpu, bool software) {
355355
y = static_cast<int>((height - h) / 2.f);
356356
}
357357

358-
float y2 = static_cast<float>(gpu->displayRangeY2 - gpu->displayRangeY1);
359-
if (gpu->gp1_08.getVerticalResoulution() == 480) y2 *= 2;
360-
// TODO: convert xy to screen space
361-
blitShader->getUniform("clipLeftTop").f(0, 0);
362-
blitShader->getUniform("clipRightBottom").f(1024, gpu->displayAreaStartY + y2);
358+
{
359+
float yOffset = y / static_cast<float>(h); // Compensate for aspect ratio
360+
float xOffset = x / static_cast<float>(w);
361+
362+
float vResolution = gpu->isNtsc() ? 240 : 256;
363+
364+
float displayTop = 0.f;
365+
float displayBottom = (gpu->displayRangeY2 - gpu->displayRangeY1) / vResolution;
366+
367+
// V aligment disabled for now
368+
// int firstLine = gpu->isNtsc() ? (0x88 - 224/2) : (0xA3 - 264/2);
369+
// y -= ((gpu->displayRangeY1 - firstLine) / vResolution) * h;
370+
371+
float hres = gpu->gp1_08.getHorizontalResoulution();
372+
int cyclesPerPixel = ceilf(640 * 4 / hres);
373+
374+
float displayXOffset = (gpu->displayRangeX1 - 0x260) / cyclesPerPixel / hres;
375+
376+
float displayLeft = 0.f;
377+
float displayRight = (gpu->displayRangeX2 - gpu->displayRangeX1) / cyclesPerPixel / hres;
378+
379+
// Move display to right by offset
380+
x += displayXOffset * w;
381+
382+
blitShader->getUniform("iResolution").f(w, h);
383+
blitShader->getUniform("displayHorizontal").f(displayLeft + xOffset, displayRight + xOffset);
384+
blitShader->getUniform("displayVertical").f(displayTop - yOffset, displayBottom - yOffset);
385+
}
386+
387+
blitShader->getUniform("displayEnabled").i(!gpu->displayDisable);
363388

364389
glViewport(x, y, w, h);
365390
blitBuffer->update(bb.size() * sizeof(BlitStruct), bb.data());

0 commit comments

Comments
 (0)