2019-07-20 01:34:56 +00:00
|
|
|
// Copyright 2019 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/RenderEncoderBase.h"
|
|
|
|
|
|
|
|
#include "common/Constants.h"
|
|
|
|
#include "dawn_native/Buffer.h"
|
|
|
|
#include "dawn_native/CommandEncoder.h"
|
|
|
|
#include "dawn_native/Commands.h"
|
|
|
|
#include "dawn_native/Device.h"
|
|
|
|
#include "dawn_native/RenderPipeline.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
2019-07-23 17:04:34 +00:00
|
|
|
#include <cstring>
|
2019-07-20 01:34:56 +00:00
|
|
|
|
|
|
|
namespace dawn_native {
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
RenderEncoderBase::RenderEncoderBase(DeviceBase* device, EncodingContext* encodingContext)
|
|
|
|
: ProgrammablePassEncoder(device, encodingContext) {
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderEncoderBase::RenderEncoderBase(DeviceBase* device,
|
2019-07-24 18:15:24 +00:00
|
|
|
EncodingContext* encodingContext,
|
2019-07-20 01:34:56 +00:00
|
|
|
ErrorTag errorTag)
|
2019-07-24 18:15:24 +00:00
|
|
|
: ProgrammablePassEncoder(device, encodingContext, errorTag) {
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::Draw(uint32_t vertexCount,
|
|
|
|
uint32_t instanceCount,
|
|
|
|
uint32_t firstVertex,
|
|
|
|
uint32_t firstInstance) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
DrawCmd* draw = allocator->Allocate<DrawCmd>(Command::Draw);
|
|
|
|
draw->vertexCount = vertexCount;
|
|
|
|
draw->instanceCount = instanceCount;
|
|
|
|
draw->firstVertex = firstVertex;
|
|
|
|
draw->firstInstance = firstInstance;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::DrawIndexed(uint32_t indexCount,
|
|
|
|
uint32_t instanceCount,
|
|
|
|
uint32_t firstIndex,
|
|
|
|
int32_t baseVertex,
|
|
|
|
uint32_t firstInstance) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
DrawIndexedCmd* draw = allocator->Allocate<DrawIndexedCmd>(Command::DrawIndexed);
|
|
|
|
draw->indexCount = indexCount;
|
|
|
|
draw->instanceCount = instanceCount;
|
|
|
|
draw->firstIndex = firstIndex;
|
|
|
|
draw->baseVertex = baseVertex;
|
|
|
|
draw->firstInstance = firstInstance;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::DrawIndirect(BufferBase* indirectBuffer, uint64_t indirectOffset) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(indirectBuffer));
|
|
|
|
|
|
|
|
if (indirectOffset >= indirectBuffer->GetSize() ||
|
|
|
|
indirectOffset + kDrawIndirectSize > indirectBuffer->GetSize()) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Indirect offset out of bounds");
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawIndirectCmd* cmd = allocator->Allocate<DrawIndirectCmd>(Command::DrawIndirect);
|
|
|
|
cmd->indirectBuffer = indirectBuffer;
|
|
|
|
cmd->indirectOffset = indirectOffset;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::DrawIndexedIndirect(BufferBase* indirectBuffer,
|
|
|
|
uint64_t indirectOffset) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(indirectBuffer));
|
|
|
|
|
|
|
|
if ((indirectOffset >= indirectBuffer->GetSize() ||
|
|
|
|
indirectOffset + kDrawIndexedIndirectSize > indirectBuffer->GetSize())) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Indirect offset out of bounds");
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawIndexedIndirectCmd* cmd =
|
|
|
|
allocator->Allocate<DrawIndexedIndirectCmd>(Command::DrawIndexedIndirect);
|
|
|
|
cmd->indirectBuffer = indirectBuffer;
|
|
|
|
cmd->indirectOffset = indirectOffset;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::SetPipeline(RenderPipelineBase* pipeline) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(pipeline));
|
|
|
|
|
|
|
|
SetRenderPipelineCmd* cmd =
|
|
|
|
allocator->Allocate<SetRenderPipelineCmd>(Command::SetRenderPipeline);
|
|
|
|
cmd->pipeline = pipeline;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderEncoderBase::SetIndexBuffer(BufferBase* buffer, uint64_t offset) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(buffer));
|
|
|
|
|
|
|
|
SetIndexBufferCmd* cmd =
|
|
|
|
allocator->Allocate<SetIndexBufferCmd>(Command::SetIndexBuffer);
|
|
|
|
cmd->buffer = buffer;
|
|
|
|
cmd->offset = offset;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 07:29:58 +00:00
|
|
|
void RenderEncoderBase::SetVertexBuffer(uint32_t slot, BufferBase* buffer, uint64_t offset) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext->TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2019-10-10 07:29:58 +00:00
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(buffer));
|
2019-07-20 01:34:56 +00:00
|
|
|
|
2019-10-10 07:29:58 +00:00
|
|
|
SetVertexBufferCmd* cmd =
|
|
|
|
allocator->Allocate<SetVertexBufferCmd>(Command::SetVertexBuffer);
|
|
|
|
cmd->slot = slot;
|
|
|
|
cmd->buffer = buffer;
|
|
|
|
cmd->offset = offset;
|
2019-07-20 01:34:56 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-07-20 01:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dawn_native
|