mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-17 17:05:31 +00:00
Remove the concept of push constants
BUG=dawn:14 Change-Id: I20587081ec806034ce4f90457c3d475a6fbe834d Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/7180 Commit-Queue: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
committed by
Commit Bot service account
parent
839053b90c
commit
8dfc593eb7
@@ -1,416 +0,0 @@
|
||||
// Copyright 2017 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/DawnTest.h"
|
||||
|
||||
#include "common/Assert.h"
|
||||
#include "common/Constants.h"
|
||||
#include "utils/ComboRenderPipelineDescriptor.h"
|
||||
#include "utils/DawnHelpers.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
class PushConstantTest: public DawnTest {
|
||||
protected:
|
||||
// Layout, bind group and friends to store results for compute tests, can have an extra buffer
|
||||
// so that two different pipeline layout can be created.
|
||||
struct TestBindings {
|
||||
dawn::PipelineLayout layout;
|
||||
dawn::BindGroup bindGroup;
|
||||
dawn::Buffer resultBuffer;
|
||||
};
|
||||
TestBindings MakeTestBindings(bool extraBuffer) {
|
||||
uint32_t one = 1;
|
||||
dawn::Buffer buf1 = utils::CreateBufferFromData(device, &one, 4, dawn::BufferUsageBit::Storage |
|
||||
dawn::BufferUsageBit::TransferSrc |
|
||||
dawn::BufferUsageBit::TransferDst);
|
||||
dawn::BufferDescriptor buf2Desc;
|
||||
buf2Desc.size = 4;
|
||||
buf2Desc.usage = dawn::BufferUsageBit::Storage;
|
||||
dawn::Buffer buf2 = device.CreateBuffer(&buf2Desc);
|
||||
|
||||
dawn::ShaderStageBit kAllStages = dawn::ShaderStageBit::Compute | dawn::ShaderStageBit::Fragment | dawn::ShaderStageBit::Vertex;
|
||||
constexpr dawn::ShaderStageBit kNoStages{};
|
||||
|
||||
dawn::BindGroupLayout bgl = utils::MakeBindGroupLayout(
|
||||
device,
|
||||
{
|
||||
{0, kAllStages, dawn::BindingType::StorageBuffer},
|
||||
{1, extraBuffer ? kAllStages : kNoStages, dawn::BindingType::StorageBuffer},
|
||||
});
|
||||
|
||||
dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
|
||||
|
||||
dawn::BindGroup bg;
|
||||
if (extraBuffer) {
|
||||
bg = utils::MakeBindGroup(device, bgl, {
|
||||
{0, buf1, 0, 4},
|
||||
{1, buf2, 0, 4},
|
||||
});
|
||||
} else {
|
||||
bg = utils::MakeBindGroup(device, bgl, {
|
||||
{0, buf1, 0, 4},
|
||||
});
|
||||
}
|
||||
|
||||
return {std::move(pl), std::move(bg), std::move(buf1)};
|
||||
}
|
||||
|
||||
// A test spec is a bunch of push constant types and expected values
|
||||
enum PushConstantType {
|
||||
Float,
|
||||
Int,
|
||||
UInt,
|
||||
};
|
||||
struct PushConstantSpecItem {
|
||||
PushConstantType type;
|
||||
int value;
|
||||
};
|
||||
using PushConstantSpec = std::vector<PushConstantSpecItem>;
|
||||
|
||||
PushConstantSpec MakeAllZeroSpec() const {
|
||||
PushConstantSpec allZeros;
|
||||
for (uint32_t i = 0; i < kMaxPushConstants; ++i) {
|
||||
allZeros.push_back({Int, 0});
|
||||
}
|
||||
return allZeros;
|
||||
}
|
||||
|
||||
// The GLSL code to define the push constant block for a given test spec
|
||||
std::string MakePushConstantBlock(PushConstantSpec spec) {
|
||||
std::string block = "layout(push_constant) uniform ConstantsBlock {\n";
|
||||
for (size_t i = 0; i < spec.size(); ++i) {
|
||||
block += " ";
|
||||
switch (spec[i].type) {
|
||||
case Float:
|
||||
block += "float";
|
||||
break;
|
||||
case Int:
|
||||
block += "int";
|
||||
break;
|
||||
case UInt:
|
||||
block += "uint";
|
||||
break;
|
||||
}
|
||||
block += " val" + std::to_string(i) + ";\n";
|
||||
}
|
||||
block += "} c;\n";
|
||||
return block;
|
||||
}
|
||||
|
||||
// The GLSL code to define the push constant test for a given test spec
|
||||
std::string MakePushConstantTest(PushConstantSpec spec, std::string varName) {
|
||||
std::string test = "bool " + varName + " = true;\n";
|
||||
for (size_t i = 0; i < spec.size(); ++i) {
|
||||
test += varName + " = " + varName + " && (c.val" + std::to_string(i) + " == ";
|
||||
switch (spec[i].type) {
|
||||
case Float:
|
||||
test += "float";
|
||||
break;
|
||||
case Int:
|
||||
test += "int";
|
||||
break;
|
||||
case UInt:
|
||||
test += "uint";
|
||||
break;
|
||||
}
|
||||
test += "(" + std::to_string(spec[i].value) + "));\n";
|
||||
}
|
||||
return test;
|
||||
}
|
||||
|
||||
// The compute pipeline ANDs the result of the test in the SSBO
|
||||
dawn::ComputePipeline MakeTestComputePipeline(const dawn::PipelineLayout& pl, PushConstantSpec spec) {
|
||||
dawn::ShaderModule module = utils::CreateShaderModule(device, dawn::ShaderStage::Compute, (R"(
|
||||
#version 450
|
||||
layout(set = 0, binding = 0) buffer Result {
|
||||
int success;
|
||||
} result;
|
||||
)" + MakePushConstantBlock(spec) + R"(
|
||||
void main() {
|
||||
)" + MakePushConstantTest(spec, "success") + R"(
|
||||
if (success && result.success == 1) {
|
||||
result.success = 1;
|
||||
} else {
|
||||
result.success = 0;
|
||||
}
|
||||
})").c_str()
|
||||
);
|
||||
|
||||
dawn::ComputePipelineDescriptor descriptor;
|
||||
descriptor.layout = pl;
|
||||
|
||||
dawn::PipelineStageDescriptor computeStage;
|
||||
computeStage.module = module;
|
||||
computeStage.entryPoint = "main";
|
||||
descriptor.computeStage = &computeStage;
|
||||
|
||||
return device.CreateComputePipeline(&descriptor);
|
||||
}
|
||||
|
||||
dawn::PipelineLayout MakeEmptyLayout() {
|
||||
return utils::MakeBasicPipelineLayout(device, nullptr);
|
||||
}
|
||||
|
||||
// The render pipeline adds one to the red channel for successful vertex push constant test
|
||||
// and adds one to green for the frgament test.
|
||||
dawn::RenderPipeline MakeTestRenderPipeline(dawn::PipelineLayout& layout, PushConstantSpec vsSpec, PushConstantSpec fsSpec) {
|
||||
dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, (R"(
|
||||
#version 450
|
||||
)" + MakePushConstantBlock(vsSpec) + R"(
|
||||
layout(location = 0) out float red;
|
||||
void main() {
|
||||
red = 0.0f;
|
||||
)" + MakePushConstantTest(vsSpec, "success") + R"(
|
||||
if (success) {red = 1.0f / 255.0f;}
|
||||
gl_Position = vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
})").c_str()
|
||||
);
|
||||
dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, (R"(
|
||||
#version 450
|
||||
)" + MakePushConstantBlock(fsSpec) + R"(
|
||||
layout(location = 0) out vec4 color;
|
||||
layout(location = 0) in float red;
|
||||
void main() {
|
||||
color = vec4(red, 0.0f, 0.0f, 0.0f);
|
||||
)" + MakePushConstantTest(fsSpec, "success") + R"(
|
||||
if (success) {color.g = 1.0f / 255.0f;}
|
||||
})").c_str()
|
||||
);
|
||||
|
||||
utils::ComboRenderPipelineDescriptor descriptor(device);
|
||||
descriptor.layout = layout;
|
||||
descriptor.cVertexStage.module = vsModule;
|
||||
descriptor.cFragmentStage.module = fsModule;
|
||||
descriptor.primitiveTopology = dawn::PrimitiveTopology::PointList;
|
||||
|
||||
return device.CreateRenderPipeline(&descriptor);
|
||||
}
|
||||
};
|
||||
|
||||
// Test that push constants default to zero at the beginning of every compute passes.
|
||||
TEST_P(PushConstantTest, ComputePassDefaultsToZero) {
|
||||
auto binding = MakeTestBindings(false);
|
||||
|
||||
// Expect push constants to be zero in all dispatches of this test.
|
||||
dawn::ComputePipeline pipeline = MakeTestComputePipeline(binding.layout, MakeAllZeroSpec());
|
||||
|
||||
uint32_t notZero = 42;
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
|
||||
// Test compute push constants are set to zero by default.
|
||||
pass.SetPipeline(pipeline);
|
||||
pass.SetBindGroup(0, binding.bindGroup, 0, nullptr);
|
||||
pass.Dispatch(1, 1, 1);
|
||||
// Set push constants to non-zero value to check they will be reset to zero
|
||||
// on the next BeginComputePass
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, 1, ¬Zero);
|
||||
|
||||
pass.EndPass();
|
||||
}
|
||||
{
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
|
||||
pass.SetPipeline(pipeline);
|
||||
pass.SetBindGroup(0, binding.bindGroup, 0, nullptr);
|
||||
pass.Dispatch(1, 1, 1);
|
||||
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
EXPECT_BUFFER_U32_EQ(1, binding.resultBuffer, 0);
|
||||
}
|
||||
|
||||
// Test that push constants default to zero at the beginning of render passes.
|
||||
TEST_P(PushConstantTest, RenderPassDefaultsToZero) {
|
||||
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, 1, 1);
|
||||
|
||||
// Expect push constants to be zero in all draws of this test.
|
||||
PushConstantSpec allZeros = MakeAllZeroSpec();
|
||||
dawn::PipelineLayout layout = MakeEmptyLayout();
|
||||
dawn::RenderPipeline pipeline = MakeTestRenderPipeline(layout, MakeAllZeroSpec(), MakeAllZeroSpec());
|
||||
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
|
||||
// Test render push constants are set to zero by default.
|
||||
pass.SetPipeline(pipeline);
|
||||
pass.Draw(1, 1, 0, 0);
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
EXPECT_PIXEL_RGBA8_EQ(RGBA8(1, 1, 0, 0), renderPass.color, 0, 0);
|
||||
}
|
||||
|
||||
// Test setting push constants of various 32bit types.
|
||||
TEST_P(PushConstantTest, VariousConstantTypes) {
|
||||
|
||||
struct {
|
||||
int32_t v1;
|
||||
uint32_t v2;
|
||||
float v3;
|
||||
} values = {-1, 3, 4.0f};
|
||||
static_assert(sizeof(values) == 3 * sizeof(uint32_t), "");
|
||||
|
||||
auto binding = MakeTestBindings(false);
|
||||
PushConstantSpec spec = {{Int, -1}, {UInt, 3}, {Float, 4}};
|
||||
dawn::ComputePipeline pipeline = MakeTestComputePipeline(binding.layout, spec);
|
||||
|
||||
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, 3, reinterpret_cast<uint32_t*>(&values));
|
||||
pass.SetPipeline(pipeline);
|
||||
pass.SetBindGroup(0, binding.bindGroup, 0, nullptr);
|
||||
pass.Dispatch(1, 1, 1);
|
||||
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
EXPECT_BUFFER_U32_EQ(1, binding.resultBuffer, 0);
|
||||
}
|
||||
|
||||
// Test that the push constants stay in between pipeline layout changes.
|
||||
TEST_P(PushConstantTest, InheritThroughPipelineLayoutChange) {
|
||||
// These bindings will have a different pipeline layout because binding 2 has an extra buffer.
|
||||
auto binding1 = MakeTestBindings(false);
|
||||
auto binding2 = MakeTestBindings(true);
|
||||
PushConstantSpec spec1 = {{Int, 1}};
|
||||
PushConstantSpec spec2 = {{Int, 2}};
|
||||
dawn::ComputePipeline pipeline1 = MakeTestComputePipeline(binding1.layout, spec1);
|
||||
dawn::ComputePipeline pipeline2 = MakeTestComputePipeline(binding2.layout, spec2);
|
||||
|
||||
uint32_t one = 1;
|
||||
uint32_t two = 2;
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
|
||||
// Set Push constant before there is a pipeline set
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, 1, &one);
|
||||
pass.SetPipeline(pipeline1);
|
||||
pass.SetBindGroup(0, binding1.bindGroup, 0, nullptr);
|
||||
pass.Dispatch(1, 1, 1);
|
||||
// Change the push constant before changing pipeline layout
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, 1, &two);
|
||||
pass.SetPipeline(pipeline2);
|
||||
pass.SetBindGroup(0, binding2.bindGroup, 0, nullptr);
|
||||
pass.Dispatch(1, 1, 1);
|
||||
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
EXPECT_BUFFER_U32_EQ(1, binding1.resultBuffer, 0);
|
||||
EXPECT_BUFFER_U32_EQ(1, binding2.resultBuffer, 0);
|
||||
}
|
||||
|
||||
// Try setting all push constants
|
||||
TEST_P(PushConstantTest, SetAllConstantsToNonZero) {
|
||||
PushConstantSpec spec;
|
||||
std::array<uint32_t, kMaxPushConstants> values;
|
||||
for (uint32_t i = 0; i < kMaxPushConstants; ++i) {
|
||||
spec.push_back({Int, static_cast<int>(i + 1)});
|
||||
values[i] = i + 1;
|
||||
}
|
||||
|
||||
auto binding = MakeTestBindings(false);
|
||||
dawn::ComputePipeline pipeline = MakeTestComputePipeline(binding.layout, spec);
|
||||
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, kMaxPushConstants, &values[0]);
|
||||
pass.SetPipeline(pipeline);
|
||||
pass.SetBindGroup(0, binding.bindGroup, 0, nullptr);
|
||||
pass.Dispatch(1, 1, 1);
|
||||
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
EXPECT_BUFFER_U32_EQ(1, binding.resultBuffer, 0);
|
||||
}
|
||||
|
||||
// Try setting separate push constants for vertex and fragment stage
|
||||
TEST_P(PushConstantTest, SeparateVertexAndFragmentConstants) {
|
||||
PushConstantSpec vsSpec = {{Int, 1}};
|
||||
PushConstantSpec fsSpec = {{Int, 2}};
|
||||
|
||||
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, 1, 1);
|
||||
|
||||
dawn::PipelineLayout layout = MakeEmptyLayout();
|
||||
dawn::RenderPipeline pipeline = MakeTestRenderPipeline(layout, vsSpec, fsSpec);
|
||||
|
||||
uint32_t one = 1;
|
||||
uint32_t two = 2;
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Vertex, 0, 1, &one);
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Fragment, 0, 1, &two);
|
||||
pass.SetPipeline(pipeline);
|
||||
pass.Draw(1, 1, 0, 0);
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
EXPECT_PIXEL_RGBA8_EQ(RGBA8(1, 1, 0, 0), renderPass.color, 0, 0);
|
||||
}
|
||||
|
||||
// Try setting push constants for vertex and fragment stage simulteanously
|
||||
TEST_P(PushConstantTest, SimultaneousVertexAndFragmentConstants) {
|
||||
PushConstantSpec spec = {{Int, 2}};
|
||||
|
||||
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, 1, 1);
|
||||
|
||||
dawn::PipelineLayout layout = MakeEmptyLayout();
|
||||
dawn::RenderPipeline pipeline = MakeTestRenderPipeline(layout, spec, spec);
|
||||
|
||||
uint32_t two = 2;
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
{
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Vertex | dawn::ShaderStageBit::Fragment, 0, 1, &two);
|
||||
pass.SetPipeline(pipeline);
|
||||
pass.Draw(1, 1, 0, 0);
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
dawn::CommandBuffer commands = encoder.Finish();
|
||||
queue.Submit(1, &commands);
|
||||
|
||||
EXPECT_PIXEL_RGBA8_EQ(RGBA8(1, 1, 0, 0), renderPass.color, 0, 0);
|
||||
}
|
||||
DAWN_INSTANTIATE_TEST(PushConstantTest, MetalBackend, OpenGLBackend);
|
||||
@@ -1,244 +0,0 @@
|
||||
// Copyright 2017 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 "common/Constants.h"
|
||||
#include "utils/DawnHelpers.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
using namespace testing;
|
||||
|
||||
class PushConstantTest : public ValidationTest {
|
||||
protected:
|
||||
dawn::Queue queue;
|
||||
uint32_t constants[kMaxPushConstants] = {0};
|
||||
|
||||
void TestCreateShaderModule(bool success, std::string vertexSource) {
|
||||
dawn::ShaderModule module;
|
||||
if (success) {
|
||||
module = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, vertexSource.c_str());
|
||||
} else {
|
||||
ASSERT_DEVICE_ERROR(module = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, vertexSource.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void SetUp() override {
|
||||
ValidationTest::SetUp();
|
||||
queue = device.CreateQueue();
|
||||
}
|
||||
};
|
||||
|
||||
// Test valid usage of the parameters to SetPushConstants
|
||||
TEST_F(PushConstantTest, Success) {
|
||||
DummyRenderPass renderpassData(device);
|
||||
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
// PushConstants in a compute pass
|
||||
{
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, 1, constants);
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
// PushConstants in a render pass
|
||||
{
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderpassData);
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Vertex | dawn::ShaderStageBit::Fragment, 0, 1, constants);
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
// Setting all constants
|
||||
{
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, kMaxPushConstants, constants);
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
// Setting constants at an offset
|
||||
{
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, kMaxPushConstants - 1, 1, constants);
|
||||
pass.EndPass();
|
||||
}
|
||||
|
||||
encoder.Finish();
|
||||
}
|
||||
|
||||
// Test check for constants being set out of bounds
|
||||
TEST_F(PushConstantTest, SetPushConstantsOOB) {
|
||||
uint32_t constants[kMaxPushConstants] = {0};
|
||||
|
||||
// Control case: setting all constants
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, kMaxPushConstants, constants);
|
||||
pass.EndPass();
|
||||
encoder.Finish();
|
||||
}
|
||||
|
||||
// OOB because count is too big.
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, kMaxPushConstants + 1, constants);
|
||||
pass.EndPass();
|
||||
ASSERT_DEVICE_ERROR(encoder.Finish());
|
||||
}
|
||||
|
||||
// OOB because of the offset.
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 1, kMaxPushConstants, constants);
|
||||
pass.EndPass();
|
||||
ASSERT_DEVICE_ERROR(encoder.Finish());
|
||||
}
|
||||
}
|
||||
|
||||
// Test valid stages for compute pass
|
||||
TEST_F(PushConstantTest, StageForComputePass) {
|
||||
// Control case: setting to the compute stage in compute passes
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, 1, constants);
|
||||
pass.EndPass();
|
||||
encoder.Finish();
|
||||
}
|
||||
|
||||
// Graphics stages are disallowed
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Vertex, 0, 1, constants);
|
||||
pass.EndPass();
|
||||
ASSERT_DEVICE_ERROR(encoder.Finish());
|
||||
}
|
||||
|
||||
// A None shader stage mask is valid.
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::None, 0, 1, constants);
|
||||
pass.EndPass();
|
||||
encoder.Finish();
|
||||
}
|
||||
}
|
||||
|
||||
// Test valid stages for render passes
|
||||
TEST_F(PushConstantTest, StageForRenderPass) {
|
||||
DummyRenderPass renderpassData(device);
|
||||
|
||||
// Control case: setting to vertex and fragment in render pass
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderpassData);
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Vertex | dawn::ShaderStageBit::Fragment, 0, 1, constants);
|
||||
pass.EndPass();
|
||||
encoder.Finish();
|
||||
}
|
||||
|
||||
// Compute stage is disallowed
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderpassData);
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::Compute, 0, 1, constants);
|
||||
pass.EndPass();
|
||||
ASSERT_DEVICE_ERROR(encoder.Finish());
|
||||
}
|
||||
|
||||
// A None shader stage mask is valid.
|
||||
{
|
||||
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
||||
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderpassData);
|
||||
pass.SetPushConstants(dawn::ShaderStageBit::None, 0, 1, constants);
|
||||
pass.EndPass();
|
||||
encoder.Finish();
|
||||
}
|
||||
}
|
||||
|
||||
// Valid shaders that use pushconstants
|
||||
TEST_F(PushConstantTest, ShaderCompilationSuccess) {
|
||||
// Test shader module not using any push constants
|
||||
TestCreateShaderModule(true, R"(
|
||||
#version 450
|
||||
void main() {
|
||||
gl_Position = vec4(0.0);
|
||||
}
|
||||
)");
|
||||
|
||||
// Test one push constant
|
||||
TestCreateShaderModule(true, R"(
|
||||
#version 450
|
||||
layout(push_constant) uniform ConstantsBlock {
|
||||
float a;
|
||||
} c;
|
||||
void main() {
|
||||
gl_Position = vec4(0.0);
|
||||
}
|
||||
)");
|
||||
|
||||
// Test one push constant with an offset
|
||||
TestCreateShaderModule(true, R"(
|
||||
#version 450
|
||||
layout(push_constant) uniform ConstantsBlock {
|
||||
float a;
|
||||
} c;
|
||||
void main() {
|
||||
gl_Position = vec4(0.0);
|
||||
}
|
||||
)");
|
||||
|
||||
// Test max push constants
|
||||
TestCreateShaderModule(true, R"(
|
||||
#version 450
|
||||
layout(push_constant) uniform ConstantsBlock {
|
||||
float a[)" + std::to_string(kMaxPushConstants) + R"(];
|
||||
} c;
|
||||
void main() {
|
||||
gl_Position = vec4(0.0);
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
// Test that shaders using a push constant block too big fail compilation
|
||||
// TODO(cwallez@chromium.org): Currently disabled because ShaderModule error handling needs refactoring
|
||||
TEST_F(PushConstantTest, DISABLED_ShaderCompilationOOB) {
|
||||
// Test one push constant over the max
|
||||
TestCreateShaderModule(false, R"(
|
||||
#version 450
|
||||
layout(push_constant) uniform ConstantsBlock {
|
||||
float a[)" + std::to_string(kMaxPushConstants + 1) + R"(];
|
||||
} c;
|
||||
void main() {
|
||||
gl_Position = vec4(0.0);
|
||||
}
|
||||
)");
|
||||
|
||||
// Test two variables in the push constant block that together overflow
|
||||
TestCreateShaderModule(false, R"(
|
||||
#version 450
|
||||
layout(push_constant) uniform ConstantsBlock {
|
||||
float a[)" + std::to_string(kMaxPushConstants) + R"(];
|
||||
float b;
|
||||
} c;
|
||||
void main() {
|
||||
gl_Position = vec4(0.0);
|
||||
}
|
||||
)");
|
||||
}
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
#include "common/Constants.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
using namespace testing;
|
||||
using namespace dawn_wire;
|
||||
|
||||
@@ -44,22 +46,33 @@ TEST_F(WireArgumentTests, ValueArgument) {
|
||||
}
|
||||
|
||||
// Test that the wire is able to send arrays of numerical values
|
||||
static constexpr uint32_t testPushConstantValues[4] = {0, 42, 0xDEADBEEFu, 0xFFFFFFFFu};
|
||||
|
||||
bool CheckPushConstantValues(const uint32_t* values) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if (values[i] != testPushConstantValues[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST_F(WireArgumentTests, ValueArrayArgument) {
|
||||
// Create a bindgroup.
|
||||
DawnBindGroupLayoutDescriptor bglDescriptor;
|
||||
bglDescriptor.nextInChain = nullptr;
|
||||
bglDescriptor.bindingCount = 0;
|
||||
bglDescriptor.bindings = nullptr;
|
||||
|
||||
DawnBindGroupLayout bgl = dawnDeviceCreateBindGroupLayout(device, &bglDescriptor);
|
||||
DawnBindGroupLayout apiBgl = api.GetNewBindGroupLayout();
|
||||
EXPECT_CALL(api, DeviceCreateBindGroupLayout(apiDevice, _)).WillOnce(Return(apiBgl));
|
||||
|
||||
DawnBindGroupDescriptor bindGroupDescriptor;
|
||||
bindGroupDescriptor.nextInChain = nullptr;
|
||||
bindGroupDescriptor.layout = bgl;
|
||||
bindGroupDescriptor.bindingCount = 0;
|
||||
bindGroupDescriptor.bindings = nullptr;
|
||||
|
||||
DawnBindGroup bindGroup = dawnDeviceCreateBindGroup(device, &bindGroupDescriptor);
|
||||
DawnBindGroup apiBindGroup = api.GetNewBindGroup();
|
||||
EXPECT_CALL(api, DeviceCreateBindGroup(apiDevice, _)).WillOnce(Return(apiBindGroup));
|
||||
|
||||
// Use the bindgroup in SetBindGroup that takes an array of value offsets.
|
||||
DawnCommandEncoder encoder = dawnDeviceCreateCommandEncoder(device);
|
||||
DawnComputePassEncoder pass = dawnCommandEncoderBeginComputePass(encoder);
|
||||
dawnComputePassEncoderSetPushConstants(pass, DAWN_SHADER_STAGE_BIT_VERTEX, 0, 4,
|
||||
testPushConstantValues);
|
||||
|
||||
std::array<uint64_t, 4> testOffsets = {0, 42, 0xDEAD'BEEF'DEAD'BEEFu, 0xFFFF'FFFF'FFFF'FFFFu};
|
||||
dawnComputePassEncoderSetBindGroup(pass, 0, bindGroup, testOffsets.size(), testOffsets.data());
|
||||
|
||||
DawnCommandEncoder apiEncoder = api.GetNewCommandEncoder();
|
||||
EXPECT_CALL(api, DeviceCreateCommandEncoder(apiDevice)).WillOnce(Return(apiEncoder));
|
||||
@@ -67,9 +80,16 @@ TEST_F(WireArgumentTests, ValueArrayArgument) {
|
||||
DawnComputePassEncoder apiPass = api.GetNewComputePassEncoder();
|
||||
EXPECT_CALL(api, CommandEncoderBeginComputePass(apiEncoder)).WillOnce(Return(apiPass));
|
||||
|
||||
EXPECT_CALL(api,
|
||||
ComputePassEncoderSetPushConstants(apiPass, DAWN_SHADER_STAGE_BIT_VERTEX, 0, 4,
|
||||
ResultOf(CheckPushConstantValues, Eq(true))));
|
||||
EXPECT_CALL(api, ComputePassEncoderSetBindGroup(
|
||||
apiPass, 0, apiBindGroup, testOffsets.size(),
|
||||
MatchesLambda([testOffsets](const uint64_t* offsets) -> bool {
|
||||
for (size_t i = 0; i < testOffsets.size(); i++) {
|
||||
if (offsets[i] != testOffsets[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
})));
|
||||
|
||||
FlushClient();
|
||||
}
|
||||
@@ -280,6 +300,7 @@ TEST_F(WireArgumentTests, StructureOfValuesArgument) {
|
||||
// Test that the wire is able to send structures that contain objects
|
||||
TEST_F(WireArgumentTests, StructureOfObjectArrayArgument) {
|
||||
DawnBindGroupLayoutDescriptor bglDescriptor;
|
||||
bglDescriptor.nextInChain = nullptr;
|
||||
bglDescriptor.bindingCount = 0;
|
||||
bglDescriptor.bindings = nullptr;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user