• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
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 
15 #include "dawn_native/d3d12/SamplerD3D12.h"
16 
17 #include "dawn_native/d3d12/DeviceD3D12.h"
18 #include "dawn_native/d3d12/UtilsD3D12.h"
19 
20 namespace dawn_native { namespace d3d12 {
21 
22     namespace {
AddressMode(wgpu::AddressMode mode)23         D3D12_TEXTURE_ADDRESS_MODE AddressMode(wgpu::AddressMode mode) {
24             switch (mode) {
25                 case wgpu::AddressMode::Repeat:
26                     return D3D12_TEXTURE_ADDRESS_MODE_WRAP;
27                 case wgpu::AddressMode::MirrorRepeat:
28                     return D3D12_TEXTURE_ADDRESS_MODE_MIRROR;
29                 case wgpu::AddressMode::ClampToEdge:
30                     return D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
31             }
32         }
33     }  // namespace
34 
35     // static
Create(Device * device,const SamplerDescriptor * descriptor)36     Ref<Sampler> Sampler::Create(Device* device, const SamplerDescriptor* descriptor) {
37         return AcquireRef(new Sampler(device, descriptor));
38     }
39 
Sampler(Device * device,const SamplerDescriptor * descriptor)40     Sampler::Sampler(Device* device, const SamplerDescriptor* descriptor)
41         : SamplerBase(device, descriptor) {
42         D3D12_FILTER_TYPE minFilter;
43         switch (descriptor->minFilter) {
44             case wgpu::FilterMode::Nearest:
45                 minFilter = D3D12_FILTER_TYPE_POINT;
46                 break;
47             case wgpu::FilterMode::Linear:
48                 minFilter = D3D12_FILTER_TYPE_LINEAR;
49                 break;
50         }
51 
52         D3D12_FILTER_TYPE magFilter;
53         switch (descriptor->magFilter) {
54             case wgpu::FilterMode::Nearest:
55                 magFilter = D3D12_FILTER_TYPE_POINT;
56                 break;
57             case wgpu::FilterMode::Linear:
58                 magFilter = D3D12_FILTER_TYPE_LINEAR;
59                 break;
60         }
61 
62         D3D12_FILTER_TYPE mipmapFilter;
63         switch (descriptor->mipmapFilter) {
64             case wgpu::FilterMode::Nearest:
65                 mipmapFilter = D3D12_FILTER_TYPE_POINT;
66                 break;
67             case wgpu::FilterMode::Linear:
68                 mipmapFilter = D3D12_FILTER_TYPE_LINEAR;
69                 break;
70         }
71 
72         D3D12_FILTER_REDUCTION_TYPE reduction =
73             descriptor->compare == wgpu::CompareFunction::Undefined
74                 ? D3D12_FILTER_REDUCTION_TYPE_STANDARD
75                 : D3D12_FILTER_REDUCTION_TYPE_COMPARISON;
76 
77         // https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_sampler_desc
78         mSamplerDesc.MaxAnisotropy = std::min<uint16_t>(GetMaxAnisotropy(), 16u);
79 
80         if (mSamplerDesc.MaxAnisotropy > 1) {
81             mSamplerDesc.Filter = D3D12_ENCODE_ANISOTROPIC_FILTER(reduction);
82         } else {
83             mSamplerDesc.Filter =
84                 D3D12_ENCODE_BASIC_FILTER(minFilter, magFilter, mipmapFilter, reduction);
85         }
86 
87         mSamplerDesc.AddressU = AddressMode(descriptor->addressModeU);
88         mSamplerDesc.AddressV = AddressMode(descriptor->addressModeV);
89         mSamplerDesc.AddressW = AddressMode(descriptor->addressModeW);
90         mSamplerDesc.MipLODBias = 0.f;
91 
92         if (descriptor->compare != wgpu::CompareFunction::Undefined) {
93             mSamplerDesc.ComparisonFunc = ToD3D12ComparisonFunc(descriptor->compare);
94         } else {
95             // Still set the function so it's not garbage.
96             mSamplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
97         }
98         mSamplerDesc.MinLOD = descriptor->lodMinClamp;
99         mSamplerDesc.MaxLOD = descriptor->lodMaxClamp;
100     }
101 
GetSamplerDescriptor() const102     const D3D12_SAMPLER_DESC& Sampler::GetSamplerDescriptor() const {
103         return mSamplerDesc;
104     }
105 
106 }}  // namespace dawn_native::d3d12
107