2017-04-20 18:38:20 +00:00
|
|
|
// Copyright 2017 The NXT Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/CommandBuffer.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/BindGroup.h"
|
|
|
|
#include "backend/Buffer.h"
|
|
|
|
#include "backend/CommandBufferStateTracker.h"
|
2017-11-24 18:59:42 +00:00
|
|
|
#include "backend/Commands.h"
|
2017-07-14 14:58:07 +00:00
|
|
|
#include "backend/ComputePipeline.h"
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/Device.h"
|
|
|
|
#include "backend/InputState.h"
|
|
|
|
#include "backend/PipelineLayout.h"
|
2017-07-14 14:58:07 +00:00
|
|
|
#include "backend/RenderPipeline.h"
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/Texture.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace backend {
|
|
|
|
|
2017-06-26 15:43:51 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
bool ValidateCopyLocationFitsInTexture(CommandBufferBuilder* builder,
|
|
|
|
const TextureCopyLocation& location) {
|
2017-06-26 15:43:51 +00:00
|
|
|
const TextureBase* texture = location.texture.Get();
|
|
|
|
if (location.level >= texture->GetNumMipLevels()) {
|
|
|
|
builder->HandleError("Copy mip-level out of range");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
// All texture dimensions are in uint32_t so by doing checks in uint64_t we avoid
|
|
|
|
// overflows.
|
2017-06-26 15:43:51 +00:00
|
|
|
uint64_t level = location.level;
|
2017-11-24 18:59:42 +00:00
|
|
|
if (uint64_t(location.x) + uint64_t(location.width) >
|
|
|
|
(static_cast<uint64_t>(texture->GetWidth()) >> level) ||
|
|
|
|
uint64_t(location.y) + uint64_t(location.height) >
|
|
|
|
(static_cast<uint64_t>(texture->GetHeight()) >> level)) {
|
2017-06-26 15:43:51 +00:00
|
|
|
builder->HandleError("Copy would touch outside of the texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
// TODO(cwallez@chromium.org): Check the depth bound differently for 2D arrays and 3D
|
|
|
|
// textures
|
2017-06-26 15:43:51 +00:00
|
|
|
if (location.z != 0 || location.depth != 1) {
|
|
|
|
builder->HandleError("No support for z != 0 and depth != 1 for now");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FitsInBuffer(const BufferBase* buffer, uint32_t offset, uint32_t size) {
|
|
|
|
uint32_t bufferSize = buffer->GetSize();
|
|
|
|
return offset <= bufferSize && (size <= (bufferSize - offset));
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
bool ValidateCopySizeFitsInBuffer(CommandBufferBuilder* builder,
|
|
|
|
const BufferCopyLocation& location,
|
|
|
|
uint32_t dataSize) {
|
2017-06-26 15:43:51 +00:00
|
|
|
if (!FitsInBuffer(location.buffer.Get(), location.offset, dataSize)) {
|
|
|
|
builder->HandleError("Copy would overflow the buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
bool ValidateTexelBufferOffset(CommandBufferBuilder* builder,
|
|
|
|
TextureBase* texture,
|
|
|
|
const BufferCopyLocation& location) {
|
|
|
|
uint32_t texelSize =
|
|
|
|
static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat()));
|
2017-07-18 17:18:58 +00:00
|
|
|
if (location.offset % texelSize != 0) {
|
|
|
|
builder->HandleError("Buffer offset must be a multiple of the texel size");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
bool ComputeTextureCopyBufferSize(CommandBufferBuilder*,
|
|
|
|
const TextureCopyLocation& location,
|
|
|
|
uint32_t rowPitch,
|
|
|
|
uint32_t* bufferSize) {
|
2017-06-26 15:43:51 +00:00
|
|
|
// TODO(cwallez@chromium.org): check for overflows
|
2017-07-13 14:23:26 +00:00
|
|
|
*bufferSize = (rowPitch * (location.height - 1) + location.width) * location.depth;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ComputeDefaultRowPitch(TextureBase* texture, uint32_t width) {
|
2017-07-22 00:00:22 +00:00
|
|
|
uint32_t texelSize = TextureFormatPixelSize(texture->GetFormat());
|
2017-07-13 14:23:26 +00:00
|
|
|
return texelSize * width;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
bool ValidateRowPitch(CommandBufferBuilder* builder,
|
|
|
|
const TextureCopyLocation& location,
|
|
|
|
uint32_t rowPitch) {
|
2017-07-13 14:23:26 +00:00
|
|
|
if (rowPitch % kTextureRowPitchAlignment != 0) {
|
|
|
|
builder->HandleError("Row pitch must be a multiple of 256");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:00:22 +00:00
|
|
|
uint32_t texelSize = TextureFormatPixelSize(location.texture.Get()->GetFormat());
|
2017-07-13 14:23:26 +00:00
|
|
|
if (rowPitch < location.width * texelSize) {
|
|
|
|
builder->HandleError("Row pitch must not be less than the number of bytes per row");
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-26 15:43:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
} // namespace
|
2017-06-26 15:43:51 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
CommandBufferBase::CommandBufferBase(CommandBufferBuilder* builder)
|
2017-11-23 18:32:51 +00:00
|
|
|
: mDevice(builder->mDevice),
|
|
|
|
mBuffersTransitioned(std::move(builder->mState->mBuffersTransitioned)),
|
|
|
|
mTexturesTransitioned(std::move(builder->mState->mTexturesTransitioned)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandBufferBase::ValidateResourceUsagesImmediate() {
|
2017-11-23 18:32:51 +00:00
|
|
|
for (auto buffer : mBuffersTransitioned) {
|
2017-04-20 18:38:20 +00:00
|
|
|
if (buffer->IsFrozen()) {
|
2017-11-23 18:32:51 +00:00
|
|
|
mDevice->HandleError("Command buffer: cannot transition buffer with frozen usage");
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-11-23 18:32:51 +00:00
|
|
|
for (auto texture : mTexturesTransitioned) {
|
2017-04-20 18:38:20 +00:00
|
|
|
if (texture->IsFrozen()) {
|
2017-11-23 18:32:51 +00:00
|
|
|
mDevice->HandleError("Command buffer: cannot transition texture with frozen usage");
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-12 00:49:20 +00:00
|
|
|
DeviceBase* CommandBufferBase::GetDevice() {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDevice;
|
2017-07-12 00:49:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
CommandBufferBuilder::CommandBufferBuilder(DeviceBase* device)
|
|
|
|
: Builder(device), mState(std::make_unique<CommandBufferStateTracker>(this)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandBufferBuilder::~CommandBufferBuilder() {
|
2017-11-23 18:32:51 +00:00
|
|
|
if (!mWereCommandsAcquired) {
|
2017-04-20 18:38:20 +00:00
|
|
|
MoveToIterator();
|
2017-11-23 18:32:51 +00:00
|
|
|
FreeCommands(&mIterator);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandBufferBuilder::ValidateGetResult() {
|
|
|
|
MoveToIterator();
|
|
|
|
|
|
|
|
Command type;
|
2017-11-23 18:32:51 +00:00
|
|
|
while (mIterator.NextCommandId(&type)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (type) {
|
2017-11-24 18:59:42 +00:00
|
|
|
case Command::BeginComputePass: {
|
|
|
|
mIterator.NextCommand<BeginComputePassCmd>();
|
|
|
|
if (!mState->BeginComputePass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::BeginRenderPass: {
|
|
|
|
BeginRenderPassCmd* cmd = mIterator.NextCommand<BeginRenderPassCmd>();
|
2018-05-11 17:04:44 +00:00
|
|
|
RenderPassDescriptorBase* info = cmd->info.Get();
|
2018-05-02 22:10:13 +00:00
|
|
|
if (!mState->BeginRenderPass(info)) {
|
2017-11-24 18:59:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::CopyBufferToBuffer: {
|
|
|
|
CopyBufferToBufferCmd* copy = mIterator.NextCommand<CopyBufferToBufferCmd>();
|
|
|
|
if (!ValidateCopySizeFitsInBuffer(this, copy->source, copy->size) ||
|
|
|
|
!ValidateCopySizeFitsInBuffer(this, copy->destination, copy->size) ||
|
|
|
|
!mState->ValidateCanCopy() ||
|
|
|
|
!mState->ValidateCanUseBufferAs(copy->source.buffer.Get(),
|
|
|
|
nxt::BufferUsageBit::TransferSrc) ||
|
|
|
|
!mState->ValidateCanUseBufferAs(copy->destination.buffer.Get(),
|
|
|
|
nxt::BufferUsageBit::TransferDst)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::CopyBufferToTexture: {
|
|
|
|
CopyBufferToTextureCmd* copy = mIterator.NextCommand<CopyBufferToTextureCmd>();
|
|
|
|
|
|
|
|
uint32_t bufferCopySize = 0;
|
|
|
|
if (!ValidateRowPitch(this, copy->destination, copy->rowPitch) ||
|
|
|
|
!ComputeTextureCopyBufferSize(this, copy->destination, copy->rowPitch,
|
|
|
|
&bufferCopySize) ||
|
|
|
|
!ValidateCopyLocationFitsInTexture(this, copy->destination) ||
|
|
|
|
!ValidateCopySizeFitsInBuffer(this, copy->source, bufferCopySize) ||
|
|
|
|
!ValidateTexelBufferOffset(this, copy->destination.texture.Get(),
|
|
|
|
copy->source) ||
|
|
|
|
!mState->ValidateCanCopy() ||
|
|
|
|
!mState->ValidateCanUseBufferAs(copy->source.buffer.Get(),
|
|
|
|
nxt::BufferUsageBit::TransferSrc) ||
|
|
|
|
!mState->ValidateCanUseTextureAs(copy->destination.texture.Get(),
|
|
|
|
nxt::TextureUsageBit::TransferDst)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::CopyTextureToBuffer: {
|
|
|
|
CopyTextureToBufferCmd* copy = mIterator.NextCommand<CopyTextureToBufferCmd>();
|
|
|
|
|
|
|
|
uint32_t bufferCopySize = 0;
|
|
|
|
if (!ValidateRowPitch(this, copy->source, copy->rowPitch) ||
|
|
|
|
!ComputeTextureCopyBufferSize(this, copy->source, copy->rowPitch,
|
|
|
|
&bufferCopySize) ||
|
|
|
|
!ValidateCopyLocationFitsInTexture(this, copy->source) ||
|
|
|
|
!ValidateCopySizeFitsInBuffer(this, copy->destination, bufferCopySize) ||
|
|
|
|
!ValidateTexelBufferOffset(this, copy->source.texture.Get(),
|
|
|
|
copy->destination) ||
|
|
|
|
!mState->ValidateCanCopy() ||
|
|
|
|
!mState->ValidateCanUseTextureAs(copy->source.texture.Get(),
|
|
|
|
nxt::TextureUsageBit::TransferSrc) ||
|
|
|
|
!mState->ValidateCanUseBufferAs(copy->destination.buffer.Get(),
|
|
|
|
nxt::BufferUsageBit::TransferDst)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::Dispatch: {
|
|
|
|
mIterator.NextCommand<DispatchCmd>();
|
|
|
|
if (!mState->ValidateCanDispatch()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::DrawArrays: {
|
|
|
|
mIterator.NextCommand<DrawArraysCmd>();
|
|
|
|
if (!mState->ValidateCanDrawArrays()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::DrawElements: {
|
|
|
|
mIterator.NextCommand<DrawElementsCmd>();
|
|
|
|
if (!mState->ValidateCanDrawElements()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::EndComputePass: {
|
|
|
|
mIterator.NextCommand<EndComputePassCmd>();
|
|
|
|
if (!mState->EndComputePass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::EndRenderPass: {
|
|
|
|
mIterator.NextCommand<EndRenderPassCmd>();
|
|
|
|
if (!mState->EndRenderPass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::SetComputePipeline: {
|
|
|
|
SetComputePipelineCmd* cmd = mIterator.NextCommand<SetComputePipelineCmd>();
|
|
|
|
ComputePipelineBase* pipeline = cmd->pipeline.Get();
|
|
|
|
if (!mState->SetComputePipeline(pipeline)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::SetRenderPipeline: {
|
|
|
|
SetRenderPipelineCmd* cmd = mIterator.NextCommand<SetRenderPipelineCmd>();
|
|
|
|
RenderPipelineBase* pipeline = cmd->pipeline.Get();
|
|
|
|
if (!mState->SetRenderPipeline(pipeline)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::SetPushConstants: {
|
|
|
|
SetPushConstantsCmd* cmd = mIterator.NextCommand<SetPushConstantsCmd>();
|
|
|
|
mIterator.NextData<uint32_t>(cmd->count);
|
|
|
|
// Validation of count and offset has already been done when the command was
|
|
|
|
// recorded because it impacts the size of an allocation in the
|
|
|
|
// CommandAllocator.
|
|
|
|
if (!mState->ValidateSetPushConstants(cmd->stages)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::SetStencilReference: {
|
|
|
|
mIterator.NextCommand<SetStencilReferenceCmd>();
|
2018-05-02 22:10:13 +00:00
|
|
|
if (!mState->HaveRenderPass()) {
|
|
|
|
HandleError("Can't set stencil reference without an active render pass");
|
2017-11-24 18:59:42 +00:00
|
|
|
return false;
|
2017-07-10 21:07:24 +00:00
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::SetBlendColor: {
|
|
|
|
mIterator.NextCommand<SetBlendColorCmd>();
|
2018-05-02 22:10:13 +00:00
|
|
|
if (!mState->HaveRenderPass()) {
|
|
|
|
HandleError("Can't set blend color without an active render pass");
|
2017-11-24 18:59:42 +00:00
|
|
|
return false;
|
2017-08-01 18:22:52 +00:00
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
} break;
|
|
|
|
|
2018-02-13 19:57:13 +00:00
|
|
|
case Command::SetScissorRect: {
|
|
|
|
mIterator.NextCommand<SetScissorRectCmd>();
|
2018-05-02 22:10:13 +00:00
|
|
|
if (!mState->HaveRenderPass()) {
|
|
|
|
HandleError("Can't set scissor rect without an active render pass");
|
2018-02-13 19:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
case Command::SetBindGroup: {
|
|
|
|
SetBindGroupCmd* cmd = mIterator.NextCommand<SetBindGroupCmd>();
|
|
|
|
if (!mState->SetBindGroup(cmd->index, cmd->group.Get())) {
|
|
|
|
return false;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case Command::SetIndexBuffer: {
|
|
|
|
SetIndexBufferCmd* cmd = mIterator.NextCommand<SetIndexBufferCmd>();
|
|
|
|
if (!mState->SetIndexBuffer(cmd->buffer.Get())) {
|
|
|
|
return false;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
} break;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
case Command::SetVertexBuffers: {
|
|
|
|
SetVertexBuffersCmd* cmd = mIterator.NextCommand<SetVertexBuffersCmd>();
|
|
|
|
auto buffers = mIterator.NextData<Ref<BufferBase>>(cmd->count);
|
|
|
|
mIterator.NextData<uint32_t>(cmd->count);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
for (uint32_t i = 0; i < cmd->count; ++i) {
|
|
|
|
mState->SetVertexBuffer(cmd->startSlot + i, buffers[i].Get());
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
} break;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
case Command::TransitionBufferUsage: {
|
|
|
|
TransitionBufferUsageCmd* cmd =
|
|
|
|
mIterator.NextCommand<TransitionBufferUsageCmd>();
|
|
|
|
if (!mState->TransitionBufferUsage(cmd->buffer.Get(), cmd->usage)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} break;
|
2017-05-16 21:04:22 +00:00
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
case Command::TransitionTextureUsage: {
|
|
|
|
TransitionTextureUsageCmd* cmd =
|
|
|
|
mIterator.NextCommand<TransitionTextureUsageCmd>();
|
|
|
|
if (!mState->TransitionTextureUsage(cmd->texture.Get(), cmd->usage)) {
|
|
|
|
return false;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
|
|
|
|
} break;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
if (!mState->ValidateEndCommandBuffer()) {
|
2017-06-09 01:25:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandIterator CommandBufferBuilder::AcquireCommands() {
|
2017-11-23 18:32:51 +00:00
|
|
|
ASSERT(!mWereCommandsAcquired);
|
|
|
|
mWereCommandsAcquired = true;
|
|
|
|
return std::move(mIterator);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
CommandBufferBase* CommandBufferBuilder::GetResultImpl() {
|
2017-04-20 18:38:20 +00:00
|
|
|
MoveToIterator();
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDevice->CreateCommandBuffer(this);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 21:07:24 +00:00
|
|
|
void CommandBufferBuilder::BeginComputePass() {
|
2017-11-23 18:32:51 +00:00
|
|
|
mAllocator.Allocate<BeginComputePassCmd>(Command::BeginComputePass);
|
2017-05-16 21:04:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 17:04:44 +00:00
|
|
|
void CommandBufferBuilder::BeginRenderPass(RenderPassDescriptorBase* info) {
|
2017-11-23 18:32:51 +00:00
|
|
|
BeginRenderPassCmd* cmd = mAllocator.Allocate<BeginRenderPassCmd>(Command::BeginRenderPass);
|
2017-11-24 18:59:42 +00:00
|
|
|
new (cmd) BeginRenderPassCmd;
|
2018-05-02 22:10:13 +00:00
|
|
|
cmd->info = info;
|
2017-07-10 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::CopyBufferToBuffer(BufferBase* source,
|
|
|
|
uint32_t sourceOffset,
|
|
|
|
BufferBase* destination,
|
|
|
|
uint32_t destinationOffset,
|
|
|
|
uint32_t size) {
|
|
|
|
CopyBufferToBufferCmd* copy =
|
|
|
|
mAllocator.Allocate<CopyBufferToBufferCmd>(Command::CopyBufferToBuffer);
|
|
|
|
new (copy) CopyBufferToBufferCmd;
|
2017-06-26 15:43:51 +00:00
|
|
|
copy->source.buffer = source;
|
|
|
|
copy->source.offset = sourceOffset;
|
|
|
|
copy->destination.buffer = destination;
|
|
|
|
copy->destination.offset = destinationOffset;
|
2017-06-12 22:12:29 +00:00
|
|
|
copy->size = size;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::CopyBufferToTexture(BufferBase* buffer,
|
|
|
|
uint32_t bufferOffset,
|
|
|
|
uint32_t rowPitch,
|
|
|
|
TextureBase* texture,
|
|
|
|
uint32_t x,
|
|
|
|
uint32_t y,
|
|
|
|
uint32_t z,
|
|
|
|
uint32_t width,
|
|
|
|
uint32_t height,
|
|
|
|
uint32_t depth,
|
|
|
|
uint32_t level) {
|
2017-07-13 14:23:26 +00:00
|
|
|
if (rowPitch == 0) {
|
|
|
|
rowPitch = ComputeDefaultRowPitch(texture, width);
|
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
CopyBufferToTextureCmd* copy =
|
|
|
|
mAllocator.Allocate<CopyBufferToTextureCmd>(Command::CopyBufferToTexture);
|
|
|
|
new (copy) CopyBufferToTextureCmd;
|
2017-06-26 15:43:51 +00:00
|
|
|
copy->source.buffer = buffer;
|
|
|
|
copy->source.offset = bufferOffset;
|
|
|
|
copy->destination.texture = texture;
|
|
|
|
copy->destination.x = x;
|
|
|
|
copy->destination.y = y;
|
|
|
|
copy->destination.z = z;
|
|
|
|
copy->destination.width = width;
|
|
|
|
copy->destination.height = height;
|
|
|
|
copy->destination.depth = depth;
|
|
|
|
copy->destination.level = level;
|
2017-07-13 14:04:23 +00:00
|
|
|
copy->rowPitch = rowPitch;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::CopyTextureToBuffer(TextureBase* texture,
|
|
|
|
uint32_t x,
|
|
|
|
uint32_t y,
|
|
|
|
uint32_t z,
|
|
|
|
uint32_t width,
|
|
|
|
uint32_t height,
|
|
|
|
uint32_t depth,
|
|
|
|
uint32_t level,
|
|
|
|
BufferBase* buffer,
|
|
|
|
uint32_t bufferOffset,
|
|
|
|
uint32_t rowPitch) {
|
2017-07-13 14:23:26 +00:00
|
|
|
if (rowPitch == 0) {
|
|
|
|
rowPitch = ComputeDefaultRowPitch(texture, width);
|
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
CopyTextureToBufferCmd* copy =
|
|
|
|
mAllocator.Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer);
|
|
|
|
new (copy) CopyTextureToBufferCmd;
|
2017-06-26 20:23:03 +00:00
|
|
|
copy->source.texture = texture;
|
|
|
|
copy->source.x = x;
|
|
|
|
copy->source.y = y;
|
|
|
|
copy->source.z = z;
|
|
|
|
copy->source.width = width;
|
|
|
|
copy->source.height = height;
|
|
|
|
copy->source.depth = depth;
|
|
|
|
copy->source.level = level;
|
|
|
|
copy->destination.buffer = buffer;
|
|
|
|
copy->destination.offset = bufferOffset;
|
2017-07-13 14:04:23 +00:00
|
|
|
copy->rowPitch = rowPitch;
|
2017-06-26 20:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
void CommandBufferBuilder::Dispatch(uint32_t x, uint32_t y, uint32_t z) {
|
2017-11-23 18:32:51 +00:00
|
|
|
DispatchCmd* dispatch = mAllocator.Allocate<DispatchCmd>(Command::Dispatch);
|
2017-11-24 18:59:42 +00:00
|
|
|
new (dispatch) DispatchCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
dispatch->x = x;
|
|
|
|
dispatch->y = y;
|
|
|
|
dispatch->z = z;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::DrawArrays(uint32_t vertexCount,
|
|
|
|
uint32_t instanceCount,
|
|
|
|
uint32_t firstVertex,
|
|
|
|
uint32_t firstInstance) {
|
2017-11-23 18:32:51 +00:00
|
|
|
DrawArraysCmd* draw = mAllocator.Allocate<DrawArraysCmd>(Command::DrawArrays);
|
2017-11-24 18:59:42 +00:00
|
|
|
new (draw) DrawArraysCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
draw->vertexCount = vertexCount;
|
|
|
|
draw->instanceCount = instanceCount;
|
|
|
|
draw->firstVertex = firstVertex;
|
|
|
|
draw->firstInstance = firstInstance;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::DrawElements(uint32_t indexCount,
|
|
|
|
uint32_t instanceCount,
|
|
|
|
uint32_t firstIndex,
|
|
|
|
uint32_t firstInstance) {
|
2017-11-23 18:32:51 +00:00
|
|
|
DrawElementsCmd* draw = mAllocator.Allocate<DrawElementsCmd>(Command::DrawElements);
|
2017-11-24 18:59:42 +00:00
|
|
|
new (draw) DrawElementsCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
draw->indexCount = indexCount;
|
|
|
|
draw->instanceCount = instanceCount;
|
|
|
|
draw->firstIndex = firstIndex;
|
|
|
|
draw->firstInstance = firstInstance;
|
|
|
|
}
|
|
|
|
|
2017-07-10 21:07:24 +00:00
|
|
|
void CommandBufferBuilder::EndComputePass() {
|
2017-11-23 18:32:51 +00:00
|
|
|
mAllocator.Allocate<EndComputePassCmd>(Command::EndComputePass);
|
2017-07-10 21:07:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 21:04:22 +00:00
|
|
|
void CommandBufferBuilder::EndRenderPass() {
|
2017-11-23 18:32:51 +00:00
|
|
|
mAllocator.Allocate<EndRenderPassCmd>(Command::EndRenderPass);
|
2017-05-16 21:04:22 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 14:58:07 +00:00
|
|
|
void CommandBufferBuilder::SetComputePipeline(ComputePipelineBase* pipeline) {
|
2017-11-24 18:59:42 +00:00
|
|
|
SetComputePipelineCmd* cmd =
|
|
|
|
mAllocator.Allocate<SetComputePipelineCmd>(Command::SetComputePipeline);
|
|
|
|
new (cmd) SetComputePipelineCmd;
|
2017-07-14 14:58:07 +00:00
|
|
|
cmd->pipeline = pipeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferBuilder::SetRenderPipeline(RenderPipelineBase* pipeline) {
|
2017-11-24 18:59:42 +00:00
|
|
|
SetRenderPipelineCmd* cmd =
|
|
|
|
mAllocator.Allocate<SetRenderPipelineCmd>(Command::SetRenderPipeline);
|
|
|
|
new (cmd) SetRenderPipelineCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
cmd->pipeline = pipeline;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::SetPushConstants(nxt::ShaderStageBit stages,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t count,
|
|
|
|
const void* data) {
|
2017-07-20 15:01:37 +00:00
|
|
|
// TODO(cwallez@chromium.org): check for overflows
|
2017-04-20 18:38:20 +00:00
|
|
|
if (offset + count > kMaxPushConstants) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting too many push constants");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
SetPushConstantsCmd* cmd =
|
|
|
|
mAllocator.Allocate<SetPushConstantsCmd>(Command::SetPushConstants);
|
|
|
|
new (cmd) SetPushConstantsCmd;
|
2017-07-20 15:01:37 +00:00
|
|
|
cmd->stages = stages;
|
2017-04-20 18:38:20 +00:00
|
|
|
cmd->offset = offset;
|
|
|
|
cmd->count = count;
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
uint32_t* values = mAllocator.AllocateData<uint32_t>(count);
|
2017-04-20 18:38:20 +00:00
|
|
|
memcpy(values, data, count * sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
2017-06-01 15:30:03 +00:00
|
|
|
void CommandBufferBuilder::SetStencilReference(uint32_t reference) {
|
2017-11-24 18:59:42 +00:00
|
|
|
SetStencilReferenceCmd* cmd =
|
|
|
|
mAllocator.Allocate<SetStencilReferenceCmd>(Command::SetStencilReference);
|
|
|
|
new (cmd) SetStencilReferenceCmd;
|
2017-06-01 15:30:03 +00:00
|
|
|
cmd->reference = reference;
|
2017-05-30 22:13:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 18:22:52 +00:00
|
|
|
void CommandBufferBuilder::SetBlendColor(float r, float g, float b, float a) {
|
2017-11-23 18:32:51 +00:00
|
|
|
SetBlendColorCmd* cmd = mAllocator.Allocate<SetBlendColorCmd>(Command::SetBlendColor);
|
2017-11-24 18:59:42 +00:00
|
|
|
new (cmd) SetBlendColorCmd;
|
2017-08-01 18:22:52 +00:00
|
|
|
cmd->r = r;
|
|
|
|
cmd->g = g;
|
|
|
|
cmd->b = b;
|
|
|
|
cmd->a = a;
|
|
|
|
}
|
|
|
|
|
2018-02-13 19:57:13 +00:00
|
|
|
void CommandBufferBuilder::SetScissorRect(uint32_t x,
|
|
|
|
uint32_t y,
|
|
|
|
uint32_t width,
|
|
|
|
uint32_t height) {
|
|
|
|
SetScissorRectCmd* cmd = mAllocator.Allocate<SetScissorRectCmd>(Command::SetScissorRect);
|
|
|
|
new (cmd) SetScissorRectCmd;
|
|
|
|
cmd->x = x;
|
|
|
|
cmd->y = y;
|
|
|
|
cmd->width = width;
|
|
|
|
cmd->height = height;
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
void CommandBufferBuilder::SetBindGroup(uint32_t groupIndex, BindGroupBase* group) {
|
|
|
|
if (groupIndex >= kMaxBindGroups) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting bind group over the max");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
SetBindGroupCmd* cmd = mAllocator.Allocate<SetBindGroupCmd>(Command::SetBindGroup);
|
2017-11-24 18:59:42 +00:00
|
|
|
new (cmd) SetBindGroupCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
cmd->index = groupIndex;
|
|
|
|
cmd->group = group;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:38:05 +00:00
|
|
|
void CommandBufferBuilder::SetIndexBuffer(BufferBase* buffer, uint32_t offset) {
|
2017-04-20 18:38:20 +00:00
|
|
|
// TODO(kainino@chromium.org): validation
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
SetIndexBufferCmd* cmd = mAllocator.Allocate<SetIndexBufferCmd>(Command::SetIndexBuffer);
|
2017-11-24 18:59:42 +00:00
|
|
|
new (cmd) SetIndexBufferCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
cmd->buffer = buffer;
|
|
|
|
cmd->offset = offset;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::SetVertexBuffers(uint32_t startSlot,
|
|
|
|
uint32_t count,
|
|
|
|
BufferBase* const* buffers,
|
|
|
|
uint32_t const* offsets) {
|
2017-04-20 18:38:20 +00:00
|
|
|
// TODO(kainino@chromium.org): validation
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
SetVertexBuffersCmd* cmd =
|
|
|
|
mAllocator.Allocate<SetVertexBuffersCmd>(Command::SetVertexBuffers);
|
|
|
|
new (cmd) SetVertexBuffersCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
cmd->startSlot = startSlot;
|
|
|
|
cmd->count = count;
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
Ref<BufferBase>* cmdBuffers = mAllocator.AllocateData<Ref<BufferBase>>(count);
|
2017-04-20 18:38:20 +00:00
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2017-11-24 18:59:42 +00:00
|
|
|
new (&cmdBuffers[i]) Ref<BufferBase>(buffers[i]);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
uint32_t* cmdOffsets = mAllocator.AllocateData<uint32_t>(count);
|
2017-04-20 18:38:20 +00:00
|
|
|
memcpy(cmdOffsets, offsets, count * sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::TransitionBufferUsage(BufferBase* buffer,
|
|
|
|
nxt::BufferUsageBit usage) {
|
|
|
|
TransitionBufferUsageCmd* cmd =
|
|
|
|
mAllocator.Allocate<TransitionBufferUsageCmd>(Command::TransitionBufferUsage);
|
|
|
|
new (cmd) TransitionBufferUsageCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
cmd->buffer = buffer;
|
|
|
|
cmd->usage = usage;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void CommandBufferBuilder::TransitionTextureUsage(TextureBase* texture,
|
|
|
|
nxt::TextureUsageBit usage) {
|
|
|
|
TransitionTextureUsageCmd* cmd =
|
|
|
|
mAllocator.Allocate<TransitionTextureUsageCmd>(Command::TransitionTextureUsage);
|
|
|
|
new (cmd) TransitionTextureUsageCmd;
|
2017-04-20 18:38:20 +00:00
|
|
|
cmd->texture = texture;
|
|
|
|
cmd->usage = usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandBufferBuilder::MoveToIterator() {
|
2017-11-23 18:32:51 +00:00
|
|
|
if (!mWasMovedToIterator) {
|
|
|
|
mIterator = std::move(mAllocator);
|
|
|
|
mWasMovedToIterator = true;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
} // namespace backend
|