1
1
use crate :: descriptor_heap:: { CpuStagingHeap , RenderTargetHeap } ;
2
2
use crate :: error:: FilterChainError ;
3
- use crate :: filter_chain :: FrameResiduals ;
3
+ use crate :: resource :: { OutlivesFrame , ResourceHandleStrategy } ;
4
4
use crate :: texture:: { D3D12OutputView , InputTexture } ;
5
5
use crate :: util:: d3d12_get_closest_format;
6
6
use crate :: { error, util} ;
@@ -17,12 +17,13 @@ use parking_lot::Mutex;
17
17
use std:: mem:: ManuallyDrop ;
18
18
use std:: sync:: Arc ;
19
19
use windows:: Win32 :: Graphics :: Direct3D12 :: {
20
- ID3D12Device , ID3D12GraphicsCommandList , D3D12_BOX , D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING ,
21
- D3D12_FEATURE_DATA_FORMAT_SUPPORT , D3D12_FORMAT_SUPPORT1_MIP ,
22
- D3D12_FORMAT_SUPPORT1_RENDER_TARGET , D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE ,
23
- D3D12_FORMAT_SUPPORT1_TEXTURE2D , D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD ,
24
- D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE , D3D12_RENDER_TARGET_VIEW_DESC ,
25
- D3D12_RENDER_TARGET_VIEW_DESC_0 , D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES , D3D12_RESOURCE_DESC ,
20
+ ID3D12Device , ID3D12GraphicsCommandList , ID3D12Resource , D3D12_BOX ,
21
+ D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING , D3D12_FEATURE_DATA_FORMAT_SUPPORT ,
22
+ D3D12_FORMAT_SUPPORT1_MIP , D3D12_FORMAT_SUPPORT1_RENDER_TARGET ,
23
+ D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE , D3D12_FORMAT_SUPPORT1_TEXTURE2D ,
24
+ D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD , D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE ,
25
+ D3D12_RENDER_TARGET_VIEW_DESC , D3D12_RENDER_TARGET_VIEW_DESC_0 ,
26
+ D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES , D3D12_RESOURCE_DESC ,
26
27
D3D12_RESOURCE_DIMENSION_TEXTURE2D , D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET ,
27
28
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ,
28
29
D3D12_RESOURCE_STATE_COPY_SOURCE , D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
@@ -36,6 +37,7 @@ use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT, DXGI_SAMPLE_DESC};
36
37
#[ derive( Debug ) ]
37
38
pub ( crate ) struct OwnedImage {
38
39
pub ( crate ) handle : ManuallyDrop < Resource > ,
40
+ pub ( crate ) resource : ManuallyDrop < ID3D12Resource > ,
39
41
pub ( crate ) size : Size < u32 > ,
40
42
pub ( crate ) format : DXGI_FORMAT ,
41
43
pub ( crate ) max_mipmap : u16 ,
@@ -113,7 +115,7 @@ impl OwnedImage {
113
115
114
116
desc. Format = d3d12_get_closest_format ( device, format_support) ;
115
117
116
- let resource = allocator. lock ( ) . create_resource ( & ResourceCreateDesc {
118
+ let allocator_resource = allocator. lock ( ) . create_resource ( & ResourceCreateDesc {
117
119
name : "ownedimage" ,
118
120
memory_location : MemoryLocation :: GpuOnly ,
119
121
resource_category : ResourceCategory :: RtvDsvTexture ,
@@ -145,8 +147,10 @@ impl OwnedImage {
145
147
// }
146
148
// assume_d3d12_init!(resource, "CreateCommittedResource");
147
149
150
+ let resource = ManuallyDrop :: new ( allocator_resource. resource ( ) . clone ( ) ) ;
148
151
Ok ( OwnedImage {
149
- handle : ManuallyDrop :: new ( resource) ,
152
+ handle : ManuallyDrop :: new ( allocator_resource) ,
153
+ resource,
150
154
size,
151
155
format : desc. Format ,
152
156
device : device. clone ( ) ,
@@ -161,17 +165,16 @@ impl OwnedImage {
161
165
& self ,
162
166
cmd : & ID3D12GraphicsCommandList ,
163
167
input : & InputTexture ,
164
- gc : & mut FrameResiduals ,
165
168
) -> error:: Result < ( ) > {
166
169
let barriers = [
167
- util:: d3d12_get_resource_transition_subresource (
170
+ util:: d3d12_get_resource_transition_subresource :: < OutlivesFrame , _ > (
168
171
& input. resource ,
169
172
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
170
173
D3D12_RESOURCE_STATE_COPY_SOURCE ,
171
174
D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES ,
172
175
) ,
173
- util:: d3d12_get_resource_transition_subresource (
174
- & self . handle . resource ( ) ,
176
+ util:: d3d12_get_resource_transition_subresource :: < OutlivesFrame , _ > (
177
+ & self . resource ,
175
178
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
176
179
D3D12_RESOURCE_STATE_COPY_DEST ,
177
180
D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES ,
@@ -180,18 +183,17 @@ impl OwnedImage {
180
183
181
184
unsafe {
182
185
cmd. ResourceBarrier ( & barriers) ;
183
- gc. dispose_barriers ( barriers) ;
184
186
185
187
let dst = D3D12_TEXTURE_COPY_LOCATION {
186
- pResource : ManuallyDrop :: new ( Some ( self . handle . resource ( ) . clone ( ) ) ) ,
188
+ pResource : OutlivesFrame :: obtain ( & self . resource ) ,
187
189
Type : D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX ,
188
190
Anonymous : D3D12_TEXTURE_COPY_LOCATION_0 {
189
191
SubresourceIndex : 0 ,
190
192
} ,
191
193
} ;
192
194
193
195
let src = D3D12_TEXTURE_COPY_LOCATION {
194
- pResource : ManuallyDrop :: new ( Some ( input. resource . clone ( ) ) ) ,
196
+ pResource : OutlivesFrame :: obtain ( & input. resource ) ,
195
197
Type : D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX ,
196
198
Anonymous : D3D12_TEXTURE_COPY_LOCATION_0 {
197
199
SubresourceIndex : 0 ,
@@ -213,20 +215,17 @@ impl OwnedImage {
213
215
back : 1 ,
214
216
} ) ,
215
217
) ;
216
-
217
- gc. dispose_resource ( dst. pResource ) ;
218
- gc. dispose_resource ( src. pResource ) ;
219
218
}
220
219
221
220
let barriers = [
222
- util:: d3d12_get_resource_transition_subresource (
221
+ util:: d3d12_get_resource_transition_subresource :: < OutlivesFrame , _ > (
223
222
& input. resource ,
224
223
D3D12_RESOURCE_STATE_COPY_SOURCE ,
225
224
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
226
225
D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES ,
227
226
) ,
228
- util:: d3d12_get_resource_transition_subresource (
229
- & self . handle . resource ( ) ,
227
+ util:: d3d12_get_resource_transition_subresource :: < OutlivesFrame , _ > (
228
+ & self . resource ,
230
229
D3D12_RESOURCE_STATE_COPY_DEST ,
231
230
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
232
231
D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES ,
@@ -237,34 +236,33 @@ impl OwnedImage {
237
236
cmd. ResourceBarrier ( & barriers) ;
238
237
}
239
238
240
- gc. dispose_barriers ( barriers) ;
241
-
242
239
Ok ( ( ) )
243
240
}
244
241
245
242
pub fn clear (
246
243
& self ,
247
244
cmd : & ID3D12GraphicsCommandList ,
248
245
heap : & mut D3D12DescriptorHeap < RenderTargetHeap > ,
249
- gc : & mut FrameResiduals ,
250
246
) -> error:: Result < ( ) > {
251
- gc. dispose_barriers ( util:: d3d12_resource_transition (
252
- cmd,
253
- & self . handle . resource ( ) ,
254
- D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
255
- D3D12_RESOURCE_STATE_RENDER_TARGET ,
256
- ) ) ;
247
+ unsafe {
248
+ util:: d3d12_resource_transition :: < OutlivesFrame , _ > (
249
+ cmd,
250
+ & self . resource ,
251
+ D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
252
+ D3D12_RESOURCE_STATE_RENDER_TARGET ,
253
+ ) ;
257
254
258
- let rtv = self . create_render_target_view ( heap) ?;
255
+ let rtv = self . create_render_target_view ( heap) ?;
259
256
260
- unsafe { cmd. ClearRenderTargetView ( * rtv. descriptor . as_ref ( ) , CLEAR , None ) }
257
+ cmd. ClearRenderTargetView ( * rtv. descriptor . as_ref ( ) , CLEAR , None ) ;
261
258
262
- gc. dispose_barriers ( util:: d3d12_resource_transition (
263
- cmd,
264
- & self . handle . resource ( ) ,
265
- D3D12_RESOURCE_STATE_RENDER_TARGET ,
266
- D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
267
- ) ) ;
259
+ util:: d3d12_resource_transition :: < OutlivesFrame , _ > (
260
+ cmd,
261
+ & self . resource ,
262
+ D3D12_RESOURCE_STATE_RENDER_TARGET ,
263
+ D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE ,
264
+ ) ;
265
+ }
268
266
269
267
Ok ( ( ) )
270
268
}
@@ -297,8 +295,8 @@ impl OwnedImage {
297
295
) ;
298
296
}
299
297
300
- Ok ( InputTexture :: new (
301
- self . handle . resource ( ) . clone ( ) ,
298
+ Ok ( InputTexture :: new :: < OutlivesFrame , _ > (
299
+ & self . resource ,
302
300
descriptor,
303
301
self . size ,
304
302
self . format ,
@@ -390,6 +388,10 @@ impl ScaleFramebuffer for OwnedImage {
390
388
391
389
impl Drop for OwnedImage {
392
390
fn drop ( & mut self ) {
391
+ // let go of the handle
392
+ unsafe {
393
+ ManuallyDrop :: drop ( & mut self . resource ) ;
394
+ }
393
395
let resource = unsafe { ManuallyDrop :: take ( & mut self . handle ) } ;
394
396
if let Err ( e) = self . allocator . lock ( ) . free_resource ( resource) {
395
397
println ! ( "librashader-runtime-d3d12: [warn] failed to deallocate owned image buffer memory {e}" )
0 commit comments