Construct VertexAttributeDescriptor, in order to match web idl

The parameters about vertex attribute were encapsulated in
VertexAttributeDescriptor in web idl. This change construct
this descriptor accordingly.

BUG=dawn:107

Change-Id: Ia1c6e53af951d8f2a0c0b69bdca09291c2661e50
Reviewed-on: https://dawn-review.googlesource.com/c/4620
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Yunchao He <yunchao.he@intel.com>
This commit is contained in:
Yunchao He 2019-02-14 00:35:39 +00:00 committed by Commit Bot service account
parent 445869d2b0
commit b2207737ab
18 changed files with 207 additions and 73 deletions

View File

@ -594,6 +594,16 @@
{"value": 1, "name": "uint32"}
]
},
"vertex attribute descriptor": {
"category": "structure",
"extensible": false,
"members": [
{"name": "shader location", "type": "uint32_t"},
{"name": "input slot", "type": "uint32_t"},
{"name": "offset", "type": "uint32_t"},
{"name": "format", "type": "vertex format"}
]
},
"input state": {
"category": "object"
},
@ -607,10 +617,7 @@
{
"name": "set attribute",
"args": [
{"name": "shader location", "type": "uint32_t"},
{"name": "binding slot", "type": "uint32_t"},
{"name": "format", "type": "vertex format"},
{"name": "offset", "type": "uint32_t"}
{"name": "attribute", "type": "vertex attribute descriptor", "annotation": "const*"}
]
},
{

View File

@ -114,13 +114,31 @@ void initRender() {
}
)");
dawn::VertexAttributeDescriptor attribute1;
attribute1.shaderLocation = 0;
attribute1.inputSlot = 0;
attribute1.offset = offsetof(Particle, pos);
attribute1.format = dawn::VertexFormat::FloatR32G32;
dawn::VertexAttributeDescriptor attribute2;
attribute2.shaderLocation = 1;
attribute2.inputSlot = 0;
attribute2.offset = offsetof(Particle, vel);
attribute2.format = dawn::VertexFormat::FloatR32G32;
dawn::VertexAttributeDescriptor attribute3;
attribute3.shaderLocation = 2;
attribute3.inputSlot = 1;
attribute3.offset = 0;
attribute3.format = dawn::VertexFormat::FloatR32G32;
dawn::InputState inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32, offsetof(Particle, pos))
.SetAttribute(1, 0, dawn::VertexFormat::FloatR32G32, offsetof(Particle, vel))
.SetInput(0, sizeof(Particle), dawn::InputStepMode::Instance)
.SetAttribute(2, 1, dawn::VertexFormat::FloatR32G32, 0)
.SetInput(1, sizeof(glm::vec2), dawn::InputStepMode::Vertex)
.GetResult();
.SetAttribute(&attribute1)
.SetAttribute(&attribute2)
.SetInput(0, sizeof(Particle), dawn::InputStepMode::Instance)
.SetAttribute(&attribute3)
.SetInput(1, sizeof(glm::vec2), dawn::InputStepMode::Vertex)
.GetResult();
depthStencilView = CreateDefaultDepthStencilView(device);

View File

