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" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 11 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 12 | #include "libANGLE/Buffer.h" |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 13 | #include "libANGLE/formatutils.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 14 | #include "libANGLE/Program.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 15 | #include "libANGLE/State.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" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/d3d/BufferD3D.h" |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 19 | #include "libANGLE/renderer/d3d/RendererD3D.h" |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 20 | #include "libANGLE/renderer/d3d/VertexBuffer.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 21 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 22 | namespace rx |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 23 | { |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 24 | namespace |
| 25 | { |
| 26 | enum |
| 27 | { |
| 28 | INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024 |
| 29 | }; |
| 30 | // This has to be at least 4k or else it fails on ATI cards. |
| 31 | enum |
| 32 | { |
| 33 | CONSTANT_VERTEX_BUFFER_SIZE = 4096 |
| 34 | }; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 35 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 36 | int ElementsInBuffer(const gl::VertexAttribute &attrib, unsigned int size) |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 37 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 38 | // Size cannot be larger than a GLsizei |
| 39 | if (size > static_cast<unsigned int>(std::numeric_limits<int>::max())) |
| 40 | { |
| 41 | size = static_cast<unsigned int>(std::numeric_limits<int>::max()); |
| 42 | } |
| 43 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 44 | GLsizei stride = static_cast<GLsizei>(ComputeVertexAttributeStride(attrib)); |
| 45 | return (size - attrib.offset % stride + |
| 46 | (stride - static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)))) / |
| 47 | stride; |
jbauman@chromium.org | 059fc15 | 2011-11-18 19:26:17 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 50 | bool DirectStoragePossible(const gl::VertexAttribute &attrib) |
| 51 | { |
| 52 | // Current value attribs may not use direct storage. |
| 53 | if (!attrib.enabled) |
| 54 | { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | gl::Buffer *buffer = attrib.buffer.get(); |
| 59 | if (!buffer) |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
| 65 | ASSERT(bufferD3D); |
| 66 | if (!bufferD3D->supportsDirectBinding()) |
| 67 | { |
| 68 | return false; |
| 69 | } |
| 70 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 71 | // Alignment restrictions: In D3D, vertex data must be aligned to the format stride, or to a |
| 72 | // 4-byte boundary, whichever is smaller. (Undocumented, and experimentally confirmed) |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 73 | size_t alignment = 4; |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 74 | |
| 75 | if (attrib.type != GL_FLOAT) |
| 76 | { |
| 77 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 78 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 79 | // TODO(jmadill): add VertexFormatCaps |
| 80 | BufferFactoryD3D *factory = bufferD3D->getFactory(); |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 81 | |
| 82 | auto errorOrElementSize = factory->getVertexSpaceRequired(attrib, 1, 0); |
| 83 | if (errorOrElementSize.isError()) |
| 84 | { |
| 85 | ERR("Unlogged error in DirectStoragePossible."); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | alignment = std::min<size_t>(errorOrElementSize.getResult(), 4); |
| 90 | |
| 91 | // CPU-converted vertex data must be converted (naturally). |
| 92 | if ((factory->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_CPU) != 0) |
| 93 | { |
| 94 | return false; |
| 95 | } |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 98 | // Final alignment check - unaligned data must be converted. |
| 99 | return (static_cast<size_t>(ComputeVertexAttributeStride(attrib)) % alignment == 0) && |
| 100 | (static_cast<size_t>(attrib.offset) % alignment == 0); |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 101 | } |
| 102 | } // anonymous namespace |
| 103 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 104 | TranslatedAttribute::TranslatedAttribute() |
| 105 | : active(false), |
| 106 | attribute(nullptr), |
| 107 | currentValueType(GL_NONE), |
| 108 | offset(0), |
| 109 | stride(0), |
| 110 | vertexBuffer(), |
| 111 | storage(nullptr), |
| 112 | serial(0), |
| 113 | divisor(0) |
| 114 | { |
| 115 | } |
| 116 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 117 | VertexDataManager::CurrentValueState::CurrentValueState() |
| 118 | : buffer(nullptr), |
| 119 | offset(0) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 120 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 121 | data.FloatValues[0] = std::numeric_limits<float>::quiet_NaN(); |
| 122 | data.FloatValues[1] = std::numeric_limits<float>::quiet_NaN(); |
| 123 | data.FloatValues[2] = std::numeric_limits<float>::quiet_NaN(); |
| 124 | data.FloatValues[3] = std::numeric_limits<float>::quiet_NaN(); |
| 125 | data.Type = GL_FLOAT; |
| 126 | } |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 127 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 128 | VertexDataManager::CurrentValueState::~CurrentValueState() |
| 129 | { |
| 130 | SafeDelete(buffer); |
| 131 | } |
| 132 | |
| 133 | VertexDataManager::VertexDataManager(BufferFactoryD3D *factory) |
| 134 | : mFactory(factory), |
| 135 | mStreamingBuffer(nullptr), |
| 136 | // TODO(jmadill): use context caps |
| 137 | mCurrentValueCache(gl::MAX_VERTEX_ATTRIBS) |
| 138 | { |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 139 | mStreamingBuffer = new StreamingVertexBufferInterface(factory, INITIAL_STREAM_BUFFER_SIZE); |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 140 | |
| 141 | if (!mStreamingBuffer) |
| 142 | { |
| 143 | ERR("Failed to allocate the streaming vertex buffer."); |
| 144 | } |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 145 | |
| 146 | // TODO(jmadill): use context caps |
| 147 | mActiveEnabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS); |
| 148 | mActiveDisabledAttributes.reserve(gl::MAX_VERTEX_ATTRIBS); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | VertexDataManager::~VertexDataManager() |
| 152 | { |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 153 | SafeDelete(mStreamingBuffer); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 156 | void VertexDataManager::unmapStreamingBuffer() |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 157 | { |
| 158 | mStreamingBuffer->getVertexBuffer()->hintUnmapResource(); |
Austin Kinross | be0facc | 2015-01-07 16:22:29 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 161 | gl::Error VertexDataManager::prepareVertexData(const gl::State &state, |
| 162 | GLint start, |
| 163 | GLsizei count, |
| 164 | std::vector<TranslatedAttribute> *translatedAttribs, |
| 165 | GLsizei instances) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 166 | { |
daniel@transgaming.com | 72b9e18 | 2011-04-13 14:58:33 +0000 | [diff] [blame] | 167 | if (!mStreamingBuffer) |
| 168 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 169 | 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] | 170 | } |
| 171 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 172 | // Compute active enabled and active disable attributes, for speed. |
| 173 | // TODO(jmadill): don't recompute if there was no state change |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 174 | const gl::VertexArray *vertexArray = state.getVertexArray(); |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 175 | const gl::Program *program = state.getProgram(); |
| 176 | const auto &vertexAttributes = vertexArray->getVertexAttributes(); |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 177 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 178 | mActiveEnabledAttributes.clear(); |
| 179 | mActiveDisabledAttributes.clear(); |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 180 | translatedAttribs->clear(); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 181 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 182 | for (size_t attribIndex = 0; attribIndex < vertexAttributes.size(); ++attribIndex) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 183 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 184 | if (program->isAttribLocationActive(attribIndex)) |
daniel@transgaming.com | c828b14 | 2010-05-12 03:42:04 +0000 | [diff] [blame] | 185 | { |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 186 | // Resize automatically puts in empty attribs |
| 187 | translatedAttribs->resize(attribIndex + 1); |
| 188 | |
| 189 | TranslatedAttribute *translated = &(*translatedAttribs)[attribIndex]; |
| 190 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 191 | // Record the attribute now |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 192 | translated->active = true; |
| 193 | translated->attribute = &vertexAttributes[attribIndex]; |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 194 | translated->currentValueType = |
| 195 | state.getVertexAttribCurrentValue(static_cast<unsigned int>(attribIndex)).Type; |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 196 | translated->divisor = vertexAttributes[attribIndex].divisor; |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 197 | |
| 198 | if (vertexAttributes[attribIndex].enabled) |
| 199 | { |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 200 | mActiveEnabledAttributes.push_back(translated); |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 201 | } |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 202 | else |
| 203 | { |
| 204 | mActiveDisabledAttributes.push_back(attribIndex); |
| 205 | } |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | // Reserve the required space in the buffers |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 210 | for (const TranslatedAttribute *activeAttrib : mActiveEnabledAttributes) |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 211 | { |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 212 | gl::Error error = reserveSpaceForAttrib(*activeAttrib, count, instances); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 213 | if (error.isError()) |
shannon.woods@transgaming.com | a9a509e | 2013-02-28 23:10:44 +0000 | [diff] [blame] | 214 | { |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 215 | return error; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | // Perform the vertex data translations |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 220 | for (TranslatedAttribute *activeAttrib : mActiveEnabledAttributes) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 221 | { |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 222 | gl::Error error = storeAttribute(activeAttrib, start, count, instances); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 223 | |
| 224 | if (error.isError()) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 225 | { |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 226 | unmapStreamingBuffer(); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 227 | return error; |
| 228 | } |
| 229 | } |
Shannon Woods | 1a96548 | 2014-09-22 18:00:32 -0400 | [diff] [blame] | 230 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 231 | unmapStreamingBuffer(); |
| 232 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 233 | for (size_t attribIndex : mActiveDisabledAttributes) |
| 234 | { |
| 235 | if (mCurrentValueCache[attribIndex].buffer == nullptr) |
| 236 | { |
| 237 | mCurrentValueCache[attribIndex].buffer = new StreamingVertexBufferInterface(mFactory, CONSTANT_VERTEX_BUFFER_SIZE); |
| 238 | } |
jbauman@chromium.org | 83b61bc | 2011-09-02 18:59:24 +0000 | [diff] [blame] | 239 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 240 | gl::Error error = storeCurrentValue( |
| 241 | state.getVertexAttribCurrentValue(static_cast<unsigned int>(attribIndex)), |
| 242 | &(*translatedAttribs)[attribIndex], &mCurrentValueCache[attribIndex]); |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 243 | if (error.isError()) |
| 244 | { |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 245 | return error; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
apatrick@chromium.org | f99fbb7 | 2010-11-16 01:57:05 +0000 | [diff] [blame] | 248 | |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 249 | for (const TranslatedAttribute *activeAttrib : mActiveEnabledAttributes) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 250 | { |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 251 | gl::Buffer *buffer = activeAttrib->attribute->buffer.get(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 252 | |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 253 | if (buffer) |
| 254 | { |
| 255 | BufferD3D *bufferD3D = GetImplAs<BufferD3D>(buffer); |
Jamie Madill | 16f99b7 | 2015-07-02 14:09:06 -0400 | [diff] [blame] | 256 | size_t typeSize = ComputeVertexAttributeTypeSize(*activeAttrib->attribute); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 257 | bufferD3D->promoteStaticUsage(count * static_cast<int>(typeSize)); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 261 | return gl::Error(GL_NO_ERROR); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 264 | gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 265 | GLsizei count, |
| 266 | GLsizei instances) const |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 267 | { |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 268 | const gl::VertexAttribute &attrib = *translatedAttrib.attribute; |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 269 | if (DirectStoragePossible(attrib)) |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 270 | { |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 271 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 274 | gl::Buffer *buffer = attrib.buffer.get(); |
| 275 | BufferD3D *bufferD3D = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr; |
| 276 | StaticVertexBufferInterface *staticBuffer = |
| 277 | bufferD3D ? bufferD3D->getStaticVertexBuffer(attrib) : nullptr; |
| 278 | |
| 279 | if (staticBuffer) |
| 280 | { |
| 281 | return gl::Error(GL_NO_ERROR); |
| 282 | } |
| 283 | |
| 284 | size_t totalCount = ComputeVertexAttributeElementCount(attrib, count, instances); |
| 285 | ASSERT(!bufferD3D || |
| 286 | ElementsInBuffer(attrib, static_cast<unsigned int>(bufferD3D->getSize())) >= |
| 287 | static_cast<int>(totalCount)); |
| 288 | |
| 289 | return mStreamingBuffer->reserveVertexSpace(attrib, static_cast<GLsizei>(totalCount), |
| 290 | instances); |
Jamie Madill | 6d11380 | 2014-08-25 15:47:52 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Jamie Madill | 3d72cc7 | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 293 | gl::Error VertexDataManager::storeAttribute(TranslatedAttribute *translated, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 294 | GLint start, |
| 295 | GLsizei count, |
| 296 | GLsizei instances) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 297 | { |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 298 | const gl::VertexAttribute &attrib = *translated->attribute; |
| 299 | |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 300 | gl::Buffer *buffer = attrib.buffer.get(); |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 301 | ASSERT(buffer || attrib.pointer); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 302 | ASSERT(attrib.enabled); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 303 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 304 | BufferD3D *storage = buffer ? GetImplAs<BufferD3D>(buffer) : nullptr; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 305 | |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 306 | // Instanced vertices do not apply the 'start' offset |
Jamie Madill | a053733 | 2015-11-19 13:55:26 -0500 | [diff] [blame] | 307 | GLint firstVertexIndex = (attrib.divisor > 0 ? 0 : start); |
Jamie Madill | d4b55a0 | 2015-01-09 14:21:49 -0500 | [diff] [blame] | 308 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 309 | if (DirectStoragePossible(attrib)) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 310 | { |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 311 | translated->vertexBuffer.set(nullptr); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 312 | translated->storage = storage; |
| 313 | translated->serial = storage->getSerial(); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 314 | translated->stride = static_cast<unsigned int>(ComputeVertexAttributeStride(attrib)); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 315 | translated->offset = static_cast<unsigned int>(attrib.offset + translated->stride * firstVertexIndex); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 316 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 317 | } |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 318 | |
| 319 | // Compute source data pointer |
| 320 | const uint8_t *sourceData = nullptr; |
| 321 | |
| 322 | if (buffer) |
| 323 | { |
| 324 | gl::Error error = storage->getData(&sourceData); |
| 325 | if (error.isError()) |
| 326 | { |
| 327 | return error; |
| 328 | } |
| 329 | sourceData += static_cast<int>(attrib.offset); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | sourceData = static_cast<const uint8_t*>(attrib.pointer); |
| 334 | } |
| 335 | |
| 336 | unsigned int streamOffset = 0; |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 337 | |
| 338 | auto errorOrOutputElementSize = mFactory->getVertexSpaceRequired(attrib, 1, 0); |
| 339 | if (errorOrOutputElementSize.isError()) |
| 340 | { |
| 341 | return errorOrOutputElementSize.getError(); |
| 342 | } |
| 343 | |
| 344 | translated->storage = nullptr; |
| 345 | translated->stride = errorOrOutputElementSize.getResult(); |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 346 | |
| 347 | gl::Error error(GL_NO_ERROR); |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 348 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 349 | auto *staticBuffer = storage ? storage->getStaticVertexBuffer(attrib) : nullptr; |
| 350 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 351 | if (staticBuffer) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 352 | { |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 353 | if (staticBuffer->empty()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 354 | { |
| 355 | // Convert the entire buffer |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 356 | int totalCount = |
| 357 | ElementsInBuffer(attrib, static_cast<unsigned int>(storage->getSize())); |
| 358 | int startIndex = static_cast<int>(attrib.offset) / |
| 359 | static_cast<int>(ComputeVertexAttributeStride(attrib)); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 360 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 361 | error = |
| 362 | staticBuffer->storeStaticAttribute(attrib, -startIndex, totalCount, 0, sourceData); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 363 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 364 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 365 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 369 | unsigned int firstElementOffset = |
| 370 | (static_cast<unsigned int>(attrib.offset) / |
| 371 | static_cast<unsigned int>(ComputeVertexAttributeStride(attrib))) * |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 372 | translated->stride; |
Jamie Madill | a053733 | 2015-11-19 13:55:26 -0500 | [diff] [blame] | 373 | ASSERT(attrib.divisor == 0 || firstVertexIndex == 0); |
Jamie Madill | d8fa921 | 2016-03-02 11:51:43 -0500 | [diff] [blame] | 374 | unsigned int startOffset = firstVertexIndex * translated->stride; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 375 | if (streamOffset + firstElementOffset + startOffset < streamOffset) |
| 376 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 377 | return gl::Error(GL_OUT_OF_MEMORY); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | streamOffset += firstElementOffset + startOffset; |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 381 | translated->vertexBuffer.set(staticBuffer->getVertexBuffer()); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 382 | } |
| 383 | else |
| 384 | { |
Geoff Lang | 3cf12ce | 2015-08-27 14:40:48 -0400 | [diff] [blame] | 385 | size_t totalCount = ComputeVertexAttributeElementCount(attrib, count, instances); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 386 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 387 | error = mStreamingBuffer->storeDynamicAttribute( |
Geoff Lang | 3cf12ce | 2015-08-27 14:40:48 -0400 | [diff] [blame] | 388 | attrib, translated->currentValueType, firstVertexIndex, |
| 389 | static_cast<GLsizei>(totalCount), instances, &streamOffset, sourceData); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 390 | if (error.isError()) |
| 391 | { |
| 392 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 393 | } |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 394 | translated->vertexBuffer.set(mStreamingBuffer->getVertexBuffer()); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 395 | } |
| 396 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 397 | ASSERT(translated->vertexBuffer.get()); |
| 398 | translated->serial = translated->vertexBuffer.get()->getSerial(); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 399 | translated->offset = streamOffset; |
| 400 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 401 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 404 | gl::Error VertexDataManager::storeCurrentValue(const gl::VertexAttribCurrentValueData ¤tValue, |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 405 | TranslatedAttribute *translated, |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 406 | CurrentValueState *cachedState) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 407 | { |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 408 | auto *buffer = cachedState->buffer; |
| 409 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 410 | if (cachedState->data != currentValue) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 411 | { |
Jamie Madill | 9c38580 | 2015-06-22 13:57:18 -0400 | [diff] [blame] | 412 | const gl::VertexAttribute &attrib = *translated->attribute; |
Jamie Madill | 27c0891 | 2015-06-22 13:57:20 -0400 | [diff] [blame] | 413 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 414 | gl::Error error = buffer->reserveVertexSpace(attrib, 1, 0); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 415 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 416 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 417 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Jamie Madill | 7d112bb | 2015-06-22 13:57:19 -0400 | [diff] [blame] | 420 | const uint8_t *sourceData = reinterpret_cast<const uint8_t*>(currentValue.FloatValues); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 421 | unsigned int streamOffset; |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 422 | error = buffer->storeDynamicAttribute(attrib, currentValue.Type, 0, 1, 0, &streamOffset, |
| 423 | sourceData); |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 424 | if (error.isError()) |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 425 | { |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 426 | return error; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 427 | } |
| 428 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 429 | buffer->getVertexBuffer()->hintUnmapResource(); |
| 430 | |
Jamie Madill | b3f4e8d | 2015-06-22 13:57:17 -0400 | [diff] [blame] | 431 | cachedState->data = currentValue; |
| 432 | cachedState->offset = streamOffset; |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 433 | } |
| 434 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 435 | translated->vertexBuffer.set(buffer->getVertexBuffer()); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 436 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 437 | translated->storage = nullptr; |
| 438 | translated->serial = buffer->getSerial(); |
| 439 | translated->divisor = 0; |
| 440 | translated->stride = 0; |
| 441 | translated->offset = static_cast<unsigned int>(cachedState->offset); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 442 | |
Geoff Lang | f7100b9 | 2014-09-08 16:17:08 -0400 | [diff] [blame] | 443 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | f41522b | 2014-08-18 16:39:49 -0400 | [diff] [blame] | 444 | } |
| 445 | |
Jamie Madill | e36b92d | 2016-03-04 15:46:58 -0500 | [diff] [blame^] | 446 | // VertexBufferBinding implementation |
| 447 | VertexBufferBinding::VertexBufferBinding() : mBoundVertexBuffer(nullptr) |
| 448 | { |
| 449 | } |
| 450 | |
| 451 | VertexBufferBinding::VertexBufferBinding(const VertexBufferBinding &other) |
| 452 | : mBoundVertexBuffer(other.mBoundVertexBuffer) |
| 453 | { |
| 454 | if (mBoundVertexBuffer) |
| 455 | { |
| 456 | mBoundVertexBuffer->addRef(); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | VertexBufferBinding::~VertexBufferBinding() |
| 461 | { |
| 462 | if (mBoundVertexBuffer) |
| 463 | { |
| 464 | mBoundVertexBuffer->release(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | VertexBufferBinding &VertexBufferBinding::operator=(const VertexBufferBinding &other) |
| 469 | { |
| 470 | mBoundVertexBuffer = other.mBoundVertexBuffer; |
| 471 | if (mBoundVertexBuffer) |
| 472 | { |
| 473 | mBoundVertexBuffer->addRef(); |
| 474 | } |
| 475 | return *this; |
| 476 | } |
| 477 | |
| 478 | void VertexBufferBinding::set(VertexBuffer *vertexBuffer) |
| 479 | { |
| 480 | if (mBoundVertexBuffer == vertexBuffer) |
| 481 | return; |
| 482 | |
| 483 | if (mBoundVertexBuffer) |
| 484 | { |
| 485 | mBoundVertexBuffer->release(); |
| 486 | } |
| 487 | if (vertexBuffer) |
| 488 | { |
| 489 | vertexBuffer->addRef(); |
| 490 | } |
| 491 | |
| 492 | mBoundVertexBuffer = vertexBuffer; |
| 493 | } |
| 494 | |
| 495 | VertexBuffer *VertexBufferBinding::get() const |
| 496 | { |
| 497 | return mBoundVertexBuffer; |
| 498 | } |
| 499 | |
Jamie Madill | dbfc6c6 | 2016-02-29 01:08:57 -0500 | [diff] [blame] | 500 | } // namespace rx |