| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/VertexShaderManager.h" |
| 5 | |
| 6 | #include <array> |
| 7 | #include <cmath> |
| 8 | #include <cstring> |
| 9 | #include <iterator> |
| 10 | |
| 11 | #include "Common/ChunkFile.h" |
| 12 | #include "Common/CommonTypes.h" |
| 13 | #include "Common/Logging/Log.h" |
| 14 | #include "Common/Matrix.h" |
| 15 | #include "VideoCommon/BPFunctions.h" |
| 16 | #include "VideoCommon/BPMemory.h" |
| 17 | #include "VideoCommon/CPMemory.h" |
| 18 | #include "VideoCommon/FramebufferManager.h" |
| 19 | #include "VideoCommon/FreeLookCamera.h" |
| 20 | #include "VideoCommon/GraphicsModSystem/Runtime/GraphicsModActionData.h" |
| 21 | #include "VideoCommon/GraphicsModSystem/Runtime/GraphicsModManager.h" |
| 22 | #include "VideoCommon/Statistics.h" |
| 23 | #include "VideoCommon/VertexManagerBase.h" |
| 24 | #include "VideoCommon/VideoCommon.h" |
| 25 | #include "VideoCommon/VideoConfig.h" |
| 26 | #include "VideoCommon/XFMemory.h" |
| 27 | #include "VideoCommon/XFStateManager.h" |
| 28 | |
| 29 | void VertexShaderManager::Init() |
| 30 | { |
| 31 | // Initialize state tracking variables |
| 32 | m_projection_graphics_mod_change = false; |
| 33 | |
| 34 | constants = {}; |
| 35 | |
| 36 | m_projection_matrix = Common::Matrix44::Identity().data; |
| 37 | |
| 38 | dirty = true; |
| 39 | } |
| 40 | |
| 41 | Common::Matrix44 VertexShaderManager::LoadProjectionMatrix() |
| 42 | { |
| 43 | const auto& rawProjection = xfmem.projection.rawProjection; |
| 44 | |
| 45 | switch (xfmem.projection.type) |
| 46 | { |
| 47 | case ProjectionType::Perspective: |
| 48 | { |
| 49 | const Common::Vec2 fov_multiplier = g_freelook_camera.IsActive() ? |
| 50 | g_freelook_camera.GetFieldOfViewMultiplier() : |
| 51 | Common::Vec2{1, 1}; |
| 52 | m_projection_matrix[0] = rawProjection[0] * g_ActiveConfig.fAspectRatioHackW * fov_multiplier.x; |
| 53 | m_projection_matrix[1] = 0.0f; |
| 54 | m_projection_matrix[2] = rawProjection[1] * g_ActiveConfig.fAspectRatioHackW * fov_multiplier.x; |
| 55 | m_projection_matrix[3] = 0.0f; |
| 56 | |
| 57 | m_projection_matrix[4] = 0.0f; |
| 58 | m_projection_matrix[5] = rawProjection[2] * g_ActiveConfig.fAspectRatioHackH * fov_multiplier.y; |
| 59 | m_projection_matrix[6] = rawProjection[3] * g_ActiveConfig.fAspectRatioHackH * fov_multiplier.y; |
| 60 | m_projection_matrix[7] = 0.0f; |
| 61 | |
| 62 | m_projection_matrix[8] = 0.0f; |
| 63 | m_projection_matrix[9] = 0.0f; |
| 64 | m_projection_matrix[10] = rawProjection[4]; |
| 65 | m_projection_matrix[11] = rawProjection[5]; |
| 66 | |
| 67 | m_projection_matrix[12] = 0.0f; |
| 68 | m_projection_matrix[13] = 0.0f; |
| 69 | |
| 70 | m_projection_matrix[14] = -1.0f; |
| 71 | m_projection_matrix[15] = 0.0f; |
| 72 | |
| 73 | g_stats.gproj = m_projection_matrix; |
| 74 | } |
| 75 | break; |
| 76 | |
| 77 | case ProjectionType::Orthographic: |
| 78 | { |
| 79 | m_projection_matrix[0] = rawProjection[0]; |
| 80 | m_projection_matrix[1] = 0.0f; |
| 81 | m_projection_matrix[2] = 0.0f; |
| 82 | m_projection_matrix[3] = rawProjection[1]; |
| 83 | |
| 84 | m_projection_matrix[4] = 0.0f; |
| 85 | m_projection_matrix[5] = rawProjection[2]; |
| 86 | m_projection_matrix[6] = 0.0f; |
| 87 | m_projection_matrix[7] = rawProjection[3]; |
| 88 | |
| 89 | m_projection_matrix[8] = 0.0f; |
| 90 | m_projection_matrix[9] = 0.0f; |
| 91 | m_projection_matrix[10] = rawProjection[4]; |
| 92 | m_projection_matrix[11] = rawProjection[5]; |
| 93 | |
| 94 | m_projection_matrix[12] = 0.0f; |
| 95 | m_projection_matrix[13] = 0.0f; |
| 96 | |
| 97 | m_projection_matrix[14] = 0.0f; |
| 98 | m_projection_matrix[15] = 1.0f; |
| 99 | |
| 100 | g_stats.g2proj = m_projection_matrix; |
| 101 | g_stats.proj = rawProjection; |
| 102 | } |
| 103 | break; |
| 104 | |
| 105 | default: |
| 106 | ERROR_LOG_FMT(VIDEO, "Unknown projection type: {}", xfmem.projection.type); |
| 107 | } |
| 108 | |
| 109 | PRIM_LOG("Projection: {} {} {} {} {} {}", rawProjection[0], rawProjection[1], rawProjection[2], |
| 110 | rawProjection[3], rawProjection[4], rawProjection[5]); |
| 111 | |
| 112 | auto corrected_matrix = Common::Matrix44::FromArray(m_projection_matrix); |
| 113 | |
| 114 | if (g_freelook_camera.IsActive() && xfmem.projection.type == ProjectionType::Perspective) |
| 115 | corrected_matrix *= g_freelook_camera.GetView(); |
| 116 | |
| 117 | g_freelook_camera.GetController()->SetClean(); |
| 118 | |
| 119 | return corrected_matrix; |
| 120 | } |
| 121 | |
| 122 | void VertexShaderManager::SetProjectionMatrix(XFStateManager& xf_state_manager) |
| 123 | { |
| 124 | if (xf_state_manager.DidProjectionChange() || g_freelook_camera.GetController()->IsDirty()) |
| 125 | { |
| 126 | xf_state_manager.ResetProjection(); |
| 127 | auto corrected_matrix = LoadProjectionMatrix(); |
| 128 | memcpy(constants.projection.data(), corrected_matrix.data.data(), 4 * sizeof(float4)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | bool VertexShaderManager::UseVertexDepthRange() |
| 133 | { |
| 134 | // Backend has full support for unrestricted depth ranges including the ability to clamp the |
| 135 | // final depth value to MAX_EFB_DEPTH. |
| 136 | if (g_backend_info.bSupportsUnrestrictedDepthRange) |
| 137 | return false; |
| 138 | |
| 139 | // We can't compute the depth range in the vertex shader if we don't support depth clamp. |
| 140 | if (!g_backend_info.bSupportsDepthClamp) |
| 141 | return false; |
| 142 | |
| 143 | // We need a full depth range if a ztexture is used. |
| 144 | if (bpmem.ztex2.op != ZTexOp::Disabled && !bpmem.zcontrol.early_ztest) |
| 145 | return true; |
| 146 | |
| 147 | // If an inverted depth range is unsupported, we also need to check if the range is inverted. |
| 148 | if (!g_backend_info.bSupportsReversedDepthRange) |
| 149 | { |
| 150 | if (xfmem.viewport.zRange < 0.0f) |
| 151 | return true; |
| 152 | |
| 153 | if (xfmem.viewport.zRange > xfmem.viewport.farZ) |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | // If an oversized depth range or a ztexture is used, we need to calculate the depth range |
| 158 | // in the vertex shader. |
| 159 | return fabs(xfmem.viewport.zRange) > 16777215.0f || fabs(xfmem.viewport.farZ) > 16777215.0f; |
| 160 | } |
| 161 | |
| 162 | // Syncs the shader constant buffers with xfmem |
| 163 | // TODO: A cleaner way to control the matrices without making a mess in the parameters field |
| 164 | void VertexShaderManager::SetConstants(std::span<const std::string> textures, |
| 165 | XFStateManager& xf_state_manager) |
| 166 | { |
| 167 | if (constants.missing_color_hex != g_ActiveConfig.iMissingColorValue) |
| 168 | { |
| 169 | const float a = (g_ActiveConfig.iMissingColorValue) & 0xFF; |
| 170 | const float b = (g_ActiveConfig.iMissingColorValue >> 8) & 0xFF; |
| 171 | const float g = (g_ActiveConfig.iMissingColorValue >> 16) & 0xFF; |
| 172 | const float r = (g_ActiveConfig.iMissingColorValue >> 24) & 0xFF; |
| 173 | constants.missing_color_hex = g_ActiveConfig.iMissingColorValue; |
| 174 | constants.missing_color_value = {r / 255, g / 255, b / 255, a / 255}; |
| 175 | |
| 176 | dirty = true; |
| 177 | } |
| 178 | |
| 179 | const auto per_vertex_transform_matrix_changes = |
| 180 | xf_state_manager.GetPerVertexTransformMatrixChanges(); |
| 181 | if (per_vertex_transform_matrix_changes[0] >= 0) |
| 182 | { |
| 183 | int startn = per_vertex_transform_matrix_changes[0] / 4; |
| 184 | int endn = (per_vertex_transform_matrix_changes[1] + 3) / 4; |
| 185 | memcpy(constants.transformmatrices[startn].data(), &xfmem.posMatrices[startn * 4], |
| 186 | (endn - startn) * sizeof(float4)); |
| 187 | dirty = true; |
| 188 | xf_state_manager.ResetPerVertexTransformMatrixChanges(); |
| 189 | } |
| 190 | |
| 191 | const auto per_vertex_normal_matrices_changed = |
| 192 | xf_state_manager.GetPerVertexNormalMatrixChanges(); |
| 193 | if (per_vertex_normal_matrices_changed[0] >= 0) |
| 194 | { |
| 195 | int startn = per_vertex_normal_matrices_changed[0] / 3; |
| 196 | int endn = (per_vertex_normal_matrices_changed[1] + 2) / 3; |
| 197 | for (int i = startn; i < endn; i++) |
| 198 | { |
| 199 | memcpy(constants.normalmatrices[i].data(), &xfmem.normalMatrices[3 * i], 12); |
| 200 | } |
| 201 | dirty = true; |
| 202 | xf_state_manager.ResetPerVertexNormalMatrixChanges(); |
| 203 | } |
| 204 | |
| 205 | const auto post_transform_matrices_changed = xf_state_manager.GetPostTransformMatrixChanges(); |
| 206 | if (post_transform_matrices_changed[0] >= 0) |
| 207 | { |
| 208 | int startn = post_transform_matrices_changed[0] / 4; |
| 209 | int endn = (post_transform_matrices_changed[1] + 3) / 4; |
| 210 | memcpy(constants.posttransformmatrices[startn].data(), &xfmem.postMatrices[startn * 4], |
| 211 | (endn - startn) * sizeof(float4)); |
| 212 | dirty = true; |
| 213 | xf_state_manager.ResetPostTransformMatrixChanges(); |
| 214 | } |
| 215 | |
| 216 | const auto light_changes = xf_state_manager.GetLightsChanged(); |
| 217 | if (light_changes[0] >= 0) |
| 218 | { |
| 219 | // TODO: Outdated comment |
| 220 | // lights don't have a 1 to 1 mapping, the color component needs to be converted to 4 floats |
| 221 | const int istart = light_changes[0] / 0x10; |
| 222 | const int iend = (light_changes[1] + 15) / 0x10; |
| 223 | |
| 224 | for (int i = istart; i < iend; ++i) |
| 225 | { |
| 226 | const Light& light = xfmem.lights[i]; |
| 227 | VertexShaderConstants::Light& dstlight = constants.lights[i]; |
| 228 | |
| 229 | // xfmem.light.color is packed as abgr in u8[4], so we have to swap the order |
| 230 | dstlight.color[0] = light.color[3]; |
| 231 | dstlight.color[1] = light.color[2]; |
| 232 | dstlight.color[2] = light.color[1]; |
| 233 | dstlight.color[3] = light.color[0]; |
| 234 | |
| 235 | dstlight.cosatt[0] = light.cosatt[0]; |
| 236 | dstlight.cosatt[1] = light.cosatt[1]; |
| 237 | dstlight.cosatt[2] = light.cosatt[2]; |
| 238 | |
| 239 | if (fabs(light.distatt[0]) < 0.00001f && fabs(light.distatt[1]) < 0.00001f && |
| 240 | fabs(light.distatt[2]) < 0.00001f) |
| 241 | { |
| 242 | // dist attenuation, make sure not equal to 0!!! |
| 243 | dstlight.distatt[0] = .00001f; |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | dstlight.distatt[0] = light.distatt[0]; |
| 248 | } |
| 249 | dstlight.distatt[1] = light.distatt[1]; |
| 250 | dstlight.distatt[2] = light.distatt[2]; |
| 251 | |
| 252 | dstlight.pos[0] = light.dpos[0]; |
| 253 | dstlight.pos[1] = light.dpos[1]; |
| 254 | dstlight.pos[2] = light.dpos[2]; |
| 255 | |
| 256 | // TODO: Hardware testing is needed to confirm that this normalization is correct |
| 257 | auto sanitize = [](float f) { |
| 258 | if (std::isnan(f)) |
| 259 | return 0.0f; |
| 260 | else if (std::isinf(f)) |
| 261 | return f > 0.0f ? 1.0f : -1.0f; |
| 262 | else |
| 263 | return f; |
| 264 | }; |
| 265 | double norm = double(light.ddir[0]) * double(light.ddir[0]) + |
| 266 | double(light.ddir[1]) * double(light.ddir[1]) + |
| 267 | double(light.ddir[2]) * double(light.ddir[2]); |
| 268 | norm = 1.0 / sqrt(norm); |
| 269 | dstlight.dir[0] = sanitize(static_cast<float>(light.ddir[0] * norm)); |
| 270 | dstlight.dir[1] = sanitize(static_cast<float>(light.ddir[1] * norm)); |
| 271 | dstlight.dir[2] = sanitize(static_cast<float>(light.ddir[2] * norm)); |
| 272 | } |
| 273 | dirty = true; |
| 274 | |
| 275 | xf_state_manager.ResetLightsChanged(); |
| 276 | } |
| 277 | |
| 278 | for (int i : xf_state_manager.GetMaterialChanges()) |
| 279 | { |
| 280 | u32 data = i >= 2 ? xfmem.matColor[i - 2] : xfmem.ambColor[i]; |
| 281 | constants.materials[i][0] = (data >> 24) & 0xFF; |
| 282 | constants.materials[i][1] = (data >> 16) & 0xFF; |
| 283 | constants.materials[i][2] = (data >> 8) & 0xFF; |
| 284 | constants.materials[i][3] = data & 0xFF; |
| 285 | dirty = true; |
| 286 | } |
| 287 | xf_state_manager.ResetMaterialChanges(); |
| 288 | |
| 289 | if (xf_state_manager.DidPosNormalChange()) |
| 290 | { |
| 291 | xf_state_manager.ResetPosNormalChange(); |
| 292 | const float* pos = &xfmem.posMatrices[g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4]; |
| 293 | const float* norm = |
| 294 | &xfmem.normalMatrices[3 * (g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31)]; |
| 295 | |
| 296 | memcpy(constants.posnormalmatrix.data(), pos, 3 * sizeof(float4)); |
| 297 | memcpy(constants.posnormalmatrix[3].data(), norm, 3 * sizeof(float)); |
| 298 | memcpy(constants.posnormalmatrix[4].data(), norm + 3, 3 * sizeof(float)); |
| 299 | memcpy(constants.posnormalmatrix[5].data(), norm + 6, 3 * sizeof(float)); |
| 300 | dirty = true; |
| 301 | } |
| 302 | |
| 303 | if (xf_state_manager.DidTexMatrixAChange()) |
| 304 | { |
| 305 | xf_state_manager.ResetTexMatrixAChange(); |
| 306 | const std::array<const float*, 4> pos_matrix_ptrs{ |
| 307 | &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4], |
| 308 | &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4], |
| 309 | &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4], |
| 310 | &xfmem.posMatrices[g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4], |
| 311 | }; |
| 312 | |
| 313 | for (size_t i = 0; i < pos_matrix_ptrs.size(); ++i) |
| 314 | { |
| 315 | memcpy(constants.texmatrices[3 * i].data(), pos_matrix_ptrs[i], 3 * sizeof(float4)); |
| 316 | } |
| 317 | dirty = true; |
| 318 | } |
| 319 | |
| 320 | if (xf_state_manager.DidTexMatrixBChange()) |
| 321 | { |
| 322 | xf_state_manager.ResetTexMatrixBChange(); |
| 323 | const std::array<const float*, 4> pos_matrix_ptrs{ |
| 324 | &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4], |
| 325 | &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4], |
| 326 | &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4], |
| 327 | &xfmem.posMatrices[g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4], |
| 328 | }; |
| 329 | |
| 330 | for (size_t i = 0; i < pos_matrix_ptrs.size(); ++i) |
| 331 | { |
| 332 | memcpy(constants.texmatrices[3 * i + 12].data(), pos_matrix_ptrs[i], 3 * sizeof(float4)); |
| 333 | } |
| 334 | dirty = true; |
| 335 | } |
| 336 | |
| 337 | if (xf_state_manager.DidViewportChange()) |
| 338 | { |
| 339 | xf_state_manager.ResetViewportChange(); |
| 340 | |
| 341 | // The console GPU places the pixel center at 7/12 unless antialiasing |
| 342 | // is enabled, while D3D and OpenGL place it at 0.5. See the comment |
| 343 | // in VertexShaderGen.cpp for details. |
| 344 | // NOTE: If we ever emulate antialiasing, the sample locations set by |
| 345 | // BP registers 0x01-0x04 need to be considered here. |
| 346 | const float pixel_center_correction = 7.0f / 12.0f - 0.5f; |
| 347 | const bool bUseVertexRounding = g_ActiveConfig.UseVertexRounding(); |
| 348 | const float viewport_width = bUseVertexRounding ? |
| 349 | (2.f * xfmem.viewport.wd) : |
| 350 | g_framebuffer_manager->EFBToScaledXf(2.f * xfmem.viewport.wd); |
| 351 | const float viewport_height = bUseVertexRounding ? |
| 352 | (2.f * xfmem.viewport.ht) : |
| 353 | g_framebuffer_manager->EFBToScaledXf(2.f * xfmem.viewport.ht); |
| 354 | const float pixel_size_x = 2.f / viewport_width; |
| 355 | const float pixel_size_y = 2.f / viewport_height; |
| 356 | constants.pixelcentercorrection[0] = pixel_center_correction * pixel_size_x; |
| 357 | constants.pixelcentercorrection[1] = pixel_center_correction * pixel_size_y; |
| 358 | |
| 359 | // By default we don't change the depth value at all in the vertex shader. |
| 360 | constants.pixelcentercorrection[2] = 1.0f; |
| 361 | constants.pixelcentercorrection[3] = 0.0f; |
| 362 | |
| 363 | constants.viewport[0] = (2.f * xfmem.viewport.wd); |
| 364 | constants.viewport[1] = (2.f * xfmem.viewport.ht); |
| 365 | |
| 366 | if (UseVertexDepthRange()) |
| 367 | { |
| 368 | // Oversized depth ranges are handled in the vertex shader. We need to reverse |
| 369 | // the far value to use the reversed-Z trick. |
| 370 | if (g_backend_info.bSupportsReversedDepthRange) |
| 371 | { |
| 372 | // Sometimes the console also tries to use the reversed-Z trick. We can only do |
| 373 | // that with the expected accuracy if the backend can reverse the depth range. |
| 374 | constants.pixelcentercorrection[2] = fabs(xfmem.viewport.zRange) / 16777215.0f; |
| 375 | if (xfmem.viewport.zRange < 0.0f) |
| 376 | constants.pixelcentercorrection[3] = xfmem.viewport.farZ / 16777215.0f; |
| 377 | else |
| 378 | constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f; |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | // For backends that don't support reversing the depth range we can still render |
| 383 | // cases where the console uses the reversed-Z trick. But we simply can't provide |
| 384 | // the expected accuracy, which might result in z-fighting. |
| 385 | constants.pixelcentercorrection[2] = xfmem.viewport.zRange / 16777215.0f; |
| 386 | constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | dirty = true; |
| 391 | BPFunctions::SetScissorAndViewport(g_framebuffer_manager.get(), bpmem.scissorTL, |
| 392 | bpmem.scissorBR, bpmem.scissorOffset, xfmem.viewport); |
| 393 | g_stats.AddScissorRect(); |
| 394 | } |
| 395 | |
| 396 | std::vector<GraphicsModAction*> projection_actions; |
| 397 | if (g_ActiveConfig.bGraphicMods) |
| 398 | { |
| 399 | for (const auto& action : g_graphics_mod_manager->GetProjectionActions(xfmem.projection.type)) |
| 400 | { |
| 401 | projection_actions.push_back(action); |
| 402 | } |
| 403 | |
| 404 | for (const auto& texture : textures) |
| 405 | { |
| 406 | for (const auto& action : |
| 407 | g_graphics_mod_manager->GetProjectionTextureActions(xfmem.projection.type, texture)) |
| 408 | { |
| 409 | projection_actions.push_back(action); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (xf_state_manager.DidProjectionChange() || g_freelook_camera.GetController()->IsDirty() || |
| 415 | !projection_actions.empty() || m_projection_graphics_mod_change) |
| 416 | { |
| 417 | xf_state_manager.ResetProjection(); |
| 418 | m_projection_graphics_mod_change = !projection_actions.empty(); |
| 419 | |
| 420 | auto corrected_matrix = LoadProjectionMatrix(); |
| 421 | |
| 422 | GraphicsModActionData::Projection projection{&corrected_matrix}; |
| 423 | for (const auto& action : projection_actions) |
| 424 | { |
| 425 | action->OnProjection(&projection); |
| 426 | } |
| 427 | |
| 428 | memcpy(constants.projection.data(), corrected_matrix.data.data(), 4 * sizeof(float4)); |
| 429 | dirty = true; |
| 430 | } |
| 431 | |
| 432 | if (xf_state_manager.DidTexMatrixInfoChange()) |
| 433 | { |
| 434 | xf_state_manager.ResetTexMatrixInfoChange(); |
| 435 | constants.xfmem_dualTexInfo = xfmem.dualTexTrans.enabled; |
| 436 | for (size_t i = 0; i < std::size(xfmem.texMtxInfo); i++) |
| 437 | constants.xfmem_pack1[i][0] = xfmem.texMtxInfo[i].hex; |
| 438 | for (size_t i = 0; i < std::size(xfmem.postMtxInfo); i++) |
| 439 | constants.xfmem_pack1[i][1] = xfmem.postMtxInfo[i].hex; |
| 440 | |
| 441 | dirty = true; |
| 442 | } |
| 443 | |
| 444 | if (xf_state_manager.DidLightingConfigChange()) |
| 445 | { |
| 446 | xf_state_manager.ResetLightingConfigChange(); |
| 447 | |
| 448 | for (size_t i = 0; i < 2; i++) |
| 449 | { |
| 450 | constants.xfmem_pack1[i][2] = xfmem.color[i].hex; |
| 451 | constants.xfmem_pack1[i][3] = xfmem.alpha[i].hex; |
| 452 | } |
| 453 | constants.xfmem_numColorChans = xfmem.numChan.numColorChans; |
| 454 | dirty = true; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void VertexShaderManager::TransformToClipSpace(const float* data, float* out, u32 MtxIdx) |
| 459 | { |
| 460 | const float* world_matrix = &xfmem.posMatrices[(MtxIdx & 0x3f) * 4]; |
| 461 | |
| 462 | // We use the projection matrix calculated by VertexShaderManager, because it |
| 463 | // includes any free look transformations. |
| 464 | // Make sure VertexShaderManager::SetConstants() has been called first. |
| 465 | const float* proj_matrix = &m_projection_matrix[0]; |
| 466 | |
| 467 | const float t[3] = {data[0] * world_matrix[0] + data[1] * world_matrix[1] + |
| 468 | data[2] * world_matrix[2] + world_matrix[3], |
| 469 | data[0] * world_matrix[4] + data[1] * world_matrix[5] + |
| 470 | data[2] * world_matrix[6] + world_matrix[7], |
| 471 | data[0] * world_matrix[8] + data[1] * world_matrix[9] + |
| 472 | data[2] * world_matrix[10] + world_matrix[11]}; |
| 473 | |
| 474 | out[0] = t[0] * proj_matrix[0] + t[1] * proj_matrix[1] + t[2] * proj_matrix[2] + proj_matrix[3]; |
| 475 | out[1] = t[0] * proj_matrix[4] + t[1] * proj_matrix[5] + t[2] * proj_matrix[6] + proj_matrix[7]; |
| 476 | out[2] = t[0] * proj_matrix[8] + t[1] * proj_matrix[9] + t[2] * proj_matrix[10] + proj_matrix[11]; |
| 477 | out[3] = |
| 478 | t[0] * proj_matrix[12] + t[1] * proj_matrix[13] + t[2] * proj_matrix[14] + proj_matrix[15]; |
| 479 | } |
| 480 | |
| 481 | void VertexShaderManager::DoState(PointerWrap& p) |
| 482 | { |
| 483 | p.DoArray(m_projection_matrix); |
| 484 | g_freelook_camera.DoState(p); |
| 485 | |
| 486 | p.Do(constants); |
| 487 | |
| 488 | if (p.IsReadMode()) |
| 489 | { |
| 490 | dirty = true; |
| 491 | } |
| 492 | } |