|
| 1 | +/* |
| 2 | + * Copyright 2024 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +/// \file |
| 30 | +/// Defines c++ graphics engine utilities |
| 31 | + |
| 32 | +#include "ShaderSourceFactoryUtils.h" |
| 33 | + |
| 34 | +#include <vector> |
| 35 | +#include <unordered_set> |
| 36 | +#include <string> |
| 37 | + |
| 38 | +#include "../../../Common/interface/RefCntAutoPtr.hpp" |
| 39 | + |
| 40 | +namespace Diligent |
| 41 | +{ |
| 42 | + |
| 43 | +/// C++ wrapper over MemoryShaderSourceFactoryCreateInfo. |
| 44 | +struct MemoryShaderSourceFactoryCreateInfoX |
| 45 | +{ |
| 46 | + MemoryShaderSourceFactoryCreateInfoX() noexcept |
| 47 | + {} |
| 48 | + |
| 49 | + MemoryShaderSourceFactoryCreateInfoX(const MemoryShaderSourceFactoryCreateInfo& _Desc) : |
| 50 | + Desc{_Desc} |
| 51 | + { |
| 52 | + if (Desc.NumSources != 0) |
| 53 | + Sources.assign(Desc.pSources, Desc.pSources + Desc.NumSources); |
| 54 | + |
| 55 | + SyncDesc(true); |
| 56 | + } |
| 57 | + |
| 58 | + MemoryShaderSourceFactoryCreateInfoX(const std::initializer_list<MemoryShaderSourceFileInfo>& _Sources) : |
| 59 | + Sources{_Sources} |
| 60 | + { |
| 61 | + SyncDesc(true); |
| 62 | + } |
| 63 | + |
| 64 | + MemoryShaderSourceFactoryCreateInfoX(const MemoryShaderSourceFactoryCreateInfoX& _DescX) : |
| 65 | + MemoryShaderSourceFactoryCreateInfoX{static_cast<const MemoryShaderSourceFactoryCreateInfo&>(_DescX)} |
| 66 | + {} |
| 67 | + |
| 68 | + MemoryShaderSourceFactoryCreateInfoX& operator=(const MemoryShaderSourceFactoryCreateInfoX& _DescX) |
| 69 | + { |
| 70 | + MemoryShaderSourceFactoryCreateInfoX Copy{_DescX}; |
| 71 | + std::swap(*this, Copy); |
| 72 | + return *this; |
| 73 | + } |
| 74 | + |
| 75 | + MemoryShaderSourceFactoryCreateInfoX(MemoryShaderSourceFactoryCreateInfoX&&) noexcept = default; |
| 76 | + MemoryShaderSourceFactoryCreateInfoX& operator=(MemoryShaderSourceFactoryCreateInfoX&&) noexcept = default; |
| 77 | + |
| 78 | + MemoryShaderSourceFactoryCreateInfoX& Add(const MemoryShaderSourceFileInfo& Elem) |
| 79 | + { |
| 80 | + Sources.push_back(Elem); |
| 81 | + auto& Name = Sources.back().Name; |
| 82 | + Name = StringPool.emplace(Name).first->c_str(); |
| 83 | + SyncDesc(); |
| 84 | + return *this; |
| 85 | + } |
| 86 | + |
| 87 | + template <typename... ArgsType> |
| 88 | + MemoryShaderSourceFactoryCreateInfoX& Add(ArgsType&&... args) |
| 89 | + { |
| 90 | + const MemoryShaderSourceFileInfo Elem{std::forward<ArgsType>(args)...}; |
| 91 | + return Add(Elem); |
| 92 | + } |
| 93 | + |
| 94 | + void Clear() |
| 95 | + { |
| 96 | + MemoryShaderSourceFactoryCreateInfoX EmptyDesc; |
| 97 | + std::swap(*this, EmptyDesc); |
| 98 | + } |
| 99 | + |
| 100 | + const MemoryShaderSourceFactoryCreateInfo& Get() const noexcept |
| 101 | + { |
| 102 | + return Desc; |
| 103 | + } |
| 104 | + |
| 105 | + Uint32 GetNumSources() const noexcept |
| 106 | + { |
| 107 | + return Desc.NumSources; |
| 108 | + } |
| 109 | + |
| 110 | + operator const MemoryShaderSourceFactoryCreateInfo&() const noexcept |
| 111 | + { |
| 112 | + return Desc; |
| 113 | + } |
| 114 | + |
| 115 | + const MemoryShaderSourceFileInfo& operator[](size_t Index) const noexcept |
| 116 | + { |
| 117 | + return Sources[Index]; |
| 118 | + } |
| 119 | + |
| 120 | + MemoryShaderSourceFileInfo& operator[](size_t Index) noexcept |
| 121 | + { |
| 122 | + return Sources[Index]; |
| 123 | + } |
| 124 | + |
| 125 | +private: |
| 126 | + void SyncDesc(bool CopyStrings = false) |
| 127 | + { |
| 128 | + Desc.NumSources = static_cast<Uint32>(Sources.size()); |
| 129 | + Desc.pSources = Desc.NumSources > 0 ? Sources.data() : nullptr; |
| 130 | + |
| 131 | + if (CopyStrings) |
| 132 | + { |
| 133 | + for (auto& Source : Sources) |
| 134 | + Source.Name = StringPool.emplace(Source.Name).first->c_str(); |
| 135 | + } |
| 136 | + } |
| 137 | + MemoryShaderSourceFactoryCreateInfo Desc; |
| 138 | + std::vector<MemoryShaderSourceFileInfo> Sources; |
| 139 | + std::unordered_set<std::string> StringPool; |
| 140 | +}; |
| 141 | + |
| 142 | +inline RefCntAutoPtr<IShaderSourceInputStreamFactory> CreateMemoryShaderSourceFactory(const MemoryShaderSourceFactoryCreateInfo& CI) |
| 143 | +{ |
| 144 | + RefCntAutoPtr<IShaderSourceInputStreamFactory> pFactory; |
| 145 | + CreateMemoryShaderSourceFactory(CI, &pFactory); |
| 146 | + return pFactory; |
| 147 | +} |
| 148 | + |
| 149 | +RefCntAutoPtr<IShaderSourceInputStreamFactory> CreateMemoryShaderSourceFactory(const std::initializer_list<MemoryShaderSourceFileInfo>& Sources) |
| 150 | +{ |
| 151 | + MemoryShaderSourceFactoryCreateInfoX CI{Sources}; |
| 152 | + return CreateMemoryShaderSourceFactory(CI); |
| 153 | +} |
| 154 | + |
| 155 | +} // namespace Diligent |
0 commit comments