summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2019-05-27 19:55:44 -0300
committerGravatar ReinUsesLisp2019-07-06 00:37:55 -0300
commitb54fb8fc4c6801ba7fa7990199071d94f463373c (patch)
treef02ea04a131684c2346ea574f90ecf01dfd64dcb /src
parentgl_rasterizer: Add some commentaries (diff)
downloadyuzu-b54fb8fc4c6801ba7fa7990199071d94f463373c.tar.gz
yuzu-b54fb8fc4c6801ba7fa7990199071d94f463373c.tar.xz
yuzu-b54fb8fc4c6801ba7fa7990199071d94f463373c.zip
gl_buffer_cache: Return used buffer from Upload function
Diffstat (limited to 'src')
-rw-r--r--src/video_core/renderer_opengl/gl_buffer_cache.cpp23
-rw-r--r--src/video_core/renderer_opengl/gl_buffer_cache.h16
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp30
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h2
4 files changed, 35 insertions, 36 deletions
diff --git a/src/video_core/renderer_opengl/gl_buffer_cache.cpp b/src/video_core/renderer_opengl/gl_buffer_cache.cpp
index 2b9bd142e..ea8b4c99f 100644
--- a/src/video_core/renderer_opengl/gl_buffer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_buffer_cache.cpp
@@ -4,6 +4,7 @@
4 4
5#include <cstring> 5#include <cstring>
6#include <memory> 6#include <memory>
7#include <utility>
7 8
8#include "common/alignment.h" 9#include "common/alignment.h"
9#include "core/core.h" 10#include "core/core.h"
@@ -21,9 +22,10 @@ CachedBufferEntry::CachedBufferEntry(VAddr cpu_addr, std::size_t size, GLintptr
21OGLBufferCache::OGLBufferCache(RasterizerOpenGL& rasterizer, std::size_t size) 22OGLBufferCache::OGLBufferCache(RasterizerOpenGL& rasterizer, std::size_t size)
22 : RasterizerCache{rasterizer}, stream_buffer(size, true) {} 23 : RasterizerCache{rasterizer}, stream_buffer(size, true) {}
23 24
24GLintptr OGLBufferCache::UploadMemory(GPUVAddr gpu_addr, std::size_t size, std::size_t alignment, 25std::pair<GLuint, GLintptr> OGLBufferCache::UploadMemory(GPUVAddr gpu_addr, std::size_t size,
25 bool cache) { 26 std::size_t alignment, bool cache) {
26 std::lock_guard lock{mutex}; 27 std::lock_guard lock{mutex};
28
27 auto& memory_manager = Core::System::GetInstance().GPU().MemoryManager(); 29 auto& memory_manager = Core::System::GetInstance().GPU().MemoryManager();
28 30
29 // Cache management is a big overhead, so only cache entries with a given size. 31 // Cache management is a big overhead, so only cache entries with a given size.
@@ -35,7 +37,7 @@ GLintptr OGLBufferCache::UploadMemory(GPUVAddr gpu_addr, std::size_t size, std::
35 auto entry = TryGet(host_ptr); 37 auto entry = TryGet(host_ptr);
36 if (entry) { 38 if (entry) {
37 if (entry->GetSize() >= size && entry->GetAlignment() == alignment) { 39 if (entry->GetSize() >= size && entry->GetAlignment() == alignment) {
38 return entry->GetOffset(); 40 return {stream_buffer.GetHandle(), entry->GetOffset()};
39 } 41 }
40 Unregister(entry); 42 Unregister(entry);
41 } 43 }
@@ -45,7 +47,7 @@ GLintptr OGLBufferCache::UploadMemory(GPUVAddr gpu_addr, std::size_t size, std::
45 const GLintptr uploaded_offset = buffer_offset; 47 const GLintptr uploaded_offset = buffer_offset;
46 48
47 if (!host_ptr) { 49 if (!host_ptr) {
48 return uploaded_offset; 50 return {stream_buffer.GetHandle(), uploaded_offset};
49 } 51 }
50 52
51 std::memcpy(buffer_ptr, host_ptr, size); 53 std::memcpy(buffer_ptr, host_ptr, size);
@@ -58,11 +60,12 @@ GLintptr OGLBufferCache::UploadMemory(GPUVAddr gpu_addr, std::size_t size, std::
58 Register(entry); 60 Register(entry);
59 } 61 }
60 62
61 return uploaded_offset; 63 return {stream_buffer.GetHandle(), uploaded_offset};
62} 64}
63 65
64GLintptr OGLBufferCache::UploadHostMemory(const void* raw_pointer, std::size_t size, 66std::pair<GLuint, GLintptr> OGLBufferCache::UploadHostMemory(const void* raw_pointer,
65 std::size_t alignment) { 67 std::size_t size,
68 std::size_t alignment) {
66 std::lock_guard lock{mutex}; 69 std::lock_guard lock{mutex};
67 AlignBuffer(alignment); 70 AlignBuffer(alignment);
68 std::memcpy(buffer_ptr, raw_pointer, size); 71 std::memcpy(buffer_ptr, raw_pointer, size);
@@ -70,7 +73,7 @@ GLintptr OGLBufferCache::UploadHostMemory(const void* raw_pointer, std::size_t s
70 73
71 buffer_ptr += size; 74 buffer_ptr += size;
72 buffer_offset += size; 75 buffer_offset += size;
73 return uploaded_offset; 76 return {stream_buffer.GetHandle(), uploaded_offset};
74} 77}
75 78
76bool OGLBufferCache::Map(std::size_t max_size) { 79bool OGLBufferCache::Map(std::size_t max_size) {
@@ -89,10 +92,6 @@ void OGLBufferCache::Unmap() {
89 stream_buffer.Unmap(buffer_offset - buffer_offset_base); 92 stream_buffer.Unmap(buffer_offset - buffer_offset_base);
90} 93}
91 94
92GLuint OGLBufferCache::GetHandle() const {
93 return stream_buffer.GetHandle();
94}
95
96void OGLBufferCache::AlignBuffer(std::size_t alignment) { 95void OGLBufferCache::AlignBuffer(std::size_t alignment) {
97 // Align the offset, not the mapped pointer 96 // Align the offset, not the mapped pointer
98 const GLintptr offset_aligned = 97 const GLintptr offset_aligned =
diff --git a/src/video_core/renderer_opengl/gl_buffer_cache.h b/src/video_core/renderer_opengl/gl_buffer_cache.h
index f2347581b..544f3b010 100644
--- a/src/video_core/renderer_opengl/gl_buffer_cache.h
+++ b/src/video_core/renderer_opengl/gl_buffer_cache.h
@@ -7,6 +7,7 @@
7#include <cstddef> 7#include <cstddef>
8#include <memory> 8#include <memory>
9#include <tuple> 9#include <tuple>
10#include <utility>
10 11
11#include "common/common_types.h" 12#include "common/common_types.h"
12#include "video_core/rasterizer_cache.h" 13#include "video_core/rasterizer_cache.h"
@@ -53,19 +54,18 @@ class OGLBufferCache final : public RasterizerCache<std::shared_ptr<CachedBuffer
53public: 54public:
54 explicit OGLBufferCache(RasterizerOpenGL& rasterizer, std::size_t size); 55 explicit OGLBufferCache(RasterizerOpenGL& rasterizer, std::size_t size);
55 56
56 /// Uploads data from a guest GPU address. Returns host's buffer offset where it's been 57 /// Uploads data from a guest GPU address. Returns the OpenGL buffer where it's located and its
57 /// allocated. 58 /// offset.
58 GLintptr UploadMemory(GPUVAddr gpu_addr, std::size_t size, std::size_t alignment = 4, 59 std::pair<GLuint, GLintptr> UploadMemory(GPUVAddr gpu_addr, std::size_t size,
59 bool cache = true); 60 std::size_t alignment = 4, bool cache = true);
60 61
61 /// Uploads from a host memory. Returns host's buffer offset where it's been allocated. 62 /// Uploads from a host memory. Returns the OpenGL buffer where it's located and its offset.
62 GLintptr UploadHostMemory(const void* raw_pointer, std::size_t size, std::size_t alignment = 4); 63 std::pair<GLuint, GLintptr> UploadHostMemory(const void* raw_pointer, std::size_t size,
64 std::size_t alignment = 4);
63 65
64 bool Map(std::size_t max_size); 66 bool Map(std::size_t max_size);
65 void Unmap(); 67 void Unmap();
66 68
67 GLuint GetHandle() const;
68
69protected: 69protected:
70 void AlignBuffer(std::size_t alignment); 70 void AlignBuffer(std::size_t alignment);
71 71
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index bfc3c4df9..d694dacfb 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -129,8 +129,6 @@ GLuint RasterizerOpenGL::SetupVertexFormat() {
129 state.draw.vertex_array = vao; 129 state.draw.vertex_array = vao;
130 state.ApplyVertexArrayState(); 130 state.ApplyVertexArrayState();
131 131
132 glVertexArrayElementBuffer(vao, buffer_cache.GetHandle());
133
134 // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL. 132 // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL.
135 // Enables the first 16 vertex attributes always, as we don't know which ones are actually 133 // Enables the first 16 vertex attributes always, as we don't know which ones are actually
136 // used until shader time. Note, Tegra technically supports 32, but we're capping this to 16 134 // used until shader time. Note, Tegra technically supports 32, but we're capping this to 16
@@ -197,10 +195,10 @@ void RasterizerOpenGL::SetupVertexBuffer(GLuint vao) {
197 195
198 ASSERT(end > start); 196 ASSERT(end > start);
199 const u64 size = end - start + 1; 197 const u64 size = end - start + 1;
200 const GLintptr vertex_buffer_offset = buffer_cache.UploadMemory(start, size); 198 const auto [vertex_buffer, vertex_buffer_offset] = buffer_cache.UploadMemory(start, size);
201 199
202 // Bind the vertex array to the buffer at the current offset. 200 // Bind the vertex array to the buffer at the current offset.
203 glVertexArrayVertexBuffer(vao, index, buffer_cache.GetHandle(), vertex_buffer_offset, 201 glVertexArrayVertexBuffer(vao, index, vertex_buffer, vertex_buffer_offset,
204 vertex_array.stride); 202 vertex_array.stride);
205 203
206 if (regs.instanced_arrays.IsInstancingEnabled(index) && vertex_array.divisor != 0) { 204 if (regs.instanced_arrays.IsInstancingEnabled(index) && vertex_array.divisor != 0) {
@@ -215,12 +213,16 @@ void RasterizerOpenGL::SetupVertexBuffer(GLuint vao) {
215 gpu.dirty_flags.vertex_array.reset(); 213 gpu.dirty_flags.vertex_array.reset();
216} 214}
217 215
218GLintptr RasterizerOpenGL::SetupIndexBuffer() { 216GLintptr RasterizerOpenGL::SetupIndexBuffer(GLuint vao) {
219 if (accelerate_draw != AccelDraw::Indexed) { 217 if (accelerate_draw != AccelDraw::Indexed) {
220 return 0; 218 return 0;
221 } 219 }
220 MICROPROFILE_SCOPE(OpenGL_Index);
222 const auto& regs = system.GPU().Maxwell3D().regs; 221 const auto& regs = system.GPU().Maxwell3D().regs;
223 return buffer_cache.UploadMemory(regs.index_array.IndexStart(), CalculateIndexBufferSize()); 222 const std::size_t size = CalculateIndexBufferSize();
223 const auto [buffer, offset] = buffer_cache.UploadMemory(regs.index_array.IndexStart(), size);
224 glVertexArrayElementBuffer(vao, buffer);
225 return offset;
224} 226}
225 227
226DrawParameters RasterizerOpenGL::SetupDraw(GLintptr index_buffer_offset) { 228DrawParameters RasterizerOpenGL::SetupDraw(GLintptr index_buffer_offset) {
@@ -235,7 +237,6 @@ DrawParameters RasterizerOpenGL::SetupDraw(GLintptr index_buffer_offset) {
235 params.primitive_mode = MaxwellToGL::PrimitiveTopology(regs.draw.topology); 237 params.primitive_mode = MaxwellToGL::PrimitiveTopology(regs.draw.topology);
236 238
237 if (is_indexed) { 239 if (is_indexed) {
238 MICROPROFILE_SCOPE(OpenGL_Index);
239 params.index_format = MaxwellToGL::IndexFormat(regs.index_array.format); 240 params.index_format = MaxwellToGL::IndexFormat(regs.index_array.format);
240 params.count = regs.index_array.count; 241 params.count = regs.index_array.count;
241 params.index_buffer_offset = index_buffer_offset; 242 params.index_buffer_offset = index_buffer_offset;
@@ -278,12 +279,11 @@ void RasterizerOpenGL::SetupShaders(GLenum primitive_mode) {
278 279
279 GLShader::MaxwellUniformData ubo{}; 280 GLShader::MaxwellUniformData ubo{};
280 ubo.SetFromRegs(gpu, stage); 281 ubo.SetFromRegs(gpu, stage);
281 const GLintptr offset = 282 const auto [buffer, offset] =
282 buffer_cache.UploadHostMemory(&ubo, sizeof(ubo), device.GetUniformBufferAlignment()); 283 buffer_cache.UploadHostMemory(&ubo, sizeof(ubo), device.GetUniformBufferAlignment());
283 284
284 // Bind the emulation info buffer 285 // Bind the emulation info buffer
285 bind_ubo_pushbuffer.Push(buffer_cache.GetHandle(), offset, 286 bind_ubo_pushbuffer.Push(buffer, offset, static_cast<GLsizeiptr>(sizeof(ubo)));
286 static_cast<GLsizeiptr>(sizeof(ubo)));
287 287
288 Shader shader{shader_cache.GetStageProgram(program)}; 288 Shader shader{shader_cache.GetStageProgram(program)};
289 289
@@ -651,11 +651,11 @@ void RasterizerOpenGL::DrawArrays() {
651 } 651 }
652 652
653 // Prepare vertex array format. 653 // Prepare vertex array format.
654 const GLuint vertex_array = SetupVertexFormat(); 654 const GLuint vao = SetupVertexFormat();
655 655
656 // Upload vertex and index data. 656 // Upload vertex and index data.
657 SetupVertexBuffer(vertex_array); 657 SetupVertexBuffer(vao);
658 const GLintptr index_buffer_offset = SetupIndexBuffer(); 658 const GLintptr index_buffer_offset = SetupIndexBuffer(vao);
659 659
660 // Setup draw parameters. It will automatically choose what glDraw* method to use. 660 // Setup draw parameters. It will automatically choose what glDraw* method to use.
661 const DrawParameters params = SetupDraw(index_buffer_offset); 661 const DrawParameters params = SetupDraw(index_buffer_offset);
@@ -791,8 +791,8 @@ void RasterizerOpenGL::SetupConstBuffer(const Tegra::Engines::ConstBufferInfo& b
791 ASSERT_MSG(size <= MaxConstbufferSize, "Constant buffer is too big"); 791 ASSERT_MSG(size <= MaxConstbufferSize, "Constant buffer is too big");
792 792
793 const std::size_t alignment = device.GetUniformBufferAlignment(); 793 const std::size_t alignment = device.GetUniformBufferAlignment();
794 const GLintptr offset = buffer_cache.UploadMemory(buffer.address, size, alignment); 794 const auto [cbuf, offset] = buffer_cache.UploadMemory(buffer.address, size, alignment);
795 bind_ubo_pushbuffer.Push(buffer_cache.GetHandle(), offset, size); 795 bind_ubo_pushbuffer.Push(cbuf, offset, size);
796} 796}
797 797
798void RasterizerOpenGL::SetupGlobalRegions(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, 798void RasterizerOpenGL::SetupGlobalRegions(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index 8f1757e25..a03bc759f 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -220,7 +220,7 @@ private:
220 220
221 void SetupVertexBuffer(GLuint vao); 221 void SetupVertexBuffer(GLuint vao);
222 222
223 GLintptr SetupIndexBuffer(); 223 GLintptr SetupIndexBuffer(GLuint vao);
224 224
225 DrawParameters SetupDraw(GLintptr index_buffer_offset); 225 DrawParameters SetupDraw(GLintptr index_buffer_offset);
226 226