blob: 431c7c2f9dfd57031f15409e6775c2374a31db3a [file] [log] [blame]
Corentin Walleze1f0d4e2019-02-15 12:54:08 +00001// 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 Wallezf20f5b92019-02-20 11:46:16 +000017#include "common/BitSetIterator.h"
Tomek Ponitka7ce49242020-08-05 16:43:24 +000018#include "common/Math.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000019#include "dawn_native/BindGroup.h"
20#include "dawn_native/Buffer.h"
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000021#include "dawn_native/CommandBuffer.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000022#include "dawn_native/CommandBufferStateTracker.h"
Austin Eng3318caa2019-08-13 00:22:28 +000023#include "dawn_native/CommandValidation.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000024#include "dawn_native/Commands.h"
25#include "dawn_native/ComputePassEncoder.h"
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000026#include "dawn_native/Device.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000027#include "dawn_native/ErrorData.h"
Hao Li6f833b72021-01-14 03:26:08 +000028#include "dawn_native/QueryHelper.h"
Hao Li5191adc2020-07-01 10:48:16 +000029#include "dawn_native/QuerySet.h"
Hao Li6f833b72021-01-14 03:26:08 +000030#include "dawn_native/Queue.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000031#include "dawn_native/RenderPassEncoder.h"
32#include "dawn_native/RenderPipeline.h"
Jiawei Shao51c347a2019-12-12 01:29:01 +000033#include "dawn_native/ValidationUtils_autogen.h"
Austin Eng73d5bb52019-10-28 23:15:40 +000034#include "dawn_platform/DawnPlatform.h"
Austin Engd4ce7362019-08-13 19:00:34 +000035#include "dawn_platform/tracing/TraceEvent.h"
Corentin Wallezf20f5b92019-02-20 11:46:16 +000036
Corentin Walleze51f8dd2020-02-18 02:44:05 +000037#include <cmath>
Corentin Wallezf20f5b92019-02-20 11:46:16 +000038#include <map>
Corentin Walleze1f0d4e2019-02-15 12:54:08 +000039
40namespace dawn_native {
41
Corentin Wallezf20f5b92019-02-20 11:46:16 +000042 namespace {
43
Brandon Jones55a38972021-06-28 18:38:24 +000044 MaybeError ValidateDeprecatedStoreOp(DeviceBase* device, wgpu::StoreOp value) {
45 if (value == wgpu::StoreOp::Clear) {
46 device->EmitDeprecationWarning(
47 "The 'clear' storeOp is deprecated. Use 'discard' instead.");
48 }
49 return ValidateStoreOp(value);
50 }
51
Corentin Wallezecf93472020-05-05 08:33:55 +000052 MaybeError ValidateB2BCopyAlignment(uint64_t dataSize,
53 uint64_t srcOffset,
54 uint64_t dstOffset) {
Yan, Shaobo738567f2019-02-27 02:46:27 +000055 // Copy size must be a multiple of 4 bytes on macOS.
56 if (dataSize % 4 != 0) {
57 return DAWN_VALIDATION_ERROR("Copy size must be a multiple of 4 bytes");
58 }
59
60 // SourceOffset and destinationOffset must be multiples of 4 bytes on macOS.
61 if (srcOffset % 4 != 0 || dstOffset % 4 != 0) {
62 return DAWN_VALIDATION_ERROR(
63 "Source offset and destination offset must be multiples of 4 bytes");
64 }
65
66 return {};
67 }
68
Austin Engf114a682020-09-01 18:40:18 +000069 MaybeError ValidateTextureSampleCountInBufferCopyCommands(const TextureBase* texture) {
Jiawei Shao081d5c22019-03-04 12:01:59 +000070 if (texture->GetSampleCount() > 1) {
Brandon Jonesd3d3aa02019-03-26 11:06:23 +000071 return DAWN_VALIDATION_ERROR(
Austin Engf114a682020-09-01 18:40:18 +000072 "The sample count of textures must be 1 when copying between buffers and "
73 "textures");
Brandon Jonesd3d3aa02019-03-26 11:06:23 +000074 }
75
76 return {};
77 }
78
Kai Ninomiya54123a32020-10-27 19:30:57 +000079 MaybeError ValidateLinearTextureCopyOffset(const TextureDataLayout& layout,
Zhaoming Jiang1af81e32021-06-24 05:38:00 +000080 const TexelBlockInfo& blockInfo,
81 const bool hasDepthOrStencil) {
82 if (hasDepthOrStencil) {
83 // For depth-stencil texture, buffer offset must be a multiple of 4.
84 if (layout.offset % 4 != 0) {
85 return DAWN_VALIDATION_ERROR(
86 "offset must be a multiple of 4 for depth/stencil texture.");
87 }
88 } else {
89 if (layout.offset % blockInfo.byteSize != 0) {
90 return DAWN_VALIDATION_ERROR(
91 "offset must be a multiple of the texel block byte size.");
92 }
Austin Eng0d9fce12020-07-30 15:29:57 +000093 }
Kai Ninomiya54123a32020-10-27 19:30:57 +000094 return {};
95 }
Austin Eng0d9fce12020-07-30 15:29:57 +000096
Corentin Wallez80915842021-03-04 18:13:45 +000097 MaybeError ValidateTextureDepthStencilToBufferCopyRestrictions(
98 const ImageCopyTexture& src) {
Kai Ninomiya54123a32020-10-27 19:30:57 +000099 Aspect aspectUsed;
Corentin Wallez80915842021-03-04 18:13:45 +0000100 DAWN_TRY_ASSIGN(aspectUsed, SingleAspectUsedByImageCopyTexture(src));
Kai Ninomiya54123a32020-10-27 19:30:57 +0000101 if (aspectUsed == Aspect::Depth) {
102 switch (src.texture->GetFormat().format) {
Austin Eng0d9fce12020-07-30 15:29:57 +0000103 case wgpu::TextureFormat::Depth24Plus:
104 case wgpu::TextureFormat::Depth24PlusStencil8:
105 return DAWN_VALIDATION_ERROR(
106 "The depth aspect of depth24plus texture cannot be selected in a "
107 "texture to buffer copy");
Austin Eng0d9fce12020-07-30 15:29:57 +0000108 case wgpu::TextureFormat::Depth32Float:
109 break;
Corentin Wallezeec9edf2020-09-24 14:56:50 +0000110
Austin Eng0d9fce12020-07-30 15:29:57 +0000111 default:
112 UNREACHABLE();
Austin Eng0d9fce12020-07-30 15:29:57 +0000113 }
114 }
115
116 return {};
117 }
118
Jiawei Shaob2c50232019-02-27 09:21:56 +0000119 MaybeError ValidateAttachmentArrayLayersAndLevelCount(const TextureViewBase* attachment) {
120 // Currently we do not support layered rendering.
121 if (attachment->GetLayerCount() > 1) {
122 return DAWN_VALIDATION_ERROR(
123 "The layer count of the texture view used as attachment cannot be greater than "
124 "1");
125 }
126
127 if (attachment->GetLevelCount() > 1) {
128 return DAWN_VALIDATION_ERROR(
129 "The mipmap level count of the texture view used as attachment cannot be "
130 "greater than 1");
131 }
132
133 return {};
134 }
135
136 MaybeError ValidateOrSetAttachmentSize(const TextureViewBase* attachment,
137 uint32_t* width,
138 uint32_t* height) {
Stephen Whiteed3a93f2020-10-02 16:15:40 +0000139 const Extent3D& attachmentSize =
140 attachment->GetTexture()->GetMipLevelVirtualSize(attachment->GetBaseMipLevel());
Jiawei Shaob2c50232019-02-27 09:21:56 +0000141
142 if (*width == 0) {
143 DAWN_ASSERT(*height == 0);
Stephen Whiteed3a93f2020-10-02 16:15:40 +0000144 *width = attachmentSize.width;
145 *height = attachmentSize.height;
Jiawei Shaob2c50232019-02-27 09:21:56 +0000146 DAWN_ASSERT(*width != 0 && *height != 0);
Stephen Whiteed3a93f2020-10-02 16:15:40 +0000147 } else if (*width != attachmentSize.width || *height != attachmentSize.height) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000148 return DAWN_VALIDATION_ERROR("Attachment size mismatch");
149 }
150
151 return {};
152 }
153
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000154 MaybeError ValidateOrSetColorAttachmentSampleCount(const TextureViewBase* colorAttachment,
155 uint32_t* sampleCount) {
156 if (*sampleCount == 0) {
157 *sampleCount = colorAttachment->GetTexture()->GetSampleCount();
158 DAWN_ASSERT(*sampleCount != 0);
159 } else if (*sampleCount != colorAttachment->GetTexture()->GetSampleCount()) {
160 return DAWN_VALIDATION_ERROR("Color attachment sample counts mismatch");
161 }
162
163 return {};
164 }
165
166 MaybeError ValidateResolveTarget(
167 const DeviceBase* device,
Corentin Walleza838c7d2019-09-20 22:59:47 +0000168 const RenderPassColorAttachmentDescriptor& colorAttachment) {
169 if (colorAttachment.resolveTarget == nullptr) {
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000170 return {};
171 }
172
Corentin Walleza838c7d2019-09-20 22:59:47 +0000173 const TextureViewBase* resolveTarget = colorAttachment.resolveTarget;
Brandon Jones5e6a0922021-04-17 01:51:53 +0000174 const TextureViewBase* attachment =
175 colorAttachment.view != nullptr ? colorAttachment.view : colorAttachment.attachment;
Corentin Walleza838c7d2019-09-20 22:59:47 +0000176 DAWN_TRY(device->ValidateObject(colorAttachment.resolveTarget));
Corentin Wallez72cd1a52021-01-22 11:31:08 +0000177 DAWN_TRY(ValidateCanUseAs(colorAttachment.resolveTarget->GetTexture(),
178 wgpu::TextureUsage::RenderAttachment));
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000179
Corentin Walleza838c7d2019-09-20 22:59:47 +0000180 if (!attachment->GetTexture()->IsMultisampledTexture()) {
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000181 return DAWN_VALIDATION_ERROR(
182 "Cannot set resolve target when the sample count of the color attachment is 1");
183 }
184
Corentin Walleza838c7d2019-09-20 22:59:47 +0000185 if (resolveTarget->GetTexture()->IsMultisampledTexture()) {
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000186 return DAWN_VALIDATION_ERROR("Cannot use multisampled texture as resolve target");
187 }
188
Corentin Walleza838c7d2019-09-20 22:59:47 +0000189 if (resolveTarget->GetLayerCount() > 1) {
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000190 return DAWN_VALIDATION_ERROR(
191 "The array layer count of the resolve target must be 1");
192 }
193
Corentin Walleza838c7d2019-09-20 22:59:47 +0000194 if (resolveTarget->GetLevelCount() > 1) {
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000195 return DAWN_VALIDATION_ERROR("The mip level count of the resolve target must be 1");
196 }
197
Yunchao He4043ee92021-04-30 17:51:58 +0000198 const Extent3D& colorTextureSize =
199 attachment->GetTexture()->GetMipLevelVirtualSize(attachment->GetBaseMipLevel());
200 const Extent3D& resolveTextureSize =
201 resolveTarget->GetTexture()->GetMipLevelVirtualSize(
202 resolveTarget->GetBaseMipLevel());
203 if (colorTextureSize.width != resolveTextureSize.width ||
204 colorTextureSize.height != resolveTextureSize.height) {
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000205 return DAWN_VALIDATION_ERROR(
206 "The size of the resolve target must be the same as the color attachment");
207 }
208
Corentin Wallez1f6c8c42019-10-23 11:57:41 +0000209 wgpu::TextureFormat resolveTargetFormat = resolveTarget->GetFormat().format;
Corentin Walleza838c7d2019-09-20 22:59:47 +0000210 if (resolveTargetFormat != attachment->GetFormat().format) {
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000211 return DAWN_VALIDATION_ERROR(
212 "The format of the resolve target must be the same as the color attachment");
213 }
214
215 return {};
216 }
217
Jiawei Shaob2c50232019-02-27 09:21:56 +0000218 MaybeError ValidateRenderPassColorAttachment(
Brandon Jones5e6a0922021-04-17 01:51:53 +0000219 DeviceBase* device,
Corentin Walleza838c7d2019-09-20 22:59:47 +0000220 const RenderPassColorAttachmentDescriptor& colorAttachment,
Jiawei Shaob2c50232019-02-27 09:21:56 +0000221 uint32_t* width,
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000222 uint32_t* height,
223 uint32_t* sampleCount) {
Brandon Jones5e6a0922021-04-17 01:51:53 +0000224 TextureViewBase* attachment;
225 if (colorAttachment.view != nullptr) {
226 if (colorAttachment.attachment != nullptr) {
227 return DAWN_VALIDATION_ERROR(
228 "Cannot specify both a attachment and view. attachment is deprecated, "
229 "favor view instead.");
230 }
231 attachment = colorAttachment.view;
232 } else if (colorAttachment.attachment != nullptr) {
233 device->EmitDeprecationWarning(
234 "RenderPassColorAttachmentDescriptor.attachment has been deprecated. Use "
235 "RenderPassColorAttachmentDescriptor.view instead.");
236 attachment = colorAttachment.attachment;
237 } else {
238 return DAWN_VALIDATION_ERROR(
239 "Must specify a view for RenderPassColorAttachmentDescriptor");
240 }
Jiawei Shaob2c50232019-02-27 09:21:56 +0000241
Brandon Jones5e6a0922021-04-17 01:51:53 +0000242 DAWN_TRY(device->ValidateObject(attachment));
243 DAWN_TRY(
244 ValidateCanUseAs(attachment->GetTexture(), wgpu::TextureUsage::RenderAttachment));
245
Corentin Wallez49c30a72020-10-15 09:15:53 +0000246 if (!(attachment->GetAspects() & Aspect::Color) ||
247 !attachment->GetFormat().isRenderable) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000248 return DAWN_VALIDATION_ERROR(
249 "The format of the texture view used as color attachment is not color "
250 "renderable");
251 }
252
Jiawei Shao51c347a2019-12-12 01:29:01 +0000253 DAWN_TRY(ValidateLoadOp(colorAttachment.loadOp));
Brandon Jones55a38972021-06-28 18:38:24 +0000254 DAWN_TRY(ValidateDeprecatedStoreOp(device, colorAttachment.storeOp));
Jiawei Shao51c347a2019-12-12 01:29:01 +0000255
256 if (colorAttachment.loadOp == wgpu::LoadOp::Clear) {
257 if (std::isnan(colorAttachment.clearColor.r) ||
258 std::isnan(colorAttachment.clearColor.g) ||
259 std::isnan(colorAttachment.clearColor.b) ||
260 std::isnan(colorAttachment.clearColor.a)) {
261 return DAWN_VALIDATION_ERROR("Color clear value cannot contain NaN");
262 }
263 }
264
Jiawei Shao1e1c13e2019-03-11 18:41:02 +0000265 DAWN_TRY(ValidateOrSetColorAttachmentSampleCount(attachment, sampleCount));
266
267 DAWN_TRY(ValidateResolveTarget(device, colorAttachment));
268
Jiawei Shaob2c50232019-02-27 09:21:56 +0000269 DAWN_TRY(ValidateAttachmentArrayLayersAndLevelCount(attachment));
270 DAWN_TRY(ValidateOrSetAttachmentSize(attachment, width, height));
271
272 return {};
273 }
274
275 MaybeError ValidateRenderPassDepthStencilAttachment(
Brandon Jones5e6a0922021-04-17 01:51:53 +0000276 DeviceBase* device,
Jiawei Shaob2c50232019-02-27 09:21:56 +0000277 const RenderPassDepthStencilAttachmentDescriptor* depthStencilAttachment,
278 uint32_t* width,
Jiawei Shao93131172019-03-15 05:16:41 +0000279 uint32_t* height,
280 uint32_t* sampleCount) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000281 DAWN_ASSERT(depthStencilAttachment != nullptr);
282
Brandon Jones5e6a0922021-04-17 01:51:53 +0000283 TextureViewBase* attachment;
284 if (depthStencilAttachment->view != nullptr) {
285 if (depthStencilAttachment->attachment != nullptr) {
286 return DAWN_VALIDATION_ERROR(
287 "Cannot specify both a attachment and view. attachment is deprecated, "
288 "favor view instead.");
289 }
290 attachment = depthStencilAttachment->view;
291 } else if (depthStencilAttachment->attachment != nullptr) {
292 device->EmitDeprecationWarning(
293 "RenderPassDepthStencilAttachmentDescriptor.attachment has been deprecated. "
294 "Use RenderPassDepthStencilAttachmentDescriptor.view instead.");
295 attachment = depthStencilAttachment->attachment;
296 } else {
297 return DAWN_VALIDATION_ERROR(
298 "Must specify a view for RenderPassDepthStencilAttachmentDescriptor");
299 }
Jiawei Shaob2c50232019-02-27 09:21:56 +0000300
Brandon Jones5e6a0922021-04-17 01:51:53 +0000301 DAWN_TRY(device->ValidateObject(attachment));
302 DAWN_TRY(
303 ValidateCanUseAs(attachment->GetTexture(), wgpu::TextureUsage::RenderAttachment));
304
Corentin Wallez5a53eb32021-06-11 14:25:29 +0000305 const Format& format = attachment->GetFormat();
306 if (!format.HasDepthOrStencil()) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000307 return DAWN_VALIDATION_ERROR(
308 "The format of the texture view used as depth stencil attachment is not a "
309 "depth stencil format");
310 }
Corentin Wallez5a53eb32021-06-11 14:25:29 +0000311 if (!format.isRenderable) {
312 return DAWN_VALIDATION_ERROR(
313 "The format of the texture view used as depth stencil attachment is not "
314 "renderable");
315 }
316 if (attachment->GetAspects() != format.aspects) {
317 // TODO(https://crbug.com/dawn/812): Investigate if this limitation should be added
318 // to the WebGPU spec of lifted from Dawn.
319 return DAWN_VALIDATION_ERROR(
320 "The texture view used as depth stencil view must encompass all aspects");
321 }
Jiawei Shaob2c50232019-02-27 09:21:56 +0000322
Jiawei Shao51c347a2019-12-12 01:29:01 +0000323 DAWN_TRY(ValidateLoadOp(depthStencilAttachment->depthLoadOp));
324 DAWN_TRY(ValidateLoadOp(depthStencilAttachment->stencilLoadOp));
Brandon Jones55a38972021-06-28 18:38:24 +0000325 DAWN_TRY(ValidateDeprecatedStoreOp(device, depthStencilAttachment->depthStoreOp));
326 DAWN_TRY(ValidateDeprecatedStoreOp(device, depthStencilAttachment->stencilStoreOp));
Jiawei Shao51c347a2019-12-12 01:29:01 +0000327
Corentin Wallez49c30a72020-10-15 09:15:53 +0000328 if (attachment->GetAspects() == (Aspect::Depth | Aspect::Stencil) &&
Brandon Jones7695afc2020-07-10 23:13:58 +0000329 depthStencilAttachment->depthReadOnly != depthStencilAttachment->stencilReadOnly) {
330 return DAWN_VALIDATION_ERROR(
331 "depthReadOnly and stencilReadOnly must be the same when texture aspect is "
332 "'all'");
333 }
334
335 if (depthStencilAttachment->depthReadOnly &&
336 (depthStencilAttachment->depthLoadOp != wgpu::LoadOp::Load ||
337 depthStencilAttachment->depthStoreOp != wgpu::StoreOp::Store)) {
338 return DAWN_VALIDATION_ERROR(
339 "depthLoadOp must be load and depthStoreOp must be store when depthReadOnly "
340 "is true.");
341 }
342
343 if (depthStencilAttachment->stencilReadOnly &&
344 (depthStencilAttachment->stencilLoadOp != wgpu::LoadOp::Load ||
345 depthStencilAttachment->stencilStoreOp != wgpu::StoreOp::Store)) {
346 return DAWN_VALIDATION_ERROR(
347 "stencilLoadOp must be load and stencilStoreOp must be store when "
348 "stencilReadOnly "
349 "is true.");
350 }
351
Jiawei Shao51c347a2019-12-12 01:29:01 +0000352 if (depthStencilAttachment->depthLoadOp == wgpu::LoadOp::Clear &&
353 std::isnan(depthStencilAttachment->clearDepth)) {
354 return DAWN_VALIDATION_ERROR("Depth clear value cannot be NaN");
355 }
356
Jiawei Shao93131172019-03-15 05:16:41 +0000357 // *sampleCount == 0 must only happen when there is no color attachment. In that case we
358 // do not need to validate the sample count of the depth stencil attachment.
Jiawei Shao54e4d472019-03-19 01:12:01 +0000359 const uint32_t depthStencilSampleCount = attachment->GetTexture()->GetSampleCount();
360 if (*sampleCount != 0) {
361 if (depthStencilSampleCount != *sampleCount) {
362 return DAWN_VALIDATION_ERROR("Depth stencil attachment sample counts mismatch");
363 }
364 } else {
365 *sampleCount = depthStencilSampleCount;
Jiawei Shao93131172019-03-15 05:16:41 +0000366 }
367
Jiawei Shaob2c50232019-02-27 09:21:56 +0000368 DAWN_TRY(ValidateAttachmentArrayLayersAndLevelCount(attachment));
369 DAWN_TRY(ValidateOrSetAttachmentSize(attachment, width, height));
370
371 return {};
372 }
373
Brandon Jones5e6a0922021-04-17 01:51:53 +0000374 MaybeError ValidateRenderPassDescriptor(DeviceBase* device,
Corentin Wallez4b90c472019-07-10 20:43:13 +0000375 const RenderPassDescriptor* descriptor,
Jiawei Shao54e4d472019-03-19 01:12:01 +0000376 uint32_t* width,
377 uint32_t* height,
378 uint32_t* sampleCount) {
Corentin Wallez4b90c472019-07-10 20:43:13 +0000379 if (descriptor->colorAttachmentCount > kMaxColorAttachments) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000380 return DAWN_VALIDATION_ERROR("Setting color attachments out of bounds");
381 }
382
Corentin Wallez4b90c472019-07-10 20:43:13 +0000383 for (uint32_t i = 0; i < descriptor->colorAttachmentCount; ++i) {
384 DAWN_TRY(ValidateRenderPassColorAttachment(device, descriptor->colorAttachments[i],
Jiawei Shao54e4d472019-03-19 01:12:01 +0000385 width, height, sampleCount));
Jiawei Shaob2c50232019-02-27 09:21:56 +0000386 }
387
Corentin Wallez4b90c472019-07-10 20:43:13 +0000388 if (descriptor->depthStencilAttachment != nullptr) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000389 DAWN_TRY(ValidateRenderPassDepthStencilAttachment(
Corentin Wallez4b90c472019-07-10 20:43:13 +0000390 device, descriptor->depthStencilAttachment, width, height, sampleCount));
Jiawei Shaob2c50232019-02-27 09:21:56 +0000391 }
392
Kai Ninomiya53405b52020-07-11 03:15:16 +0000393 if (descriptor->occlusionQuerySet != nullptr) {
Hao Li01e44502020-11-18 09:47:52 +0000394 DAWN_TRY(device->ValidateObject(descriptor->occlusionQuerySet));
395
Hao Li01e44502020-11-18 09:47:52 +0000396 if (descriptor->occlusionQuerySet->GetQueryType() != wgpu::QueryType::Occlusion) {
397 return DAWN_VALIDATION_ERROR("The type of query set must be Occlusion");
398 }
Kai Ninomiya53405b52020-07-11 03:15:16 +0000399 }
400
Corentin Wallez4b90c472019-07-10 20:43:13 +0000401 if (descriptor->colorAttachmentCount == 0 &&
402 descriptor->depthStencilAttachment == nullptr) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000403 return DAWN_VALIDATION_ERROR("Cannot use render pass with no attachments.");
404 }
405
406 return {};
407 }
408
Corentin Wallez4b90c472019-07-10 20:43:13 +0000409 MaybeError ValidateComputePassDescriptor(const DeviceBase* device,
410 const ComputePassDescriptor* descriptor) {
411 return {};
412 }
413
Hao Li5c89c8d2020-07-17 09:02:46 +0000414 MaybeError ValidateQuerySetResolve(const QuerySetBase* querySet,
415 uint32_t firstQuery,
416 uint32_t queryCount,
417 const BufferBase* destination,
418 uint64_t destinationOffset) {
419 if (firstQuery >= querySet->GetQueryCount()) {
420 return DAWN_VALIDATION_ERROR("Query index out of bounds");
421 }
422
423 if (queryCount > querySet->GetQueryCount() - firstQuery) {
424 return DAWN_VALIDATION_ERROR(
425 "The sum of firstQuery and queryCount exceeds the number of queries in query "
426 "set");
427 }
428
Hao Li5c89c8d2020-07-17 09:02:46 +0000429 // The destinationOffset must be a multiple of 8 bytes on D3D12 and Vulkan
430 if (destinationOffset % 8 != 0) {
431 return DAWN_VALIDATION_ERROR(
432 "The alignment offset into the destination buffer must be a multiple of 8 "
433 "bytes");
434 }
435
436 uint64_t bufferSize = destination->GetSize();
437 // The destination buffer must have enough storage, from destination offset, to contain
438 // the result of resolved queries
439 bool fitsInBuffer = destinationOffset <= bufferSize &&
440 (static_cast<uint64_t>(queryCount) * sizeof(uint64_t) <=
441 (bufferSize - destinationOffset));
442 if (!fitsInBuffer) {
443 return DAWN_VALIDATION_ERROR("The resolved query data would overflow the buffer");
444 }
445
446 return {};
447 }
448
Corentin Wallez79c9d122021-03-31 11:24:42 +0000449 MaybeError EncodeTimestampsToNanosecondsConversion(CommandEncoder* encoder,
450 QuerySetBase* querySet,
Hao Li880a3d62021-05-18 01:13:08 +0000451 uint32_t firstQuery,
Corentin Wallez79c9d122021-03-31 11:24:42 +0000452 uint32_t queryCount,
453 BufferBase* destination,
454 uint64_t destinationOffset) {
Hao Li6f833b72021-01-14 03:26:08 +0000455 DeviceBase* device = encoder->GetDevice();
456
Hao Lie0a58872021-02-10 14:01:36 +0000457 // The availability got from query set is a reference to vector<bool>, need to covert
458 // bool to uint32_t due to a user input in pipeline must not contain a bool type in
459 // WGSL.
460 std::vector<uint32_t> availability{querySet->GetQueryAvailability().begin(),
461 querySet->GetQueryAvailability().end()};
Hao Li6f833b72021-01-14 03:26:08 +0000462
463 // Timestamp availability storage buffer
464 BufferDescriptor availabilityDesc = {};
465 availabilityDesc.usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopyDst;
466 availabilityDesc.size = querySet->GetQueryCount() * sizeof(uint32_t);
Corentin Wallez0af4a832021-04-19 08:52:35 +0000467 Ref<BufferBase> availabilityBuffer;
468 DAWN_TRY_ASSIGN(availabilityBuffer, device->CreateBuffer(&availabilityDesc));
469
Corentin Wallez79c9d122021-03-31 11:24:42 +0000470 DAWN_TRY(device->GetQueue()->WriteBuffer(availabilityBuffer.Get(), 0,
471 availability.data(),
472 availability.size() * sizeof(uint32_t)));
Hao Li6f833b72021-01-14 03:26:08 +0000473
474 // Timestamp params uniform buffer
Hao Li880a3d62021-05-18 01:13:08 +0000475 TimestampParams params = {firstQuery, queryCount,
476 static_cast<uint32_t>(destinationOffset),
Hao Li6f833b72021-01-14 03:26:08 +0000477 device->GetTimestampPeriodInNS()};
Corentin Wallez0af4a832021-04-19 08:52:35 +0000478
Hao Li6f833b72021-01-14 03:26:08 +0000479 BufferDescriptor parmsDesc = {};
480 parmsDesc.usage = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
481 parmsDesc.size = sizeof(params);
Corentin Wallez0af4a832021-04-19 08:52:35 +0000482 Ref<BufferBase> paramsBuffer;
483 DAWN_TRY_ASSIGN(paramsBuffer, device->CreateBuffer(&parmsDesc));
484
Corentin Wallez79c9d122021-03-31 11:24:42 +0000485 DAWN_TRY(
486 device->GetQueue()->WriteBuffer(paramsBuffer.Get(), 0, &params, sizeof(params)));
Hao Li6f833b72021-01-14 03:26:08 +0000487
Corentin Wallez0af4a832021-04-19 08:52:35 +0000488 return EncodeConvertTimestampsToNanoseconds(
489 encoder, destination, availabilityBuffer.Get(), paramsBuffer.Get());
Hao Li6f833b72021-01-14 03:26:08 +0000490 }
491
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000492 } // namespace
493
Corentin Wallez321c1222019-11-13 17:00:37 +0000494 CommandEncoder::CommandEncoder(DeviceBase* device, const CommandEncoderDescriptor*)
Austin Engfde94902019-07-24 18:15:24 +0000495 : ObjectBase(device), mEncodingContext(device, this) {
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000496 }
497
Corentin Wallez321c1222019-11-13 17:00:37 +0000498 CommandBufferResourceUsage CommandEncoder::AcquireResourceUsages() {
Corentin Wallez2dd2d672021-05-05 15:41:13 +0000499 return CommandBufferResourceUsage{
500 mEncodingContext.AcquireRenderPassUsages(), mEncodingContext.AcquireComputePassUsages(),
501 std::move(mTopLevelBuffers), std::move(mTopLevelTextures), std::move(mUsedQuerySets)};
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000502 }
503
Corentin Wallez321c1222019-11-13 17:00:37 +0000504 CommandIterator CommandEncoder::AcquireCommands() {
Austin Engfde94902019-07-24 18:15:24 +0000505 return mEncodingContext.AcquireCommands();
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000506 }
507
Hao Li5191adc2020-07-01 10:48:16 +0000508 void CommandEncoder::TrackUsedQuerySet(QuerySetBase* querySet) {
509 mUsedQuerySets.insert(querySet);
510 }
511
Hao Li575729e2020-11-16 02:24:06 +0000512 void CommandEncoder::TrackQueryAvailability(QuerySetBase* querySet, uint32_t queryIndex) {
513 DAWN_ASSERT(querySet != nullptr);
514
515 if (GetDevice()->IsValidationEnabled()) {
516 TrackUsedQuerySet(querySet);
Hao Lie2cbcc92020-10-21 08:38:31 +0000517 }
Hao Li575729e2020-11-16 02:24:06 +0000518
Hao Lie0a58872021-02-10 14:01:36 +0000519 // Set the query at queryIndex to available for resolving in query set.
Hao Lid75f7c02021-04-07 05:39:21 +0000520 querySet->SetQueryAvailability(queryIndex, true);
Hao Lie2cbcc92020-10-21 08:38:31 +0000521 }
522
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000523 // Implementation of the API's command recording methods
524
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000525 ComputePassEncoder* CommandEncoder::APIBeginComputePass(
526 const ComputePassDescriptor* descriptor) {
Jiawei Shaob47470d2019-03-05 01:02:47 +0000527 DeviceBase* device = GetDevice();
Austin Engfde94902019-07-24 18:15:24 +0000528
529 bool success =
530 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
531 DAWN_TRY(ValidateComputePassDescriptor(device, descriptor));
532
533 allocator->Allocate<BeginComputePassCmd>(Command::BeginComputePass);
534
535 return {};
536 });
537
538 if (success) {
Corentin Wallez321c1222019-11-13 17:00:37 +0000539 ComputePassEncoder* passEncoder =
540 new ComputePassEncoder(device, this, &mEncodingContext);
Austin Engfde94902019-07-24 18:15:24 +0000541 mEncodingContext.EnterPass(passEncoder);
542 return passEncoder;
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000543 }
544
Corentin Wallez321c1222019-11-13 17:00:37 +0000545 return ComputePassEncoder::MakeError(device, this, &mEncodingContext);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000546 }
547
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000548 RenderPassEncoder* CommandEncoder::APIBeginRenderPass(const RenderPassDescriptor* descriptor) {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000549 DeviceBase* device = GetDevice();
550
Corentin Wallezec7ea6a2021-05-05 19:55:23 +0000551 RenderPassResourceUsageTracker usageTracker;
Corentin Wallez6c3da3d2020-10-11 18:39:32 +0000552
553 uint32_t width = 0;
554 uint32_t height = 0;
Corentin Wallez95ff8342021-01-27 17:20:16 +0000555 Ref<AttachmentState> attachmentState;
Austin Engfde94902019-07-24 18:15:24 +0000556 bool success =
557 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
Austin Engfde94902019-07-24 18:15:24 +0000558 uint32_t sampleCount = 0;
559
560 DAWN_TRY(ValidateRenderPassDescriptor(device, descriptor, &width, &height,
561 &sampleCount));
562
563 ASSERT(width > 0 && height > 0 && sampleCount > 0);
564
565 BeginRenderPassCmd* cmd =
566 allocator->Allocate<BeginRenderPassCmd>(Command::BeginRenderPass);
567
Austin Engb98f0fa2019-07-26 19:08:18 +0000568 cmd->attachmentState = device->GetOrCreateAttachmentState(descriptor);
Corentin Wallez95ff8342021-01-27 17:20:16 +0000569 attachmentState = cmd->attachmentState;
Austin Engb98f0fa2019-07-26 19:08:18 +0000570
Austin Eng7b7e0982020-09-09 00:08:38 +0000571 for (ColorAttachmentIndex index :
572 IterateBitSet(cmd->attachmentState->GetColorAttachmentsMask())) {
573 uint8_t i = static_cast<uint8_t>(index);
Brandon Jones5e6a0922021-04-17 01:51:53 +0000574 TextureViewBase* view = descriptor->colorAttachments[i].view;
575 if (view == nullptr) {
576 view = descriptor->colorAttachments[i].attachment;
577 }
Austin Eng4b0b7a52019-11-21 22:09:41 +0000578 TextureViewBase* resolveTarget = descriptor->colorAttachments[i].resolveTarget;
579
Austin Eng7b7e0982020-09-09 00:08:38 +0000580 cmd->colorAttachments[index].view = view;
581 cmd->colorAttachments[index].resolveTarget = resolveTarget;
582 cmd->colorAttachments[index].loadOp = descriptor->colorAttachments[i].loadOp;
583 cmd->colorAttachments[index].storeOp = descriptor->colorAttachments[i].storeOp;
584 cmd->colorAttachments[index].clearColor =
Corentin Walleza838c7d2019-09-20 22:59:47 +0000585 descriptor->colorAttachments[i].clearColor;
Austin Eng4b0b7a52019-11-21 22:09:41 +0000586
Corentin Wallez6b087812020-10-27 15:35:56 +0000587 usageTracker.TextureViewUsedAs(view, wgpu::TextureUsage::RenderAttachment);
Austin Eng4b0b7a52019-11-21 22:09:41 +0000588
589 if (resolveTarget != nullptr) {
Yunchao He23428ea2020-05-04 17:10:49 +0000590 usageTracker.TextureViewUsedAs(resolveTarget,
Corentin Wallez6b087812020-10-27 15:35:56 +0000591 wgpu::TextureUsage::RenderAttachment);
Austin Eng4b0b7a52019-11-21 22:09:41 +0000592 }
Austin Engfde94902019-07-24 18:15:24 +0000593 }
594
Austin Engb98f0fa2019-07-26 19:08:18 +0000595 if (cmd->attachmentState->HasDepthStencilAttachment()) {
Brandon Jones5e6a0922021-04-17 01:51:53 +0000596 TextureViewBase* view = descriptor->depthStencilAttachment->view;
597 if (view == nullptr) {
598 view = descriptor->depthStencilAttachment->attachment;
599 }
Austin Eng4b0b7a52019-11-21 22:09:41 +0000600
601 cmd->depthStencilAttachment.view = view;
Austin Engfde94902019-07-24 18:15:24 +0000602 cmd->depthStencilAttachment.clearDepth =
603 descriptor->depthStencilAttachment->clearDepth;
604 cmd->depthStencilAttachment.clearStencil =
605 descriptor->depthStencilAttachment->clearStencil;
606 cmd->depthStencilAttachment.depthLoadOp =
607 descriptor->depthStencilAttachment->depthLoadOp;
608 cmd->depthStencilAttachment.depthStoreOp =
609 descriptor->depthStencilAttachment->depthStoreOp;
610 cmd->depthStencilAttachment.stencilLoadOp =
611 descriptor->depthStencilAttachment->stencilLoadOp;
612 cmd->depthStencilAttachment.stencilStoreOp =
613 descriptor->depthStencilAttachment->stencilStoreOp;
Austin Eng4b0b7a52019-11-21 22:09:41 +0000614
Corentin Wallez6b087812020-10-27 15:35:56 +0000615 usageTracker.TextureViewUsedAs(view, wgpu::TextureUsage::RenderAttachment);
Austin Engfde94902019-07-24 18:15:24 +0000616 }
617
618 cmd->width = width;
619 cmd->height = height;
Austin Engfde94902019-07-24 18:15:24 +0000620
Hao Lid11f4c32021-02-02 05:30:50 +0000621 cmd->occlusionQuerySet = descriptor->occlusionQuerySet;
622
Austin Engfde94902019-07-24 18:15:24 +0000623 return {};
624 });
625
626 if (success) {
Corentin Wallez95ff8342021-01-27 17:20:16 +0000627 RenderPassEncoder* passEncoder = new RenderPassEncoder(
628 device, this, &mEncodingContext, std::move(usageTracker),
629 std::move(attachmentState), descriptor->occlusionQuerySet, width, height);
Austin Engfde94902019-07-24 18:15:24 +0000630 mEncodingContext.EnterPass(passEncoder);
631 return passEncoder;
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000632 }
633
Corentin Wallez321c1222019-11-13 17:00:37 +0000634 return RenderPassEncoder::MakeError(device, this, &mEncodingContext);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000635 }
636
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000637 void CommandEncoder::APICopyBufferToBuffer(BufferBase* source,
638 uint64_t sourceOffset,
639 BufferBase* destination,
640 uint64_t destinationOffset,
641 uint64_t size) {
Austin Engfde94902019-07-24 18:15:24 +0000642 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
Corentin Wallezecf93472020-05-05 08:33:55 +0000643 if (GetDevice()->IsValidationEnabled()) {
644 DAWN_TRY(GetDevice()->ValidateObject(source));
645 DAWN_TRY(GetDevice()->ValidateObject(destination));
646
Jiawei Shao2ae84e92020-05-19 00:11:11 +0000647 if (source == destination) {
648 return DAWN_VALIDATION_ERROR(
649 "Source and destination cannot be the same buffer.");
650 }
651
Corentin Wallezecf93472020-05-05 08:33:55 +0000652 DAWN_TRY(ValidateCopySizeFitsInBuffer(source, sourceOffset, size));
653 DAWN_TRY(ValidateCopySizeFitsInBuffer(destination, destinationOffset, size));
654 DAWN_TRY(ValidateB2BCopyAlignment(size, sourceOffset, destinationOffset));
655
656 DAWN_TRY(ValidateCanUseAs(source, wgpu::BufferUsage::CopySrc));
657 DAWN_TRY(ValidateCanUseAs(destination, wgpu::BufferUsage::CopyDst));
658
659 mTopLevelBuffers.insert(source);
660 mTopLevelBuffers.insert(destination);
661 }
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000662
Austin Engf8a0f822021-06-10 02:40:18 +0000663 CopyBufferToBufferCmd* copy =
664 allocator->Allocate<CopyBufferToBufferCmd>(Command::CopyBufferToBuffer);
665 copy->source = source;
666 copy->sourceOffset = sourceOffset;
667 copy->destination = destination;
668 copy->destinationOffset = destinationOffset;
669 copy->size = size;
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000670
Austin Engfde94902019-07-24 18:15:24 +0000671 return {};
672 });
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000673 }
674
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000675 void CommandEncoder::APICopyBufferToTexture(const ImageCopyBuffer* source,
676 const ImageCopyTexture* destination,
677 const Extent3D* copySize) {
Austin Engfde94902019-07-24 18:15:24 +0000678 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
Corentin Wallezecf93472020-05-05 08:33:55 +0000679 if (GetDevice()->IsValidationEnabled()) {
Corentin Wallez80915842021-03-04 18:13:45 +0000680 DAWN_TRY(ValidateImageCopyBuffer(GetDevice(), *source));
Tomek Ponitka0f5d4962020-07-07 10:18:51 +0000681 DAWN_TRY(ValidateCanUseAs(source->buffer, wgpu::BufferUsage::CopySrc));
682
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000683 DAWN_TRY(ValidateImageCopyTexture(GetDevice(), *destination, *copySize));
Tomek Ponitka0f5d4962020-07-07 10:18:51 +0000684 DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst));
Austin Engf114a682020-09-01 18:40:18 +0000685 DAWN_TRY(ValidateTextureSampleCountInBufferCopyCommands(destination->texture));
Tomek Ponitka0f5d4962020-07-07 10:18:51 +0000686
Kai Ninomiya54123a32020-10-27 19:30:57 +0000687 DAWN_TRY(ValidateLinearToDepthStencilCopyRestrictions(*destination));
Tomek Ponitka73652262020-07-17 09:44:46 +0000688 // We validate texture copy range before validating linear texture data,
689 // because in the latter we divide copyExtent.width by blockWidth and
690 // copyExtent.height by blockHeight while the divisibility conditions are
691 // checked in validating texture copy range.
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000692 DAWN_TRY(ValidateTextureCopyRange(GetDevice(), *destination, *copySize));
Kai Ninomiyad1bca092020-10-12 23:13:53 +0000693 }
694 const TexelBlockInfo& blockInfo =
Corentin Wallez6298d2b2020-10-15 09:05:03 +0000695 destination->texture->GetFormat().GetAspectInfo(destination->aspect).block;
Kai Ninomiyad1bca092020-10-12 23:13:53 +0000696 if (GetDevice()->IsValidationEnabled()) {
Zhaoming Jiang1af81e32021-06-24 05:38:00 +0000697 DAWN_TRY(ValidateLinearTextureCopyOffset(
Brandon Jonesaeff2352021-07-04 18:31:29 +0000698 source->layout, blockInfo,
699 destination->texture->GetFormat().HasDepthOrStencil()));
700 DAWN_TRY(ValidateLinearTextureData(source->layout, source->buffer->GetSize(),
701 blockInfo, *copySize));
Tomek Ponitka0f5d4962020-07-07 10:18:51 +0000702
703 mTopLevelBuffers.insert(source->buffer);
704 mTopLevelTextures.insert(destination->texture);
Corentin Wallezecf93472020-05-05 08:33:55 +0000705 }
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000706
Brandon Jonesaeff2352021-07-04 18:31:29 +0000707 TextureDataLayout srcLayout = source->layout;
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000708 ApplyDefaultTextureDataLayoutOptions(&srcLayout, blockInfo, *copySize);
Tomek Ponitka7ce49242020-08-05 16:43:24 +0000709
Austin Engf8a0f822021-06-10 02:40:18 +0000710 CopyBufferToTextureCmd* copy =
711 allocator->Allocate<CopyBufferToTextureCmd>(Command::CopyBufferToTexture);
712 copy->source.buffer = source->buffer;
713 copy->source.offset = srcLayout.offset;
714 copy->source.bytesPerRow = srcLayout.bytesPerRow;
715 copy->source.rowsPerImage = srcLayout.rowsPerImage;
716 copy->destination.texture = destination->texture;
717 copy->destination.origin = destination->origin;
718 copy->destination.mipLevel = destination->mipLevel;
719 copy->destination.aspect =
720 ConvertAspect(destination->texture->GetFormat(), destination->aspect);
721 copy->copySize = *copySize;
Corentin Wallez984493d2020-06-16 03:05:17 +0000722
Austin Engfde94902019-07-24 18:15:24 +0000723 return {};
724 });
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000725 }
726
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000727 void CommandEncoder::APICopyTextureToBuffer(const ImageCopyTexture* source,
728 const ImageCopyBuffer* destination,
729 const Extent3D* copySize) {
Austin Engfde94902019-07-24 18:15:24 +0000730 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
Corentin Wallezecf93472020-05-05 08:33:55 +0000731 if (GetDevice()->IsValidationEnabled()) {
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000732 DAWN_TRY(ValidateImageCopyTexture(GetDevice(), *source, *copySize));
Tomek Ponitka0f5d4962020-07-07 10:18:51 +0000733 DAWN_TRY(ValidateCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
Austin Engf114a682020-09-01 18:40:18 +0000734 DAWN_TRY(ValidateTextureSampleCountInBufferCopyCommands(source->texture));
Kai Ninomiya54123a32020-10-27 19:30:57 +0000735 DAWN_TRY(ValidateTextureDepthStencilToBufferCopyRestrictions(*source));
Tomek Ponitka0f5d4962020-07-07 10:18:51 +0000736
Corentin Wallez80915842021-03-04 18:13:45 +0000737 DAWN_TRY(ValidateImageCopyBuffer(GetDevice(), *destination));
Tomek Ponitka0f5d4962020-07-07 10:18:51 +0000738 DAWN_TRY(ValidateCanUseAs(destination->buffer, wgpu::BufferUsage::CopyDst));
739
Tomek Ponitka73652262020-07-17 09:44:46 +0000740 // We validate texture copy range before validating linear texture data,
741 // because in the latter we divide copyExtent.width by blockWidth and
742 // copyExtent.height by blockHeight while the divisibility conditions are
743 // checked in validating texture copy range.
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000744 DAWN_TRY(ValidateTextureCopyRange(GetDevice(), *source, *copySize));
Kai Ninomiyad1bca092020-10-12 23:13:53 +0000745 }
746 const TexelBlockInfo& blockInfo =
Corentin Wallez6298d2b2020-10-15 09:05:03 +0000747 source->texture->GetFormat().GetAspectInfo(source->aspect).block;
Kai Ninomiyad1bca092020-10-12 23:13:53 +0000748 if (GetDevice()->IsValidationEnabled()) {
Zhaoming Jiang1af81e32021-06-24 05:38:00 +0000749 DAWN_TRY(ValidateLinearTextureCopyOffset(
Brandon Jonesaeff2352021-07-04 18:31:29 +0000750 destination->layout, blockInfo,
751 source->texture->GetFormat().HasDepthOrStencil()));
752 DAWN_TRY(ValidateLinearTextureData(
753 destination->layout, destination->buffer->GetSize(), blockInfo, *copySize));
Tomek Ponitka0f5d4962020-07-07 10:18:51 +0000754
755 mTopLevelTextures.insert(source->texture);
756 mTopLevelBuffers.insert(destination->buffer);
Corentin Wallezecf93472020-05-05 08:33:55 +0000757 }
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000758
Brandon Jonesaeff2352021-07-04 18:31:29 +0000759 TextureDataLayout dstLayout = destination->layout;
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000760 ApplyDefaultTextureDataLayoutOptions(&dstLayout, blockInfo, *copySize);
Tomek Ponitka7ce49242020-08-05 16:43:24 +0000761
Austin Engf8a0f822021-06-10 02:40:18 +0000762 CopyTextureToBufferCmd* copy =
763 allocator->Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer);
764 copy->source.texture = source->texture;
765 copy->source.origin = source->origin;
766 copy->source.mipLevel = source->mipLevel;
767 copy->source.aspect = ConvertAspect(source->texture->GetFormat(), source->aspect);
768 copy->destination.buffer = destination->buffer;
769 copy->destination.offset = dstLayout.offset;
770 copy->destination.bytesPerRow = dstLayout.bytesPerRow;
771 copy->destination.rowsPerImage = dstLayout.rowsPerImage;
772 copy->copySize = *copySize;
Corentin Wallez984493d2020-06-16 03:05:17 +0000773
Austin Engfde94902019-07-24 18:15:24 +0000774 return {};
775 });
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000776 }
777
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000778 void CommandEncoder::APICopyTextureToTexture(const ImageCopyTexture* source,
779 const ImageCopyTexture* destination,
780 const Extent3D* copySize) {
Juanmif00c68a2021-08-05 22:55:09 +0000781 APICopyTextureToTextureHelper<false>(source, destination, copySize);
782 }
783
784 void CommandEncoder::APICopyTextureToTextureInternal(const ImageCopyTexture* source,
785 const ImageCopyTexture* destination,
786 const Extent3D* copySize) {
787 APICopyTextureToTextureHelper<true>(source, destination, copySize);
788 }
789
790 template <bool Internal>
791 void CommandEncoder::APICopyTextureToTextureHelper(const ImageCopyTexture* source,
792 const ImageCopyTexture* destination,
793 const Extent3D* copySize) {
Austin Engfde94902019-07-24 18:15:24 +0000794 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
Corentin Wallezecf93472020-05-05 08:33:55 +0000795 if (GetDevice()->IsValidationEnabled()) {
796 DAWN_TRY(GetDevice()->ValidateObject(source->texture));
797 DAWN_TRY(GetDevice()->ValidateObject(destination->texture));
798
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000799 DAWN_TRY(ValidateImageCopyTexture(GetDevice(), *source, *copySize));
800 DAWN_TRY(ValidateImageCopyTexture(GetDevice(), *destination, *copySize));
Austin Engea822722020-09-02 18:50:09 +0000801
Corentin Wallezecf93472020-05-05 08:33:55 +0000802 DAWN_TRY(
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000803 ValidateTextureToTextureCopyRestrictions(*source, *destination, *copySize));
Corentin Wallezecf93472020-05-05 08:33:55 +0000804
Brandon Jones76e5a9f2021-05-13 17:51:23 +0000805 DAWN_TRY(ValidateTextureCopyRange(GetDevice(), *source, *copySize));
806 DAWN_TRY(ValidateTextureCopyRange(GetDevice(), *destination, *copySize));
Corentin Wallezecf93472020-05-05 08:33:55 +0000807
Juanmif00c68a2021-08-05 22:55:09 +0000808 // For internal usages (CopyToCopyInternal) we don't care if the user has added
809 // CopySrc as a usage for this texture, but we will always add it internally.
810 if (Internal) {
811 DAWN_TRY(
812 ValidateInternalCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
813 DAWN_TRY(ValidateInternalCanUseAs(destination->texture,
814 wgpu::TextureUsage::CopyDst));
815 } else {
816 DAWN_TRY(ValidateCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
817 DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst));
818 }
Corentin Wallezecf93472020-05-05 08:33:55 +0000819
820 mTopLevelTextures.insert(source->texture);
821 mTopLevelTextures.insert(destination->texture);
822 }
Brandon Jonesd3d3aa02019-03-26 11:06:23 +0000823
Austin Engf8a0f822021-06-10 02:40:18 +0000824 CopyTextureToTextureCmd* copy =
825 allocator->Allocate<CopyTextureToTextureCmd>(Command::CopyTextureToTexture);
826 copy->source.texture = source->texture;
827 copy->source.origin = source->origin;
828 copy->source.mipLevel = source->mipLevel;
829 copy->source.aspect = ConvertAspect(source->texture->GetFormat(), source->aspect);
830 copy->destination.texture = destination->texture;
831 copy->destination.origin = destination->origin;
832 copy->destination.mipLevel = destination->mipLevel;
833 copy->destination.aspect =
834 ConvertAspect(destination->texture->GetFormat(), destination->aspect);
835 copy->copySize = *copySize;
Brandon Jonesd3d3aa02019-03-26 11:06:23 +0000836
Austin Engfde94902019-07-24 18:15:24 +0000837 return {};
838 });
Brandon Jonesd3d3aa02019-03-26 11:06:23 +0000839 }
840
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000841 void CommandEncoder::APIInjectValidationError(const char* message) {
Austin Eng464aaeb2020-11-14 01:24:03 +0000842 if (mEncodingContext.CheckCurrentEncoder(this)) {
Corentin Wallezd98b3072021-05-05 17:37:43 +0000843 mEncodingContext.HandleError(DAWN_VALIDATION_ERROR(message));
Austin Eng464aaeb2020-11-14 01:24:03 +0000844 }
845 }
846
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000847 void CommandEncoder::APIInsertDebugMarker(const char* groupLabel) {
Corentin Wallezb8ea84c2019-09-10 08:20:40 +0000848 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
849 InsertDebugMarkerCmd* cmd =
850 allocator->Allocate<InsertDebugMarkerCmd>(Command::InsertDebugMarker);
851 cmd->length = strlen(groupLabel);
852
853 char* label = allocator->AllocateData<char>(cmd->length + 1);
854 memcpy(label, groupLabel, cmd->length + 1);
855
856 return {};
857 });
858 }
859
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000860 void CommandEncoder::APIPopDebugGroup() {
Corentin Wallezb8ea84c2019-09-10 08:20:40 +0000861 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
Corentin Wallez7ffaa212021-01-27 16:03:32 +0000862 if (GetDevice()->IsValidationEnabled()) {
863 if (mDebugGroupStackSize == 0) {
864 return DAWN_VALIDATION_ERROR("Pop must be balanced by a corresponding Push.");
865 }
866 }
Corentin Wallezb8ea84c2019-09-10 08:20:40 +0000867 allocator->Allocate<PopDebugGroupCmd>(Command::PopDebugGroup);
Corentin Wallez7ffaa212021-01-27 16:03:32 +0000868 mDebugGroupStackSize--;
Corentin Wallezb8ea84c2019-09-10 08:20:40 +0000869
870 return {};
871 });
872 }
873
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000874 void CommandEncoder::APIPushDebugGroup(const char* groupLabel) {
Corentin Wallezb8ea84c2019-09-10 08:20:40 +0000875 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
876 PushDebugGroupCmd* cmd =
877 allocator->Allocate<PushDebugGroupCmd>(Command::PushDebugGroup);
878 cmd->length = strlen(groupLabel);
879
880 char* label = allocator->AllocateData<char>(cmd->length + 1);
881 memcpy(label, groupLabel, cmd->length + 1);
882
Corentin Wallez7ffaa212021-01-27 16:03:32 +0000883 mDebugGroupStackSize++;
884
Corentin Wallezb8ea84c2019-09-10 08:20:40 +0000885 return {};
886 });
887 }
888
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000889 void CommandEncoder::APIResolveQuerySet(QuerySetBase* querySet,
890 uint32_t firstQuery,
891 uint32_t queryCount,
892 BufferBase* destination,
893 uint64_t destinationOffset) {
Hao Li5c89c8d2020-07-17 09:02:46 +0000894 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
895 if (GetDevice()->IsValidationEnabled()) {
896 DAWN_TRY(GetDevice()->ValidateObject(querySet));
897 DAWN_TRY(GetDevice()->ValidateObject(destination));
898
899 DAWN_TRY(ValidateQuerySetResolve(querySet, firstQuery, queryCount, destination,
900 destinationOffset));
901
902 DAWN_TRY(ValidateCanUseAs(destination, wgpu::BufferUsage::QueryResolve));
903
904 TrackUsedQuerySet(querySet);
905 mTopLevelBuffers.insert(destination);
906 }
907
908 ResolveQuerySetCmd* cmd =
909 allocator->Allocate<ResolveQuerySetCmd>(Command::ResolveQuerySet);
910 cmd->querySet = querySet;
911 cmd->firstQuery = firstQuery;
912 cmd->queryCount = queryCount;
913 cmd->destination = destination;
914 cmd->destinationOffset = destinationOffset;
915
Hao Li6f833b72021-01-14 03:26:08 +0000916 // Encode internal compute pipeline for timestamp query
Corentin Wallezad879f52021-03-31 19:31:42 +0000917 if (querySet->GetQueryType() == wgpu::QueryType::Timestamp) {
Hao Li880a3d62021-05-18 01:13:08 +0000918 DAWN_TRY(EncodeTimestampsToNanosecondsConversion(
919 this, querySet, firstQuery, queryCount, destination, destinationOffset));
Hao Li6f833b72021-01-14 03:26:08 +0000920 }
921
Hao Li5c89c8d2020-07-17 09:02:46 +0000922 return {};
923 });
924 }
925
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000926 void CommandEncoder::APIWriteTimestamp(QuerySetBase* querySet, uint32_t queryIndex) {
Hao Li5191adc2020-07-01 10:48:16 +0000927 mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
928 if (GetDevice()->IsValidationEnabled()) {
929 DAWN_TRY(GetDevice()->ValidateObject(querySet));
Hao Li575729e2020-11-16 02:24:06 +0000930 DAWN_TRY(ValidateTimestampQuery(querySet, queryIndex));
Hao Li5191adc2020-07-01 10:48:16 +0000931 }
932
Hao Li575729e2020-11-16 02:24:06 +0000933 TrackQueryAvailability(querySet, queryIndex);
Hao Lie2cbcc92020-10-21 08:38:31 +0000934
Hao Li5191adc2020-07-01 10:48:16 +0000935 WriteTimestampCmd* cmd =
936 allocator->Allocate<WriteTimestampCmd>(Command::WriteTimestamp);
937 cmd->querySet = querySet;
938 cmd->queryIndex = queryIndex;
939
940 return {};
941 });
942 }
943
Corentin Wallez2ce4b902021-03-29 14:02:05 +0000944 CommandBufferBase* CommandEncoder::APIFinish(const CommandBufferDescriptor* descriptor) {
Corentin Wallez50f99582021-03-31 18:36:32 +0000945 Ref<CommandBufferBase> commandBuffer;
946 if (GetDevice()->ConsumedError(FinishInternal(descriptor), &commandBuffer)) {
947 return CommandBufferBase::MakeError(GetDevice());
948 }
949 ASSERT(!IsError());
950 return commandBuffer.Detach();
951 }
952
953 ResultOrError<Ref<CommandBufferBase>> CommandEncoder::FinishInternal(
954 const CommandBufferDescriptor* descriptor) {
Austin Eng4b0b7a52019-11-21 22:09:41 +0000955 DeviceBase* device = GetDevice();
Corentin Wallez50f99582021-03-31 18:36:32 +0000956
Austin Eng4b0b7a52019-11-21 22:09:41 +0000957 // Even if mEncodingContext.Finish() validation fails, calling it will mutate the internal
958 // state of the encoding context. The internal state is set to finished, and subsequent
959 // calls to encode commands will generate errors.
Corentin Wallez50f99582021-03-31 18:36:32 +0000960 DAWN_TRY(mEncodingContext.Finish());
961 DAWN_TRY(device->ValidateIsAlive());
962
963 if (device->IsValidationEnabled()) {
Corentin Wallez2dd2d672021-05-05 15:41:13 +0000964 DAWN_TRY(ValidateFinish());
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000965 }
Austin Eng4b0b7a52019-11-21 22:09:41 +0000966 return device->CreateCommandBuffer(this, descriptor);
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000967 }
968
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000969 // Implementation of the command buffer validation that can be precomputed before submit
Corentin Wallez2dd2d672021-05-05 15:41:13 +0000970 MaybeError CommandEncoder::ValidateFinish() const {
Corentin Wallez321c1222019-11-13 17:00:37 +0000971 TRACE_EVENT0(GetDevice()->GetPlatform(), Validation, "CommandEncoder::ValidateFinish");
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000972 DAWN_TRY(GetDevice()->ValidateObject(this));
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000973
Corentin Wallezec7ea6a2021-05-05 19:55:23 +0000974 for (const RenderPassResourceUsage& passUsage : mEncodingContext.GetRenderPassUsages()) {
Corentin Wallez2dd2d672021-05-05 15:41:13 +0000975 DAWN_TRY(ValidateSyncScopeResourceUsage(passUsage));
Austin Eng4b0b7a52019-11-21 22:09:41 +0000976 }
Corentin Wallez76732ab2021-05-06 19:20:14 +0000977
978 for (const ComputePassResourceUsage& passUsage : mEncodingContext.GetComputePassUsages()) {
979 for (const SyncScopeResourceUsage& scope : passUsage.dispatchUsages) {
980 DAWN_TRY(ValidateSyncScopeResourceUsage(scope));
981 }
982 }
Corentin Wallezf20f5b92019-02-20 11:46:16 +0000983
Corentin Wallez7ffaa212021-01-27 16:03:32 +0000984 if (mDebugGroupStackSize != 0) {
985 return DAWN_VALIDATION_ERROR("Each Push must be balanced by a corresponding Pop.");
986 }
Corentin Wallezb8ea84c2019-09-10 08:20:40 +0000987
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000988 return {};
989 }
990
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000991} // namespace dawn_native