Hash :
c4765732
Author :
Date :
2022-12-16T18:07:18
Revert "D3D: Remove ResourceSerial" This reverts commit 214742be91b89087dd26acbae09a070a91730ecc. Reason for revert: Regression https://bugs.chromium.org/p/chromium/issues/detail?id=1401503 Original change's description: > D3D: Remove ResourceSerial > > The only difference between ResourceSerial and UniqueueSerial (formerly > Serial) appears to be ResourceSerial is wrapper of object pointer. This > CL removes ResourceSerial class and switched it to UniqueSerial class > instead. (We may need to see why it needs to use pointer to initialize > the serial, that sounds like the exact thing serial try to avoid.) > > Bug: b/262047600 > Change-Id: Ic8e1b26d7da9304ff02fd9a7e1b584bb70efcb77 > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4102107 > Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> > Commit-Queue: Charlie Lao <cclao@google.com> Bug: b/262047600 Change-Id: I757451a487839db17390dde18f1e2bc5eda36d37 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4114273 Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Charlie Lao <cclao@google.com> Reviewed-by: Yuxin Hu <yuxinhu@google.com> Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com>
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ResourceManager11:
// Centralized point of allocation for all D3D11 Resources.
#ifndef LIBANGLE_RENDERER_D3D_D3D11_RESOURCEFACTORY11_H_
#define LIBANGLE_RENDERER_D3D_D3D11_RESOURCEFACTORY11_H_
#include <array>
#include <atomic>
#include <memory>
#include "common/MemoryBuffer.h"
#include "common/angleutils.h"
#include "common/debug.h"
#include "libANGLE/Error.h"
#include "libANGLE/renderer/serial_utils.h"
namespace rx
{
// These two methods are declared here to prevent circular includes.
namespace d3d11
{
HRESULT SetDebugName(ID3D11DeviceChild *resource,
const char *internalName,
const std::string *khrDebugName);
template <typename T>
HRESULT SetDebugName(angle::ComPtr<T> &resource,
const char *internalName,
const std::string *khrDebugName)
{
return SetDebugName(resource.Get(), internalName, khrDebugName);
}
} // namespace d3d11
namespace d3d
{
class Context;
} // namespace d3d
class Renderer11;
class ResourceManager11;
template <typename T>
class SharedResource11;
class TextureHelper11;
using InputElementArray = WrappedArray<D3D11_INPUT_ELEMENT_DESC>;
using ShaderData = WrappedArray<uint8_t>;
// Format: ResourceType, D3D11 type, DESC type, init data type.
#define ANGLE_RESOURCE_TYPE_OP(NAME, OP) \
OP(NAME, BlendState, ID3D11BlendState, D3D11_BLEND_DESC, void) \
OP(NAME, Buffer, ID3D11Buffer, D3D11_BUFFER_DESC, const D3D11_SUBRESOURCE_DATA) \
OP(NAME, ComputeShader, ID3D11ComputeShader, ShaderData, void) \
OP(NAME, DepthStencilState, ID3D11DepthStencilState, D3D11_DEPTH_STENCIL_DESC, void) \
OP(NAME, DepthStencilView, ID3D11DepthStencilView, D3D11_DEPTH_STENCIL_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, GeometryShader, ID3D11GeometryShader, ShaderData, \
const std::vector<D3D11_SO_DECLARATION_ENTRY>) \
OP(NAME, InputLayout, ID3D11InputLayout, InputElementArray, const ShaderData) \
OP(NAME, PixelShader, ID3D11PixelShader, ShaderData, void) \
OP(NAME, Query, ID3D11Query, D3D11_QUERY_DESC, void) \
OP(NAME, RasterizerState, ID3D11RasterizerState, D3D11_RASTERIZER_DESC, void) \
OP(NAME, RenderTargetView, ID3D11RenderTargetView, D3D11_RENDER_TARGET_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, SamplerState, ID3D11SamplerState, D3D11_SAMPLER_DESC, void) \
OP(NAME, ShaderResourceView, ID3D11ShaderResourceView, D3D11_SHADER_RESOURCE_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, UnorderedAccessView, ID3D11UnorderedAccessView, D3D11_UNORDERED_ACCESS_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, Texture2D, ID3D11Texture2D, D3D11_TEXTURE2D_DESC, const D3D11_SUBRESOURCE_DATA) \
OP(NAME, Texture3D, ID3D11Texture3D, D3D11_TEXTURE3D_DESC, const D3D11_SUBRESOURCE_DATA) \
OP(NAME, VertexShader, ID3D11VertexShader, ShaderData, void)
#define ANGLE_RESOURCE_TYPE_LIST(NAME, RESTYPE, D3D11TYPE, DESCTYPE, INITDATATYPE) RESTYPE,
enum class ResourceType
{
ANGLE_RESOURCE_TYPE_OP(List, ANGLE_RESOURCE_TYPE_LIST) Last
};
#undef ANGLE_RESOURCE_TYPE_LIST
constexpr size_t ResourceTypeIndex(ResourceType resourceType)
{
return static_cast<size_t>(resourceType);
}
constexpr size_t NumResourceTypes = ResourceTypeIndex(ResourceType::Last);
#define ANGLE_RESOURCE_TYPE_TO_D3D11(NAME, RESTYPE, D3D11TYPE, DESCTYPE, INITDATATYPE) \
\
template <> \
struct NAME<ResourceType::RESTYPE> \
{ \
using Value = D3D11TYPE; \
};
#define ANGLE_RESOURCE_TYPE_TO_DESC(NAME, RESTYPE, D3D11TYPE, DESCTYPE, INITDATATYPE) \
\
template <> \
struct NAME<ResourceType::RESTYPE> \
{ \
using Value = DESCTYPE; \
};
#define ANGLE_RESOURCE_TYPE_TO_INIT_DATA(NAME, RESTYPE, D3D11TYPE, DESCTYPE, INITDATATYPE) \
\
template <> \
struct NAME<ResourceType::RESTYPE> \
{ \
using Value = INITDATATYPE; \
};
#define ANGLE_RESOURCE_TYPE_TO_TYPE(NAME, OP) \
template <ResourceType Param> \
struct NAME; \
ANGLE_RESOURCE_TYPE_OP(NAME, OP) \
\
template <ResourceType Param> \
struct NAME \
{}; \
\
template <ResourceType Param> \
using Get##NAME = typename NAME<Param>::Value;
ANGLE_RESOURCE_TYPE_TO_TYPE(D3D11Type, ANGLE_RESOURCE_TYPE_TO_D3D11)
ANGLE_RESOURCE_TYPE_TO_TYPE(DescType, ANGLE_RESOURCE_TYPE_TO_DESC)
ANGLE_RESOURCE_TYPE_TO_TYPE(InitDataType, ANGLE_RESOURCE_TYPE_TO_INIT_DATA)
#undef ANGLE_RESOURCE_TYPE_TO_D3D11
#undef ANGLE_RESOURCE_TYPE_TO_DESC
#undef ANGLE_RESOURCE_TYPE_TO_INIT_DATA
#undef ANGLE_RESOURCE_TYPE_TO_TYPE
#define ANGLE_TYPE_TO_RESOURCE_TYPE(NAME, OP) \
template <typename Param> \
struct NAME; \
ANGLE_RESOURCE_TYPE_OP(NAME, OP) \
\
template <typename Param> \
struct NAME \
{}; \
\
template <typename Param> \
constexpr ResourceType Get##NAME() \
{ \
return NAME<Param>::Value; \
}
#define ANGLE_D3D11_TO_RESOURCE_TYPE(NAME, RESTYPE, D3D11TYPE, DESCTYPE, INITDATATYPE) \
\
template <> \
struct NAME<D3D11TYPE> \
{ \
static constexpr ResourceType Value = ResourceType::RESTYPE; \
};
ANGLE_TYPE_TO_RESOURCE_TYPE(ResourceTypeFromD3D11, ANGLE_D3D11_TO_RESOURCE_TYPE)
#undef ANGLE_D3D11_TO_RESOURCE_TYPE
#undef ANGLE_TYPE_TO_RESOURCE_TYPE
template <typename T>
using GetDescFromD3D11 = GetDescType<ResourceTypeFromD3D11<T>::Value>;
template <typename T>
using GetInitDataFromD3D11 = GetInitDataType<ResourceTypeFromD3D11<T>::Value>;
template <typename T>
constexpr size_t ResourceTypeIndex()
{
return static_cast<size_t>(GetResourceTypeFromD3D11<T>());
}
template <typename T>
struct TypedData
{
TypedData() {}
~TypedData();
T *object = nullptr;
ResourceManager11 *manager = nullptr;
};
// Smart pointer type. Wraps the resource and a factory for safe deletion.
template <typename T, template <class> class Pointer, typename DataT>
class Resource11Base : angle::NonCopyable
{
public:
T *get() const { return mData->object; }
T *const *getPointer() const { return &mData->object; }
void setInternalName(const char *name)
{
mInternalDebugName = name;
UpdateDebugNameWithD3D();
}
void setKHRDebugLabel(const std::string *label)
{
mKhrDebugName = label;
UpdateDebugNameWithD3D();
}
void setLabels(const char *name, const std::string *label)
{
mInternalDebugName = name;
mKhrDebugName = label;
UpdateDebugNameWithD3D();
}
void set(T *object)
{
ASSERT(!valid());
mData->object = object;
}
bool valid() const { return (mData->object != nullptr); }
void reset()
{
if (valid())
mData.reset(new DataT());
}
ResourceSerial getSerial() const
{
return ResourceSerial(reinterpret_cast<uintptr_t>(mData->object));
}
protected:
friend class TextureHelper11;
Resource11Base() : mData(new DataT()) {}
Resource11Base(Resource11Base &&movedObj) : mData(new DataT())
{
std::swap(mData, movedObj.mData);
}
virtual ~Resource11Base() { mData.reset(); }
Resource11Base &operator=(Resource11Base &&movedObj)
{
std::swap(mData, movedObj.mData);
return *this;
}
Pointer<DataT> mData;
private:
void UpdateDebugNameWithD3D()
{
d3d11::SetDebugName(mData->object, mInternalDebugName, mKhrDebugName);
}
const std::string *mKhrDebugName = nullptr;
const char *mInternalDebugName = nullptr;
};
template <typename T>
using UniquePtr = typename std::unique_ptr<T, std::default_delete<T>>;
template <typename ResourceT>
class Resource11 : public Resource11Base<ResourceT, UniquePtr, TypedData<ResourceT>>
{
public:
Resource11() {}
Resource11(Resource11 &&other)
: Resource11Base<ResourceT, UniquePtr, TypedData<ResourceT>>(std::move(other))
{}
Resource11 &operator=(Resource11 &&other)
{
std::swap(this->mData, other.mData);
return *this;
}
private:
template <typename T>
friend class SharedResource11;
friend class ResourceManager11;
Resource11(ResourceT *object, ResourceManager11 *manager)
{
this->mData->object = object;
this->mData->manager = manager;
}
};
template <typename T>
class SharedResource11 : public Resource11Base<T, std::shared_ptr, TypedData<T>>
{
public:
SharedResource11() {}
SharedResource11(SharedResource11 &&movedObj)
: Resource11Base<T, std::shared_ptr, TypedData<T>>(std::move(movedObj))
{}
SharedResource11 &operator=(SharedResource11 &&other)
{
std::swap(this->mData, other.mData);
return *this;
}
SharedResource11 makeCopy() const
{
SharedResource11 copy;
copy.mData = this->mData;
return std::move(copy);
}
private:
friend class ResourceManager11;
SharedResource11(Resource11<T> &&obj) : Resource11Base<T, std::shared_ptr, TypedData<T>>()
{
std::swap(this->mData->manager, obj.mData->manager);
// Can't use std::swap because of ID3D11Resource.
auto temp = this->mData->object;
this->mData->object = obj.mData->object;
obj.mData->object = static_cast<T *>(temp);
}
};
class ResourceManager11 final : angle::NonCopyable
{
public:
ResourceManager11();
~ResourceManager11();
template <typename T>
angle::Result allocate(d3d::Context *context,
Renderer11 *renderer,
const GetDescFromD3D11<T> *desc,
GetInitDataFromD3D11<T> *initData,
Resource11<T> *resourceOut);
template <typename T>
angle::Result allocate(d3d::Context *context,
Renderer11 *renderer,
const GetDescFromD3D11<T> *desc,
GetInitDataFromD3D11<T> *initData,
SharedResource11<T> *sharedRes)
{
Resource11<T> res;
ANGLE_TRY(allocate(context, renderer, desc, initData, &res));
*sharedRes = std::move(res);
return angle::Result::Continue;
}
template <typename T>
void onRelease(T *resource)
{
onReleaseGeneric(GetResourceTypeFromD3D11<T>(), resource);
}
void onReleaseGeneric(ResourceType resourceType, ID3D11DeviceChild *resource);
void setAllocationsInitialized(bool initialize);
private:
void incrResource(ResourceType resourceType, uint64_t memorySize);
void decrResource(ResourceType resourceType, uint64_t memorySize);
template <typename T>
GetInitDataFromD3D11<T> *createInitDataIfNeeded(const GetDescFromD3D11<T> *desc);
bool mInitializeAllocations;
std::array<std::atomic_size_t, NumResourceTypes> mAllocatedResourceCounts;
std::array<std::atomic_uint64_t, NumResourceTypes> mAllocatedResourceDeviceMemory;
angle::MemoryBuffer mZeroMemory;
std::vector<D3D11_SUBRESOURCE_DATA> mShadowInitData;
};
template <typename ResourceT>
TypedData<ResourceT>::~TypedData()
{
if (object)
{
// We can have a nullptr factory when holding passed-in resources.
if (manager)
{
manager->onRelease(object);
}
object->Release();
}
}
#define ANGLE_RESOURCE_TYPE_CLASS(NAME, RESTYPE, D3D11TYPE, DESCTYPE, INITDATATYPE) \
using RESTYPE = Resource11<D3D11TYPE>;
namespace d3d11
{
ANGLE_RESOURCE_TYPE_OP(ClassList, ANGLE_RESOURCE_TYPE_CLASS)
using SharedSRV = SharedResource11<ID3D11ShaderResourceView>;
using SharedUAV = SharedResource11<ID3D11UnorderedAccessView>;
} // namespace d3d11
#undef ANGLE_RESOURCE_TYPE_CLASS
} // namespace rx
#endif // LIBANGLE_RENDERER_D3D_D3D11_RESOURCEFACTORY11_H_