#include "stdafx.h" #include "GLGSRender.h" #include "../rsx_methods.h" #include "../Common/BufferUtils.h" #include "GLHelpers.h" namespace { static constexpr std::array s_reg_table = { "in_pos_buffer", "in_weight_buffer", "in_normal_buffer", "in_diff_color_buffer", "in_spec_color_buffer", "in_fog_buffer", "in_point_size_buffer", "in_7_buffer", "in_tc0_buffer", "in_tc1_buffer", "in_tc2_buffer", "in_tc3_buffer", "in_tc4_buffer", "in_tc5_buffer", "in_tc6_buffer", "in_tc7_buffer" }; } namespace { // return vertex count if primitive type is not native (empty array otherwise) std::tuple get_index_array_for_emulated_non_indexed_draw(const std::vector> &first_count_commands, rsx::primitive_type primitive_mode, gl::ring_buffer &dst) { //This is an emulated buffer, so our indices only range from 0->original_vertex_array_length u32 vertex_count = 0; u32 element_count = 0; verify(HERE), !gl::is_primitive_native(primitive_mode); for (const auto &pair : first_count_commands) { element_count += (u32)get_index_count(primitive_mode, pair.second); vertex_count += pair.second; } auto mapping = dst.alloc_from_heap(element_count * sizeof(u16), 256); char *mapped_buffer = (char *)mapping.first; write_index_array_for_non_indexed_non_native_primitive_to_buffer(mapped_buffer, primitive_mode, vertex_count); return std::make_tuple(element_count, mapping.second); } std::tuple upload_index_buffer(gsl::span raw_index_buffer, void *ptr, rsx::index_array_type type, rsx::primitive_type draw_mode, const std::vector>& first_count_commands, u32 initial_vertex_count) { u32 min_index, max_index, vertex_draw_count = initial_vertex_count; if (!gl::is_primitive_native(draw_mode)) vertex_draw_count = (u32)get_index_count(draw_mode, ::narrow(vertex_draw_count)); u32 type_size = ::narrow(get_index_type_size(type)); u32 block_sz = vertex_draw_count * type_size; gsl::span dst{ reinterpret_cast(ptr), ::narrow(block_sz) }; std::tie(min_index, max_index) = write_index_array_data_to_buffer(dst, raw_index_buffer, type, draw_mode, rsx::method_registers.restart_index_enabled(), rsx::method_registers.restart_index(), first_count_commands, [](auto prim) { return !gl::is_primitive_native(prim); }); return std::make_tuple(min_index, max_index, vertex_draw_count); } } namespace { GLenum get_index_type(rsx::index_array_type type) { switch (type) { case rsx::index_array_type::u16: return GL_UNSIGNED_SHORT; case rsx::index_array_type::u32: return GL_UNSIGNED_INT; } throw; } struct vertex_input_state { u32 vertex_draw_count; u32 allocated_vertex_count; u32 vertex_data_base; u32 vertex_index_base; std::optional> index_info; }; struct draw_command_visitor { using attribute_storage = std::vector< std::variant>; draw_command_visitor(gl::ring_buffer& index_ring_buffer, rsx::vertex_input_layout& vertex_layout) : m_index_ring_buffer(index_ring_buffer) , m_vertex_layout(vertex_layout) {} vertex_input_state operator()(const rsx::draw_array_command& command) { const u32 vertex_count = rsx::method_registers.current_draw_clause.get_elements_count(); const u32 min_index = rsx::method_registers.current_draw_clause.first_count_commands.front().first; if (!gl::is_primitive_native(rsx::method_registers.current_draw_clause.primitive)) { u32 index_count; u32 offset_in_index_buffer; std::tie(index_count, offset_in_index_buffer) = get_index_array_for_emulated_non_indexed_draw( rsx::method_registers.current_draw_clause.first_count_commands, rsx::method_registers.current_draw_clause.primitive, m_index_ring_buffer); return{ index_count, vertex_count, min_index, 0, std::make_tuple(static_cast(GL_UNSIGNED_SHORT), offset_in_index_buffer) }; } return{ vertex_count, vertex_count, min_index, 0, std::optional>() }; } vertex_input_state operator()(const rsx::draw_indexed_array_command& command) { u32 min_index = 0, max_index = 0; rsx::index_array_type type = rsx::method_registers.current_draw_clause.is_immediate_draw? rsx::index_array_type::u32: rsx::method_registers.index_type(); u32 type_size = ::narrow(get_index_type_size(type)); const u32 vertex_count = rsx::method_registers.current_draw_clause.get_elements_count(); u32 index_count = vertex_count; if (!gl::is_primitive_native(rsx::method_registers.current_draw_clause.primitive)) index_count = (u32)get_index_count(rsx::method_registers.current_draw_clause.primitive, vertex_count); u32 max_size = index_count * type_size; auto mapping = m_index_ring_buffer.alloc_from_heap(max_size, 256); void* ptr = mapping.first; u32 offset_in_index_buffer = mapping.second; std::tie(min_index, max_index, index_count) = upload_index_buffer( command.raw_index_buffer, ptr, type, rsx::method_registers.current_draw_clause.primitive, rsx::method_registers.current_draw_clause.first_count_commands, vertex_count); //check for vertex arrays with frquency modifiers for (auto &block : m_vertex_layout.interleaved_blocks) { if (block.min_divisor > 1) { //Ignore base offsets and return real results //The upload function will optimize the uploaded range anyway return{ index_count, max_index, 0, 0, std::make_tuple(get_index_type(type), offset_in_index_buffer) }; } } //Prefer only reading the vertices that are referenced in the index buffer itself //Offset data source by min_index verts, but also notify the shader to offset the vertexID return{ index_count, (max_index - min_index + 1), min_index, min_index, std::make_tuple(get_index_type(type), offset_in_index_buffer) }; } vertex_input_state operator()(const rsx::draw_inlined_array& command) { const u32 vertex_count = (u32)command.inline_vertex_array.size() * sizeof(u32) / m_vertex_layout.interleaved_blocks[0].attribute_stride; if (!gl::is_primitive_native(rsx::method_registers.current_draw_clause.primitive)) { u32 offset_in_index_buffer; u32 index_count; std::tie(index_count, offset_in_index_buffer) = get_index_array_for_emulated_non_indexed_draw( { std::make_pair(0, vertex_count) }, rsx::method_registers.current_draw_clause.primitive, m_index_ring_buffer); return{ index_count, vertex_count, 0, 0, std::make_tuple(static_cast(GL_UNSIGNED_SHORT), offset_in_index_buffer) }; } return{ vertex_count, vertex_count, 0, 0, std::optional>() }; } private: gl::ring_buffer& m_index_ring_buffer; rsx::vertex_input_layout& m_vertex_layout; }; } std::tuple>> GLGSRender::set_vertex_buffer() { std::chrono::time_point then = steady_clock::now(); m_vertex_layout = analyse_inputs_interleaved(); //Write index buffers and count verts auto result = std::apply_visitor(draw_command_visitor(*m_index_ring_buffer, m_vertex_layout), get_draw_command(rsx::method_registers)); auto &vertex_count = result.allocated_vertex_count; auto &vertex_base = result.vertex_data_base; //Do actual vertex upload auto required = calculate_memory_requirements(m_vertex_layout, vertex_count); std::pair persistent_mapping = {}, volatile_mapping = {}; if (required.first > 0) { //Check if cacheable //Only data in the 'persistent' block may be cached //TODO: make vertex cache keep local data beyond frame boundaries and hook notify command bool in_cache = false; bool to_store = false; u32 storage_address = UINT32_MAX; if (m_vertex_layout.interleaved_blocks.size() == 1 && rsx::method_registers.current_draw_clause.command != rsx::draw_command::inlined_array) { storage_address = m_vertex_layout.interleaved_blocks[0].real_offset_address + vertex_base; if (auto cached = m_vertex_cache->find_vertex_range(storage_address, GL_R8UI, required.first)) { in_cache = true; m_gl_persistent_stream_buffer.copy_from(*m_attrib_ring_buffer, GL_R8UI, cached->offset_in_heap, required.first); } else { to_store = true; } } if (!in_cache) { persistent_mapping = m_attrib_ring_buffer->alloc_from_heap(required.first, m_min_texbuffer_alignment); m_gl_persistent_stream_buffer.copy_from(*m_attrib_ring_buffer, GL_R8UI, persistent_mapping.second, required.first); if (to_store) { //store ref in vertex cache m_vertex_cache->store_range(storage_address, GL_R8UI, required.first, persistent_mapping.second); } } } if (required.second > 0) { volatile_mapping = m_attrib_ring_buffer->alloc_from_heap(required.second, m_min_texbuffer_alignment); m_gl_volatile_stream_buffer.copy_from(*m_attrib_ring_buffer, GL_R8UI, volatile_mapping.second, required.second); } //Write all the data write_vertex_data_to_memory(m_vertex_layout, vertex_base, vertex_count, persistent_mapping.first, volatile_mapping.first); std::chrono::time_point now = steady_clock::now(); m_vertex_upload_time += std::chrono::duration_cast(now - then).count(); return std::make_tuple(result.vertex_draw_count, result.allocated_vertex_count, result.vertex_index_base, result.index_info); } namespace { } // End anonymous namespace