2015-05-23 22:32:32 -06:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-23 22:32:32 -06:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2010-10-03 02:20:24 -06:00
|
|
|
|
2015-12-20 19:49:49 -07:00
|
|
|
#include <memory>
|
2013-02-26 21:47:50 -07:00
|
|
|
#include <vector>
|
2015-12-20 19:49:49 -07:00
|
|
|
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2018-05-10 17:06:19 -06:00
|
|
|
#include "Common/MathUtil.h"
|
2019-12-05 08:01:33 -07:00
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
2017-04-30 02:07:57 -06:00
|
|
|
#include "VideoCommon/RenderState.h"
|
2018-02-24 08:15:35 -07:00
|
|
|
#include "VideoCommon/ShaderCache.h"
|
2013-02-26 21:47:50 -07:00
|
|
|
|
2016-01-31 12:51:55 -07:00
|
|
|
class DataReader;
|
2010-11-26 02:25:08 -07:00
|
|
|
class NativeVertexFormat;
|
2012-01-04 01:42:22 -07:00
|
|
|
class PointerWrap;
|
2016-01-17 14:54:31 -07:00
|
|
|
struct PortableVertexDeclaration;
|
2010-11-26 02:25:08 -07:00
|
|
|
|
2015-01-13 02:55:25 -07:00
|
|
|
struct Slope
|
|
|
|
{
|
|
|
|
float dfdx;
|
|
|
|
float dfdy;
|
|
|
|
float f0;
|
2015-01-23 07:15:09 -07:00
|
|
|
bool dirty;
|
2015-01-13 02:55:25 -07:00
|
|
|
};
|
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
// View format of the input data to the texture decoding shader.
|
|
|
|
enum TexelBufferFormat : u32
|
|
|
|
{
|
|
|
|
TEXEL_BUFFER_FORMAT_R8_UINT,
|
|
|
|
TEXEL_BUFFER_FORMAT_R16_UINT,
|
|
|
|
TEXEL_BUFFER_FORMAT_RGBA8_UINT,
|
|
|
|
TEXEL_BUFFER_FORMAT_R32G32_UINT,
|
|
|
|
NUM_TEXEL_BUFFER_FORMATS
|
|
|
|
};
|
|
|
|
|
2021-04-30 15:57:12 -06:00
|
|
|
namespace OpcodeDecoder
|
|
|
|
{
|
|
|
|
enum class Primitive : u8;
|
|
|
|
};
|
|
|
|
|
2015-11-01 14:54:41 -07:00
|
|
|
class VertexManagerBase
|
2010-10-02 18:41:06 -06:00
|
|
|
{
|
2013-02-21 22:12:53 -07:00
|
|
|
private:
|
2016-10-01 01:37:17 -06:00
|
|
|
// 3 pos
|
|
|
|
static constexpr u32 SMALLEST_POSSIBLE_VERTEX = sizeof(float) * 3;
|
|
|
|
// 3 pos, 3*3 normal, 2*u32 color, 8*4 tex, 1 posMat
|
|
|
|
static constexpr u32 LARGEST_POSSIBLE_VERTEX = sizeof(float) * 45 + sizeof(u32) * 2;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-10-01 01:37:17 -06:00
|
|
|
static constexpr u32 MAX_PRIMITIVES_PER_COMMAND = 65535;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2020-01-25 18:04:50 -07:00
|
|
|
// Used for 16:9 anamorphic widescreen heuristic.
|
|
|
|
struct FlushStatistics
|
|
|
|
{
|
|
|
|
struct ProjectionCounts
|
|
|
|
{
|
|
|
|
size_t normal_flush_count;
|
|
|
|
size_t anamorphic_flush_count;
|
|
|
|
size_t other_flush_count;
|
|
|
|
|
|
|
|
size_t normal_vertex_count;
|
|
|
|
size_t anamorphic_vertex_count;
|
|
|
|
size_t other_vertex_count;
|
|
|
|
|
|
|
|
size_t GetTotalFlushCount() const
|
|
|
|
{
|
|
|
|
return normal_flush_count + anamorphic_flush_count + other_flush_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetTotalVertexCount() const
|
|
|
|
{
|
|
|
|
return normal_vertex_count + anamorphic_vertex_count + other_vertex_count;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ProjectionCounts perspective;
|
|
|
|
ProjectionCounts orthographic;
|
|
|
|
};
|
|
|
|
|
2010-10-02 18:41:06 -06:00
|
|
|
public:
|
2016-10-01 01:37:17 -06:00
|
|
|
static constexpr u32 MAXVBUFFERSIZE =
|
2018-05-10 17:06:19 -06:00
|
|
|
MathUtil::NextPowerOf2(MAX_PRIMITIVES_PER_COMMAND * LARGEST_POSSIBLE_VERTEX);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-02-21 22:12:53 -07:00
|
|
|
// We may convert triangle-fans to triangle-lists, almost 3x as many indices.
|
2018-05-10 17:06:19 -06:00
|
|
|
static constexpr u32 MAXIBUFFERSIZE = MathUtil::NextPowerOf2(MAX_PRIMITIVES_PER_COMMAND * 3);
|
2010-10-03 02:20:24 -06:00
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
// Streaming buffer sizes.
|
|
|
|
// Texel buffer will fit the maximum size of an encoded GX texture. 1024x1024, RGBA8 = 4MB.
|
2019-03-23 22:24:03 -06:00
|
|
|
static constexpr u32 VERTEX_STREAM_BUFFER_SIZE = 48 * 1024 * 1024;
|
|
|
|
static constexpr u32 INDEX_STREAM_BUFFER_SIZE = 8 * 1024 * 1024;
|
|
|
|
static constexpr u32 UNIFORM_STREAM_BUFFER_SIZE = 32 * 1024 * 1024;
|
2019-02-14 18:59:50 -07:00
|
|
|
static constexpr u32 TEXEL_STREAM_BUFFER_SIZE = 16 * 1024 * 1024;
|
|
|
|
|
2015-11-01 14:54:41 -07:00
|
|
|
VertexManagerBase();
|
|
|
|
virtual ~VertexManagerBase();
|
2010-10-03 02:20:24 -06:00
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
virtual bool Initialize();
|
|
|
|
|
2017-04-30 02:07:57 -06:00
|
|
|
PrimitiveType GetCurrentPrimitiveType() const { return m_current_primitive_type; }
|
2021-04-30 15:57:12 -06:00
|
|
|
void AddIndices(OpcodeDecoder::Primitive primitive, u32 num_vertices);
|
2021-11-29 18:51:02 -07:00
|
|
|
virtual DataReader PrepareForAdditionalData(OpcodeDecoder::Primitive primitive, u32 count,
|
|
|
|
u32 stride, bool cullall);
|
2016-08-21 21:02:37 -06:00
|
|
|
void FlushData(u32 count, u32 stride);
|
2010-10-03 02:20:24 -06:00
|
|
|
|
2016-08-21 21:02:37 -06:00
|
|
|
void Flush();
|
2010-10-03 02:20:24 -06:00
|
|
|
|
2016-08-21 21:02:37 -06:00
|
|
|
void DoState(PointerWrap& p);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2020-01-25 18:04:50 -07:00
|
|
|
FlushStatistics ResetFlushAspectRatioCount();
|
2017-03-03 15:36:51 -07:00
|
|
|
|
2018-02-24 08:15:35 -07:00
|
|
|
// State setters, called from register update functions.
|
|
|
|
void SetRasterizationStateChanged() { m_rasterization_state_changed = true; }
|
|
|
|
void SetDepthStateChanged() { m_depth_state_changed = true; }
|
|
|
|
void SetBlendingStateChanged() { m_blending_state_changed = true; }
|
|
|
|
void InvalidatePipelineObject()
|
|
|
|
{
|
|
|
|
m_current_pipeline_object = nullptr;
|
|
|
|
m_pipeline_config_changed = true;
|
|
|
|
}
|
|
|
|
|
2018-11-27 00:16:53 -07:00
|
|
|
// Utility pipeline drawing (e.g. EFB copies, post-processing, UI).
|
2019-02-14 18:59:50 -07:00
|
|
|
virtual void UploadUtilityUniforms(const void* uniforms, u32 uniforms_size);
|
2018-11-27 00:16:53 -07:00
|
|
|
void UploadUtilityVertices(const void* vertices, u32 vertex_stride, u32 num_vertices,
|
|
|
|
const u16* indices, u32 num_indices, u32* out_base_vertex,
|
|
|
|
u32* out_base_index);
|
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
// Determine how many bytes there are in each element of the texel buffer.
|
|
|
|
// Needed for alignment and stride calculations.
|
|
|
|
static u32 GetTexelBufferElementSize(TexelBufferFormat buffer_format);
|
|
|
|
|
|
|
|
// Texel buffer, used for palette conversion.
|
|
|
|
virtual bool UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format,
|
|
|
|
u32* out_offset);
|
|
|
|
|
|
|
|
// The second set of parameters uploads a second blob in the same buffer, used for GPU texture
|
|
|
|
// decoding for palette textures, as both the texture data and palette must be uploaded.
|
|
|
|
virtual bool UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format,
|
|
|
|
u32* out_offset, const void* palette_data, u32 palette_size,
|
|
|
|
TexelBufferFormat palette_format, u32* out_palette_offset);
|
|
|
|
|
|
|
|
// CPU access tracking - call after a draw call is made.
|
|
|
|
void OnDraw();
|
|
|
|
|
|
|
|
// Call after CPU access is requested.
|
|
|
|
void OnCPUEFBAccess();
|
|
|
|
|
|
|
|
// Call after an EFB copy to RAM. If true, the current command buffer should be executed.
|
|
|
|
void OnEFBCopyToRAM();
|
|
|
|
|
|
|
|
// Call at the end of a frame.
|
|
|
|
void OnEndFrame();
|
|
|
|
|
2010-10-02 18:41:06 -06:00
|
|
|
protected:
|
2019-02-14 18:59:50 -07:00
|
|
|
// When utility uniforms are used, the GX uniforms need to be re-written afterwards.
|
|
|
|
static void InvalidateConstants();
|
2018-11-27 00:16:53 -07:00
|
|
|
|
|
|
|
// Prepares the buffer for the next batch of vertices.
|
2019-02-14 18:59:50 -07:00
|
|
|
virtual void ResetBuffer(u32 vertex_stride);
|
2018-11-27 00:16:53 -07:00
|
|
|
|
|
|
|
// Commits/uploads the current batch of vertices.
|
|
|
|
virtual void CommitBuffer(u32 num_vertices, u32 vertex_stride, u32 num_indices,
|
2019-02-14 18:59:50 -07:00
|
|
|
u32* out_base_vertex, u32* out_base_index);
|
2018-11-27 00:16:53 -07:00
|
|
|
|
|
|
|
// Uploads uniform buffers for GX draws.
|
2019-02-14 18:59:50 -07:00
|
|
|
virtual void UploadUniforms();
|
2018-11-27 00:16:53 -07:00
|
|
|
|
|
|
|
// Issues the draw call for the current batch in the backend.
|
2019-02-14 18:59:50 -07:00
|
|
|
virtual void DrawCurrentBatch(u32 base_index, u32 num_indices, u32 base_vertex);
|
|
|
|
|
|
|
|
u32 GetRemainingSize() const;
|
2021-04-30 15:57:12 -06:00
|
|
|
u32 GetRemainingIndices(OpcodeDecoder::Primitive primitive) const;
|
2019-02-14 18:59:50 -07:00
|
|
|
|
|
|
|
void CalculateZSlope(NativeVertexFormat* format);
|
2022-04-13 23:03:34 -06:00
|
|
|
void CalculateBinormals(NativeVertexFormat* format);
|
2019-02-14 18:59:50 -07:00
|
|
|
void LoadTextures();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-08-21 21:02:37 -06:00
|
|
|
u8* m_cur_buffer_pointer = nullptr;
|
|
|
|
u8* m_base_buffer_pointer = nullptr;
|
|
|
|
u8* m_end_buffer_pointer = nullptr;
|
2014-12-09 00:35:04 -07:00
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
// Alternative buffers in CPU memory for primitives we are going to discard.
|
|
|
|
std::vector<u8> m_cpu_vertex_buffer;
|
|
|
|
std::vector<u16> m_cpu_index_buffer;
|
2014-12-09 00:35:04 -07:00
|
|
|
|
2016-08-21 21:02:37 -06:00
|
|
|
Slope m_zslope = {};
|
2015-01-13 02:55:25 -07:00
|
|
|
|
2018-03-01 02:21:06 -07:00
|
|
|
VideoCommon::GXPipelineUid m_current_pipeline_config;
|
|
|
|
VideoCommon::GXUberPipelineUid m_current_uber_pipeline_config;
|
2018-02-24 08:15:35 -07:00
|
|
|
const AbstractPipeline* m_current_pipeline_object = nullptr;
|
2017-04-30 02:07:57 -06:00
|
|
|
PrimitiveType m_current_primitive_type = PrimitiveType::Points;
|
2018-02-24 08:15:35 -07:00
|
|
|
bool m_pipeline_config_changed = true;
|
|
|
|
bool m_rasterization_state_changed = true;
|
|
|
|
bool m_depth_state_changed = true;
|
|
|
|
bool m_blending_state_changed = true;
|
|
|
|
bool m_cull_all = false;
|
2015-01-23 18:37:20 -07:00
|
|
|
|
2019-12-05 08:01:33 -07:00
|
|
|
IndexGenerator m_index_generator;
|
|
|
|
|
2014-01-23 05:11:38 -07:00
|
|
|
private:
|
2019-02-14 18:59:50 -07:00
|
|
|
// Minimum number of draws per command buffer when attempting to preempt a readback operation.
|
|
|
|
static constexpr u32 MINIMUM_DRAW_CALLS_PER_COMMAND_BUFFER_FOR_READBACK = 10;
|
|
|
|
|
|
|
|
void UpdatePipelineConfig();
|
|
|
|
void UpdatePipelineObject();
|
|
|
|
|
2016-08-21 21:02:37 -06:00
|
|
|
bool m_is_flushed = true;
|
2020-01-25 18:04:50 -07:00
|
|
|
FlushStatistics m_flush_statistics = {};
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
// CPU access tracking
|
|
|
|
u32 m_draw_counter = 0;
|
|
|
|
u32 m_last_efb_copy_draw_counter = 0;
|
|
|
|
std::vector<u32> m_cpu_accesses_this_frame;
|
|
|
|
std::vector<u32> m_scheduled_command_buffer_kicks;
|
|
|
|
bool m_allow_background_execution = true;
|
2010-10-02 18:41:06 -06:00
|
|
|
};
|
2010-10-03 02:20:24 -06:00
|
|
|
|
2015-12-20 19:49:49 -07:00
|
|
|
extern std::unique_ptr<VertexManagerBase> g_vertex_manager;
|