2015-05-24 04:32:32 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-24 04:32:32 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2010-10-03 08:20:24 +00:00
|
|
|
|
2015-12-21 02:49:49 +00:00
|
|
|
#include <memory>
|
2013-02-27 04:47:50 +00:00
|
|
|
#include <vector>
|
2015-12-21 02:49:49 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2018-05-10 23:06:19 +00:00
|
|
|
#include "Common/MathUtil.h"
|
2019-12-05 15:01:33 +00:00
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
2017-04-30 08:07:57 +00:00
|
|
|
#include "VideoCommon/RenderState.h"
|
2018-02-24 15:15:35 +00:00
|
|
|
#include "VideoCommon/ShaderCache.h"
|
2013-02-27 04:47:50 +00:00
|
|
|
|
2016-01-31 19:51:55 +00:00
|
|
|
class DataReader;
|
2010-11-26 09:25:08 +00:00
|
|
|
class NativeVertexFormat;
|
2012-01-04 08:42:22 +00:00
|
|
|
class PointerWrap;
|
2016-01-17 21:54:31 +00:00
|
|
|
struct PortableVertexDeclaration;
|
2010-11-26 09:25:08 +00:00
|
|
|
|
2015-01-13 09:55:25 +00:00
|
|
|
struct Slope
|
|
|
|
{
|
|
|
|
float dfdx;
|
|
|
|
float dfdy;
|
|
|
|
float f0;
|
2015-01-23 14:15:09 +00:00
|
|
|
bool dirty;
|
2015-01-13 09:55:25 +00:00
|
|
|
};
|
|
|
|
|
2019-02-15 01:59:50 +00: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 21:57:12 +00:00
|
|
|
namespace OpcodeDecoder
|
|
|
|
{
|
|
|
|
enum class Primitive : u8;
|
|
|
|
};
|
|
|
|
|
2015-11-01 21:54:41 +00:00
|
|
|
class VertexManagerBase
|
2010-10-03 00:41:06 +00:00
|
|
|
{
|
2013-02-22 05:12:53 +00:00
|
|
|
private:
|
2016-10-01 07:37:17 +00: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-29 05:23:17 +00:00
|
|
|
|
2016-10-01 07:37:17 +00:00
|
|
|
static constexpr u32 MAX_PRIMITIVES_PER_COMMAND = 65535;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2020-01-26 01:04:50 +00: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-03 00:41:06 +00:00
|
|
|
public:
|
2016-10-01 07:37:17 +00:00
|
|
|
static constexpr u32 MAXVBUFFERSIZE =
|
2018-05-10 23:06:19 +00:00
|
|
|
MathUtil::NextPowerOf2(MAX_PRIMITIVES_PER_COMMAND * LARGEST_POSSIBLE_VERTEX);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2013-02-22 05:12:53 +00:00
|
|
|
// We may convert triangle-fans to triangle-lists, almost 3x as many indices.
|
2018-05-10 23:06:19 +00:00
|
|
|
static constexpr u32 MAXIBUFFERSIZE = MathUtil::NextPowerOf2(MAX_PRIMITIVES_PER_COMMAND * 3);
|
2010-10-03 08:20:24 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
// Streaming buffer sizes.
|
|
|
|
// Texel buffer will fit the maximum size of an encoded GX texture. 1024x1024, RGBA8 = 4MB.
|
2019-03-24 04:24:03 +00: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-15 01:59:50 +00:00
|
|
|
static constexpr u32 TEXEL_STREAM_BUFFER_SIZE = 16 * 1024 * 1024;
|
|
|
|
|
2015-11-01 21:54:41 +00:00
|
|
|
VertexManagerBase();
|
|
|
|
virtual ~VertexManagerBase();
|
2010-10-03 08:20:24 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
virtual bool Initialize();
|
|
|
|
|
2017-04-30 08:07:57 +00:00
|
|
|
PrimitiveType GetCurrentPrimitiveType() const { return m_current_primitive_type; }
|
2021-04-30 21:57:12 +00:00
|
|
|
void AddIndices(OpcodeDecoder::Primitive primitive, u32 num_vertices);
|
2021-11-30 01:51:02 +00:00
|
|
|
virtual DataReader PrepareForAdditionalData(OpcodeDecoder::Primitive primitive, u32 count,
|
|
|
|
u32 stride, bool cullall);
|
2016-08-22 03:02:37 +00:00
|
|
|
void FlushData(u32 count, u32 stride);
|
2010-10-03 08:20:24 +00:00
|
|
|
|
2016-08-22 03:02:37 +00:00
|
|
|
void Flush();
|
2010-10-03 08:20:24 +00:00
|
|
|
|
2016-08-22 03:02:37 +00:00
|
|
|
void DoState(PointerWrap& p);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2020-01-26 01:04:50 +00:00
|
|
|
FlushStatistics ResetFlushAspectRatioCount();
|
2017-03-03 22:36:51 +00:00
|
|
|
|
2018-02-24 15:15:35 +00: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 07:16:53 +00:00
|
|
|
// Utility pipeline drawing (e.g. EFB copies, post-processing, UI).
|
2019-02-15 01:59:50 +00:00
|
|
|
virtual void UploadUtilityUniforms(const void* uniforms, u32 uniforms_size);
|
2018-11-27 07:16:53 +00: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-15 01:59:50 +00: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-03 00:41:06 +00:00
|
|
|
protected:
|
2019-02-15 01:59:50 +00:00
|
|
|
// When utility uniforms are used, the GX uniforms need to be re-written afterwards.
|
|
|
|
static void InvalidateConstants();
|
2018-11-27 07:16:53 +00:00
|
|
|
|
|
|
|
// Prepares the buffer for the next batch of vertices.
|
2019-02-15 01:59:50 +00:00
|
|
|
virtual void ResetBuffer(u32 vertex_stride);
|
2018-11-27 07:16:53 +00:00
|
|
|
|
|
|
|
// Commits/uploads the current batch of vertices.
|
|
|
|
virtual void CommitBuffer(u32 num_vertices, u32 vertex_stride, u32 num_indices,
|
2019-02-15 01:59:50 +00:00
|
|
|
u32* out_base_vertex, u32* out_base_index);
|
2018-11-27 07:16:53 +00:00
|
|
|
|
|
|
|
// Uploads uniform buffers for GX draws.
|
2019-02-15 01:59:50 +00:00
|
|
|
virtual void UploadUniforms();
|
2018-11-27 07:16:53 +00:00
|
|
|
|
|
|
|
// Issues the draw call for the current batch in the backend.
|
2019-02-15 01:59:50 +00:00
|
|
|
virtual void DrawCurrentBatch(u32 base_index, u32 num_indices, u32 base_vertex);
|
|
|
|
|
|
|
|
u32 GetRemainingSize() const;
|
2021-04-30 21:57:12 +00:00
|
|
|
u32 GetRemainingIndices(OpcodeDecoder::Primitive primitive) const;
|
2019-02-15 01:59:50 +00:00
|
|
|
|
|
|
|
void CalculateZSlope(NativeVertexFormat* format);
|
|
|
|
void LoadTextures();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-08-22 03:02:37 +00:00
|
|
|
u8* m_cur_buffer_pointer = nullptr;
|
|
|
|
u8* m_base_buffer_pointer = nullptr;
|
|
|
|
u8* m_end_buffer_pointer = nullptr;
|
2014-12-09 07:35:04 +00:00
|
|
|
|
2019-02-15 01:59:50 +00: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 07:35:04 +00:00
|
|
|
|
2016-08-22 03:02:37 +00:00
|
|
|
Slope m_zslope = {};
|
2015-01-13 09:55:25 +00:00
|
|
|
|
2018-03-01 09:21:06 +00:00
|
|
|
VideoCommon::GXPipelineUid m_current_pipeline_config;
|
|
|
|
VideoCommon::GXUberPipelineUid m_current_uber_pipeline_config;
|
2018-02-24 15:15:35 +00:00
|
|
|
const AbstractPipeline* m_current_pipeline_object = nullptr;
|
2017-04-30 08:07:57 +00:00
|
|
|
PrimitiveType m_current_primitive_type = PrimitiveType::Points;
|
2018-02-24 15:15:35 +00: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-24 01:37:20 +00:00
|
|
|
|
2019-12-05 15:01:33 +00:00
|
|
|
IndexGenerator m_index_generator;
|
|
|
|
|
2014-01-23 12:11:38 +00:00
|
|
|
private:
|
2019-02-15 01:59:50 +00: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-22 03:02:37 +00:00
|
|
|
bool m_is_flushed = true;
|
2020-01-26 01:04:50 +00:00
|
|
|
FlushStatistics m_flush_statistics = {};
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2019-02-15 01:59:50 +00: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-03 00:41:06 +00:00
|
|
|
};
|
2010-10-03 08:20:24 +00:00
|
|
|
|
2015-12-21 02:49:49 +00:00
|
|
|
extern std::unique_ptr<VertexManagerBase> g_vertex_manager;
|