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