public static H1GpuMemoryPageTexture2D CreatePage(H1GpuMemoryChunk owner, H1Texture2D.Description desc)
        {
            H1GpuMemoryPageTexture2D newTexture2D = new H1GpuMemoryPageTexture2D(owner);

            // convert texture1D description to appropriate input parameters to create texture1D
            H1PixelFormat format           = desc.Format;
            Int32         width            = Convert.ToInt32(desc.Width);
            Int32         height           = Convert.ToInt32(desc.Height);
            Int32         depthOrArraySize = Convert.ToInt32(desc.ArraySize);

            H1OptionalParametersForTexture options = null;

            if (desc.MipLevels != 1 || desc.SampleDesc.Count != 1 || desc.SampleDesc.Quality != 0) // if there is any optional property to apply
            {
                options = new H1OptionalParametersForTexture()
                {
                    MipLevels  = Convert.ToUInt16(desc.MipLevels),
                    SampleDesc = new H1SampleDescription()
                    {
                        Count   = desc.SampleDesc.Count,
                        Quality = desc.SampleDesc.Quality,
                    }
                };
            }

            if (!newTexture2D.CreatePage(format, width, height, depthOrArraySize, options))
            {
                return(null);
            }

            return(newTexture2D);
        }
        public static H1GpuTexture2D Create(Vector4 clearValue, H1Texture2D.Description desc, H1SubresourceData initialData)
        {
            H1GpuTexture2D newTex2D = new H1GpuTexture2D();

            CreatePlatformDependent(clearValue, desc, initialData, ref newTex2D);

            if (newTex2D.Resource == null)
            {
                newTex2D.Destroy();
                return(null);
            }

            return(newTex2D);
        }
        static void CreatePlatformDependent(Vector4 clearValue, H1Texture2D.Description desc, H1SubresourceData initialData, ref H1GpuTexture2D result)
        {
            // get device for directX 12
            Device deviceDX12 = H1Global <H1ManagedRenderer> .Instance.Device;

            H1GpuResourceDesc desc12         = H1RHIDefinitionHelper.Texture2DDescToGpuResourceDesc(desc);
            H1HeapType        heapType       = H1RHIDefinitionHelper.GetHeapTypeFromTexture2DDesc(desc);
            H1ResourceStates  resourceStates = H1RHIDefinitionHelper.GetResourceStatesFromTexture2DDesc(desc);

            // generate resource
            //if (result != null)
            //    result.Resource.CreateResource(heapType, desc12, resourceStates);

            // generate RHI resource description (need resource description for generating UAV or SRV or etc.)
            result.CreateResourceDescription(desc12);
        }
        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);
        }