2018-09-21 00:24:37 +00:00
|
|
|
// Copyright 2018 The Dawn 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.
|
|
|
|
|
|
|
|
#include "dawn_native/RenderPassEncoder.h"
|
|
|
|
|
2019-06-10 20:56:27 +00:00
|
|
|
#include "common/Constants.h"
|
2018-09-21 00:24:37 +00:00
|
|
|
#include "dawn_native/Buffer.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "dawn_native/CommandEncoder.h"
|
2020-07-01 10:48:16 +00:00
|
|
|
#include "dawn_native/CommandValidation.h"
|
2018-09-21 00:24:37 +00:00
|
|
|
#include "dawn_native/Commands.h"
|
2019-02-13 13:09:18 +00:00
|
|
|
#include "dawn_native/Device.h"
|
2020-07-01 10:48:16 +00:00
|
|
|
#include "dawn_native/QuerySet.h"
|
2019-08-13 22:12:54 +00:00
|
|
|
#include "dawn_native/RenderBundle.h"
|
2018-09-21 00:24:37 +00:00
|
|
|
#include "dawn_native/RenderPipeline.h"
|
|
|
|
|
2019-07-04 15:30:59 +00:00
|
|
|
#include <math.h>
|
2019-07-23 17:04:34 +00:00
|
|
|
#include <cstring>
|
2018-12-03 14:28:04 +00:00
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
namespace dawn_native {
|
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
// The usage tracker is passed in here, because it is prepopulated with usages from the
|
|
|
|
// BeginRenderPassCmd. If we had RenderPassEncoder responsible for recording the
|
|
|
|
// command, then this wouldn't be necessary.
|
2019-11-13 17:00:37 +00:00
|
|
|
RenderPassEncoder::RenderPassEncoder(DeviceBase* device,
|
|
|
|
CommandEncoder* commandEncoder,
|
2019-11-21 22:09:41 +00:00
|
|
|
EncodingContext* encodingContext,
|
2020-10-11 18:39:32 +00:00
|
|
|
PassResourceUsageTracker usageTracker,
|
|
|
|
uint32_t renderTargetWidth,
|
|
|
|
uint32_t renderTargetHeight)
|
|
|
|
: RenderEncoderBase(device, encodingContext),
|
|
|
|
mCommandEncoder(commandEncoder),
|
|
|
|
mRenderTargetWidth(renderTargetWidth),
|
|
|
|
mRenderTargetHeight(renderTargetHeight) {
|
2019-11-21 22:09:41 +00:00
|
|
|
mUsageTracker = std::move(usageTracker);
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
RenderPassEncoder::RenderPassEncoder(DeviceBase* device,
|
|
|
|
CommandEncoder* commandEncoder,
|
|
|
|
EncodingContext* encodingContext,
|
|
|
|
ErrorTag errorTag)
|
2019-07-24 18:15:24 +00:00
|
|
|
: RenderEncoderBase(device, encodingContext, errorTag), mCommandEncoder(commandEncoder) {
|
2019-03-05 01:02:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
RenderPassEncoder* RenderPassEncoder::MakeError(DeviceBase* device,
|
|
|
|
CommandEncoder* commandEncoder,
|
|
|
|
EncodingContext* encodingContext) {
|
|
|
|
return new RenderPassEncoder(device, commandEncoder, encodingContext, ObjectBase::kError);
|
2019-03-05 01:02:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void RenderPassEncoder::EndPass() {
|
2019-07-24 18:15:24 +00:00
|
|
|
if (mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
allocator->Allocate<EndRenderPassCmd>(Command::EndRenderPass);
|
2018-11-21 11:19:36 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
})) {
|
2019-11-21 22:09:41 +00:00
|
|
|
mEncodingContext->ExitPass(this, mUsageTracker.AcquireResourceUsage());
|
2019-07-24 18:15:24 +00:00
|
|
|
}
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void RenderPassEncoder::SetStencilReference(uint32_t reference) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
SetStencilReferenceCmd* cmd =
|
|
|
|
allocator->Allocate<SetStencilReferenceCmd>(Command::SetStencilReference);
|
|
|
|
cmd->reference = reference;
|
2018-11-21 11:19:36 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void RenderPassEncoder::SetBlendColor(const Color* color) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
SetBlendColorCmd* cmd = allocator->Allocate<SetBlendColorCmd>(Command::SetBlendColor);
|
|
|
|
cmd->color = *color;
|
2018-11-21 11:19:36 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void RenderPassEncoder::SetViewport(float x,
|
|
|
|
float y,
|
|
|
|
float width,
|
|
|
|
float height,
|
|
|
|
float minDepth,
|
|
|
|
float maxDepth) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
if ((isnan(x) || isnan(y) || isnan(width) || isnan(height) || isnan(minDepth) ||
|
|
|
|
isnan(maxDepth))) {
|
|
|
|
return DAWN_VALIDATION_ERROR("NaN is not allowed.");
|
|
|
|
}
|
|
|
|
|
2020-10-11 18:39:32 +00:00
|
|
|
if (x < 0 || y < 0 || width < 0 || height < 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("X, Y, width and height must be non-negative.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x + width > mRenderTargetWidth || y + height > mRenderTargetHeight) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The viewport must be contained in the render targets");
|
2019-07-24 18:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 18:39:32 +00:00
|
|
|
// Check for depths being in [0, 1] and min <= max in 3 checks instead of 5.
|
|
|
|
if (minDepth < 0 || minDepth > maxDepth || maxDepth > 1) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"minDepth and maxDepth must be in [0, 1] and minDepth <= maxDepth.");
|
2019-07-24 18:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SetViewportCmd* cmd = allocator->Allocate<SetViewportCmd>(Command::SetViewport);
|
|
|
|
cmd->x = x;
|
|
|
|
cmd->y = y;
|
|
|
|
cmd->width = width;
|
|
|
|
cmd->height = height;
|
|
|
|
cmd->minDepth = minDepth;
|
|
|
|
cmd->maxDepth = maxDepth;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-04 15:30:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void RenderPassEncoder::SetScissorRect(uint32_t x,
|
|
|
|
uint32_t y,
|
|
|
|
uint32_t width,
|
|
|
|
uint32_t height) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Width and height must be greater than 0.");
|
|
|
|
}
|
|
|
|
|
|
|
|
SetScissorRectCmd* cmd =
|
|
|
|
allocator->Allocate<SetScissorRectCmd>(Command::SetScissorRect);
|
|
|
|
cmd->x = x;
|
|
|
|
cmd->y = y;
|
|
|
|
cmd->width = width;
|
|
|
|
cmd->height = height;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2018-09-21 00:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void RenderPassEncoder::ExecuteBundles(uint32_t count, RenderBundleBase* const* renderBundles) {
|
2019-08-13 22:12:54 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(renderBundles[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecuteBundlesCmd* cmd =
|
|
|
|
allocator->Allocate<ExecuteBundlesCmd>(Command::ExecuteBundles);
|
|
|
|
cmd->count = count;
|
|
|
|
|
|
|
|
Ref<RenderBundleBase>* bundles = allocator->AllocateData<Ref<RenderBundleBase>>(count);
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
|
|
bundles[i] = renderBundles[i];
|
2019-11-21 22:09:41 +00:00
|
|
|
|
|
|
|
const PassResourceUsage& usages = bundles[i]->GetResourceUsage();
|
|
|
|
for (uint32_t i = 0; i < usages.buffers.size(); ++i) {
|
|
|
|
mUsageTracker.BufferUsedAs(usages.buffers[i], usages.bufferUsages[i]);
|
|
|
|
}
|
2020-05-04 17:10:49 +00:00
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
for (uint32_t i = 0; i < usages.textures.size(); ++i) {
|
2020-05-04 17:10:49 +00:00
|
|
|
mUsageTracker.AddTextureUsage(usages.textures[i], usages.textureUsages[i]);
|
2019-11-21 22:09:41 +00:00
|
|
|
}
|
2019-08-13 22:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-01 10:48:16 +00:00
|
|
|
void RenderPassEncoder::WriteTimestamp(QuerySetBase* querySet, uint32_t queryIndex) {
|
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(querySet));
|
|
|
|
DAWN_TRY(ValidateTimestampQuery(querySet, queryIndex));
|
|
|
|
mCommandEncoder->TrackUsedQuerySet(querySet);
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteTimestampCmd* cmd =
|
|
|
|
allocator->Allocate<WriteTimestampCmd>(Command::WriteTimestamp);
|
|
|
|
cmd->querySet = querySet;
|
|
|
|
cmd->queryIndex = queryIndex;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
} // namespace dawn_native
|