daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | 8ca9c6e | 2012-01-27 15:38:54 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
daniel@transgaming.com | 8fd34bd | 2011-02-18 02:52:14 +0000 | [diff] [blame] | 7 | // VertexDataManager.h: Defines the VertexDataManager, a class that |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 8 | // runs the Buffer translation process. |
| 9 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 10 | #include "libANGLE/renderer/d3d/VertexDataManager.h" |
| 11 | #include "libANGLE/renderer/d3d/BufferD3D.h" |
| 12 | #include "libANGLE/renderer/d3d/VertexBuffer.h" |
| 13 | #include "libANGLE/renderer/Renderer.h" |
| 14 | #include "libANGLE/Buffer.h" |
Geoff Lang | 2132941 | 2014-12-02 20:50:30 +0000 | [diff] [blame^] | 15 | #include "libANGLE/ProgramBinary.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 16 | #include "libANGLE/VertexAttribute.h" |
| 17 | #include "libANGLE/State.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 18 | |
| 19 | namespace |
| 20 | { |
| 21 | enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 }; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 22 | // This has to be at least 4k or else it fails on ATI cards. |
| 23 | enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 }; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 24 | } |
| 25 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 26 | namespace rx |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 27 | { |
| 28 | |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 29 | static int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size) |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 30 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 31 | // Size cannot be larger than a GLsizei |
| 32 | if (size > static_cast<unsigned int>(std::numeric_limits<int>::max())) |
| 33 | { |
| 34 | size = static_cast<unsigned int>(std::numeric_limits<int>::max()); |
| 35 | } |
| 36 | |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 37 | GLsizei stride = ComputeVertexAttributeStride(attrib); |
| 38 | return (size - attrib.offset % stride + (stride - ComputeVertexAttributeTypeSize(attrib))) / stride; |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 41 | static int StreamingBufferElementCount(const gl::VertexAttribute &attrib, int vertexDrawCount, int instanceDrawCount) |
Jamie Madill | 9b4f384 | 2013-08-26 15:29:30 -0400 | [diff] [blame] | 42 | { |
| 43 | // For instanced rendering, we draw "instanceDrawCount" sets of "vertexDrawCount" vertices. |
| 44 | // |
| 45 | // A vertex attribute with a positive divisor loads one instanced vertex for every set of |
| 46 | // non-instanced vertices, and the instanced vertex index advances once every "mDivisor" instances. |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 47 | if (instanceDrawCount > 0 && attrib.divisor > 0) |
Jamie Madill | 9b4f384 | 2013-08-26 15:29:30 -0400 | [diff] [blame] | 48 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 49 | return instanceDrawCount / attrib.divisor; |
Jamie Madill | 9b4f384 | 2013-08-26 15:29:30 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return vertexDrawCount; |
| 53 | } |
| 54 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 55 | VertexDataManager::VertexDataManager(RendererD3D *renderer) : mRenderer(renderer) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 56 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 57 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 58 | { |
shannon.woods%transgaming.com@gtempaccount.com | 3026dc7 | 2013-04-13 03:37:27 +0000 | [diff] [blame] | 59 | mCurrentValue[i].FloatValues[0] = std::numeric_limits<float>::quiet_NaN(); |
| 60 | mCurrentValue[i].FloatValues[1] = std::numeric_limits<float>::quiet_NaN(); |
| 61 | mCurrentValue[i].FloatValues[2] = std::numeric_limits<float>::quiet_NaN(); |
| 62 | mCurrentValue[i].FloatValues[3] = std::numeric_limits<float>::quiet_NaN(); |
| 63 | mCurrentValue[i].Type = GL_FLOAT; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 64 | mCurrentValueBuffer[i] = NULL; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 65 | mCurrentValueOffsets[i] = 0; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 66 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 67 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 68 | mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 69 | |
| 70 | if (!mStreamingBuffer) |
| 71 | { |
| 72 | ERR("Failed to allocate the streaming vertex buffer."); |
| 73 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | VertexDataManager::~VertexDataManager() |
| 77 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 78 | delete mStreamingBuffer; |
| 79 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 80 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 81 | { |
| 82 | delete mCurrentValueBuffer[i]; |
| 83 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 86 | gl::Error VertexDataManager::prepareVertexData(const gl::State &state, GLint start, GLsizei count, |
| 87 | TranslatedAttribute *translated, GLsizei instances) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 88 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 89 | if (!mStreamingBuffer) |
| 90 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 91 | return gl::Error(GL_OUT_OF_MEMORY, "Internal streaming vertex buffer is unexpectedly NULL."); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 94 | // Invalidate static buffers that don't contain matching attributes |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 95 | for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 96 | { |
Geoff Lang | 2132941 | 2014-12-02 20:50:30 +0000 | [diff] [blame^] | 97 | translated[attributeIndex].active = (state.getCurrentProgramBinary()->getSemanticIndex(attributeIndex) != -1); |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 98 | const gl::VertexAttribute &curAttrib = state.getVertexAttribState(attributeIndex); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 99 | |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 100 | if (translated[attributeIndex].active && curAttrib.enabled) |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 101 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 102 | invalidateMatchingStaticData(curAttrib, state.getVertexAttribCurrentValue(attributeIndex)); |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | // Reserve the required space in the buffers |
| 107 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 108 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 109 | const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i); |
| 110 | if (translated[i].active && curAttrib.enabled) |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 111 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 112 | gl::Error error = reserveSpaceForAttrib(curAttrib, state.getVertexAttribCurrentValue(i), count, instances); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 113 | if (error.isError()) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 114 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 115 | return error; |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 116 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | // Perform the vertex data translations |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 121 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 122 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 123 | const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 124 | if (translated[i].active) |
| 125 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 126 | if (curAttrib.enabled) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 127 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 128 | gl::Error error = storeAttribute(curAttrib, state.getVertexAttribCurrentValue(i), |
| 129 | &translated[i], start, count, instances); |
| 130 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 131 | if (error.isError()) |
| 132 | { |
| 133 | return error; |
| 134 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 135 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 136 | else |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 137 | { |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 138 | if (!mCurrentValueBuffer[i]) |
| 139 | { |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 140 | mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE); |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 143 | gl::Error error = storeCurrentValue(curAttrib, state.getVertexAttribCurrentValue(i), &translated[i], |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 144 | &mCurrentValue[i], &mCurrentValueOffsets[i], |
| 145 | mCurrentValueBuffer[i]); |
| 146 | if (error.isError()) |
| 147 | { |
| 148 | return error; |
| 149 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 150 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 153 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 154 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 155 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 156 | const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i); |
| 157 | if (translated[i].active && curAttrib.enabled) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 158 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 159 | gl::Buffer *buffer = curAttrib.buffer.get(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 160 | |
| 161 | if (buffer) |
| 162 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 163 | BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation()); |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 164 | bufferImpl->promoteStaticUsage(count * ComputeVertexAttributeTypeSize(curAttrib)); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 169 | return gl::Error(GL_NO_ERROR); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 172 | void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib, |
| 173 | const gl::VertexAttribCurrentValueData ¤tValue) const |
| 174 | { |
| 175 | gl::Buffer *buffer = attrib.buffer.get(); |
| 176 | |
| 177 | if (buffer) |
| 178 | { |
| 179 | BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation()); |
| 180 | StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer(); |
| 181 | |
| 182 | if (staticBuffer && |
| 183 | staticBuffer->getBufferSize() > 0 && |
| 184 | !staticBuffer->lookupAttribute(attrib, NULL) && |
| 185 | !staticBuffer->directStoragePossible(attrib, currentValue)) |
| 186 | { |
| 187 | bufferImpl->invalidateStaticData(); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 192 | gl::Error VertexDataManager::reserveSpaceForAttrib(const gl::VertexAttribute &attrib, |
| 193 | const gl::VertexAttribCurrentValueData ¤tValue, |
| 194 | GLsizei count, |
| 195 | GLsizei instances) const |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 196 | { |
| 197 | gl::Buffer *buffer = attrib.buffer.get(); |
| 198 | BufferD3D *bufferImpl = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL; |
| 199 | StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL; |
| 200 | VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer); |
| 201 | |
| 202 | if (!vertexBuffer->directStoragePossible(attrib, currentValue)) |
| 203 | { |
| 204 | if (staticBuffer) |
| 205 | { |
| 206 | if (staticBuffer->getBufferSize() == 0) |
| 207 | { |
| 208 | int totalCount = ElementsInBuffer(attrib, bufferImpl->getSize()); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 209 | gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0); |
| 210 | if (error.isError()) |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 211 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 212 | return error; |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | int totalCount = StreamingBufferElementCount(attrib, count, instances); |
| 219 | ASSERT(!bufferImpl || ElementsInBuffer(attrib, bufferImpl->getSize()) >= totalCount); |
| 220 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 221 | gl::Error error = mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances); |
| 222 | if (error.isError()) |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 223 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 224 | return error; |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 229 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 232 | gl::Error VertexDataManager::storeAttribute(const gl::VertexAttribute &attrib, |
| 233 | const gl::VertexAttribCurrentValueData ¤tValue, |
| 234 | TranslatedAttribute *translated, |
| 235 | GLint start, |
| 236 | GLsizei count, |
| 237 | GLsizei instances) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 238 | { |
| 239 | gl::Buffer *buffer = attrib.buffer.get(); |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 240 | ASSERT(buffer || attrib.pointer); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 241 | |
| 242 | BufferD3D *storage = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL; |
| 243 | StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL; |
| 244 | VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer); |
| 245 | bool directStorage = vertexBuffer->directStoragePossible(attrib, currentValue); |
| 246 | |
| 247 | unsigned int streamOffset = 0; |
| 248 | unsigned int outputElementSize = 0; |
| 249 | |
| 250 | if (directStorage) |
| 251 | { |
| 252 | outputElementSize = ComputeVertexAttributeStride(attrib); |
| 253 | streamOffset = attrib.offset + outputElementSize * start; |
| 254 | } |
| 255 | else if (staticBuffer) |
| 256 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 257 | gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize); |
| 258 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 259 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 260 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | if (!staticBuffer->lookupAttribute(attrib, &streamOffset)) |
| 264 | { |
| 265 | // Convert the entire buffer |
| 266 | int totalCount = ElementsInBuffer(attrib, storage->getSize()); |
| 267 | int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib); |
| 268 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 269 | gl::Error error = staticBuffer->storeVertexAttributes(attrib, currentValue, -startIndex, totalCount, |
| 270 | 0, &streamOffset); |
| 271 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 272 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 273 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize; |
| 278 | unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? start * outputElementSize : 0; |
| 279 | if (streamOffset + firstElementOffset + startOffset < streamOffset) |
| 280 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 281 | return gl::Error(GL_OUT_OF_MEMORY); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | streamOffset += firstElementOffset + startOffset; |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | int totalCount = StreamingBufferElementCount(attrib, count, instances); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 289 | gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize); |
| 290 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 291 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 292 | return error; |
| 293 | } |
| 294 | |
| 295 | error = mStreamingBuffer->storeVertexAttributes(attrib, currentValue, start, totalCount, instances, &streamOffset); |
| 296 | if (error.isError()) |
| 297 | { |
| 298 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | translated->storage = directStorage ? storage : NULL; |
| 303 | translated->vertexBuffer = vertexBuffer->getVertexBuffer(); |
| 304 | translated->serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial(); |
| 305 | translated->divisor = attrib.divisor; |
| 306 | |
| 307 | translated->attribute = &attrib; |
| 308 | translated->currentValueType = currentValue.Type; |
| 309 | translated->stride = outputElementSize; |
| 310 | translated->offset = streamOffset; |
| 311 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 312 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 315 | gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribute &attrib, |
| 316 | const gl::VertexAttribCurrentValueData ¤tValue, |
| 317 | TranslatedAttribute *translated, |
| 318 | gl::VertexAttribCurrentValueData *cachedValue, |
| 319 | size_t *cachedOffset, |
| 320 | StreamingVertexBufferInterface *buffer) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 321 | { |
| 322 | if (*cachedValue != currentValue) |
| 323 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 324 | gl::Error error = buffer->reserveVertexSpace(attrib, 1, 0); |
| 325 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 326 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 327 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | unsigned int streamOffset; |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 331 | error = buffer->storeVertexAttributes(attrib, currentValue, 0, 1, 0, &streamOffset); |
| 332 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 333 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 334 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | *cachedValue = currentValue; |
| 338 | *cachedOffset = streamOffset; |
| 339 | } |
| 340 | |
| 341 | translated->storage = NULL; |
| 342 | translated->vertexBuffer = buffer->getVertexBuffer(); |
| 343 | translated->serial = buffer->getSerial(); |
| 344 | translated->divisor = 0; |
| 345 | |
| 346 | translated->attribute = &attrib; |
| 347 | translated->currentValueType = currentValue.Type; |
| 348 | translated->stride = 0; |
| 349 | translated->offset = *cachedOffset; |
| 350 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 351 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 352 | } |
| 353 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 354 | } |