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 | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 15 | #include "libANGLE/Program.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 | { |
Gregoire Payen de La Garanderie | 2c7ea05 | 2015-01-07 12:47:35 +0000 | [diff] [blame] | 49 | // When instanceDrawCount is not a multiple attrib.divisor, the division must round up. |
| 50 | // For instance, with 5 non-instanced vertices and a divisor equal to 3, we need 2 instanced vertices. |
| 51 | return (instanceDrawCount + attrib.divisor - 1) / attrib.divisor; |
Jamie Madill | 9b4f384 | 2013-08-26 15:29:30 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | return vertexDrawCount; |
| 55 | } |
| 56 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 57 | VertexDataManager::VertexDataManager(RendererD3D *renderer) : mRenderer(renderer) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 58 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 59 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 60 | { |
shannon.woods%transgaming.com@gtempaccount.com | 3026dc7 | 2013-04-13 03:37:27 +0000 | [diff] [blame] | 61 | mCurrentValue[i].FloatValues[0] = std::numeric_limits<float>::quiet_NaN(); |
| 62 | mCurrentValue[i].FloatValues[1] = std::numeric_limits<float>::quiet_NaN(); |
| 63 | mCurrentValue[i].FloatValues[2] = std::numeric_limits<float>::quiet_NaN(); |
| 64 | mCurrentValue[i].FloatValues[3] = std::numeric_limits<float>::quiet_NaN(); |
| 65 | mCurrentValue[i].Type = GL_FLOAT; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 66 | mCurrentValueBuffer[i] = NULL; |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 67 | mCurrentValueOffsets[i] = 0; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 68 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 69 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 70 | mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 71 | |
| 72 | if (!mStreamingBuffer) |
| 73 | { |
| 74 | ERR("Failed to allocate the streaming vertex buffer."); |
| 75 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | VertexDataManager::~VertexDataManager() |
| 79 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 80 | delete mStreamingBuffer; |
| 81 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 82 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 83 | { |
| 84 | delete mCurrentValueBuffer[i]; |
| 85 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame^] | 88 | void VertexDataManager::hintUnmapAllResources(const gl::State &state) |
| 89 | { |
| 90 | mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); |
| 91 | |
| 92 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 93 | { |
| 94 | const gl::VertexAttribute &attrib = state.getVertexAttribState(i); |
| 95 | if (attrib.enabled) |
| 96 | { |
| 97 | gl::Buffer *buffer = attrib.buffer.get(); |
| 98 | BufferD3D *storage = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL; |
| 99 | StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL; |
| 100 | |
| 101 | if (staticBuffer) |
| 102 | { |
| 103 | staticBuffer->getVertexBuffer()->hintUnmapResource(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 109 | { |
| 110 | if (mCurrentValueBuffer[i] != NULL) |
| 111 | { |
| 112 | mCurrentValueBuffer[i]->getVertexBuffer()->hintUnmapResource(); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 117 | gl::Error VertexDataManager::prepareVertexData(const gl::State &state, GLint start, GLsizei count, |
| 118 | TranslatedAttribute *translated, GLsizei instances) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 119 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 120 | if (!mStreamingBuffer) |
| 121 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 122 | 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] | 123 | } |
| 124 | |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 125 | // Invalidate static buffers that don't contain matching attributes |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 126 | for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 127 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 128 | translated[attributeIndex].active = (state.getProgram()->getSemanticIndex(attributeIndex) != -1); |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 129 | const gl::VertexAttribute &curAttrib = state.getVertexAttribState(attributeIndex); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 130 | |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 131 | if (translated[attributeIndex].active && curAttrib.enabled) |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 132 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 133 | invalidateMatchingStaticData(curAttrib, state.getVertexAttribCurrentValue(attributeIndex)); |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | // Reserve the required space in the buffers |
| 138 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 139 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 140 | const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i); |
| 141 | if (translated[i].active && curAttrib.enabled) |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 142 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 143 | gl::Error error = reserveSpaceForAttrib(curAttrib, state.getVertexAttribCurrentValue(i), count, instances); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 144 | if (error.isError()) |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 145 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 146 | return error; |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 147 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | // Perform the vertex data translations |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 152 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 153 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 154 | const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 155 | if (translated[i].active) |
| 156 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 157 | if (curAttrib.enabled) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 158 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 159 | gl::Error error = storeAttribute(curAttrib, state.getVertexAttribCurrentValue(i), |
| 160 | &translated[i], start, count, instances); |
| 161 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 162 | if (error.isError()) |
| 163 | { |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame^] | 164 | hintUnmapAllResources(state); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 165 | return error; |
| 166 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 167 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 168 | else |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 169 | { |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 170 | if (!mCurrentValueBuffer[i]) |
| 171 | { |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 172 | mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE); |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 175 | gl::Error error = storeCurrentValue(curAttrib, state.getVertexAttribCurrentValue(i), &translated[i], |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 176 | &mCurrentValue[i], &mCurrentValueOffsets[i], |
| 177 | mCurrentValueBuffer[i]); |
| 178 | if (error.isError()) |
| 179 | { |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame^] | 180 | hintUnmapAllResources(state); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 181 | return error; |
| 182 | } |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 183 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 186 | |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame^] | 187 | // Hint to unmap all the resources |
| 188 | hintUnmapAllResources(state); |
| 189 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 190 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 191 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 192 | const gl::VertexAttribute &curAttrib = state.getVertexAttribState(i); |
| 193 | if (translated[i].active && curAttrib.enabled) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 194 | { |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 195 | gl::Buffer *buffer = curAttrib.buffer.get(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 196 | |
| 197 | if (buffer) |
| 198 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 199 | BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation()); |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 200 | bufferImpl->promoteStaticUsage(count * ComputeVertexAttributeTypeSize(curAttrib)); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 205 | return gl::Error(GL_NO_ERROR); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 208 | void VertexDataManager::invalidateMatchingStaticData(const gl::VertexAttribute &attrib, |
| 209 | const gl::VertexAttribCurrentValueData ¤tValue) const |
| 210 | { |
| 211 | gl::Buffer *buffer = attrib.buffer.get(); |
| 212 | |
| 213 | if (buffer) |
| 214 | { |
| 215 | BufferD3D *bufferImpl = BufferD3D::makeBufferD3D(buffer->getImplementation()); |
| 216 | StaticVertexBufferInterface *staticBuffer = bufferImpl->getStaticVertexBuffer(); |
| 217 | |
| 218 | if (staticBuffer && |
| 219 | staticBuffer->getBufferSize() > 0 && |
| 220 | !staticBuffer->lookupAttribute(attrib, NULL) && |
| 221 | !staticBuffer->directStoragePossible(attrib, currentValue)) |
| 222 | { |
| 223 | bufferImpl->invalidateStaticData(); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 228 | gl::Error VertexDataManager::reserveSpaceForAttrib(const gl::VertexAttribute &attrib, |
| 229 | const gl::VertexAttribCurrentValueData ¤tValue, |
| 230 | GLsizei count, |
| 231 | GLsizei instances) const |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 232 | { |
| 233 | gl::Buffer *buffer = attrib.buffer.get(); |
| 234 | BufferD3D *bufferImpl = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL; |
| 235 | StaticVertexBufferInterface *staticBuffer = bufferImpl ? bufferImpl->getStaticVertexBuffer() : NULL; |
| 236 | VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer); |
| 237 | |
| 238 | if (!vertexBuffer->directStoragePossible(attrib, currentValue)) |
| 239 | { |
| 240 | if (staticBuffer) |
| 241 | { |
| 242 | if (staticBuffer->getBufferSize() == 0) |
| 243 | { |
| 244 | int totalCount = ElementsInBuffer(attrib, bufferImpl->getSize()); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 245 | gl::Error error = staticBuffer->reserveVertexSpace(attrib, totalCount, 0); |
| 246 | if (error.isError()) |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 247 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 248 | return error; |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | int totalCount = StreamingBufferElementCount(attrib, count, instances); |
| 255 | ASSERT(!bufferImpl || ElementsInBuffer(attrib, bufferImpl->getSize()) >= totalCount); |
| 256 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 257 | gl::Error error = mStreamingBuffer->reserveVertexSpace(attrib, totalCount, instances); |
| 258 | if (error.isError()) |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 259 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 260 | return error; |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 265 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 268 | gl::Error VertexDataManager::storeAttribute(const gl::VertexAttribute &attrib, |
| 269 | const gl::VertexAttribCurrentValueData ¤tValue, |
| 270 | TranslatedAttribute *translated, |
| 271 | GLint start, |
| 272 | GLsizei count, |
| 273 | GLsizei instances) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 274 | { |
| 275 | gl::Buffer *buffer = attrib.buffer.get(); |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 276 | ASSERT(buffer || attrib.pointer); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 277 | |
| 278 | BufferD3D *storage = buffer ? BufferD3D::makeBufferD3D(buffer->getImplementation()) : NULL; |
| 279 | StaticVertexBufferInterface *staticBuffer = storage ? storage->getStaticVertexBuffer() : NULL; |
| 280 | VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer); |
| 281 | bool directStorage = vertexBuffer->directStoragePossible(attrib, currentValue); |
| 282 | |
| 283 | unsigned int streamOffset = 0; |
| 284 | unsigned int outputElementSize = 0; |
| 285 | |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 286 | // Instanced vertices do not apply the 'start' offset |
| 287 | GLint firstVertexIndex = (instances > 0 && attrib.divisor > 0 ? 0 : start); |
| 288 | |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 289 | if (directStorage) |
| 290 | { |
| 291 | outputElementSize = ComputeVertexAttributeStride(attrib); |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 292 | streamOffset = attrib.offset + outputElementSize * firstVertexIndex; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 293 | } |
| 294 | else if (staticBuffer) |
| 295 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 296 | gl::Error error = staticBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize); |
| 297 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 298 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 299 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | if (!staticBuffer->lookupAttribute(attrib, &streamOffset)) |
| 303 | { |
| 304 | // Convert the entire buffer |
| 305 | int totalCount = ElementsInBuffer(attrib, storage->getSize()); |
| 306 | int startIndex = attrib.offset / ComputeVertexAttributeStride(attrib); |
| 307 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 308 | gl::Error error = staticBuffer->storeVertexAttributes(attrib, currentValue, -startIndex, totalCount, |
| 309 | 0, &streamOffset); |
| 310 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 311 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 312 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | unsigned int firstElementOffset = (attrib.offset / ComputeVertexAttributeStride(attrib)) * outputElementSize; |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 317 | unsigned int startOffset = (instances == 0 || attrib.divisor == 0) ? firstVertexIndex * outputElementSize : 0; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 318 | if (streamOffset + firstElementOffset + startOffset < streamOffset) |
| 319 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 320 | return gl::Error(GL_OUT_OF_MEMORY); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | streamOffset += firstElementOffset + startOffset; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | int totalCount = StreamingBufferElementCount(attrib, count, instances); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 328 | gl::Error error = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attrib, 1, 0, &outputElementSize); |
| 329 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 330 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 331 | return error; |
| 332 | } |
| 333 | |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 334 | error = mStreamingBuffer->storeVertexAttributes(attrib, currentValue, firstVertexIndex, |
| 335 | totalCount, instances, &streamOffset); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 336 | if (error.isError()) |
| 337 | { |
| 338 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | translated->storage = directStorage ? storage : NULL; |
| 343 | translated->vertexBuffer = vertexBuffer->getVertexBuffer(); |
| 344 | translated->serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial(); |
| 345 | translated->divisor = attrib.divisor; |
| 346 | |
| 347 | translated->attribute = &attrib; |
| 348 | translated->currentValueType = currentValue.Type; |
| 349 | translated->stride = outputElementSize; |
| 350 | translated->offset = streamOffset; |
| 351 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 352 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 353 | } |
| 354 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 355 | gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribute &attrib, |
| 356 | const gl::VertexAttribCurrentValueData ¤tValue, |
| 357 | TranslatedAttribute *translated, |
| 358 | gl::VertexAttribCurrentValueData *cachedValue, |
| 359 | size_t *cachedOffset, |
| 360 | StreamingVertexBufferInterface *buffer) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 361 | { |
| 362 | if (*cachedValue != currentValue) |
| 363 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 364 | gl::Error error = buffer->reserveVertexSpace(attrib, 1, 0); |
| 365 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 366 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 367 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | unsigned int streamOffset; |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 371 | error = buffer->storeVertexAttributes(attrib, currentValue, 0, 1, 0, &streamOffset); |
| 372 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 373 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 374 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | *cachedValue = currentValue; |
| 378 | *cachedOffset = streamOffset; |
| 379 | } |
| 380 | |
| 381 | translated->storage = NULL; |
| 382 | translated->vertexBuffer = buffer->getVertexBuffer(); |
| 383 | translated->serial = buffer->getSerial(); |
| 384 | translated->divisor = 0; |
| 385 | |
| 386 | translated->attribute = &attrib; |
| 387 | translated->currentValueType = currentValue.Type; |
| 388 | translated->stride = 0; |
| 389 | translated->offset = *cachedOffset; |
| 390 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 391 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 392 | } |
| 393 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 394 | } |