|
3 | 3 |
|
4 | 4 | #if STRIDE_GRAPHICS_API_DIRECT3D11 |
5 | 5 |
|
| 6 | +using Silk.NET.Core.Native; |
6 | 7 | using Silk.NET.Direct3D11; |
7 | | - |
8 | | -using static Stride.Graphics.DebugHelpers; |
| 8 | +using Stride.Core; |
9 | 9 |
|
10 | 10 | namespace Stride.Graphics |
11 | 11 | { |
12 | | - /// <summary> |
13 | | - /// GraphicsResource class |
14 | | - /// </summary> |
15 | 12 | public abstract unsafe partial class GraphicsResource |
16 | 13 | { |
17 | | - private ID3D11ShaderResourceView* shaderResourceView; |
18 | | - private ID3D11UnorderedAccessView* unorderedAccessView; |
| 14 | + private ComPtr<ID3D11ShaderResourceView> shaderResourceView; |
| 15 | + private ComPtr<ID3D11UnorderedAccessView> unorderedAccessView; |
19 | 16 |
|
20 | | - // Used to internally force a WriteDiscard (to force a rename) with the GraphicsResourceAllocator |
| 17 | + /// <summary> |
| 18 | + /// Used to internally force a <c>WriteDiscard</c> (to force a rename) with the <see cref="GraphicsResourceAllocator"/>. |
| 19 | + /// </summary> |
21 | 20 | internal bool DiscardNextMap; |
22 | 21 |
|
| 22 | + /// <summary> |
| 23 | + /// Gets a value indicating whether this graphics resource is in "Debug mode". |
| 24 | + /// </summary> |
| 25 | + /// <value> |
| 26 | + /// <see langword="true"/> if this graphics resource is initialized in "Debug mode"; otherwise, <see langword="false"/>. |
| 27 | + /// </value> |
23 | 28 | protected bool IsDebugMode => GraphicsDevice?.IsDebugMode == true; |
24 | 29 |
|
| 30 | + /// <inheritdoc/> |
25 | 31 | protected override void OnNameChanged() |
26 | 32 | { |
27 | 33 | base.OnNameChanged(); |
28 | 34 |
|
29 | 35 | if (IsDebugMode) |
30 | 36 | { |
31 | | - if (shaderResourceView != null) |
| 37 | + if (shaderResourceView.Handle != null) |
32 | 38 | { |
33 | | - SetDebugName((ID3D11DeviceChild*) shaderResourceView, Name is null ? null : $"{Name} SRV"); |
| 39 | + using var srv = shaderResourceView.QueryInterface<ID3D11DeviceChild>(); |
| 40 | + srv.SetDebugName(Name is null ? null : $"{Name} SRV", owningObject: this); |
34 | 41 | } |
35 | | - if (unorderedAccessView != null) |
| 42 | + if (unorderedAccessView.Handle != null) |
36 | 43 | { |
37 | | - SetDebugName((ID3D11DeviceChild*) unorderedAccessView, Name is null ? null : $"{Name} UAV"); |
| 44 | + using var uav = unorderedAccessView.QueryInterface<ID3D11DeviceChild>(); |
| 45 | + uav.SetDebugName(Name is null ? null : $"{Name} UAV", owningObject: this); |
38 | 46 | } |
39 | 47 | } |
40 | 48 | } |
41 | 49 |
|
42 | 50 | /// <summary> |
43 | | - /// Gets or sets the ShaderResourceView attached to this GraphicsResource. |
44 | | - /// Note that only Texture, Texture3D, RenderTarget2D, RenderTarget3D, DepthStencil are using this ShaderResourceView |
| 51 | + /// Gets or sets the <see cref="ID3D11ShaderResourceView"/> attached to this <see cref="GraphicsResource"/>. |
45 | 52 | /// </summary> |
46 | | - /// <value>The device child.</value> |
47 | | - protected internal ID3D11ShaderResourceView* NativeShaderResourceView |
| 53 | + /// <value>The Shader Resource View associated with this graphics resource.</value> |
| 54 | + /// <remarks> |
| 55 | + /// Only <see cref="Texture"/>s are using this Shader Resource View. |
| 56 | + /// </remarks> |
| 57 | + protected internal ComPtr<ID3D11ShaderResourceView> NativeShaderResourceView |
48 | 58 | { |
49 | 59 | get => shaderResourceView; |
50 | | - |
51 | 60 | set |
52 | 61 | { |
| 62 | + var previousShaderResourceView = shaderResourceView; |
| 63 | + |
53 | 64 | shaderResourceView = value; |
54 | 65 |
|
55 | | - if (IsDebugMode && shaderResourceView != null) |
| 66 | + if (shaderResourceView.Handle != previousShaderResourceView.Handle) |
| 67 | + { |
| 68 | + previousShaderResourceView.RemoveDisposeBy(this); |
| 69 | + previousShaderResourceView.Release(); |
| 70 | + |
| 71 | + shaderResourceView.DisposeBy(this); |
| 72 | + } |
| 73 | + |
| 74 | + if (IsDebugMode && shaderResourceView.Handle != null) |
56 | 75 | { |
57 | | - SetDebugName((ID3D11DeviceChild*)shaderResourceView, Name is null ? null : $"{Name} SRV"); |
| 76 | + using var srv = shaderResourceView.QueryInterface<ID3D11DeviceChild>(); |
| 77 | + srv.SetDebugName(Name is null ? null : $"{Name} SRV", owningObject: this); |
58 | 78 | } |
59 | 79 | } |
60 | 80 | } |
61 | 81 |
|
62 | 82 | /// <summary> |
63 | | - /// Gets or sets the UnorderedAccessView attached to this GraphicsResource. |
| 83 | + /// Gets or sets the <see cref="ID3D11UnorderedAccessView"/> attached to this <see cref="GraphicsResource"/>. |
64 | 84 | /// </summary> |
65 | | - /// <value>The device child.</value> |
| 85 | + /// <value>The Unordered Access View associated with this graphics resource.</value> |
66 | 86 | protected internal ID3D11UnorderedAccessView* NativeUnorderedAccessView |
67 | 87 | { |
68 | 88 | get => unorderedAccessView; |
69 | | - |
70 | 89 | set |
71 | 90 | { |
| 91 | + var previousUnorderedAccessView = unorderedAccessView; |
| 92 | + |
72 | 93 | unorderedAccessView = value; |
73 | 94 |
|
74 | | - if (IsDebugMode && unorderedAccessView != null) |
| 95 | + if (unorderedAccessView.Handle != previousUnorderedAccessView.Handle) |
75 | 96 | { |
76 | | - SetDebugName((ID3D11DeviceChild*)unorderedAccessView, Name is null ? null : $"{Name} UAV"); |
77 | | - } |
78 | | - } |
79 | | - } |
| 97 | + previousUnorderedAccessView.RemoveDisposeBy(this); |
| 98 | + previousUnorderedAccessView.Release(); |
80 | 99 |
|
81 | | - protected internal override void OnDestroyed() |
82 | | - { |
83 | | - if (shaderResourceView != null) |
84 | | - shaderResourceView->Release(); |
85 | | - |
86 | | - if (unorderedAccessView != null) |
87 | | - unorderedAccessView->Release(); |
| 100 | + unorderedAccessView.DisposeBy(this); |
| 101 | + } |
88 | 102 |
|
89 | | - base.OnDestroyed(); |
| 103 | + if (IsDebugMode && unorderedAccessView.Handle != null) |
| 104 | + { |
| 105 | + using var uav = unorderedAccessView.QueryInterface<ID3D11DeviceChild>(); |
| 106 | + uav.SetDebugName(Name is null ? null : $"{Name} UAV", owningObject: this); |
| 107 | + } |
| 108 | + } |
90 | 109 | } |
91 | 110 | } |
92 | 111 | } |
|
0 commit comments