2 // Copyright (c) Microsoft. All rights reserved.
3 // This code is licensed under the MIT License (MIT).
4 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9 // Developed by Minigraph
11 // Author: James Stanard
16 #include "PixelBuffer.h"
21 class ColorBuffer
: public PixelBuffer
24 ColorBuffer( Color ClearColor
= Color(0.0f
, 0.0f
, 0.0f
, 0.0f
) )
25 : m_ClearColor(ClearColor
), m_NumMipMaps(0), m_FragmentCount(1), m_SampleCount(1)
27 m_SRVHandle
.ptr
= D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN
;
28 m_RTVHandle
.ptr
= D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN
;
29 memset(m_UAVHandle
, 0xff, sizeof(m_UAVHandle
));
32 // Create a color buffer from a swap chain buffer. Unordered access is restricted.
33 void CreateFromSwapChain( const std::wstring
& Name
, ID3D12Resource
* BaseResource
);
35 // Create a color buffer. If an address is supplied, memory will not be allocated.
36 // The vmem address allows you to alias buffers (which can be especially useful for
37 // reusing ESRAM across a frame.)
38 void Create(const std::wstring
& Name
, uint32_t Width
, uint32_t Height
, uint32_t NumMips
,
39 DXGI_FORMAT Format
, D3D12_GPU_VIRTUAL_ADDRESS VidMemPtr
= D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN
);
41 // Create a color buffer. Memory will be allocated in ESRAM (on Xbox One). On Windows,
42 // this functions the same as Create() without a video address.
43 void Create(const std::wstring
& Name
, uint32_t Width
, uint32_t Height
, uint32_t NumMips
,
44 DXGI_FORMAT Format
, EsramAllocator
& Allocator
);
46 // Create a color buffer. If an address is supplied, memory will not be allocated.
47 // The vmem address allows you to alias buffers (which can be especially useful for
48 // reusing ESRAM across a frame.)
49 void CreateArray(const std::wstring
& Name
, uint32_t Width
, uint32_t Height
, uint32_t ArrayCount
,
50 DXGI_FORMAT Format
, D3D12_GPU_VIRTUAL_ADDRESS VidMemPtr
= D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN
);
52 // Create a color buffer. Memory will be allocated in ESRAM (on Xbox One). On Windows,
53 // this functions the same as Create() without a video address.
54 void CreateArray(const std::wstring
& Name
, uint32_t Width
, uint32_t Height
, uint32_t ArrayCount
,
55 DXGI_FORMAT Format
, EsramAllocator
& Allocator
);
57 // Get pre-created CPU-visible descriptor handles
58 const D3D12_CPU_DESCRIPTOR_HANDLE
& GetSRV(void) const { return m_SRVHandle
; }
59 const D3D12_CPU_DESCRIPTOR_HANDLE
& GetRTV(void) const { return m_RTVHandle
; }
60 const D3D12_CPU_DESCRIPTOR_HANDLE
& GetUAV(void) const { return m_UAVHandle
[0]; }
62 void SetClearColor( Color ClearColor
) { m_ClearColor
= ClearColor
; }
64 void SetMsaaMode( uint32_t NumColorSamples
, uint32_t NumCoverageSamples
)
66 ASSERT(NumCoverageSamples
>= NumColorSamples
);
67 m_FragmentCount
= NumColorSamples
;
68 m_SampleCount
= NumCoverageSamples
;
71 Color
GetClearColor(void) const { return m_ClearColor
; }
73 // This will work for all texture sizes, but it's recommended for speed and quality
74 // that you use dimensions with powers of two (but not necessarily square.) Pass
75 // 0 for ArrayCount to reserve space for mips at creation time.
76 void GenerateMipMaps(CommandContext
& Context
);
80 D3D12_RESOURCE_FLAGS
CombineResourceFlags( void ) const
82 D3D12_RESOURCE_FLAGS Flags
= D3D12_RESOURCE_FLAG_NONE
;
84 if (Flags
== D3D12_RESOURCE_FLAG_NONE
&& m_FragmentCount
== 1)
85 Flags
|= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS
;
87 return D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET
| Flags
;
90 // Compute the number of texture levels needed to reduce to 1x1. This uses
91 // _BitScanReverse to find the highest set bit. Each dimension reduces by
92 // half and truncates bits. The dimension 256 (0x100) has 9 mip levels, same
93 // as the dimension 511 (0x1FF).
94 static inline uint32_t ComputeNumMips(uint32_t Width
, uint32_t Height
)
96 unsigned long HighBit
;
97 _BitScanReverse(&HighBit
, Width
| Height
);
101 void CreateDerivedViews(ID3D12Device
* Device
, DXGI_FORMAT Format
, uint32_t ArraySize
, uint32_t NumMips
= 1);
104 D3D12_CPU_DESCRIPTOR_HANDLE m_SRVHandle
;
105 D3D12_CPU_DESCRIPTOR_HANDLE m_RTVHandle
;
106 D3D12_CPU_DESCRIPTOR_HANDLE m_UAVHandle
[12];
107 uint32_t m_NumMipMaps
; // number of texture sublevels
108 uint32_t m_FragmentCount
;
109 uint32_t m_SampleCount
;