B├ęziers on the GPU.
[ipdf/code.git] / src / graphicsbuffer.cpp
1 #include "graphicsbuffer.h"
2 #include "log.h"
3
4 using namespace IPDF;
5
6 static GLenum BufferUsageToGLUsage(GraphicsBuffer::BufferUsage buffer_usage)
7 {
8         GLenum usage;
9         switch (buffer_usage)
10         {
11         case GraphicsBuffer::BufferUsageStaticDraw:
12                 usage = GL_STATIC_DRAW;
13                 break;
14         case GraphicsBuffer::BufferUsageStaticRead:
15                 usage = GL_STATIC_READ;
16                 break;
17         case GraphicsBuffer::BufferUsageStaticCopy:
18                 usage = GL_STATIC_COPY;
19                 break;
20         case GraphicsBuffer::BufferUsageDynamicDraw:
21                 usage = GL_DYNAMIC_DRAW;
22                 break;
23         case GraphicsBuffer::BufferUsageDynamicRead:
24                 usage = GL_DYNAMIC_READ;
25                 break;
26         case GraphicsBuffer::BufferUsageDynamicCopy:
27                 usage = GL_DYNAMIC_COPY;
28                 break;
29         case GraphicsBuffer::BufferUsageStreamDraw:
30                 usage = GL_STREAM_DRAW;
31                 break;
32         case GraphicsBuffer::BufferUsageStreamRead:
33                 usage = GL_STREAM_READ;
34                 break;
35         case GraphicsBuffer::BufferUsageStreamCopy:
36                 usage = GL_STREAM_COPY;
37                 break;
38         default:
39                 SDL_assert(false && "Unknown buffer usage type."); //WTF?
40                 usage = GL_DYNAMIC_DRAW;
41         }
42         return usage;
43 }
44
45 static GLenum BufferTypeToGLType(GraphicsBuffer::BufferType buffer_type)
46 {
47         switch (buffer_type)
48         {
49         case GraphicsBuffer::BufferTypeVertex:
50                 return GL_ARRAY_BUFFER;
51         case GraphicsBuffer::BufferTypeIndex:
52                 return GL_ELEMENT_ARRAY_BUFFER;
53         case GraphicsBuffer::BufferTypePixelPack:
54                 return GL_PIXEL_PACK_BUFFER;
55         case GraphicsBuffer::BufferTypePixelUnpack:
56                 return GL_PIXEL_UNPACK_BUFFER;
57         case GraphicsBuffer::BufferTypeUniform:
58                 return GL_UNIFORM_BUFFER;
59         case GraphicsBuffer::BufferTypeTexture:
60                 return GL_TEXTURE_BUFFER;
61         case GraphicsBuffer::BufferTypeDrawIndirect:
62                 return GL_DRAW_INDIRECT_BUFFER;
63         default:
64                 return GL_COPY_READ_BUFFER;
65         }
66 }
67
68 GraphicsBuffer::GraphicsBuffer()
69 {
70         m_invalidated = true;
71         m_map_pointer = NULL;
72         m_buffer_size = 0;
73         m_buffer_shape_dirty = true;
74         m_buffer_handle = 0;
75         m_buffer_usage = BufferUsageDynamicDraw;
76         m_faking_map = false;
77         SetUsage(BufferUsageStaticDraw);
78 }
79
80 GraphicsBuffer::~GraphicsBuffer()
81 {
82         if (m_map_pointer)
83         {
84                 UnMap();
85         }
86         glDeleteBuffers(1, &m_buffer_handle);
87 }
88
89 void GraphicsBuffer::SetType(GraphicsBuffer::BufferType bufType)
90 {
91         m_buffer_type = bufType;
92 }
93
94 void GraphicsBuffer::SetUsage(GraphicsBuffer::BufferUsage bufUsage)
95 {
96         if (bufUsage != m_buffer_usage)
97         {
98                 m_buffer_usage = bufUsage;
99                 m_buffer_shape_dirty = true;
100         }
101 }
102
103 void GraphicsBuffer::Invalidate()
104 {
105         m_invalidated = true;
106         if (!m_buffer_shape_dirty)
107         {
108                 // Orphan the block of memory we're pointing to.
109                 Upload(m_buffer_size, NULL);
110         }
111         // Apparently not supported.
112         //glInvalidateBufferData(m_buffer_handle);
113 }
114
115 bool GraphicsBuffer::RecreateBuffer(const void *data)
116 {
117         // If the buffer is not dirty, don't recreate it.
118         if (!m_buffer_shape_dirty) return false;
119         // If the buffer is mapped, don't recreate it.
120         if (!m_faking_map && m_map_pointer) return false;
121         // If the buffer has data in it we need, don't recreate it.
122         if (!m_invalidated) return false;
123         if (m_buffer_handle)
124         {
125                 glDeleteBuffers(1, &m_buffer_handle);
126         }
127         glGenBuffers(1, &m_buffer_handle);
128         m_buffer_shape_dirty = false;
129         if (m_buffer_size)
130                 Upload(m_buffer_size, data);
131         return true;
132 }
133
134 void* GraphicsBuffer::Map(bool read, bool write, bool invalidate)
135 {
136         GLbitfield access = ((read)?GL_MAP_READ_BIT:0) | ((write)?GL_MAP_WRITE_BIT:0) | ((invalidate)?GL_MAP_INVALIDATE_BUFFER_BIT:0);
137         GLenum target = BufferTypeToGLType(m_buffer_type);
138
139         if (invalidate)
140         {
141                 m_invalidated = true;
142
143                 // Intel's Mesa driver does not rename the buffer when we map with GL_MAP_INVALIDATE_BUFFER_BIT,
144                 // resulting in the CPU stalling waiting for rendering from the buffer to complete on the GPU.
145                 // We manually force the buffer to be renamed here to avoid this.
146                 m_buffer_shape_dirty = true;
147         }
148
149         if (m_map_pointer)
150                 Warn("Tried to map already mapped buffer!");    
151
152
153         if (!read && m_buffer_usage == BufferUsage::BufferUsageStaticDraw)
154         {
155                 m_map_pointer = malloc(m_buffer_size);
156                 m_faking_map = true;
157                 return m_map_pointer;
158         }
159
160         RecreateBuffer();
161
162         Bind();
163         
164         m_map_pointer = glMapBufferRange(target, 0, m_buffer_size, access);
165         
166         return m_map_pointer;
167 }
168
169 void* GraphicsBuffer::MapRange(int offset, int length, bool read, bool write, bool invalidate)
170 {
171         GLbitfield access = ((read)?GL_MAP_READ_BIT:0) | ((write)?GL_MAP_WRITE_BIT:0) | ((invalidate)?GL_MAP_INVALIDATE_RANGE_BIT:0);
172         GLenum target = BufferTypeToGLType(m_buffer_type);
173
174         if (m_map_pointer)
175                 Warn("Tried to map already mapped buffer!");    
176
177         RecreateBuffer();
178
179         Bind();
180         
181         m_map_pointer = glMapBufferRange(target, offset, length, access);
182         return m_map_pointer;
183 }
184
185 void GraphicsBuffer::UnMap()
186 {
187         GLenum target = BufferTypeToGLType(m_buffer_type);
188
189         if (m_faking_map)
190         {
191                 Upload(m_buffer_size, m_map_pointer);
192                 free(m_map_pointer);
193                 m_map_pointer = nullptr;
194                 m_invalidated = false;
195                 m_faking_map = false;
196                 return;
197         }
198         
199         Bind();
200         glUnmapBuffer(target);
201         m_map_pointer = nullptr;
202         m_invalidated = false;
203 }
204
205 void GraphicsBuffer::Upload(size_t length, const void* data)
206 {
207         GLenum target = BufferTypeToGLType(m_buffer_type);
208         
209         GLenum usage = BufferUsageToGLUsage(m_buffer_usage);
210
211         m_invalidated = true;
212         m_buffer_size = length;
213         if (!RecreateBuffer(data))
214         {
215                 Bind();
216                 glBufferData(target, length, data, usage);
217         }
218         if (data != nullptr)
219                 m_invalidated = false;
220 }
221
222 void GraphicsBuffer::UploadRange(size_t length, intptr_t offset, const void* data)
223 {
224         GLenum target = BufferTypeToGLType(m_buffer_type);
225
226         RecreateBuffer();
227         
228         Bind();
229         glBufferSubData(target, offset, length, data);
230         m_invalidated = false;
231 }
232
233 void GraphicsBuffer::Resize(size_t length)
234 {
235         if (m_invalidated)
236         {
237                 m_buffer_size = length;
238         }
239         else
240         {
241                 // Create a new buffer and copy the old data into it.
242                 UnMap();
243                 GLuint old_buffer = m_buffer_handle;    
244                 glGenBuffers(1, &m_buffer_handle);
245                 Upload(length, nullptr);
246                 glBindBuffer(GL_COPY_READ_BUFFER, old_buffer);
247                 glBindBuffer(GL_COPY_WRITE_BUFFER, m_buffer_handle);
248                 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, m_buffer_size);
249                 glDeleteBuffers(1, &old_buffer);
250                 m_buffer_size = length;
251         }
252 }
253
254 void GraphicsBuffer::Bind() const
255 {
256         if (m_buffer_type == BufferTypeUniform)
257                 glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_buffer_handle);
258         else
259                 glBindBuffer(BufferTypeToGLType(m_buffer_type), m_buffer_handle);
260 }
261

UCC git Repository :: git.ucc.asn.au