Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 2 | // |
| 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 Wallez | d37523f | 2018-07-24 13:53:51 +0200 | [diff] [blame] | 15 | #include "dawn_native/d3d12/InputStateD3D12.h" |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 17 | #include "common/BitSetIterator.h" |
| 18 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 19 | namespace dawn_native { namespace d3d12 { |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 20 | |
Corentin Wallez | 226110f | 2018-07-18 11:38:11 +0200 | [diff] [blame] | 21 | static DXGI_FORMAT VertexFormatType(dawn::VertexFormat format) { |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 22 | switch (format) { |
Yan, Shaobo | e105f96 | 2019-03-21 11:18:31 +0000 | [diff] [blame] | 23 | 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 White | 8d742d1 | 2018-02-04 08:49:43 -0500 | [diff] [blame] | 32 | return DXGI_FORMAT_R8G8_UNORM; |
Yan, Shaobo | e105f96 | 2019-03-21 11:18:31 +0000 | [diff] [blame] | 33 | 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 Wallez | 96acaef | 2017-07-10 20:31:47 -0400 | [diff] [blame] | 83 | default: |
| 84 | UNREACHABLE(); |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Corentin Wallez | 226110f | 2018-07-18 11:38:11 +0200 | [diff] [blame] | 88 | static D3D12_INPUT_CLASSIFICATION InputStepModeFunction(dawn::InputStepMode mode) { |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 89 | switch (mode) { |
Corentin Wallez | 226110f | 2018-07-18 11:38:11 +0200 | [diff] [blame] | 90 | case dawn::InputStepMode::Vertex: |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 91 | return D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA; |
Corentin Wallez | 226110f | 2018-07-18 11:38:11 +0200 | [diff] [blame] | 92 | case dawn::InputStepMode::Instance: |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 93 | return D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA; |
Corentin Wallez | 96acaef | 2017-07-10 20:31:47 -0400 | [diff] [blame] | 94 | default: |
| 95 | UNREACHABLE(); |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Corentin Wallez | 5b61abc | 2018-08-13 17:50:22 +0200 | [diff] [blame] | 99 | InputState::InputState(InputStateBuilder* builder) : InputStateBase(builder) { |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 100 | const auto& attributesSetMask = GetAttributesSetMask(); |
| 101 | |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 102 | unsigned int count = 0; |
| 103 | for (auto i : IterateBitSet(attributesSetMask)) { |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 104 | if (!attributesSetMask[i]) { |
| 105 | continue; |
| 106 | } |
| 107 | |
Corentin Wallez | e00385a | 2017-11-23 11:14:03 -0800 | [diff] [blame] | 108 | D3D12_INPUT_ELEMENT_DESC& inputElementDescriptor = mInputElementDescriptors[count++]; |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 109 | |
Yunchao He | 87bf834 | 2019-02-22 15:08:03 +0000 | [diff] [blame] | 110 | const VertexAttributeDescriptor& attribute = GetAttribute(i); |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 111 | |
Corentin Wallez | 2d62a37 | 2017-11-24 14:04:22 -0500 | [diff] [blame] | 112 | // If the HLSL semantic is TEXCOORDN the SemanticName should be "TEXCOORD" and the |
| 113 | // SemanticIndex N |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 114 | inputElementDescriptor.SemanticName = "TEXCOORD"; |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 115 | inputElementDescriptor.SemanticIndex = static_cast<uint32_t>(i); |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 116 | inputElementDescriptor.Format = VertexFormatType(attribute.format); |
Yunchao He | b220773 | 2019-02-14 00:35:39 +0000 | [diff] [blame] | 117 | inputElementDescriptor.InputSlot = attribute.inputSlot; |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 118 | |
Yunchao He | 87bf834 | 2019-02-22 15:08:03 +0000 | [diff] [blame] | 119 | const VertexInputDescriptor& input = GetInput(attribute.inputSlot); |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 120 | |
| 121 | inputElementDescriptor.AlignedByteOffset = attribute.offset; |
| 122 | inputElementDescriptor.InputSlotClass = InputStepModeFunction(input.stepMode); |
Corentin Wallez | 2d62a37 | 2017-11-24 14:04:22 -0500 | [diff] [blame] | 123 | if (inputElementDescriptor.InputSlotClass == |
| 124 | D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA) { |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 125 | inputElementDescriptor.InstanceDataStepRate = 0; |
| 126 | } else { |
| 127 | inputElementDescriptor.InstanceDataStepRate = 1; |
| 128 | } |
| 129 | } |
| 130 | |
Corentin Wallez | e00385a | 2017-11-23 11:14:03 -0800 | [diff] [blame] | 131 | mInputLayoutDescriptor.pInputElementDescs = mInputElementDescriptors; |
| 132 | mInputLayoutDescriptor.NumElements = count; |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | const D3D12_INPUT_LAYOUT_DESC& InputState::GetD3D12InputLayoutDescriptor() const { |
Corentin Wallez | e00385a | 2017-11-23 11:14:03 -0800 | [diff] [blame] | 136 | return mInputLayoutDescriptor; |
Austin Eng | d251356 | 2017-06-09 12:50:51 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Corentin Wallez | 49a65d0 | 2018-07-24 16:45:45 +0200 | [diff] [blame] | 139 | }} // namespace dawn_native::d3d12 |