|
34 | 34 | #include <stdint.h> |
35 | 35 | #include <string.h> |
36 | 36 |
|
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 | | - |
44 | 37 | static const float twl_wiiu_texcoords[] = { |
45 | 38 | 0.0f, 1.0f, |
46 | 39 | 1.0f, 1.0f, |
47 | 40 | 0.0f, 0.0f, |
48 | 41 | 1.0f, 0.0f, |
49 | 42 | }; |
50 | 43 |
|
| 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 | + |
51 | 71 | static bool twl_wiiu_create_buffer( |
52 | 72 | GX2RBuffer *buffer, uint32_t elem_size, uint32_t elem_count, |
53 | 73 | const void *data) { |
@@ -108,8 +128,11 @@ static void twl_wiiu_teardown(TwlWiiU *wiiu) { |
108 | 128 | GX2RDestroySurfaceEx(&wiiu->texture.surface, 0); |
109 | 129 | wiiu->texture_ready = false; |
110 | 130 | } |
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); |
113 | 136 | } |
114 | 137 | if (wiiu->texcoord_buffer.flags) { |
115 | 138 | GX2RDestroyBufferEx(&wiiu->texcoord_buffer, 0); |
@@ -187,13 +210,29 @@ TwlResult twl_backend_init( |
187 | 210 | WHBFreeWholeFile(shader_file); |
188 | 211 | wiiu->shader_ready = true; |
189 | 212 |
|
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 | + } |
197 | 236 | } |
198 | 237 |
|
199 | 238 | if (!twl_wiiu_create_texture( |
|
0 commit comments