예제 #1
0
        public int CreateTexture2D(H1PixelFormat elementType, int width, int height, Vector4 clearValue, H1SubresourceData[] subResources)
        {
            // @TODO - I will change change this more flexible memory management with 'Heap' class in SharpDX
            HeapProperties heapProperties = new HeapProperties(
                HeapType.Upload // specifies a heap for uploading, it has CPU access optimized for uploading to the GPU - best for CPU-write-once, GPU-read-once
                );

            // HeapType (from msdn)
            // 1. Upload Heaps - a heap for loading data from the CPU to the GPU, typically containing vertex, index or constant buffers or textures and other resources
            // 2. Readback Heaps - a heap for loading data back from the GPU to the CPU. Typically data that would be collected by the GPU and downloaded to the CPU would be image data from
            //                     a captured screen shot and statistical and performance data such as counters and timings
            // 3. Default Heaps - a heap which supports all GPU operations. This resource heap is focused on containing data that is persistently required by the GPU, such as index and vertex data that maybe required for many frames

            // Resources (from msdn)
            // 1. Commited resources
            //    - commited resources are the most common idea of D3D resources over the generations.
            //    - creating such a resource allocates virtual address range, an implicit heap large enough to fit the whole resource and commits the virtual address range to the physical memory encapsulated by the heap
            //    - the implicit heap properties must be passed to match functional parity with previous D3D versions.
            // 2. Reserved resources
            //    - Reserved resources are equivalent to D3D11 tiled resources.
            //    - On their creation, only a virtual address range is allocated and not mapped to any heap.
            //    - the application will map such resources to heaps latere
            //    - the capabilities of such resources are currently unchanged over D3D11, as they can be mapped to a heap at a 64KB tile granularity with 'UpdateTileMappings'
            // 3. Placed resources
            //    - new for D3D12, applications may create heaps seperate from resources.
            //    - afterward, the application may locate multiple resources within a single heap.
            //    - this can be done without creating tiled or reserved resources, enabling the capabilities for all resource types able to be created directly by applications.
            //    - multiple resources may overlap, and the application must use the 'TiledResourceBarrier' to re-use physical memory correctly.

            // create new resource
            H1Texture2D.Description desc = new H1Texture2D.Description();
            desc.Width              = Convert.ToUInt32(width);
            desc.Height             = Convert.ToUInt32(height);
            desc.Format             = elementType;
            desc.SampleDesc.Count   = 1;
            desc.SampleDesc.Quality = 0;
            desc.MipLevels          = 0;
            desc.ArraySize          = 1;

            Direct3D12.H1DX12Texture2D texture2D = Direct3D12.H1DX12Texture2D.Create(Device, clearValue, desc, subResources);

            Int32 index = m_Texture2Ds.Count;

            m_Texture2Ds.Add(texture2D);

            return(index);
        }
        public static H1DX12Texture2D Create(Device device, Vector4 clearValue, H1Texture2D.Description desc, H1SubresourceData[] initialData)
        {
            // converting description to DX12 description
            ResourceDescription desc12 = new ResourceDescription
            {
                Format           = H1RHIDefinitionHelper.ConvertToFormat(desc.Format),
                Width            = Convert.ToInt32(desc.Width),
                Height           = Convert.ToInt32(desc.Height),
                DepthOrArraySize = Convert.ToInt16(desc.ArraySize),
                MipLevels        = Convert.ToInt16(desc.MipLevels),
                Flags            = ResourceFlags.None,
                Layout           = TextureLayout.Unknown,
                Dimension        = ResourceDimension.Texture2D,
            };

            desc12.SampleDescription.Count   = Convert.ToInt32(desc.SampleDesc.Count);
            desc12.SampleDescription.Quality = Convert.ToInt32(desc.SampleDesc.Quality);

            HeapProperties heapProperties  = new HeapProperties(HeapType.Default);
            ResourceStates resourceUsage   = ResourceStates.CopyDestination;
            ClearValue     value           = H1GlobalDX12Definitions.GetDXGIFormatClearValue(desc12.Format, (desc.BindFlags & Convert.ToUInt32(H1BindFlag.DepthStencil)) != 0);
            Boolean        allowClearValue = desc12.Dimension == ResourceDimension.Buffer;

            if (desc.Usage == H1Usage.Immutable)
            {
                heapProperties = new HeapProperties(HeapType.Default);
                resourceUsage  = ResourceStates.CopyDestination;
            }

            else if (desc.Usage == H1Usage.Staging)
            {
                heapProperties = new HeapProperties(HeapType.Readback);
                resourceUsage  = ResourceStates.CopyDestination;
            }

            else if (desc.Usage == H1Usage.Dynamic)
            {
                heapProperties = new HeapProperties(HeapType.Upload);
                resourceUsage  = ResourceStates.GenericRead;
            }

            if (desc.CPUAccessFlags != 0)
            {
                if (desc.CPUAccessFlags == Convert.ToUInt32(H1CPUAccessFlag.Write))
                {
                    heapProperties = new HeapProperties(HeapType.Upload);
                    resourceUsage  = ResourceStates.GenericRead;
                }

                else if (desc.CPUAccessFlags == Convert.ToUInt32(H1CPUAccessFlag.Read))
                {
                    heapProperties = new HeapProperties(HeapType.Readback);
                    resourceUsage  = ResourceStates.CopyDestination;
                }

                else
                {
                    return(null);
                }
            }

            if (clearValue != null)
            {
                if ((desc.BindFlags & Convert.ToUInt32(H1BindFlag.DepthStencil)) != 0)
                {
                    value.DepthStencil.Depth   = clearValue.X;
                    value.DepthStencil.Stencil = Convert.ToByte(clearValue.Y);
                }
                else
                {
                    value.Color = clearValue;
                }
            }

            if ((desc.BindFlags & Convert.ToUInt32(H1BindFlag.UnorderedAccess)) != 0)
            {
                desc12.Flags |= ResourceFlags.AllowUnorderedAccess;
                resourceUsage = ResourceStates.UnorderedAccess;
            }

            if ((desc.BindFlags & Convert.ToUInt32(H1BindFlag.DepthStencil)) != 0)
            {
                desc12.Flags   |= ResourceFlags.AllowDepthStencil;
                allowClearValue = true;
                resourceUsage   = ResourceStates.DepthWrite;
            }

            if ((desc.BindFlags & Convert.ToUInt32(H1BindFlag.RenderTarget)) != 0)
            {
                desc12.Flags   |= ResourceFlags.AllowRenderTarget;
                allowClearValue = true;
                resourceUsage   = ResourceStates.RenderTarget;
            }

            Resource resource = device.CreateCommittedResource(
                heapProperties,
                HeapFlags.None,
                desc12,
                resourceUsage,
                allowClearValue ? value : default(ClearValue?));

            H1DX12Texture2D newTexture = new H1DX12Texture2D();

            newTexture.m_Resource = new H1DX12Resource(resource, resourceUsage, resource.Description, initialData, Convert.ToUInt32(desc12.DepthOrArraySize * desc12.MipLevels));
            return(newTexture);
        }