Skip to content

Commit 7e0ba4f

Browse files
author
Trulio
committed
Fix wiiu aspect ratio
1 parent e08305a commit 7e0ba4f

5 files changed

Lines changed: 81 additions & 34 deletions

File tree

‎.github/workflows/switch.yml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ name: Build Nintendo Switch NRO
22

33
on:
44
push:
5-
branches:
6-
- master
75
paths:
86
- '.github/workflows/switch.yml'
97
- 'CMakeLists.txt'
@@ -22,10 +20,6 @@ on:
2220
permissions:
2321
contents: read
2422

25-
concurrency:
26-
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
27-
cancel-in-progress: true
28-
2923
jobs:
3024
build:
3125
runs-on: ubuntu-latest

‎thirdparty/twl/backends/wiiu/backend.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434

3535
typedef struct {
3636
WHBGfxShaderGroup shader;
37-
GX2RBuffer position_buffer;
37+
GX2RBuffer tv_position_buffer;
38+
GX2RBuffer drc_position_buffer;
3839
GX2RBuffer texcoord_buffer;
3940
GX2Texture texture;
4041
GX2Sampler sampler;

‎thirdparty/twl/backends/wiiu/input.c‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,26 @@ static void twl_wiiu_read_touch(
9898
VPADGetTPCalibratedPointEx(
9999
VPAD_CHAN_0, VPAD_TP_854X480, &touch, &status->tpNormal);
100100
if (touch.touched) {
101-
const int32_t x =
102-
twl_wiiu_clamp((int32_t) touch.x, 0, TWL_WIIU_TP_WIDTH - 1) *
103-
(int32_t) wiiu->frame_width / TWL_WIIU_TP_WIDTH;
104-
const int32_t y =
105-
twl_wiiu_clamp((int32_t) touch.y, 0, TWL_WIIU_TP_HEIGHT - 1) *
106-
(int32_t) wiiu->frame_height / TWL_WIIU_TP_HEIGHT;
101+
const int32_t frame_w = (int32_t) wiiu->frame_width;
102+
const int32_t frame_h = (int32_t) wiiu->frame_height;
103+
int32_t content_w = TWL_WIIU_TP_WIDTH;
104+
int32_t content_h = TWL_WIIU_TP_HEIGHT;
105+
int32_t origin_x = 0;
106+
int32_t origin_y = 0;
107+
int32_t x;
108+
int32_t y;
109+
if ((int64_t) TWL_WIIU_TP_WIDTH * frame_h >=
110+
(int64_t) TWL_WIIU_TP_HEIGHT * frame_w) {
111+
content_w = TWL_WIIU_TP_HEIGHT * frame_w / frame_h;
112+
origin_x = (TWL_WIIU_TP_WIDTH - content_w) / 2;
113+
} else {
114+
content_h = TWL_WIIU_TP_WIDTH * frame_h / frame_w;
115+
origin_y = (TWL_WIIU_TP_HEIGHT - content_h) / 2;
116+
}
117+
x = twl_wiiu_clamp(
118+
((int32_t) touch.x - origin_x) * frame_w / content_w, 0, frame_w - 1);
119+
y = twl_wiiu_clamp(
120+
((int32_t) touch.y - origin_y) * frame_h / content_h, 0, frame_h - 1);
107121
if (!wiiu->touching || x != wiiu->pointer_x || y != wiiu->pointer_y) {
108122
twl_wiiu_push_pointer(twl, TWL_EVENT_POINTER_MOVE, x, y);
109123
}

‎thirdparty/twl/backends/wiiu/presentation.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,15 @@ static void twl_wiiu_upload(TwlWiiU *wiiu, const TwlSurface *surface) {
6060
GX2RUnlockSurfaceEx(&wiiu->texture.surface, 0, 0);
6161
}
6262

63-
static void twl_wiiu_draw(TwlWiiU *wiiu) {
63+
static void twl_wiiu_draw(TwlWiiU *wiiu, GX2RBuffer *positions) {
6464
GX2SetCullOnlyControl(GX2_FRONT_FACE_CCW, FALSE, FALSE);
6565
GX2SetDepthOnlyControl(FALSE, FALSE, GX2_COMPARE_FUNC_ALWAYS);
6666
GX2SetFetchShader(&wiiu->shader.fetchShader);
6767
GX2SetVertexShader(wiiu->shader.vertexShader);
6868
GX2SetPixelShader(wiiu->shader.pixelShader);
6969
GX2SetPixelTexture(&wiiu->texture, 0);
7070
GX2SetPixelSampler(&wiiu->sampler, 0);
71-
GX2RSetAttributeBuffer(
72-
&wiiu->position_buffer, 0, wiiu->position_buffer.elemSize, 0);
71+
GX2RSetAttributeBuffer(positions, 0, positions->elemSize, 0);
7372
GX2RSetAttributeBuffer(
7473
&wiiu->texcoord_buffer, 1, wiiu->texcoord_buffer.elemSize, 0);
7574
GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLE_STRIP, 4, 0, 1);
@@ -101,12 +100,12 @@ TwlResult twl_backend_prepare_frame(Twl *twl, const TwlSurface *surface) {
101100

102101
WHBGfxBeginRenderTV();
103102
WHBGfxClearColor(0.0f, 0.0f, 0.0f, 1.0f);
104-
twl_wiiu_draw(wiiu);
103+
twl_wiiu_draw(wiiu, &wiiu->tv_position_buffer);
105104
WHBGfxFinishRenderTV();
106105

107106
WHBGfxBeginRenderDRC();
108107
WHBGfxClearColor(0.0f, 0.0f, 0.0f, 1.0f);
109-
twl_wiiu_draw(wiiu);
108+
twl_wiiu_draw(wiiu, &wiiu->drc_position_buffer);
110109
WHBGfxFinishRenderDRC();
111110

112111
return TWL_RESULT_OK;

‎thirdparty/twl/backends/wiiu/window.c‎

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,40 @@
3434
#include <stdint.h>
3535
#include <string.h>
3636

37-
static const float twl_wiiu_positions[] = {
38-
-1.0f, -1.0f, 0.0f, 1.0f,
39-
1.0f, -1.0f, 0.0f, 1.0f,
40-
-1.0f, 1.0f, 0.0f, 1.0f,
41-
1.0f, 1.0f, 0.0f, 1.0f,
42-
};
43-
4437
static const float twl_wiiu_texcoords[] = {
4538
0.0f, 1.0f,
4639
1.0f, 1.0f,
4740
0.0f, 0.0f,
4841
1.0f, 0.0f,
4942
};
5043

44+
static void twl_wiiu_letterbox_positions(
45+
float *positions, uint32_t content_w, uint32_t content_h,
46+
uint32_t screen_w, uint32_t screen_h) {
47+
float scale_x = 1.0f;
48+
float scale_y = 1.0f;
49+
size_t vertex;
50+
if (content_w > 0u && content_h > 0u && screen_w > 0u && screen_h > 0u) {
51+
const uint64_t content = (uint64_t) content_w * screen_h;
52+
const uint64_t screen = (uint64_t) screen_w * content_h;
53+
if (screen > content) {
54+
scale_x = (float) content / (float) screen;
55+
} else if (content > screen) {
56+
scale_y = (float) screen / (float) content;
57+
}
58+
}
59+
{
60+
const float xs[4] = {-scale_x, scale_x, -scale_x, scale_x};
61+
const float ys[4] = {-scale_y, -scale_y, scale_y, scale_y};
62+
for (vertex = 0u; vertex < 4u; ++vertex) {
63+
positions[vertex * 4u + 0u] = xs[vertex];
64+
positions[vertex * 4u + 1u] = ys[vertex];
65+
positions[vertex * 4u + 2u] = 0.0f;
66+
positions[vertex * 4u + 3u] = 1.0f;
67+
}
68+
}
69+
}
70+
5171
static bool twl_wiiu_create_buffer(
5272
GX2RBuffer *buffer, uint32_t elem_size, uint32_t elem_count,
5373
const void *data) {
@@ -108,8 +128,11 @@ static void twl_wiiu_teardown(TwlWiiU *wiiu) {
108128
GX2RDestroySurfaceEx(&wiiu->texture.surface, 0);
109129
wiiu->texture_ready = false;
110130
}
111-
if (wiiu->position_buffer.flags) {
112-
GX2RDestroyBufferEx(&wiiu->position_buffer, 0);
131+
if (wiiu->tv_position_buffer.flags) {
132+
GX2RDestroyBufferEx(&wiiu->tv_position_buffer, 0);
133+
}
134+
if (wiiu->drc_position_buffer.flags) {
135+
GX2RDestroyBufferEx(&wiiu->drc_position_buffer, 0);
113136
}
114137
if (wiiu->texcoord_buffer.flags) {
115138
GX2RDestroyBufferEx(&wiiu->texcoord_buffer, 0);
@@ -187,13 +210,29 @@ TwlResult twl_backend_init(
187210
WHBFreeWholeFile(shader_file);
188211
wiiu->shader_ready = true;
189212

190-
if (!twl_wiiu_create_buffer(
191-
&wiiu->position_buffer, 4u * sizeof(float), 4u, twl_wiiu_positions) ||
192-
!twl_wiiu_create_buffer(
193-
&wiiu->texcoord_buffer, 2u * sizeof(float), 4u, twl_wiiu_texcoords)) {
194-
WHBLogPrint("[twl-wiiu] ERROR: attribute buffer creation failed");
195-
twl_wiiu_teardown(wiiu);
196-
return TWL_RESULT_INSUFFICIENT_MEMORY;
213+
{
214+
const GX2ColorBuffer *tv = WHBGfxGetTVColourBuffer();
215+
const GX2ColorBuffer *drc = WHBGfxGetDRCColourBuffer();
216+
float tv_positions[16];
217+
float drc_positions[16];
218+
twl_wiiu_letterbox_positions(
219+
tv_positions, wiiu->frame_width, wiiu->frame_height,
220+
tv ? tv->surface.width : wiiu->frame_width,
221+
tv ? tv->surface.height : wiiu->frame_height);
222+
twl_wiiu_letterbox_positions(
223+
drc_positions, wiiu->frame_width, wiiu->frame_height,
224+
drc ? drc->surface.width : wiiu->frame_width,
225+
drc ? drc->surface.height : wiiu->frame_height);
226+
if (!twl_wiiu_create_buffer(
227+
&wiiu->tv_position_buffer, 4u * sizeof(float), 4u, tv_positions) ||
228+
!twl_wiiu_create_buffer(
229+
&wiiu->drc_position_buffer, 4u * sizeof(float), 4u, drc_positions) ||
230+
!twl_wiiu_create_buffer(
231+
&wiiu->texcoord_buffer, 2u * sizeof(float), 4u, twl_wiiu_texcoords)) {
232+
WHBLogPrint("[twl-wiiu] ERROR: attribute buffer creation failed");
233+
twl_wiiu_teardown(wiiu);
234+
return TWL_RESULT_INSUFFICIENT_MEMORY;
235+
}
197236
}
198237

199238
if (!twl_wiiu_create_texture(

0 commit comments

Comments
 (0)