| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/XFStateManager.h" |
| 5 | |
| 6 | #include "Common/ChunkFile.h" |
| 7 | |
| 8 | #include "VideoCommon/FramebufferManager.h" |
| 9 | #include "VideoCommon/VertexManagerBase.h" |
| 10 | #include "VideoCommon/XFMemory.h" |
| 11 | |
| 12 | void XFStateManager::Init() |
| 13 | { |
| 14 | // Initialize state tracking variables |
| 15 | ResetTexMatrixAChange(); |
| 16 | ResetTexMatrixBChange(); |
| 17 | ResetPosNormalChange(); |
| 18 | ResetProjection(); |
| 19 | ResetViewportChange(); |
| 20 | ResetTexMatrixInfoChange(); |
| 21 | ResetLightingConfigChange(); |
| 22 | ResetLightsChanged(); |
| 23 | ResetMaterialChanges(); |
| 24 | ResetPerVertexTransformMatrixChanges(); |
| 25 | ResetPerVertexNormalMatrixChanges(); |
| 26 | ResetPostTransformMatrixChanges(); |
| 27 | |
| 28 | std::memset(static_cast<void*>(&xfmem), 0, sizeof(xfmem)); |
| 29 | } |
| 30 | |
| 31 | void XFStateManager::DoState(PointerWrap& p) |
| 32 | { |
| 33 | p.DoArray(m_minmax_transform_matrices_changed); |
| 34 | p.DoArray(m_minmax_normal_matrices_changed); |
| 35 | p.DoArray(m_minmax_post_transform_matrices_changed); |
| 36 | p.DoArray(m_minmax_lights_changed); |
| 37 | |
| 38 | p.Do(m_materials_changed); |
| 39 | p.DoArray(m_tex_matrices_changed); |
| 40 | p.Do(m_pos_normal_matrix_changed); |
| 41 | p.Do(m_projection_changed); |
| 42 | p.Do(m_viewport_changed); |
| 43 | p.Do(m_tex_mtx_info_changed); |
| 44 | p.Do(m_lighting_config_changed); |
| 45 | |
| 46 | if (p.IsReadMode()) |
| 47 | { |
| 48 | // This is called after a savestate is loaded. |
| 49 | // Any constants that can changed based on settings should be re-calculated |
| 50 | m_projection_changed = true; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void XFStateManager::InvalidateXFRange(int start, int end) |
| 55 | { |
| 56 | if (((u32)start >= (u32)g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4 && |
| 57 | (u32)start < (u32)g_main_cp_state.matrix_index_a.PosNormalMtxIdx * 4 + 12) || |
| 58 | ((u32)start >= |
| 59 | XFMEM_NORMALMATRICES + ((u32)g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31) * 3 && |
| 60 | (u32)start < XFMEM_NORMALMATRICES + |
| 61 | ((u32)g_main_cp_state.matrix_index_a.PosNormalMtxIdx & 31) * 3 + 9)) |
| 62 | { |
| 63 | m_pos_normal_matrix_changed = true; |
| 64 | } |
| 65 | |
| 66 | if (((u32)start >= (u32)g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4 && |
| 67 | (u32)start < (u32)g_main_cp_state.matrix_index_a.Tex0MtxIdx * 4 + 12) || |
| 68 | ((u32)start >= (u32)g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4 && |
| 69 | (u32)start < (u32)g_main_cp_state.matrix_index_a.Tex1MtxIdx * 4 + 12) || |
| 70 | ((u32)start >= (u32)g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4 && |
| 71 | (u32)start < (u32)g_main_cp_state.matrix_index_a.Tex2MtxIdx * 4 + 12) || |
| 72 | ((u32)start >= (u32)g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4 && |
| 73 | (u32)start < (u32)g_main_cp_state.matrix_index_a.Tex3MtxIdx * 4 + 12)) |
| 74 | { |
| 75 | m_tex_matrices_changed[0] = true; |
| 76 | } |
| 77 | |
| 78 | if (((u32)start >= (u32)g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4 && |
| 79 | (u32)start < (u32)g_main_cp_state.matrix_index_b.Tex4MtxIdx * 4 + 12) || |
| 80 | ((u32)start >= (u32)g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4 && |
| 81 | (u32)start < (u32)g_main_cp_state.matrix_index_b.Tex5MtxIdx * 4 + 12) || |
| 82 | ((u32)start >= (u32)g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4 && |
| 83 | (u32)start < (u32)g_main_cp_state.matrix_index_b.Tex6MtxIdx * 4 + 12) || |
| 84 | ((u32)start >= (u32)g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4 && |
| 85 | (u32)start < (u32)g_main_cp_state.matrix_index_b.Tex7MtxIdx * 4 + 12)) |
| 86 | { |
| 87 | m_tex_matrices_changed[1] = true; |
| 88 | } |
| 89 | |
| 90 | if (start < XFMEM_POSMATRICES_END) |
| 91 | { |
| 92 | if (m_minmax_transform_matrices_changed[0] == -1) |
| 93 | { |
| 94 | m_minmax_transform_matrices_changed[0] = start; |
| 95 | m_minmax_transform_matrices_changed[1] = |
| 96 | end > XFMEM_POSMATRICES_END ? XFMEM_POSMATRICES_END : end; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | if (m_minmax_transform_matrices_changed[0] > start) |
| 101 | m_minmax_transform_matrices_changed[0] = start; |
| 102 | |
| 103 | if (m_minmax_transform_matrices_changed[1] < end) |
| 104 | m_minmax_transform_matrices_changed[1] = |
| 105 | end > XFMEM_POSMATRICES_END ? XFMEM_POSMATRICES_END : end; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (start < XFMEM_NORMALMATRICES_END && end > XFMEM_NORMALMATRICES) |
| 110 | { |
| 111 | int _start = start < XFMEM_NORMALMATRICES ? 0 : start - XFMEM_NORMALMATRICES; |
| 112 | int _end = end < XFMEM_NORMALMATRICES_END ? end - XFMEM_NORMALMATRICES : |
| 113 | XFMEM_NORMALMATRICES_END - XFMEM_NORMALMATRICES; |
| 114 | |
| 115 | if (m_minmax_normal_matrices_changed[0] == -1) |
| 116 | { |
| 117 | m_minmax_normal_matrices_changed[0] = _start; |
| 118 | m_minmax_normal_matrices_changed[1] = _end; |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | if (m_minmax_normal_matrices_changed[0] > _start) |
| 123 | m_minmax_normal_matrices_changed[0] = _start; |
| 124 | |
| 125 | if (m_minmax_normal_matrices_changed[1] < _end) |
| 126 | m_minmax_normal_matrices_changed[1] = _end; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (start < XFMEM_POSTMATRICES_END && end > XFMEM_POSTMATRICES) |
| 131 | { |
| 132 | int _start = start < XFMEM_POSTMATRICES ? XFMEM_POSTMATRICES : start - XFMEM_POSTMATRICES; |
| 133 | int _end = end < XFMEM_POSTMATRICES_END ? end - XFMEM_POSTMATRICES : |
| 134 | XFMEM_POSTMATRICES_END - XFMEM_POSTMATRICES; |
| 135 | |
| 136 | if (m_minmax_post_transform_matrices_changed[0] == -1) |
| 137 | { |
| 138 | m_minmax_post_transform_matrices_changed[0] = _start; |
| 139 | m_minmax_post_transform_matrices_changed[1] = _end; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | if (m_minmax_post_transform_matrices_changed[0] > _start) |
| 144 | m_minmax_post_transform_matrices_changed[0] = _start; |
| 145 | |
| 146 | if (m_minmax_post_transform_matrices_changed[1] < _end) |
| 147 | m_minmax_post_transform_matrices_changed[1] = _end; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (start < XFMEM_LIGHTS_END && end > XFMEM_LIGHTS) |
| 152 | { |
| 153 | int _start = start < XFMEM_LIGHTS ? XFMEM_LIGHTS : start - XFMEM_LIGHTS; |
| 154 | int _end = end < XFMEM_LIGHTS_END ? end - XFMEM_LIGHTS : XFMEM_LIGHTS_END - XFMEM_LIGHTS; |
| 155 | |
| 156 | if (m_minmax_lights_changed[0] == -1) |
| 157 | { |
| 158 | m_minmax_lights_changed[0] = _start; |
| 159 | m_minmax_lights_changed[1] = _end; |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | if (m_minmax_lights_changed[0] > _start) |
| 164 | m_minmax_lights_changed[0] = _start; |
| 165 | |
| 166 | if (m_minmax_lights_changed[1] < _end) |
| 167 | m_minmax_lights_changed[1] = _end; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void XFStateManager::SetTexMatrixChangedA(u32 Value) |
| 173 | { |
| 174 | if (g_main_cp_state.matrix_index_a.Hex != Value) |
| 175 | { |
| 176 | g_vertex_manager->Flush(); |
| 177 | if (g_main_cp_state.matrix_index_a.PosNormalMtxIdx != (Value & 0x3f)) |
| 178 | m_pos_normal_matrix_changed = true; |
| 179 | m_tex_matrices_changed[0] = true; |
| 180 | g_main_cp_state.matrix_index_a.Hex = Value; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void XFStateManager::ResetTexMatrixAChange() |
| 185 | { |
| 186 | m_tex_matrices_changed[0] = false; |
| 187 | } |
| 188 | |
| 189 | void XFStateManager::SetTexMatrixChangedB(u32 Value) |
| 190 | { |
| 191 | if (g_main_cp_state.matrix_index_b.Hex != Value) |
| 192 | { |
| 193 | g_vertex_manager->Flush(); |
| 194 | m_tex_matrices_changed[1] = true; |
| 195 | g_main_cp_state.matrix_index_b.Hex = Value; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void XFStateManager::ResetTexMatrixBChange() |
| 200 | { |
| 201 | m_tex_matrices_changed[1] = false; |
| 202 | } |
| 203 | |
| 204 | void XFStateManager::ResetPosNormalChange() |
| 205 | { |
| 206 | m_pos_normal_matrix_changed = false; |
| 207 | } |
| 208 | |
| 209 | void XFStateManager::SetProjectionChanged() |
| 210 | { |
| 211 | m_projection_changed = true; |
| 212 | } |
| 213 | |
| 214 | void XFStateManager::ResetProjection() |
| 215 | { |
| 216 | m_projection_changed = false; |
| 217 | } |
| 218 | |
| 219 | void XFStateManager::SetViewportChanged() |
| 220 | { |
| 221 | m_viewport_changed = true; |
| 222 | } |
| 223 | |
| 224 | void XFStateManager::ResetViewportChange() |
| 225 | { |
| 226 | m_viewport_changed = false; |
| 227 | } |
| 228 | |
| 229 | void XFStateManager::SetTexMatrixInfoChanged(int index) |
| 230 | { |
| 231 | // TODO: Should we track this with more precision, like which indices changed? |
| 232 | // The whole vertex constants are probably going to be uploaded regardless. |
| 233 | m_tex_mtx_info_changed = true; |
| 234 | } |
| 235 | |
| 236 | void XFStateManager::ResetTexMatrixInfoChange() |
| 237 | { |
| 238 | m_tex_mtx_info_changed = false; |
| 239 | } |
| 240 | |
| 241 | void XFStateManager::SetLightingConfigChanged() |
| 242 | { |
| 243 | m_lighting_config_changed = true; |
| 244 | } |
| 245 | |
| 246 | void XFStateManager::ResetLightingConfigChange() |
| 247 | { |
| 248 | m_lighting_config_changed = false; |
| 249 | } |
| 250 | |
| 251 | void XFStateManager::ResetLightsChanged() |
| 252 | { |
| 253 | m_minmax_lights_changed.fill(-1); |
| 254 | } |
| 255 | |
| 256 | void XFStateManager::SetMaterialColorChanged(int index) |
| 257 | { |
| 258 | m_materials_changed[index] = true; |
| 259 | } |
| 260 | |
| 261 | void XFStateManager::ResetMaterialChanges() |
| 262 | { |
| 263 | m_materials_changed = BitSet32(0); |
| 264 | } |
| 265 | |
| 266 | void XFStateManager::ResetPerVertexTransformMatrixChanges() |
| 267 | { |
| 268 | m_minmax_transform_matrices_changed.fill(-1); |
| 269 | } |
| 270 | |
| 271 | void XFStateManager::ResetPerVertexNormalMatrixChanges() |
| 272 | { |
| 273 | m_minmax_normal_matrices_changed.fill(-1); |
| 274 | } |
| 275 | |
| 276 | void XFStateManager::ResetPostTransformMatrixChanges() |
| 277 | { |
| 278 | m_minmax_post_transform_matrices_changed.fill(-1); |
| 279 | } |