blob: 32ea15375d4d3c87849fd1f9d53e9eb28b7bd2bf [file] [log] [blame]
// Copyright 2020 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 "tests/unittests/validation/ValidationTest.h"
#include "utils/ComboRenderBundleEncoderDescriptor.h"
class IndexBufferValidationTest : public ValidationTest {};
// Test that for OOB validation of index buffer offset and size.
TEST_F(IndexBufferValidationTest, IndexBufferOffsetOOBValidation) {
wgpu::BufferDescriptor bufferDesc;
bufferDesc.usage = wgpu::BufferUsage::Index;
bufferDesc.size = 256;
wgpu::Buffer buffer = device.CreateBuffer(&bufferDesc);
DummyRenderPass renderPass(device);
// Control case, using the full buffer, with or without an explicit size is valid.
{
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
// Explicit size
pass.SetIndexBuffer(buffer, 0, 256);
// Implicit size
pass.SetIndexBuffer(buffer, 0, 0);
pass.SetIndexBuffer(buffer, 256 - 4, 0);
pass.SetIndexBuffer(buffer, 4, 0);
// Implicit size of zero
pass.SetIndexBuffer(buffer, 256, 0);
pass.EndPass();
encoder.Finish();
}
// Bad case, offset + size is larger than the buffer
{
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetIndexBuffer(buffer, 4, 256);
pass.EndPass();
ASSERT_DEVICE_ERROR(encoder.Finish());
}
// Bad case, size is 0 but the offset is larger than the buffer
{
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetIndexBuffer(buffer, 256 + 4, 0);
pass.EndPass();
ASSERT_DEVICE_ERROR(encoder.Finish());
}
utils::ComboRenderBundleEncoderDescriptor renderBundleDesc = {};
renderBundleDesc.colorFormatsCount = 1;
renderBundleDesc.cColorFormats[0] = wgpu::TextureFormat::RGBA8Unorm;
// Control case, using the full buffer, with or without an explicit size is valid.
{
wgpu::RenderBundleEncoder encoder = device.CreateRenderBundleEncoder(&renderBundleDesc);
// Explicit size
encoder.SetIndexBuffer(buffer, 0, 256);
// Implicit size
encoder.SetIndexBuffer(buffer, 0, 0);
encoder.SetIndexBuffer(buffer, 256 - 4, 0);
encoder.SetIndexBuffer(buffer, 4, 0);
// Implicit size of zero
encoder.SetIndexBuffer(buffer, 256, 0);
encoder.Finish();
}
// Bad case, offset + size is larger than the buffer
{
wgpu::RenderBundleEncoder encoder = device.CreateRenderBundleEncoder(&renderBundleDesc);
encoder.SetIndexBuffer(buffer, 4, 256);
ASSERT_DEVICE_ERROR(encoder.Finish());
}
// Bad case, size is 0 but the offset is larger than the buffer
{
wgpu::RenderBundleEncoder encoder = device.CreateRenderBundleEncoder(&renderBundleDesc);
encoder.SetIndexBuffer(buffer, 256 + 4, 0);
ASSERT_DEVICE_ERROR(encoder.Finish());
}
}