@ -111,10 +111,15 @@ void init() {
fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
})");
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32G32B32A32;
auto inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.GetResult();
.SetAttribute(&attribute)
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.GetResult();
auto bgl = utils::MakeBindGroupLayout(
device, {

View File

@ -156,11 +156,23 @@ void init() {
fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0);
})");
dawn::VertexAttributeDescriptor attribute1;
attribute1.shaderLocation = 0;
attribute1.inputSlot = 0;
attribute1.offset = 0;
attribute1.format = dawn::VertexFormat::FloatR32G32B32;
dawn::VertexAttributeDescriptor attribute2;
attribute2.shaderLocation = 1;
attribute2.inputSlot = 0;
attribute2.offset = 3 * sizeof(float);
attribute2.format = dawn::VertexFormat::FloatR32G32B32;
auto inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32, 0)
.SetAttribute(1, 0, dawn::VertexFormat::FloatR32G32B32, 3 * sizeof(float))
.SetInput(0, 6 * sizeof(float), dawn::InputStepMode::Vertex)
.GetResult();
.SetAttribute(&attribute1)
.SetAttribute(&attribute2)
.SetInput(0, 6 * sizeof(float), dawn::InputStepMode::Vertex)
.GetResult();
auto bgl = utils::MakeBindGroupLayout(
device, {

View File

@ -247,16 +247,25 @@ namespace {
fprintf(stderr, "unsupported technique parameter type %d\n", iParameter.type);
continue;
}
dawn::VertexAttributeDescriptor attribute;
attribute.offset = 0;
attribute.format = format;
if (iParameter.semantic == "POSITION") {
builder.SetAttribute(0, 0, format, 0);
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
builder.SetAttribute(&attribute);
builder.SetInput(0, static_cast<uint32_t>(stridePos), dawn::InputStepMode::Vertex);
slotsSet.set(0);
} else if (iParameter.semantic == "NORMAL") {
builder.SetAttribute(1, 1, format, 0);
attribute.shaderLocation = 1;
attribute.inputSlot = 1;
builder.SetAttribute(&attribute);
builder.SetInput(1, static_cast<uint32_t>(strideNor), dawn::InputStepMode::Vertex);
slotsSet.set(1);
} else if (iParameter.semantic == "TEXCOORD_0") {
builder.SetAttribute(2, 2, format, 0);
attribute.shaderLocation = 2;
attribute.inputSlot = 2;
builder.SetAttribute(&attribute);
builder.SetInput(2, static_cast<uint32_t>(strideTxc), dawn::InputStepMode::Vertex);
slotsSet.set(2);
} else {
@ -268,7 +277,13 @@ namespace {
if (slotsSet[i]) {
continue;
}
builder.SetAttribute(i, i, dawn::VertexFormat::FloatR32G32B32A32, 0);
dawn::VertexAttributeDescriptor attribute;
attribute.offset = 0;
attribute.shaderLocation = i;
attribute.inputSlot = i;
attribute.format = dawn::VertexFormat::FloatR32G32B32A32;
builder.SetAttribute(&attribute);
builder.SetInput(i, 0, dawn::InputStepMode::Vertex);
}
auto inputState = builder.GetResult();

View File

@ -117,7 +117,7 @@ namespace dawn_native {
InputStateBase* InputStateBuilder::GetResultImpl() {
for (uint32_t location = 0; location < kMaxVertexAttributes; ++location) {
if (mAttributesSetMask[location] &&
!mInputsSetMask[mAttributeInfos[location].bindingSlot]) {
!mInputsSetMask[mAttributeInfos[location].inputSlot]) {
HandleError("Attribute uses unset input");
return nullptr;
}
@ -126,28 +126,25 @@ namespace dawn_native {
return GetDevice()->CreateInputState(this);
}
void InputStateBuilder::SetAttribute(uint32_t shaderLocation,
uint32_t bindingSlot,
dawn::VertexFormat format,
uint32_t offset) {
if (shaderLocation >= kMaxVertexAttributes) {
void InputStateBuilder::SetAttribute(const VertexAttributeDescriptor* attribute) {
if (attribute->shaderLocation >= kMaxVertexAttributes) {
HandleError("Setting attribute out of bounds");
return;
}
if (bindingSlot >= kMaxVertexInputs) {
if (attribute->inputSlot >= kMaxVertexInputs) {
HandleError("Binding slot out of bounds");
return;
}
if (mAttributesSetMask[shaderLocation]) {
if (mAttributesSetMask[attribute->shaderLocation]) {
HandleError("Setting already set attribute");
return;
}
mAttributesSetMask.set(shaderLocation);
auto& info = mAttributeInfos[shaderLocation];
info.bindingSlot = bindingSlot;
info.format = format;
info.offset = offset;
mAttributesSetMask.set(attribute->shaderLocation);
auto& info = mAttributeInfos[attribute->shaderLocation];
info.inputSlot = attribute->inputSlot;
info.offset = attribute->offset;
info.format = attribute->format;
}
void InputStateBuilder::SetInput(uint32_t bindingSlot,

View File

@ -37,7 +37,7 @@ namespace dawn_native {
InputStateBase(InputStateBuilder* builder);
struct AttributeInfo {
uint32_t bindingSlot;
uint32_t inputSlot;
dawn::VertexFormat format;
uint32_t offset;
};
@ -64,10 +64,7 @@ namespace dawn_native {
InputStateBuilder(DeviceBase* device);
// Dawn API
void SetAttribute(uint32_t shaderLocation,
uint32_t bindingSlot,
dawn::VertexFormat format,
uint32_t offset);
void SetAttribute(const VertexAttributeDescriptor* attribute);
void SetInput(uint32_t bindingSlot, uint32_t stride, dawn::InputStepMode stepMode);
private:

View File

@ -78,9 +78,9 @@ namespace dawn_native { namespace d3d12 {
inputElementDescriptor.SemanticName = "TEXCOORD";
inputElementDescriptor.SemanticIndex = static_cast<uint32_t>(i);
inputElementDescriptor.Format = VertexFormatType(attribute.format);
inputElementDescriptor.InputSlot = attribute.bindingSlot;
inputElementDescriptor.InputSlot = attribute.inputSlot;
const InputInfo& input = GetInput(attribute.bindingSlot);
const InputInfo& input = GetInput(attribute.inputSlot);
inputElementDescriptor.AlignedByteOffset = attribute.offset;
inputElementDescriptor.InputSlotClass = InputStepModeFunction(input.stepMode);

View File

@ -71,7 +71,7 @@ namespace dawn_native { namespace metal {
auto attribDesc = [MTLVertexAttributeDescriptor new];
attribDesc.format = VertexFormatType(info.format);
attribDesc.offset = info.offset;
attribDesc.bufferIndex = kMaxBindingsPerGroup + info.bindingSlot;
attribDesc.bufferIndex = kMaxBindingsPerGroup + info.inputSlot;
mMtlVertexDescriptor.attributes[i] = attribDesc;
[attribDesc release];
}

View File

@ -29,8 +29,8 @@ namespace dawn_native { namespace opengl {
auto attribute = GetAttribute(location);
glEnableVertexAttribArray(location);
attributesUsingInput[attribute.bindingSlot][location] = true;
auto input = GetInput(attribute.bindingSlot);
attributesUsingInput[attribute.inputSlot][location] = true;
auto input = GetInput(attribute.inputSlot);
if (input.stride == 0) {
// Emulate a stride of zero (constant vertex attribute) by

View File

@ -85,7 +85,7 @@ namespace dawn_native { namespace vulkan {
auto& attributeDesc = mAttributes[attributeCount];
attributeDesc.location = i;
attributeDesc.binding = attributeInfo.bindingSlot;
attributeDesc.binding = attributeInfo.inputSlot;
attributeDesc.format = VulkanVertexFormat(attributeInfo.format);
attributeDesc.offset = attributeInfo.offset;

View File

@ -26,10 +26,17 @@ class DrawIndexedTest : public DawnTest {
renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
dawn::InputState inputState = device.CreateInputStateBuilder()
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
.GetResult();
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32G32B32A32;
dawn::InputState inputState =
device.CreateInputStateBuilder()
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.SetAttribute(&attribute)
.GetResult();
dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
#version 450

View File

@ -26,10 +26,16 @@ class DrawTest : public DawnTest {
renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32G32B32A32;
dawn::InputState inputState =
device.CreateInputStateBuilder()
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
.SetAttribute(&attribute)
.GetResult();
dawn::ShaderModule vsModule =

View File

@ -31,10 +31,17 @@ class IndexFormatTest : public DawnTest {
utils::BasicRenderPass renderPass;
dawn::RenderPipeline MakeTestPipeline(dawn::IndexFormat format) {
dawn::InputState inputState = device.CreateInputStateBuilder()
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
.GetResult();
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32G32B32A32;
dawn::InputState inputState =
device.CreateInputStateBuilder()
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.SetAttribute(&attribute)
.GetResult();
dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
#version 450

View File

@ -149,7 +149,13 @@ class InputStateTest : public DawnTest {
}
for (const auto& attribute : attributes) {
builder.SetAttribute(attribute.location, attribute.slot, attribute.format, attribute.offset);
dawn::VertexAttributeDescriptor descriptor;
descriptor.shaderLocation = attribute.location;
descriptor.inputSlot = attribute.slot;
descriptor.offset = attribute.offset;
descriptor.format = attribute.format;
builder.SetAttribute(&descriptor);
}
return builder.GetResult();

View File

@ -165,10 +165,16 @@ class PrimitiveTopologyTest : public DawnTest {
fragColor = vec4(0.0, 1.0, 0.0, 1.0);
})");
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32G32B32A32;
inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32G32B32A32, 0)
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.GetResult();
.SetAttribute(&attribute)
.SetInput(0, 4 * sizeof(float), dawn::InputStepMode::Vertex)
.GetResult();
vertexBuffer = utils::CreateBufferFromData(device, kVertices, sizeof(kVertices), dawn::BufferUsageBit::Vertex);
}

View File

@ -60,11 +60,23 @@ TEST_F(InputStateTest, EmptyIsOk) {
// Check validation that pipeline vertex inputs are backed by attributes in the input state
TEST_F(InputStateTest, PipelineCompatibility) {
dawn::VertexAttributeDescriptor attribute1;
attribute1.shaderLocation = 0;
attribute1.inputSlot = 0;
attribute1.offset = 0;
attribute1.format = dawn::VertexFormat::FloatR32;
dawn::VertexAttributeDescriptor attribute2;
attribute2.shaderLocation = 1;
attribute2.inputSlot = 0;
attribute2.offset = sizeof(float);
attribute2.format = dawn::VertexFormat::FloatR32;
dawn::InputState state = AssertWillBeSuccess(device.CreateInputStateBuilder())
.SetInput(0, 2 * sizeof(float), dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(1, 0, dawn::VertexFormat::FloatR32, sizeof(float))
.GetResult();
.SetInput(0, 2 * sizeof(float), dawn::InputStepMode::Vertex)
.SetAttribute(&attribute1)
.SetAttribute(&attribute2)
.GetResult();
// Control case: pipeline with one input per attribute
CreatePipeline(true, state, R"(
@ -103,9 +115,15 @@ TEST_F(InputStateTest, StrideZero) {
.GetResult();
// Works ok with attributes at a large-ish offset
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 128;
attribute.format = dawn::VertexFormat::FloatR32;
AssertWillBeSuccess(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32, 128)
.SetAttribute(&attribute)
.GetResult();
}
@ -139,60 +157,87 @@ TEST_F(InputStateTest, SetInputOutOfBounds) {
// Test that we cannot set an already set attribute
TEST_F(InputStateTest, AlreadySetAttribute) {
// Control case, setting last attribute
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32;
AssertWillBeSuccess(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(&attribute)
.GetResult();
// Oh no, attribute 0 is set twice
AssertWillBeError(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(&attribute)
.SetAttribute(&attribute)
.GetResult();
}
// Check out of bounds condition on SetAttribute
TEST_F(InputStateTest, SetAttributeOutOfBounds) {
// Control case, setting last attribute
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = kMaxVertexAttributes - 1;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32;
AssertWillBeSuccess(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(kMaxVertexAttributes - 1, 0, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(&attribute)
.GetResult();
// Test OOB
attribute.shaderLocation = kMaxVertexAttributes;
AssertWillBeError(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(kMaxVertexAttributes, 0, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(&attribute)
.GetResult();
}
// Check that all attributes must be backed by an input
TEST_F(InputStateTest, RequireInputForAttribute) {
// Control case
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32;
AssertWillBeSuccess(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(&attribute)
.GetResult();
// Attribute 0 uses input 1 which doesn't exist
attribute.inputSlot = 1;
AssertWillBeError(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(0, 1, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(&attribute)
.GetResult();
}
// Check OOB checks for an attribute's input
TEST_F(InputStateTest, SetAttributeOOBCheckForInputs) {
// Control case
dawn::VertexAttributeDescriptor attribute;
attribute.shaderLocation = 0;
attribute.inputSlot = 0;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32;
AssertWillBeSuccess(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(0, 0, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(&attribute)
.GetResult();
// Could crash if we didn't check for OOB
attribute.inputSlot = 1000000;
AssertWillBeError(device.CreateInputStateBuilder())
.SetInput(0, 0, dawn::InputStepMode::Vertex)
.SetAttribute(0, 1000000, dawn::VertexFormat::FloatR32, 0)
.SetAttribute(&attribute)
.GetResult();
}

View File

@ -71,8 +71,14 @@ class VertexBufferValidationTest : public ValidationTest {
dawn::InputState MakeInputState(unsigned int numInputs) {
auto builder = device.CreateInputStateBuilder();
dawn::VertexAttributeDescriptor attribute;
attribute.offset = 0;
attribute.format = dawn::VertexFormat::FloatR32G32B32;
for (unsigned int i = 0; i < numInputs; ++i) {
builder.SetAttribute(i, i, dawn::VertexFormat::FloatR32G32B32, 0);
attribute.shaderLocation = i;
attribute.inputSlot = i;
builder.SetAttribute(&attribute);
builder.SetInput(i, 0, dawn::InputStepMode::Vertex);
}
return builder.GetResult();