Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Dawn Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "dawn_native/CommandEncoder.h" |
| 16 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 17 | #include "common/BitSetIterator.h" |
| 18 | #include "dawn_native/BindGroup.h" |
| 19 | #include "dawn_native/Buffer.h" |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 20 | #include "dawn_native/CommandBuffer.h" |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 21 | #include "dawn_native/CommandBufferStateTracker.h" |
| 22 | #include "dawn_native/Commands.h" |
| 23 | #include "dawn_native/ComputePassEncoder.h" |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 24 | #include "dawn_native/Device.h" |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 25 | #include "dawn_native/ErrorData.h" |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 26 | #include "dawn_native/RenderPassEncoder.h" |
| 27 | #include "dawn_native/RenderPipeline.h" |
| 28 | |
| 29 | #include <map> |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 30 | |
| 31 | namespace dawn_native { |
| 32 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | MaybeError ValidateCopySizeFitsInTexture(const TextureCopy& textureCopy, |
| 36 | const Extent3D& copySize) { |
| 37 | const TextureBase* texture = textureCopy.texture.Get(); |
| 38 | if (textureCopy.level >= texture->GetNumMipLevels()) { |
| 39 | return DAWN_VALIDATION_ERROR("Copy mip-level out of range"); |
| 40 | } |
| 41 | |
| 42 | if (textureCopy.slice >= texture->GetArrayLayers()) { |
| 43 | return DAWN_VALIDATION_ERROR("Copy array-layer out of range"); |
| 44 | } |
| 45 | |
| 46 | // All texture dimensions are in uint32_t so by doing checks in uint64_t we avoid |
| 47 | // overflows. |
| 48 | uint64_t level = textureCopy.level; |
| 49 | if (uint64_t(textureCopy.origin.x) + uint64_t(copySize.width) > |
| 50 | (static_cast<uint64_t>(texture->GetSize().width) >> level) || |
| 51 | uint64_t(textureCopy.origin.y) + uint64_t(copySize.height) > |
| 52 | (static_cast<uint64_t>(texture->GetSize().height) >> level)) { |
| 53 | return DAWN_VALIDATION_ERROR("Copy would touch outside of the texture"); |
| 54 | } |
| 55 | |
| 56 | // TODO(cwallez@chromium.org): Check the depth bound differently for 2D arrays and 3D |
| 57 | // textures |
| 58 | if (textureCopy.origin.z != 0 || copySize.depth != 1) { |
| 59 | return DAWN_VALIDATION_ERROR("No support for z != 0 and depth != 1 for now"); |
| 60 | } |
| 61 | |
| 62 | return {}; |
| 63 | } |
| 64 | |
| 65 | bool FitsInBuffer(const BufferBase* buffer, uint32_t offset, uint32_t size) { |
| 66 | uint32_t bufferSize = buffer->GetSize(); |
| 67 | return offset <= bufferSize && (size <= (bufferSize - offset)); |
| 68 | } |
| 69 | |
| 70 | MaybeError ValidateCopySizeFitsInBuffer(const BufferCopy& bufferCopy, uint32_t dataSize) { |
| 71 | if (!FitsInBuffer(bufferCopy.buffer.Get(), bufferCopy.offset, dataSize)) { |
| 72 | return DAWN_VALIDATION_ERROR("Copy would overflow the buffer"); |
| 73 | } |
| 74 | |
| 75 | return {}; |
| 76 | } |
| 77 | |
Yan, Shaobo | 738567f | 2019-02-27 02:46:27 +0000 | [diff] [blame] | 78 | MaybeError ValidateB2BCopySizeAlignment(uint32_t dataSize, |
| 79 | uint32_t srcOffset, |
| 80 | uint32_t dstOffset) { |
| 81 | // Copy size must be a multiple of 4 bytes on macOS. |
| 82 | if (dataSize % 4 != 0) { |
| 83 | return DAWN_VALIDATION_ERROR("Copy size must be a multiple of 4 bytes"); |
| 84 | } |
| 85 | |
| 86 | // SourceOffset and destinationOffset must be multiples of 4 bytes on macOS. |
| 87 | if (srcOffset % 4 != 0 || dstOffset % 4 != 0) { |
| 88 | return DAWN_VALIDATION_ERROR( |
| 89 | "Source offset and destination offset must be multiples of 4 bytes"); |
| 90 | } |
| 91 | |
| 92 | return {}; |
| 93 | } |
| 94 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 95 | MaybeError ValidateTexelBufferOffset(TextureBase* texture, const BufferCopy& bufferCopy) { |
| 96 | uint32_t texelSize = |
| 97 | static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat())); |
| 98 | if (bufferCopy.offset % texelSize != 0) { |
| 99 | return DAWN_VALIDATION_ERROR("Buffer offset must be a multiple of the texel size"); |
| 100 | } |
| 101 | |
| 102 | return {}; |
| 103 | } |
| 104 | |
| 105 | MaybeError ValidateImageHeight(uint32_t imageHeight, uint32_t copyHeight) { |
| 106 | if (imageHeight < copyHeight) { |
| 107 | return DAWN_VALIDATION_ERROR("Image height must not be less than the copy height."); |
| 108 | } |
| 109 | |
| 110 | return {}; |
| 111 | } |
| 112 | |
Brandon Jones | 11d32c8 | 2019-02-20 20:21:00 +0000 | [diff] [blame] | 113 | inline MaybeError PushDebugMarkerStack(unsigned int* counter) { |
| 114 | *counter += 1; |
| 115 | return {}; |
| 116 | } |
| 117 | |
| 118 | inline MaybeError PopDebugMarkerStack(unsigned int* counter) { |
| 119 | if (*counter == 0) { |
| 120 | return DAWN_VALIDATION_ERROR("Pop must be balanced by a corresponding Push."); |
| 121 | } else { |
| 122 | *counter -= 1; |
| 123 | } |
| 124 | |
| 125 | return {}; |
| 126 | } |
| 127 | |
| 128 | inline MaybeError ValidateDebugGroups(const unsigned int counter) { |
| 129 | if (counter != 0) { |
| 130 | return DAWN_VALIDATION_ERROR("Each Push must be balanced by a corresponding Pop."); |
| 131 | } |
| 132 | |
| 133 | return {}; |
| 134 | } |
| 135 | |
Jiawei Shao | 081d5c2 | 2019-03-04 12:01:59 +0000 | [diff] [blame] | 136 | MaybeError ValidateTextureSampleCountInCopyCommands(const TextureBase* texture) { |
| 137 | if (texture->GetSampleCount() > 1) { |
| 138 | return DAWN_VALIDATION_ERROR("The sample count of textures must be 1"); |
| 139 | } |
| 140 | |
| 141 | return {}; |
| 142 | } |
| 143 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 144 | MaybeError ComputeTextureCopyBufferSize(const Extent3D& copySize, |
| 145 | uint32_t rowPitch, |
| 146 | uint32_t imageHeight, |
| 147 | uint32_t* bufferSize) { |
| 148 | DAWN_TRY(ValidateImageHeight(imageHeight, copySize.height)); |
| 149 | |
| 150 | // TODO(cwallez@chromium.org): check for overflows |
| 151 | uint32_t slicePitch = rowPitch * imageHeight; |
| 152 | uint32_t sliceSize = rowPitch * (copySize.height - 1) + copySize.width; |
| 153 | *bufferSize = (slicePitch * (copySize.depth - 1)) + sliceSize; |
| 154 | |
| 155 | return {}; |
| 156 | } |
| 157 | |
| 158 | uint32_t ComputeDefaultRowPitch(TextureBase* texture, uint32_t width) { |
| 159 | uint32_t texelSize = TextureFormatPixelSize(texture->GetFormat()); |
| 160 | return texelSize * width; |
| 161 | } |
| 162 | |
| 163 | MaybeError ValidateRowPitch(dawn::TextureFormat format, |
| 164 | const Extent3D& copySize, |
| 165 | uint32_t rowPitch) { |
| 166 | if (rowPitch % kTextureRowPitchAlignment != 0) { |
| 167 | return DAWN_VALIDATION_ERROR("Row pitch must be a multiple of 256"); |
| 168 | } |
| 169 | |
| 170 | uint32_t texelSize = TextureFormatPixelSize(format); |
| 171 | if (rowPitch < copySize.width * texelSize) { |
| 172 | return DAWN_VALIDATION_ERROR( |
| 173 | "Row pitch must not be less than the number of bytes per row"); |
| 174 | } |
| 175 | |
| 176 | return {}; |
| 177 | } |
| 178 | |
| 179 | MaybeError ValidateCanUseAs(BufferBase* buffer, dawn::BufferUsageBit usage) { |
| 180 | ASSERT(HasZeroOrOneBits(usage)); |
| 181 | if (!(buffer->GetUsage() & usage)) { |
| 182 | return DAWN_VALIDATION_ERROR("buffer doesn't have the required usage."); |
| 183 | } |
| 184 | |
| 185 | return {}; |
| 186 | } |
| 187 | |
| 188 | MaybeError ValidateCanUseAs(TextureBase* texture, dawn::TextureUsageBit usage) { |
| 189 | ASSERT(HasZeroOrOneBits(usage)); |
| 190 | if (!(texture->GetUsage() & usage)) { |
| 191 | return DAWN_VALIDATION_ERROR("texture doesn't have the required usage."); |
| 192 | } |
| 193 | |
| 194 | return {}; |
| 195 | } |
| 196 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 197 | MaybeError ValidateAttachmentArrayLayersAndLevelCount(const TextureViewBase* attachment) { |
| 198 | // Currently we do not support layered rendering. |
| 199 | if (attachment->GetLayerCount() > 1) { |
| 200 | return DAWN_VALIDATION_ERROR( |
| 201 | "The layer count of the texture view used as attachment cannot be greater than " |
| 202 | "1"); |
| 203 | } |
| 204 | |
| 205 | if (attachment->GetLevelCount() > 1) { |
| 206 | return DAWN_VALIDATION_ERROR( |
| 207 | "The mipmap level count of the texture view used as attachment cannot be " |
| 208 | "greater than 1"); |
| 209 | } |
| 210 | |
| 211 | return {}; |
| 212 | } |
| 213 | |
| 214 | MaybeError ValidateOrSetAttachmentSize(const TextureViewBase* attachment, |
| 215 | uint32_t* width, |
| 216 | uint32_t* height) { |
| 217 | const Extent3D& textureSize = attachment->GetTexture()->GetSize(); |
| 218 | const uint32_t attachmentWidth = textureSize.width >> attachment->GetBaseMipLevel(); |
| 219 | const uint32_t attachmentHeight = textureSize.height >> attachment->GetBaseMipLevel(); |
| 220 | |
| 221 | if (*width == 0) { |
| 222 | DAWN_ASSERT(*height == 0); |
| 223 | *width = attachmentWidth; |
| 224 | *height = attachmentHeight; |
| 225 | DAWN_ASSERT(*width != 0 && *height != 0); |
| 226 | } else if (*width != attachmentWidth || *height != attachmentHeight) { |
| 227 | return DAWN_VALIDATION_ERROR("Attachment size mismatch"); |
| 228 | } |
| 229 | |
| 230 | return {}; |
| 231 | } |
| 232 | |
Jiawei Shao | 1e1c13e | 2019-03-11 18:41:02 +0000 | [diff] [blame] | 233 | MaybeError ValidateOrSetColorAttachmentSampleCount(const TextureViewBase* colorAttachment, |
| 234 | uint32_t* sampleCount) { |
| 235 | if (*sampleCount == 0) { |
| 236 | *sampleCount = colorAttachment->GetTexture()->GetSampleCount(); |
| 237 | DAWN_ASSERT(*sampleCount != 0); |
| 238 | } else if (*sampleCount != colorAttachment->GetTexture()->GetSampleCount()) { |
| 239 | return DAWN_VALIDATION_ERROR("Color attachment sample counts mismatch"); |
| 240 | } |
| 241 | |
| 242 | return {}; |
| 243 | } |
| 244 | |
| 245 | MaybeError ValidateResolveTarget( |
| 246 | const DeviceBase* device, |
| 247 | const RenderPassColorAttachmentDescriptor* colorAttachment) { |
| 248 | if (colorAttachment->resolveTarget == nullptr) { |
| 249 | return {}; |
| 250 | } |
| 251 | |
| 252 | DAWN_TRY(device->ValidateObject(colorAttachment->resolveTarget)); |
| 253 | |
| 254 | if (!colorAttachment->attachment->GetTexture()->IsMultisampledTexture()) { |
| 255 | return DAWN_VALIDATION_ERROR( |
| 256 | "Cannot set resolve target when the sample count of the color attachment is 1"); |
| 257 | } |
| 258 | |
| 259 | if (colorAttachment->resolveTarget->GetTexture()->IsMultisampledTexture()) { |
| 260 | return DAWN_VALIDATION_ERROR("Cannot use multisampled texture as resolve target"); |
| 261 | } |
| 262 | |
| 263 | if (colorAttachment->resolveTarget->GetLayerCount() > 1) { |
| 264 | return DAWN_VALIDATION_ERROR( |
| 265 | "The array layer count of the resolve target must be 1"); |
| 266 | } |
| 267 | |
| 268 | if (colorAttachment->resolveTarget->GetLevelCount() > 1) { |
| 269 | return DAWN_VALIDATION_ERROR("The mip level count of the resolve target must be 1"); |
| 270 | } |
| 271 | |
| 272 | uint32_t colorAttachmentBaseMipLevel = colorAttachment->attachment->GetBaseMipLevel(); |
| 273 | const Extent3D& colorTextureSize = colorAttachment->attachment->GetTexture()->GetSize(); |
| 274 | uint32_t colorAttachmentWidth = colorTextureSize.width >> colorAttachmentBaseMipLevel; |
| 275 | uint32_t colorAttachmentHeight = colorTextureSize.height >> colorAttachmentBaseMipLevel; |
| 276 | |
| 277 | uint32_t resolveTargetBaseMipLevel = colorAttachment->resolveTarget->GetBaseMipLevel(); |
| 278 | const Extent3D& resolveTextureSize = |
| 279 | colorAttachment->resolveTarget->GetTexture()->GetSize(); |
| 280 | uint32_t resolveTargetWidth = resolveTextureSize.width >> resolveTargetBaseMipLevel; |
| 281 | uint32_t resolveTargetHeight = resolveTextureSize.height >> resolveTargetBaseMipLevel; |
| 282 | if (colorAttachmentWidth != resolveTargetWidth || |
| 283 | colorAttachmentHeight != resolveTargetHeight) { |
| 284 | return DAWN_VALIDATION_ERROR( |
| 285 | "The size of the resolve target must be the same as the color attachment"); |
| 286 | } |
| 287 | |
| 288 | dawn::TextureFormat resolveTargetFormat = colorAttachment->resolveTarget->GetFormat(); |
| 289 | if (resolveTargetFormat != colorAttachment->attachment->GetFormat()) { |
| 290 | return DAWN_VALIDATION_ERROR( |
| 291 | "The format of the resolve target must be the same as the color attachment"); |
| 292 | } |
| 293 | |
| 294 | return {}; |
| 295 | } |
| 296 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 297 | MaybeError ValidateRenderPassColorAttachment( |
| 298 | const DeviceBase* device, |
| 299 | const RenderPassColorAttachmentDescriptor* colorAttachment, |
| 300 | uint32_t* width, |
Jiawei Shao | 1e1c13e | 2019-03-11 18:41:02 +0000 | [diff] [blame] | 301 | uint32_t* height, |
| 302 | uint32_t* sampleCount) { |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 303 | DAWN_ASSERT(colorAttachment != nullptr); |
| 304 | |
| 305 | DAWN_TRY(device->ValidateObject(colorAttachment->attachment)); |
| 306 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 307 | const TextureViewBase* attachment = colorAttachment->attachment; |
| 308 | if (!IsColorRenderableTextureFormat(attachment->GetFormat())) { |
| 309 | return DAWN_VALIDATION_ERROR( |
| 310 | "The format of the texture view used as color attachment is not color " |
| 311 | "renderable"); |
| 312 | } |
| 313 | |
Jiawei Shao | 1e1c13e | 2019-03-11 18:41:02 +0000 | [diff] [blame] | 314 | DAWN_TRY(ValidateOrSetColorAttachmentSampleCount(attachment, sampleCount)); |
| 315 | |
| 316 | DAWN_TRY(ValidateResolveTarget(device, colorAttachment)); |
| 317 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 318 | DAWN_TRY(ValidateAttachmentArrayLayersAndLevelCount(attachment)); |
| 319 | DAWN_TRY(ValidateOrSetAttachmentSize(attachment, width, height)); |
| 320 | |
| 321 | return {}; |
| 322 | } |
| 323 | |
| 324 | MaybeError ValidateRenderPassDepthStencilAttachment( |
| 325 | const DeviceBase* device, |
| 326 | const RenderPassDepthStencilAttachmentDescriptor* depthStencilAttachment, |
| 327 | uint32_t* width, |
Jiawei Shao | 9313117 | 2019-03-15 05:16:41 +0000 | [diff] [blame^] | 328 | uint32_t* height, |
| 329 | uint32_t* sampleCount) { |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 330 | DAWN_ASSERT(depthStencilAttachment != nullptr); |
| 331 | |
| 332 | DAWN_TRY(device->ValidateObject(depthStencilAttachment->attachment)); |
| 333 | |
| 334 | const TextureViewBase* attachment = depthStencilAttachment->attachment; |
| 335 | if (!TextureFormatHasDepthOrStencil(attachment->GetFormat())) { |
| 336 | return DAWN_VALIDATION_ERROR( |
| 337 | "The format of the texture view used as depth stencil attachment is not a " |
| 338 | "depth stencil format"); |
| 339 | } |
| 340 | |
Jiawei Shao | 9313117 | 2019-03-15 05:16:41 +0000 | [diff] [blame^] | 341 | // *sampleCount == 0 must only happen when there is no color attachment. In that case we |
| 342 | // do not need to validate the sample count of the depth stencil attachment. |
| 343 | if (*sampleCount != 0 && (attachment->GetTexture()->GetSampleCount() != *sampleCount)) { |
| 344 | return DAWN_VALIDATION_ERROR("Depth stencil attachment sample counts mismatch"); |
| 345 | } |
| 346 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 347 | DAWN_TRY(ValidateAttachmentArrayLayersAndLevelCount(attachment)); |
| 348 | DAWN_TRY(ValidateOrSetAttachmentSize(attachment, width, height)); |
| 349 | |
| 350 | return {}; |
| 351 | } |
| 352 | |
| 353 | MaybeError ValidateRenderPassDescriptorAndSetSize(const DeviceBase* device, |
| 354 | const RenderPassDescriptor* renderPass, |
| 355 | uint32_t* width, |
| 356 | uint32_t* height) { |
| 357 | if (renderPass->colorAttachmentCount > kMaxColorAttachments) { |
| 358 | return DAWN_VALIDATION_ERROR("Setting color attachments out of bounds"); |
| 359 | } |
| 360 | |
Jiawei Shao | 1e1c13e | 2019-03-11 18:41:02 +0000 | [diff] [blame] | 361 | uint32_t sampleCount = 0; |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 362 | for (uint32_t i = 0; i < renderPass->colorAttachmentCount; ++i) { |
| 363 | DAWN_TRY(ValidateRenderPassColorAttachment(device, renderPass->colorAttachments[i], |
Jiawei Shao | 1e1c13e | 2019-03-11 18:41:02 +0000 | [diff] [blame] | 364 | width, height, &sampleCount)); |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | if (renderPass->depthStencilAttachment != nullptr) { |
| 368 | DAWN_TRY(ValidateRenderPassDepthStencilAttachment( |
Jiawei Shao | 9313117 | 2019-03-15 05:16:41 +0000 | [diff] [blame^] | 369 | device, renderPass->depthStencilAttachment, width, height, &sampleCount)); |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | if (renderPass->colorAttachmentCount == 0 && |
| 373 | renderPass->depthStencilAttachment == nullptr) { |
| 374 | return DAWN_VALIDATION_ERROR("Cannot use render pass with no attachments."); |
| 375 | } |
| 376 | |
| 377 | return {}; |
| 378 | } |
| 379 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 380 | enum class PassType { |
| 381 | Render, |
| 382 | Compute, |
| 383 | }; |
| 384 | |
| 385 | // Helper class to encapsulate the logic of tracking per-resource usage during the |
| 386 | // validation of command buffer passes. It is used both to know if there are validation |
| 387 | // errors, and to get a list of resources used per pass for backends that need the |
| 388 | // information. |
| 389 | class PassResourceUsageTracker { |
| 390 | public: |
| 391 | void BufferUsedAs(BufferBase* buffer, dawn::BufferUsageBit usage) { |
| 392 | // std::map's operator[] will create the key and return 0 if the key didn't exist |
| 393 | // before. |
| 394 | dawn::BufferUsageBit& storedUsage = mBufferUsages[buffer]; |
| 395 | |
| 396 | if (usage == dawn::BufferUsageBit::Storage && |
| 397 | storedUsage & dawn::BufferUsageBit::Storage) { |
| 398 | mStorageUsedMultipleTimes = true; |
| 399 | } |
| 400 | |
| 401 | storedUsage |= usage; |
| 402 | } |
| 403 | |
| 404 | void TextureUsedAs(TextureBase* texture, dawn::TextureUsageBit usage) { |
| 405 | // std::map's operator[] will create the key and return 0 if the key didn't exist |
| 406 | // before. |
| 407 | dawn::TextureUsageBit& storedUsage = mTextureUsages[texture]; |
| 408 | |
| 409 | if (usage == dawn::TextureUsageBit::Storage && |
| 410 | storedUsage & dawn::TextureUsageBit::Storage) { |
| 411 | mStorageUsedMultipleTimes = true; |
| 412 | } |
| 413 | |
| 414 | storedUsage |= usage; |
| 415 | } |
| 416 | |
| 417 | // Performs the per-pass usage validation checks |
| 418 | MaybeError ValidateUsages(PassType pass) const { |
| 419 | // Storage resources cannot be used twice in the same compute pass |
| 420 | if (pass == PassType::Compute && mStorageUsedMultipleTimes) { |
| 421 | return DAWN_VALIDATION_ERROR( |
| 422 | "Storage resource used multiple times in compute pass"); |
| 423 | } |
| 424 | |
| 425 | // Buffers can only be used as single-write or multiple read. |
| 426 | for (auto& it : mBufferUsages) { |
| 427 | BufferBase* buffer = it.first; |
| 428 | dawn::BufferUsageBit usage = it.second; |
| 429 | |
| 430 | if (usage & ~buffer->GetUsage()) { |
| 431 | return DAWN_VALIDATION_ERROR("Buffer missing usage for the pass"); |
| 432 | } |
| 433 | |
| 434 | bool readOnly = (usage & kReadOnlyBufferUsages) == usage; |
| 435 | bool singleUse = dawn::HasZeroOrOneBits(usage); |
| 436 | |
| 437 | if (!readOnly && !singleUse) { |
| 438 | return DAWN_VALIDATION_ERROR( |
| 439 | "Buffer used as writeable usage and another usage in pass"); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // Textures can only be used as single-write or multiple read. |
| 444 | // TODO(cwallez@chromium.org): implement per-subresource tracking |
| 445 | for (auto& it : mTextureUsages) { |
| 446 | TextureBase* texture = it.first; |
| 447 | dawn::TextureUsageBit usage = it.second; |
| 448 | |
| 449 | if (usage & ~texture->GetUsage()) { |
| 450 | return DAWN_VALIDATION_ERROR("Texture missing usage for the pass"); |
| 451 | } |
| 452 | |
| 453 | // For textures the only read-only usage in a pass is Sampled, so checking the |
| 454 | // usage constraint simplifies to checking a single usage bit is set. |
| 455 | if (!dawn::HasZeroOrOneBits(it.second)) { |
| 456 | return DAWN_VALIDATION_ERROR( |
| 457 | "Texture used with more than one usage in pass"); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return {}; |
| 462 | } |
| 463 | |
| 464 | // Returns the per-pass usage for use by backends for APIs with explicit barriers. |
| 465 | PassResourceUsage AcquireResourceUsage() { |
| 466 | PassResourceUsage result; |
| 467 | result.buffers.reserve(mBufferUsages.size()); |
| 468 | result.bufferUsages.reserve(mBufferUsages.size()); |
| 469 | result.textures.reserve(mTextureUsages.size()); |
| 470 | result.textureUsages.reserve(mTextureUsages.size()); |
| 471 | |
| 472 | for (auto& it : mBufferUsages) { |
| 473 | result.buffers.push_back(it.first); |
| 474 | result.bufferUsages.push_back(it.second); |
| 475 | } |
| 476 | |
| 477 | for (auto& it : mTextureUsages) { |
| 478 | result.textures.push_back(it.first); |
| 479 | result.textureUsages.push_back(it.second); |
| 480 | } |
| 481 | |
| 482 | return result; |
| 483 | } |
| 484 | |
| 485 | private: |
| 486 | std::map<BufferBase*, dawn::BufferUsageBit> mBufferUsages; |
| 487 | std::map<TextureBase*, dawn::TextureUsageBit> mTextureUsages; |
| 488 | bool mStorageUsedMultipleTimes = false; |
| 489 | }; |
| 490 | |
| 491 | void TrackBindGroupResourceUsage(BindGroupBase* group, PassResourceUsageTracker* tracker) { |
| 492 | const auto& layoutInfo = group->GetLayout()->GetBindingInfo(); |
| 493 | |
| 494 | for (uint32_t i : IterateBitSet(layoutInfo.mask)) { |
| 495 | dawn::BindingType type = layoutInfo.types[i]; |
| 496 | |
| 497 | switch (type) { |
| 498 | case dawn::BindingType::UniformBuffer: { |
| 499 | BufferBase* buffer = group->GetBindingAsBufferBinding(i).buffer; |
| 500 | tracker->BufferUsedAs(buffer, dawn::BufferUsageBit::Uniform); |
| 501 | } break; |
| 502 | |
| 503 | case dawn::BindingType::StorageBuffer: { |
| 504 | BufferBase* buffer = group->GetBindingAsBufferBinding(i).buffer; |
| 505 | tracker->BufferUsedAs(buffer, dawn::BufferUsageBit::Storage); |
| 506 | } break; |
| 507 | |
| 508 | case dawn::BindingType::SampledTexture: { |
| 509 | TextureBase* texture = group->GetBindingAsTextureView(i)->GetTexture(); |
| 510 | tracker->TextureUsedAs(texture, dawn::TextureUsageBit::Sampled); |
| 511 | } break; |
| 512 | |
| 513 | case dawn::BindingType::Sampler: |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | } // namespace |
| 520 | |
| 521 | enum class CommandEncoderBase::EncodingState : uint8_t { |
| 522 | TopLevel, |
| 523 | ComputePass, |
| 524 | RenderPass, |
| 525 | Finished |
| 526 | }; |
| 527 | |
| 528 | CommandEncoderBase::CommandEncoderBase(DeviceBase* device) |
| 529 | : ObjectBase(device), mEncodingState(EncodingState::TopLevel) { |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | CommandEncoderBase::~CommandEncoderBase() { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 533 | if (!mWereCommandsAcquired) { |
| 534 | MoveToIterator(); |
| 535 | FreeCommands(&mIterator); |
| 536 | } |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 539 | CommandIterator CommandEncoderBase::AcquireCommands() { |
| 540 | ASSERT(!mWereCommandsAcquired); |
| 541 | mWereCommandsAcquired = true; |
| 542 | return std::move(mIterator); |
| 543 | } |
| 544 | |
| 545 | CommandBufferResourceUsage CommandEncoderBase::AcquireResourceUsages() { |
| 546 | ASSERT(!mWereResourceUsagesAcquired); |
| 547 | mWereResourceUsagesAcquired = true; |
| 548 | return std::move(mResourceUsages); |
| 549 | } |
| 550 | |
| 551 | void CommandEncoderBase::MoveToIterator() { |
| 552 | if (!mWasMovedToIterator) { |
| 553 | mIterator = std::move(mAllocator); |
| 554 | mWasMovedToIterator = true; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | // Implementation of the API's command recording methods |
| 559 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 560 | ComputePassEncoderBase* CommandEncoderBase::BeginComputePass() { |
Jiawei Shao | b47470d | 2019-03-05 01:02:47 +0000 | [diff] [blame] | 561 | DeviceBase* device = GetDevice(); |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 562 | if (ConsumedError(ValidateCanRecordTopLevelCommands())) { |
Jiawei Shao | b47470d | 2019-03-05 01:02:47 +0000 | [diff] [blame] | 563 | return ComputePassEncoderBase::MakeError(device, this); |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | mAllocator.Allocate<BeginComputePassCmd>(Command::BeginComputePass); |
| 567 | |
| 568 | mEncodingState = EncodingState::ComputePass; |
Jiawei Shao | b47470d | 2019-03-05 01:02:47 +0000 | [diff] [blame] | 569 | return new ComputePassEncoderBase(device, this, &mAllocator); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 572 | RenderPassEncoderBase* CommandEncoderBase::BeginRenderPass(const RenderPassDescriptor* info) { |
| 573 | DeviceBase* device = GetDevice(); |
| 574 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 575 | if (ConsumedError(ValidateCanRecordTopLevelCommands())) { |
Jiawei Shao | b47470d | 2019-03-05 01:02:47 +0000 | [diff] [blame] | 576 | return RenderPassEncoderBase::MakeError(device, this); |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 579 | uint32_t width = 0; |
| 580 | uint32_t height = 0; |
| 581 | if (ConsumedError(ValidateRenderPassDescriptorAndSetSize(device, info, &width, &height))) { |
Jiawei Shao | b47470d | 2019-03-05 01:02:47 +0000 | [diff] [blame] | 582 | return RenderPassEncoderBase::MakeError(device, this); |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 585 | mEncodingState = EncodingState::RenderPass; |
| 586 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 587 | BeginRenderPassCmd* cmd = mAllocator.Allocate<BeginRenderPassCmd>(Command::BeginRenderPass); |
| 588 | new (cmd) BeginRenderPassCmd; |
| 589 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 590 | for (uint32_t i = 0; i < info->colorAttachmentCount; ++i) { |
| 591 | if (info->colorAttachments[i] != nullptr) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 592 | cmd->colorAttachmentsSet.set(i); |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 593 | cmd->colorAttachments[i].view = info->colorAttachments[i]->attachment; |
| 594 | cmd->colorAttachments[i].resolveTarget = info->colorAttachments[i]->resolveTarget; |
| 595 | cmd->colorAttachments[i].loadOp = info->colorAttachments[i]->loadOp; |
| 596 | cmd->colorAttachments[i].storeOp = info->colorAttachments[i]->storeOp; |
| 597 | cmd->colorAttachments[i].clearColor = info->colorAttachments[i]->clearColor; |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 601 | cmd->hasDepthStencilAttachment = info->depthStencilAttachment != nullptr; |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 602 | if (cmd->hasDepthStencilAttachment) { |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 603 | cmd->hasDepthStencilAttachment = true; |
| 604 | cmd->depthStencilAttachment.view = info->depthStencilAttachment->attachment; |
| 605 | cmd->depthStencilAttachment.clearDepth = info->depthStencilAttachment->clearDepth; |
| 606 | cmd->depthStencilAttachment.clearStencil = info->depthStencilAttachment->clearStencil; |
| 607 | cmd->depthStencilAttachment.depthLoadOp = info->depthStencilAttachment->depthLoadOp; |
| 608 | cmd->depthStencilAttachment.depthStoreOp = info->depthStencilAttachment->depthStoreOp; |
| 609 | cmd->depthStencilAttachment.stencilLoadOp = info->depthStencilAttachment->stencilLoadOp; |
| 610 | cmd->depthStencilAttachment.stencilStoreOp = |
| 611 | info->depthStencilAttachment->stencilStoreOp; |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Jiawei Shao | b2c5023 | 2019-02-27 09:21:56 +0000 | [diff] [blame] | 614 | cmd->width = width; |
| 615 | cmd->height = height; |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 616 | |
Jiawei Shao | b47470d | 2019-03-05 01:02:47 +0000 | [diff] [blame] | 617 | return new RenderPassEncoderBase(device, this, &mAllocator); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | void CommandEncoderBase::CopyBufferToBuffer(BufferBase* source, |
| 621 | uint32_t sourceOffset, |
| 622 | BufferBase* destination, |
| 623 | uint32_t destinationOffset, |
| 624 | uint32_t size) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 625 | if (ConsumedError(ValidateCanRecordTopLevelCommands())) { |
| 626 | return; |
| 627 | } |
| 628 | |
Jiawei Shao | d6eb2e7 | 2019-03-05 00:03:28 +0000 | [diff] [blame] | 629 | if (ConsumedError(GetDevice()->ValidateObject(source))) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 630 | return; |
| 631 | } |
| 632 | |
Jiawei Shao | d6eb2e7 | 2019-03-05 00:03:28 +0000 | [diff] [blame] | 633 | if (ConsumedError(GetDevice()->ValidateObject(destination))) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 634 | return; |
| 635 | } |
| 636 | |
| 637 | CopyBufferToBufferCmd* copy = |
| 638 | mAllocator.Allocate<CopyBufferToBufferCmd>(Command::CopyBufferToBuffer); |
| 639 | new (copy) CopyBufferToBufferCmd; |
| 640 | copy->source.buffer = source; |
| 641 | copy->source.offset = sourceOffset; |
| 642 | copy->destination.buffer = destination; |
| 643 | copy->destination.offset = destinationOffset; |
| 644 | copy->size = size; |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void CommandEncoderBase::CopyBufferToTexture(const BufferCopyView* source, |
| 648 | const TextureCopyView* destination, |
| 649 | const Extent3D* copySize) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 650 | if (ConsumedError(ValidateCanRecordTopLevelCommands())) { |
| 651 | return; |
| 652 | } |
| 653 | |
Jiawei Shao | d6eb2e7 | 2019-03-05 00:03:28 +0000 | [diff] [blame] | 654 | if (ConsumedError(GetDevice()->ValidateObject(source->buffer))) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 655 | return; |
| 656 | } |
| 657 | |
Jiawei Shao | d6eb2e7 | 2019-03-05 00:03:28 +0000 | [diff] [blame] | 658 | if (ConsumedError(GetDevice()->ValidateObject(destination->texture))) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | |
| 662 | CopyBufferToTextureCmd* copy = |
| 663 | mAllocator.Allocate<CopyBufferToTextureCmd>(Command::CopyBufferToTexture); |
| 664 | new (copy) CopyBufferToTextureCmd; |
| 665 | copy->source.buffer = source->buffer; |
| 666 | copy->source.offset = source->offset; |
| 667 | copy->destination.texture = destination->texture; |
| 668 | copy->destination.origin = destination->origin; |
| 669 | copy->copySize = *copySize; |
| 670 | copy->destination.level = destination->level; |
| 671 | copy->destination.slice = destination->slice; |
| 672 | if (source->rowPitch == 0) { |
| 673 | copy->source.rowPitch = ComputeDefaultRowPitch(destination->texture, copySize->width); |
| 674 | } else { |
| 675 | copy->source.rowPitch = source->rowPitch; |
| 676 | } |
| 677 | if (source->imageHeight == 0) { |
| 678 | copy->source.imageHeight = copySize->height; |
| 679 | } else { |
| 680 | copy->source.imageHeight = source->imageHeight; |
| 681 | } |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void CommandEncoderBase::CopyTextureToBuffer(const TextureCopyView* source, |
| 685 | const BufferCopyView* destination, |
| 686 | const Extent3D* copySize) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 687 | if (ConsumedError(ValidateCanRecordTopLevelCommands())) { |
| 688 | return; |
| 689 | } |
| 690 | |
Jiawei Shao | d6eb2e7 | 2019-03-05 00:03:28 +0000 | [diff] [blame] | 691 | if (ConsumedError(GetDevice()->ValidateObject(source->texture))) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 692 | return; |
| 693 | } |
| 694 | |
Jiawei Shao | d6eb2e7 | 2019-03-05 00:03:28 +0000 | [diff] [blame] | 695 | if (ConsumedError(GetDevice()->ValidateObject(destination->buffer))) { |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 696 | return; |
| 697 | } |
| 698 | |
| 699 | CopyTextureToBufferCmd* copy = |
| 700 | mAllocator.Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer); |
| 701 | new (copy) CopyTextureToBufferCmd; |
| 702 | copy->source.texture = source->texture; |
| 703 | copy->source.origin = source->origin; |
| 704 | copy->copySize = *copySize; |
| 705 | copy->source.level = source->level; |
| 706 | copy->source.slice = source->slice; |
| 707 | copy->destination.buffer = destination->buffer; |
| 708 | copy->destination.offset = destination->offset; |
| 709 | if (destination->rowPitch == 0) { |
| 710 | copy->destination.rowPitch = ComputeDefaultRowPitch(source->texture, copySize->width); |
| 711 | } else { |
| 712 | copy->destination.rowPitch = destination->rowPitch; |
| 713 | } |
| 714 | if (destination->imageHeight == 0) { |
| 715 | copy->destination.imageHeight = copySize->height; |
| 716 | } else { |
| 717 | copy->destination.imageHeight = destination->imageHeight; |
| 718 | } |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | CommandBufferBase* CommandEncoderBase::Finish() { |
| 722 | if (GetDevice()->ConsumedError(ValidateFinish())) { |
| 723 | return CommandBufferBase::MakeError(GetDevice()); |
| 724 | } |
| 725 | ASSERT(!IsError()); |
| 726 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 727 | mEncodingState = EncodingState::Finished; |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 728 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 729 | MoveToIterator(); |
| 730 | return GetDevice()->CreateCommandBuffer(this); |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 733 | // Implementation of functions to interact with sub-encoders |
| 734 | |
| 735 | void CommandEncoderBase::HandleError(const char* message) { |
| 736 | if (!mGotError) { |
| 737 | mGotError = true; |
| 738 | mErrorMessage = message; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void CommandEncoderBase::ConsumeError(ErrorData* error) { |
| 743 | HandleError(error->GetMessage().c_str()); |
| 744 | delete error; |
| 745 | } |
| 746 | |
| 747 | void CommandEncoderBase::PassEnded() { |
| 748 | if (mEncodingState == EncodingState::ComputePass) { |
| 749 | mAllocator.Allocate<EndComputePassCmd>(Command::EndComputePass); |
| 750 | } else { |
| 751 | ASSERT(mEncodingState == EncodingState::RenderPass); |
| 752 | mAllocator.Allocate<EndRenderPassCmd>(Command::EndRenderPass); |
| 753 | } |
| 754 | mEncodingState = EncodingState::TopLevel; |
| 755 | } |
| 756 | |
| 757 | // Implementation of the command buffer validation that can be precomputed before submit |
| 758 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 759 | MaybeError CommandEncoderBase::ValidateFinish() { |
| 760 | DAWN_TRY(GetDevice()->ValidateObject(this)); |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 761 | |
| 762 | if (mGotError) { |
| 763 | return DAWN_VALIDATION_ERROR(mErrorMessage); |
| 764 | } |
| 765 | |
| 766 | if (mEncodingState != EncodingState::TopLevel) { |
| 767 | return DAWN_VALIDATION_ERROR("Command buffer recording ended mid-pass"); |
| 768 | } |
| 769 | |
| 770 | MoveToIterator(); |
| 771 | mIterator.Reset(); |
| 772 | |
| 773 | Command type; |
| 774 | while (mIterator.NextCommandId(&type)) { |
| 775 | switch (type) { |
| 776 | case Command::BeginComputePass: { |
| 777 | mIterator.NextCommand<BeginComputePassCmd>(); |
| 778 | DAWN_TRY(ValidateComputePass()); |
| 779 | } break; |
| 780 | |
| 781 | case Command::BeginRenderPass: { |
| 782 | BeginRenderPassCmd* cmd = mIterator.NextCommand<BeginRenderPassCmd>(); |
| 783 | DAWN_TRY(ValidateRenderPass(cmd)); |
| 784 | } break; |
| 785 | |
| 786 | case Command::CopyBufferToBuffer: { |
| 787 | CopyBufferToBufferCmd* copy = mIterator.NextCommand<CopyBufferToBufferCmd>(); |
| 788 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 789 | DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->source, copy->size)); |
| 790 | DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->destination, copy->size)); |
Yan, Shaobo | 738567f | 2019-02-27 02:46:27 +0000 | [diff] [blame] | 791 | DAWN_TRY(ValidateB2BCopySizeAlignment(copy->size, copy->source.offset, |
| 792 | copy->destination.offset)); |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 793 | |
| 794 | DAWN_TRY(ValidateCanUseAs(copy->source.buffer.Get(), |
| 795 | dawn::BufferUsageBit::TransferSrc)); |
| 796 | DAWN_TRY(ValidateCanUseAs(copy->destination.buffer.Get(), |
| 797 | dawn::BufferUsageBit::TransferDst)); |
| 798 | |
| 799 | mResourceUsages.topLevelBuffers.insert(copy->source.buffer.Get()); |
| 800 | mResourceUsages.topLevelBuffers.insert(copy->destination.buffer.Get()); |
| 801 | } break; |
| 802 | |
| 803 | case Command::CopyBufferToTexture: { |
| 804 | CopyBufferToTextureCmd* copy = mIterator.NextCommand<CopyBufferToTextureCmd>(); |
| 805 | |
Jiawei Shao | 081d5c2 | 2019-03-04 12:01:59 +0000 | [diff] [blame] | 806 | DAWN_TRY( |
| 807 | ValidateTextureSampleCountInCopyCommands(copy->destination.texture.Get())); |
| 808 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 809 | uint32_t bufferCopySize = 0; |
| 810 | DAWN_TRY(ValidateRowPitch(copy->destination.texture->GetFormat(), |
| 811 | copy->copySize, copy->source.rowPitch)); |
| 812 | |
| 813 | DAWN_TRY(ComputeTextureCopyBufferSize(copy->copySize, copy->source.rowPitch, |
| 814 | copy->source.imageHeight, |
| 815 | &bufferCopySize)); |
| 816 | |
| 817 | DAWN_TRY(ValidateCopySizeFitsInTexture(copy->destination, copy->copySize)); |
| 818 | DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->source, bufferCopySize)); |
| 819 | DAWN_TRY( |
| 820 | ValidateTexelBufferOffset(copy->destination.texture.Get(), copy->source)); |
| 821 | |
| 822 | DAWN_TRY(ValidateCanUseAs(copy->source.buffer.Get(), |
| 823 | dawn::BufferUsageBit::TransferSrc)); |
| 824 | DAWN_TRY(ValidateCanUseAs(copy->destination.texture.Get(), |
| 825 | dawn::TextureUsageBit::TransferDst)); |
| 826 | |
| 827 | mResourceUsages.topLevelBuffers.insert(copy->source.buffer.Get()); |
| 828 | mResourceUsages.topLevelTextures.insert(copy->destination.texture.Get()); |
| 829 | } break; |
| 830 | |
| 831 | case Command::CopyTextureToBuffer: { |
| 832 | CopyTextureToBufferCmd* copy = mIterator.NextCommand<CopyTextureToBufferCmd>(); |
| 833 | |
Jiawei Shao | 081d5c2 | 2019-03-04 12:01:59 +0000 | [diff] [blame] | 834 | DAWN_TRY(ValidateTextureSampleCountInCopyCommands(copy->source.texture.Get())); |
| 835 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 836 | uint32_t bufferCopySize = 0; |
| 837 | DAWN_TRY(ValidateRowPitch(copy->source.texture->GetFormat(), copy->copySize, |
| 838 | copy->destination.rowPitch)); |
| 839 | DAWN_TRY(ComputeTextureCopyBufferSize( |
| 840 | copy->copySize, copy->destination.rowPitch, copy->destination.imageHeight, |
| 841 | &bufferCopySize)); |
| 842 | |
| 843 | DAWN_TRY(ValidateCopySizeFitsInTexture(copy->source, copy->copySize)); |
| 844 | DAWN_TRY(ValidateCopySizeFitsInBuffer(copy->destination, bufferCopySize)); |
| 845 | DAWN_TRY( |
| 846 | ValidateTexelBufferOffset(copy->source.texture.Get(), copy->destination)); |
| 847 | |
| 848 | DAWN_TRY(ValidateCanUseAs(copy->source.texture.Get(), |
| 849 | dawn::TextureUsageBit::TransferSrc)); |
| 850 | DAWN_TRY(ValidateCanUseAs(copy->destination.buffer.Get(), |
| 851 | dawn::BufferUsageBit::TransferDst)); |
| 852 | |
| 853 | mResourceUsages.topLevelTextures.insert(copy->source.texture.Get()); |
| 854 | mResourceUsages.topLevelBuffers.insert(copy->destination.buffer.Get()); |
| 855 | } break; |
| 856 | |
| 857 | default: |
| 858 | return DAWN_VALIDATION_ERROR("Command disallowed outside of a pass"); |
| 859 | } |
| 860 | } |
| 861 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 862 | return {}; |
| 863 | } |
| 864 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 865 | MaybeError CommandEncoderBase::ValidateComputePass() { |
| 866 | PassResourceUsageTracker usageTracker; |
| 867 | CommandBufferStateTracker persistentState; |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 868 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 869 | Command type; |
| 870 | while (mIterator.NextCommandId(&type)) { |
| 871 | switch (type) { |
| 872 | case Command::EndComputePass: { |
| 873 | mIterator.NextCommand<EndComputePassCmd>(); |
| 874 | |
Brandon Jones | 11d32c8 | 2019-02-20 20:21:00 +0000 | [diff] [blame] | 875 | DAWN_TRY(ValidateDebugGroups(mDebugGroupStackSize)); |
| 876 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 877 | DAWN_TRY(usageTracker.ValidateUsages(PassType::Compute)); |
| 878 | mResourceUsages.perPass.push_back(usageTracker.AcquireResourceUsage()); |
| 879 | return {}; |
| 880 | } break; |
| 881 | |
| 882 | case Command::Dispatch: { |
| 883 | mIterator.NextCommand<DispatchCmd>(); |
| 884 | DAWN_TRY(persistentState.ValidateCanDispatch()); |
| 885 | } break; |
| 886 | |
Brandon Jones | 11d32c8 | 2019-02-20 20:21:00 +0000 | [diff] [blame] | 887 | case Command::InsertDebugMarker: { |
| 888 | InsertDebugMarkerCmd* cmd = mIterator.NextCommand<InsertDebugMarkerCmd>(); |
| 889 | mIterator.NextData<char>(cmd->length + 1); |
| 890 | } break; |
| 891 | |
| 892 | case Command::PopDebugGroup: { |
| 893 | mIterator.NextCommand<PopDebugGroupCmd>(); |
| 894 | DAWN_TRY(PopDebugMarkerStack(&mDebugGroupStackSize)); |
| 895 | } break; |
| 896 | |
| 897 | case Command::PushDebugGroup: { |
| 898 | PushDebugGroupCmd* cmd = mIterator.NextCommand<PushDebugGroupCmd>(); |
| 899 | mIterator.NextData<char>(cmd->length + 1); |
| 900 | DAWN_TRY(PushDebugMarkerStack(&mDebugGroupStackSize)); |
| 901 | } break; |
| 902 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 903 | case Command::SetComputePipeline: { |
| 904 | SetComputePipelineCmd* cmd = mIterator.NextCommand<SetComputePipelineCmd>(); |
| 905 | ComputePipelineBase* pipeline = cmd->pipeline.Get(); |
| 906 | persistentState.SetComputePipeline(pipeline); |
| 907 | } break; |
| 908 | |
| 909 | case Command::SetPushConstants: { |
| 910 | SetPushConstantsCmd* cmd = mIterator.NextCommand<SetPushConstantsCmd>(); |
| 911 | mIterator.NextData<uint32_t>(cmd->count); |
| 912 | // Validation of count and offset has already been done when the command was |
| 913 | // recorded because it impacts the size of an allocation in the |
| 914 | // CommandAllocator. |
| 915 | if (cmd->stages & ~dawn::ShaderStageBit::Compute) { |
| 916 | return DAWN_VALIDATION_ERROR( |
| 917 | "SetPushConstants stage must be compute or 0 in compute passes"); |
| 918 | } |
| 919 | } break; |
| 920 | |
| 921 | case Command::SetBindGroup: { |
| 922 | SetBindGroupCmd* cmd = mIterator.NextCommand<SetBindGroupCmd>(); |
| 923 | |
| 924 | TrackBindGroupResourceUsage(cmd->group.Get(), &usageTracker); |
| 925 | persistentState.SetBindGroup(cmd->index, cmd->group.Get()); |
| 926 | } break; |
| 927 | |
| 928 | default: |
| 929 | return DAWN_VALIDATION_ERROR("Command disallowed inside a compute pass"); |
| 930 | } |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 931 | } |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 932 | |
| 933 | UNREACHABLE(); |
| 934 | return DAWN_VALIDATION_ERROR("Unfinished compute pass"); |
| 935 | } |
| 936 | |
| 937 | MaybeError CommandEncoderBase::ValidateRenderPass(BeginRenderPassCmd* renderPass) { |
| 938 | PassResourceUsageTracker usageTracker; |
| 939 | CommandBufferStateTracker persistentState; |
| 940 | |
| 941 | // Track usage of the render pass attachments |
| 942 | for (uint32_t i : IterateBitSet(renderPass->colorAttachmentsSet)) { |
| 943 | RenderPassColorAttachmentInfo* colorAttachment = &renderPass->colorAttachments[i]; |
| 944 | TextureBase* texture = colorAttachment->view->GetTexture(); |
| 945 | usageTracker.TextureUsedAs(texture, dawn::TextureUsageBit::OutputAttachment); |
Jiawei Shao | 1e1c13e | 2019-03-11 18:41:02 +0000 | [diff] [blame] | 946 | |
| 947 | TextureViewBase* resolveTarget = colorAttachment->resolveTarget.Get(); |
| 948 | if (resolveTarget != nullptr) { |
| 949 | usageTracker.TextureUsedAs(resolveTarget->GetTexture(), |
| 950 | dawn::TextureUsageBit::OutputAttachment); |
| 951 | } |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | if (renderPass->hasDepthStencilAttachment) { |
| 955 | TextureBase* texture = renderPass->depthStencilAttachment.view->GetTexture(); |
| 956 | usageTracker.TextureUsedAs(texture, dawn::TextureUsageBit::OutputAttachment); |
| 957 | } |
| 958 | |
| 959 | Command type; |
| 960 | while (mIterator.NextCommandId(&type)) { |
| 961 | switch (type) { |
| 962 | case Command::EndRenderPass: { |
| 963 | mIterator.NextCommand<EndRenderPassCmd>(); |
| 964 | |
Brandon Jones | 11d32c8 | 2019-02-20 20:21:00 +0000 | [diff] [blame] | 965 | DAWN_TRY(ValidateDebugGroups(mDebugGroupStackSize)); |
| 966 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 967 | DAWN_TRY(usageTracker.ValidateUsages(PassType::Render)); |
| 968 | mResourceUsages.perPass.push_back(usageTracker.AcquireResourceUsage()); |
| 969 | return {}; |
| 970 | } break; |
| 971 | |
| 972 | case Command::Draw: { |
| 973 | mIterator.NextCommand<DrawCmd>(); |
| 974 | DAWN_TRY(persistentState.ValidateCanDraw()); |
| 975 | } break; |
| 976 | |
| 977 | case Command::DrawIndexed: { |
| 978 | mIterator.NextCommand<DrawIndexedCmd>(); |
| 979 | DAWN_TRY(persistentState.ValidateCanDrawIndexed()); |
| 980 | } break; |
| 981 | |
Brandon Jones | 11d32c8 | 2019-02-20 20:21:00 +0000 | [diff] [blame] | 982 | case Command::InsertDebugMarker: { |
| 983 | InsertDebugMarkerCmd* cmd = mIterator.NextCommand<InsertDebugMarkerCmd>(); |
| 984 | mIterator.NextData<char>(cmd->length + 1); |
| 985 | } break; |
| 986 | |
| 987 | case Command::PopDebugGroup: { |
| 988 | mIterator.NextCommand<PopDebugGroupCmd>(); |
| 989 | DAWN_TRY(PopDebugMarkerStack(&mDebugGroupStackSize)); |
| 990 | } break; |
| 991 | |
| 992 | case Command::PushDebugGroup: { |
| 993 | PushDebugGroupCmd* cmd = mIterator.NextCommand<PushDebugGroupCmd>(); |
| 994 | mIterator.NextData<char>(cmd->length + 1); |
| 995 | DAWN_TRY(PushDebugMarkerStack(&mDebugGroupStackSize)); |
| 996 | } break; |
| 997 | |
Corentin Wallez | f20f5b9 | 2019-02-20 11:46:16 +0000 | [diff] [blame] | 998 | case Command::SetRenderPipeline: { |
| 999 | SetRenderPipelineCmd* cmd = mIterator.NextCommand<SetRenderPipelineCmd>(); |
| 1000 | RenderPipelineBase* pipeline = cmd->pipeline.Get(); |
| 1001 | |
| 1002 | if (!pipeline->IsCompatibleWith(renderPass)) { |
| 1003 | return DAWN_VALIDATION_ERROR( |
| 1004 | "Pipeline is incompatible with this render pass"); |
| 1005 | } |
| 1006 | |
| 1007 | persistentState.SetRenderPipeline(pipeline); |
| 1008 | } break; |
| 1009 | |
| 1010 | case Command::SetPushConstants: { |
| 1011 | SetPushConstantsCmd* cmd = mIterator.NextCommand<SetPushConstantsCmd>(); |
| 1012 | mIterator.NextData<uint32_t>(cmd->count); |
| 1013 | // Validation of count and offset has already been done when the command was |
| 1014 | // recorded because it impacts the size of an allocation in the |
| 1015 | // CommandAllocator. |
| 1016 | if (cmd->stages & |
| 1017 | ~(dawn::ShaderStageBit::Vertex | dawn::ShaderStageBit::Fragment)) { |
| 1018 | return DAWN_VALIDATION_ERROR( |
| 1019 | "SetPushConstants stage must be a subset of (vertex|fragment) in " |
| 1020 | "render passes"); |
| 1021 | } |
| 1022 | } break; |
| 1023 | |
| 1024 | case Command::SetStencilReference: { |
| 1025 | mIterator.NextCommand<SetStencilReferenceCmd>(); |
| 1026 | } break; |
| 1027 | |
| 1028 | case Command::SetBlendColor: { |
| 1029 | mIterator.NextCommand<SetBlendColorCmd>(); |
| 1030 | } break; |
| 1031 | |
| 1032 | case Command::SetScissorRect: { |
| 1033 | mIterator.NextCommand<SetScissorRectCmd>(); |
| 1034 | } break; |
| 1035 | |
| 1036 | case Command::SetBindGroup: { |
| 1037 | SetBindGroupCmd* cmd = mIterator.NextCommand<SetBindGroupCmd>(); |
| 1038 | |
| 1039 | TrackBindGroupResourceUsage(cmd->group.Get(), &usageTracker); |
| 1040 | persistentState.SetBindGroup(cmd->index, cmd->group.Get()); |
| 1041 | } break; |
| 1042 | |
| 1043 | case Command::SetIndexBuffer: { |
| 1044 | SetIndexBufferCmd* cmd = mIterator.NextCommand<SetIndexBufferCmd>(); |
| 1045 | |
| 1046 | usageTracker.BufferUsedAs(cmd->buffer.Get(), dawn::BufferUsageBit::Index); |
| 1047 | persistentState.SetIndexBuffer(); |
| 1048 | } break; |
| 1049 | |
| 1050 | case Command::SetVertexBuffers: { |
| 1051 | SetVertexBuffersCmd* cmd = mIterator.NextCommand<SetVertexBuffersCmd>(); |
| 1052 | auto buffers = mIterator.NextData<Ref<BufferBase>>(cmd->count); |
| 1053 | mIterator.NextData<uint32_t>(cmd->count); |
| 1054 | |
| 1055 | for (uint32_t i = 0; i < cmd->count; ++i) { |
| 1056 | usageTracker.BufferUsedAs(buffers[i].Get(), dawn::BufferUsageBit::Vertex); |
| 1057 | } |
| 1058 | persistentState.SetVertexBuffer(cmd->startSlot, cmd->count); |
| 1059 | } break; |
| 1060 | |
| 1061 | default: |
| 1062 | return DAWN_VALIDATION_ERROR("Command disallowed inside a render pass"); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | UNREACHABLE(); |
| 1067 | return DAWN_VALIDATION_ERROR("Unfinished render pass"); |
| 1068 | } |
| 1069 | |
| 1070 | MaybeError CommandEncoderBase::ValidateCanRecordTopLevelCommands() const { |
| 1071 | if (mEncodingState != EncodingState::TopLevel) { |
| 1072 | return DAWN_VALIDATION_ERROR("Command cannot be recorded inside a pass"); |
| 1073 | } |
| 1074 | return {}; |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | } // namespace dawn_native |