blob: fcfa6b18c56881df50d3a3e792dfa598cedfe802 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Austin Engd2513562017-06-09 12:50:51 -04002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Corentin Wallezd37523f2018-07-24 13:53:51 +020015#include "dawn_native/d3d12/InputStateD3D12.h"
Austin Engd2513562017-06-09 12:50:51 -040016
Corentin Wallez83e779d2017-07-10 21:44:06 -040017#include "common/BitSetIterator.h"
18
Corentin Wallez49a65d02018-07-24 16:45:45 +020019namespace dawn_native { namespace d3d12 {
Austin Engd2513562017-06-09 12:50:51 -040020
Corentin Wallez226110f2018-07-18 11:38:11 +020021 static DXGI_FORMAT VertexFormatType(dawn::VertexFormat format) {
Austin Engd2513562017-06-09 12:50:51 -040022 switch (format) {
Yan, Shaoboe105f962019-03-21 11:18:31 +000023 case dawn::VertexFormat::UChar2:
24 return DXGI_FORMAT_R8G8_UINT;
25 case dawn::VertexFormat::UChar4:
26 return DXGI_FORMAT_R8G8B8A8_UINT;
27 case dawn::VertexFormat::Char2:
28 return DXGI_FORMAT_R8G8_SINT;
29 case dawn::VertexFormat::Char4:
30 return DXGI_FORMAT_R8G8B8A8_SINT;
31 case dawn::VertexFormat::UChar2Norm:
Stephen White8d742d12018-02-04 08:49:43 -050032 return DXGI_FORMAT_R8G8_UNORM;
Yan, Shaoboe105f962019-03-21 11:18:31 +000033 case dawn::VertexFormat::UChar4Norm:
34 return DXGI_FORMAT_R8G8B8A8_UNORM;
35 case dawn::VertexFormat::Char2Norm:
36 return DXGI_FORMAT_R8G8_SNORM;
37 case dawn::VertexFormat::Char4Norm:
38 return DXGI_FORMAT_R8G8B8A8_SNORM;
39 case dawn::VertexFormat::UShort2:
40 return DXGI_FORMAT_R16G16_UINT;
41 case dawn::VertexFormat::UShort4:
42 return DXGI_FORMAT_R16G16B16A16_UINT;
43 case dawn::VertexFormat::Short2:
44 return DXGI_FORMAT_R16G16_SINT;
45 case dawn::VertexFormat::Short4:
46 return DXGI_FORMAT_R16G16B16A16_SINT;
47 case dawn::VertexFormat::UShort2Norm:
48 return DXGI_FORMAT_R16G16_UNORM;
49 case dawn::VertexFormat::UShort4Norm:
50 return DXGI_FORMAT_R16G16B16A16_UNORM;
51 case dawn::VertexFormat::Short2Norm:
52 return DXGI_FORMAT_R16G16_SNORM;
53 case dawn::VertexFormat::Short4Norm:
54 return DXGI_FORMAT_R16G16B16A16_SNORM;
55 case dawn::VertexFormat::Half2:
56 return DXGI_FORMAT_R16G16_FLOAT;
57 case dawn::VertexFormat::Half4:
58 return DXGI_FORMAT_R16G16B16A16_FLOAT;
59 case dawn::VertexFormat::Float:
60 return DXGI_FORMAT_R32_FLOAT;
61 case dawn::VertexFormat::Float2:
62 return DXGI_FORMAT_R32G32_FLOAT;
63 case dawn::VertexFormat::Float3:
64 return DXGI_FORMAT_R32G32B32_FLOAT;
65 case dawn::VertexFormat::Float4:
66 return DXGI_FORMAT_R32G32B32A32_FLOAT;
67 case dawn::VertexFormat::UInt:
68 return DXGI_FORMAT_R32_UINT;
69 case dawn::VertexFormat::UInt2:
70 return DXGI_FORMAT_R32G32_UINT;
71 case dawn::VertexFormat::UInt3:
72 return DXGI_FORMAT_R32G32B32_UINT;
73 case dawn::VertexFormat::UInt4:
74 return DXGI_FORMAT_R32G32B32A32_UINT;
75 case dawn::VertexFormat::Int:
76 return DXGI_FORMAT_R32_SINT;
77 case dawn::VertexFormat::Int2:
78 return DXGI_FORMAT_R32G32_SINT;
79 case dawn::VertexFormat::Int3:
80 return DXGI_FORMAT_R32G32B32_SINT;
81 case dawn::VertexFormat::Int4:
82 return DXGI_FORMAT_R32G32B32A32_SINT;
Corentin Wallez96acaef2017-07-10 20:31:47 -040083 default:
84 UNREACHABLE();
Austin Engd2513562017-06-09 12:50:51 -040085 }
86 }
87
Corentin Wallez226110f2018-07-18 11:38:11 +020088 static D3D12_INPUT_CLASSIFICATION InputStepModeFunction(dawn::InputStepMode mode) {
Austin Engd2513562017-06-09 12:50:51 -040089 switch (mode) {
Corentin Wallez226110f2018-07-18 11:38:11 +020090 case dawn::InputStepMode::Vertex:
Austin Engd2513562017-06-09 12:50:51 -040091 return D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
Corentin Wallez226110f2018-07-18 11:38:11 +020092 case dawn::InputStepMode::Instance:
Austin Engd2513562017-06-09 12:50:51 -040093 return D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA;
Corentin Wallez96acaef2017-07-10 20:31:47 -040094 default:
95 UNREACHABLE();
Austin Engd2513562017-06-09 12:50:51 -040096 }
97 }
98
Corentin Wallez5b61abc2018-08-13 17:50:22 +020099 InputState::InputState(InputStateBuilder* builder) : InputStateBase(builder) {
Austin Engd2513562017-06-09 12:50:51 -0400100 const auto& attributesSetMask = GetAttributesSetMask();
101
Corentin Wallez83e779d2017-07-10 21:44:06 -0400102 unsigned int count = 0;
103 for (auto i : IterateBitSet(attributesSetMask)) {
Austin Engd2513562017-06-09 12:50:51 -0400104 if (!attributesSetMask[i]) {
105 continue;
106 }
107
Corentin Walleze00385a2017-11-23 11:14:03 -0800108 D3D12_INPUT_ELEMENT_DESC& inputElementDescriptor = mInputElementDescriptors[count++];
Austin Engd2513562017-06-09 12:50:51 -0400109
Yunchao He87bf8342019-02-22 15:08:03 +0000110 const VertexAttributeDescriptor& attribute = GetAttribute(i);
Austin Engd2513562017-06-09 12:50:51 -0400111
Corentin Wallez2d62a372017-11-24 14:04:22 -0500112 // If the HLSL semantic is TEXCOORDN the SemanticName should be "TEXCOORD" and the
113 // SemanticIndex N
Austin Engd2513562017-06-09 12:50:51 -0400114 inputElementDescriptor.SemanticName = "TEXCOORD";
Corentin Wallez83e779d2017-07-10 21:44:06 -0400115 inputElementDescriptor.SemanticIndex = static_cast<uint32_t>(i);
Austin Engd2513562017-06-09 12:50:51 -0400116 inputElementDescriptor.Format = VertexFormatType(attribute.format);
Yunchao Heb2207732019-02-14 00:35:39 +0000117 inputElementDescriptor.InputSlot = attribute.inputSlot;
Austin Engd2513562017-06-09 12:50:51 -0400118
Yunchao He87bf8342019-02-22 15:08:03 +0000119 const VertexInputDescriptor& input = GetInput(attribute.inputSlot);
Austin Engd2513562017-06-09 12:50:51 -0400120
121 inputElementDescriptor.AlignedByteOffset = attribute.offset;
122 inputElementDescriptor.InputSlotClass = InputStepModeFunction(input.stepMode);
Corentin Wallez2d62a372017-11-24 14:04:22 -0500123 if (inputElementDescriptor.InputSlotClass ==
124 D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA) {
Austin Engd2513562017-06-09 12:50:51 -0400125 inputElementDescriptor.InstanceDataStepRate = 0;
126 } else {
127 inputElementDescriptor.InstanceDataStepRate = 1;
128 }
129 }
130
Corentin Walleze00385a2017-11-23 11:14:03 -0800131 mInputLayoutDescriptor.pInputElementDescs = mInputElementDescriptors;
132 mInputLayoutDescriptor.NumElements = count;
Austin Engd2513562017-06-09 12:50:51 -0400133 }
134
135 const D3D12_INPUT_LAYOUT_DESC& InputState::GetD3D12InputLayoutDescriptor() const {
Corentin Walleze00385a2017-11-23 11:14:03 -0800136 return mInputLayoutDescriptor;
Austin Engd2513562017-06-09 12:50:51 -0400137 }
138
Corentin Wallez49a65d02018-07-24 16:45:45 +0200139}} // namespace dawn_native::d3d12