summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp
blob: fbedb21cd79f3c041b463ad17802a3ad0f194283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 *  Copyright 2019-2021 Diligent Graphics LLC
 *  Copyright 2015-2019 Egor Yusov
 *  
 *  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.
 *
 *  In no event and under no legal theory, whether in tort (including negligence), 
 *  contract, or otherwise, unless required by applicable law (such as deliberate 
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental, 
 *  or consequential damages of any character arising as a result of this License or 
 *  out of the use or inability to use the software (including but not limited to damages 
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and 
 *  all other commercial damages or losses), even if such Contributor has been advised 
 *  of the possibility of such damages.
 */

#include "pch.h"

#include "ShaderD3D11Impl.hpp"
#include "RenderDeviceD3D11Impl.hpp"

namespace Diligent
{

static const ShaderVersion HLSLValidateShaderVersion(const ShaderVersion& Version, const ShaderVersion& MaxVersion)
{
    ShaderVersion ModelVer;
    if (Version.Major > MaxVersion.Major || (Version.Major == MaxVersion.Major && Version.Minor > MaxVersion.Minor))
    {
        ModelVer = MaxVersion;
        LOG_ERROR_MESSAGE("Shader model ", Uint32{Version.Major}, "_", Uint32{Version.Minor},
                          " is not supported by this device. Attempting to use the maximum supported model ",
                          Uint32{MaxVersion.Major}, "_", Uint32{MaxVersion.Minor}, '.');
    }
    else
    {
        ModelVer = Version;
    }
    return ModelVer;
}

static const ShaderVersion GetD3D11ShaderModel(ID3D11Device* pd3d11Device, const ShaderVersion& HLSLVersion)
{
    auto d3dDeviceFeatureLevel = pd3d11Device->GetFeatureLevel();
    switch (d3dDeviceFeatureLevel)
    {
        // Direct3D11 only supports shader model 5.0 even if the device feature level is
        // above 11.0 (for example, 11.1 or 12.0).
        // https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-devices-downlevel-intro#overview-for-each-feature-level
#if defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
        case D3D_FEATURE_LEVEL_12_1:
        case D3D_FEATURE_LEVEL_12_0:
#endif
        case D3D_FEATURE_LEVEL_11_1:
        case D3D_FEATURE_LEVEL_11_0:
            return (HLSLVersion.Major == 0 && HLSLVersion.Minor == 0) ?
                ShaderVersion{5, 0} :
                HLSLValidateShaderVersion(HLSLVersion, {5, 0});

        case D3D_FEATURE_LEVEL_10_1:
            return (HLSLVersion.Major == 0 && HLSLVersion.Minor == 0) ?
                ShaderVersion{4, 1} :
                HLSLValidateShaderVersion(HLSLVersion, {4, 1});

        case D3D_FEATURE_LEVEL_10_0:
            return (HLSLVersion.Major == 0 && HLSLVersion.Minor == 0) ?
                ShaderVersion{4, 0} :
                HLSLValidateShaderVersion(HLSLVersion, {4, 0});

        default:
            UNEXPECTED("Unexpected D3D feature level ", static_cast<Uint32>(d3dDeviceFeatureLevel));
            return ShaderVersion{4, 0};
    }
}

ShaderD3D11Impl::ShaderD3D11Impl(IReferenceCounters*     pRefCounters,
                                 RenderDeviceD3D11Impl*  pRenderDeviceD3D11,
                                 const ShaderCreateInfo& ShaderCI) :
    // clang-format off
    TShaderBase
    {
        pRefCounters,
        pRenderDeviceD3D11,
        ShaderCI.Desc
    },
    ShaderD3DBase{ShaderCI, GetD3D11ShaderModel(pRenderDeviceD3D11->GetD3D11Device(), ShaderCI.HLSLVersion), nullptr}
// clang-format on
{
    auto* pDeviceD3D11 = pRenderDeviceD3D11->GetD3D11Device();
    switch (ShaderCI.Desc.ShaderType)
    {

#define CREATE_SHADER(SHADER_NAME, ShaderName)                                                                                                                                \
    case SHADER_TYPE_##SHADER_NAME:                                                                                                                                           \
    {                                                                                                                                                                         \
        ID3D11##ShaderName##Shader* pShader;                                                                                                                                  \
        HRESULT                     hr = pDeviceD3D11->Create##ShaderName##Shader(m_pShaderByteCode->GetBufferPointer(), m_pShaderByteCode->GetBufferSize(), NULL, &pShader); \
        CHECK_D3D_RESULT_THROW(hr, "Failed to create D3D11 shader");                                                                                                          \
        if (SUCCEEDED(hr))                                                                                                                                                    \
        {                                                                                                                                                                     \
            pShader->QueryInterface(__uuidof(ID3D11DeviceChild), reinterpret_cast<void**>(static_cast<ID3D11DeviceChild**>(&m_pShader)));                                     \
            pShader->Release();                                                                                                                                               \
        }                                                                                                                                                                     \
        break;                                                                                                                                                                \
    }

        CREATE_SHADER(VERTEX, Vertex)
        CREATE_SHADER(PIXEL, Pixel)
        CREATE_SHADER(GEOMETRY, Geometry)
        CREATE_SHADER(DOMAIN, Domain)
        CREATE_SHADER(HULL, Hull)
        CREATE_SHADER(COMPUTE, Compute)

        default: UNEXPECTED("Unknown shader type");
    }

    if (!m_pShader)
        LOG_ERROR_AND_THROW("Failed to create the shader from the byte code");

    if (*m_Desc.Name != 0)
    {
        auto hr = m_pShader->SetPrivateData(WKPDID_D3DDebugObjectName, static_cast<UINT>(strlen(m_Desc.Name)), m_Desc.Name);
        DEV_CHECK_ERR(SUCCEEDED(hr), "Failed to set shader name");
    }

    // Load shader resources
    auto& Allocator  = GetRawAllocator();
    auto* pRawMem    = ALLOCATE(Allocator, "Allocator for ShaderResources", ShaderResourcesD3D11, 1);
    auto* pResources = new (pRawMem) ShaderResourcesD3D11(pRenderDeviceD3D11, m_pShaderByteCode, m_Desc, ShaderCI.UseCombinedTextureSamplers ? ShaderCI.CombinedSamplerSuffix : nullptr);
    m_pShaderResources.reset(pResources, STDDeleterRawMem<ShaderResourcesD3D11>(Allocator));
}

ShaderD3D11Impl::~ShaderD3D11Impl()
{
}

void ShaderD3D11Impl::QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)
{
    if (ppInterface == nullptr)
        return;
    if (IID == IID_ShaderD3D || IID == IID_ShaderD3D11)
    {
        *ppInterface = this;
        (*ppInterface)->AddRef();
    }
    else
    {
        TShaderBase::QueryInterface(IID, ppInterface);
    }
}

} // namespace Diligent