Example #1
0
        public D3DSamplerState(
            Device device,
            SamplerAddressMode addressU,
            SamplerAddressMode addressV,
            SamplerAddressMode addressW,
            SamplerFilter filter,
            int maxAnisotropy,
            RgbaFloat borderColor,
            DepthComparison comparison,
            int minimumLod,
            int maximumLod,
            int lodBias)
        {
            SamplerStateDescription ssd = new SamplerStateDescription();

            ssd.AddressU           = D3DFormats.VeldridToD3DSamplerAddressMode(addressU);
            ssd.AddressV           = D3DFormats.VeldridToD3DSamplerAddressMode(addressV);
            ssd.AddressW           = D3DFormats.VeldridToD3DSamplerAddressMode(addressW);
            ssd.Filter             = D3DFormats.VeldridToD3DSamplerFilter(filter);
            ssd.MaximumAnisotropy  = maxAnisotropy;
            ssd.BorderColor        = new RawColor4(borderColor.R, borderColor.G, borderColor.B, borderColor.A);
            ssd.ComparisonFunction = D3DFormats.VeldridToD3DDepthComparison(comparison);
            ssd.MinimumLod         = minimumLod;
            ssd.MaximumLod         = maximumLod;
            ssd.MipLodBias         = lodBias;
            _d3dSamplerState       = new SharpDX.Direct3D11.SamplerState(device, ssd);
        }
Example #2
0
        public void SetIndices <T>(T[] indices, IndexFormat format, int stride, int elementOffset) where T : struct
        {
            _format = D3DFormats.ConvertIndexFormat(format);
            int elementSizeInBytes = Unsafe.SizeOf <T>();

            SetData(indices, elementSizeInBytes * indices.Length, elementOffset * elementSizeInBytes);
        }
Example #3
0
        public D3DBlendState(
            Device device, bool isBlendEnabled,
            Blend srcAlpha, Blend destAlpha, BlendFunction alphaBlendFunc,
            Blend srcColor, Blend destColor, BlendFunction colorBlendFunc,
            RgbaFloat blendFactor)
        {
            _device               = device;
            IsBlendEnabled        = isBlendEnabled;
            SourceAlphaBlend      = srcAlpha;
            DestinationAlphaBlend = destAlpha;
            AlphaBlendFunction    = alphaBlendFunc;
            SourceColorBlend      = srcColor;
            DestinationColorBlend = destColor;
            ColorBlendFunction    = colorBlendFunc;
            BlendFactor           = blendFactor;

            var desc = new BlendStateDescription();

            desc.RenderTarget[0].SourceAlphaBlend      = D3DFormats.VeldridToD3DBlend(SourceAlphaBlend);
            desc.RenderTarget[0].DestinationAlphaBlend = D3DFormats.VeldridToD3DBlend(DestinationAlphaBlend);
            desc.RenderTarget[0].AlphaBlendOperation   = D3DFormats.VeldridToD3DBlendFunction(AlphaBlendFunction);
            desc.RenderTarget[0].SourceBlend           = D3DFormats.VeldridToD3DBlend(SourceColorBlend);
            desc.RenderTarget[0].DestinationBlend      = D3DFormats.VeldridToD3DBlend(DestinationColorBlend);
            desc.RenderTarget[0].BlendOperation        = D3DFormats.VeldridToD3DBlendFunction(ColorBlendFunction);
            desc.RenderTarget[0].IsBlendEnabled        = isBlendEnabled;
            desc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            _deviceBlendState = new SharpDX.Direct3D11.BlendState(_device, desc);
        }
Example #4
0
        private static InputLayout CreateLayout(Device device, byte[] shaderBytecode, MaterialVertexInput[] vertexInputs)
        {
            int count   = vertexInputs.Sum(mvi => mvi.Elements.Length);
            int element = 0;

            InputElement[]  elements       = new InputElement[count];
            SemanticIndices indicesTracker = new SemanticIndices();

            for (int vbSlot = 0; vbSlot < vertexInputs.Length; vbSlot++)
            {
                MaterialVertexInput bufferInput = vertexInputs[vbSlot];
                int numElements   = bufferInput.Elements.Length;
                int currentOffset = 0;
                for (int i = 0; i < numElements; i++)
                {
                    var genericElement = bufferInput.Elements[i];
                    elements[element] = new InputElement(
                        GetSemanticName(genericElement.SemanticType),
                        indicesTracker.GetAndIncrement(genericElement.SemanticType),
                        ConvertGenericFormat(genericElement.ElementFormat),
                        currentOffset,
                        vbSlot,
                        D3DFormats.ConvertInputClass(genericElement.StorageClassifier),
                        genericElement.InstanceStepRate);
                    currentOffset += genericElement.SizeInBytes;
                    element       += 1;
                }
            }

            return(new InputLayout(device, shaderBytecode, elements));
        }
Example #5
0
        public void SetIndices(IntPtr indices, IndexFormat format, int count, int elementOffset)
        {
            int elementSizeInBytes = format == IndexFormat.UInt16 ? sizeof(ushort) : sizeof(uint);

            SetData(indices, elementSizeInBytes * count, elementSizeInBytes * elementOffset);
            SharpDX.DXGI.Format dxgiFormat = D3DFormats.VeldridToD3DIndexFormat(format);
            _format = dxgiFormat;
        }
Example #6
0
        public void GetTextureData(int mipLevel, IntPtr destination, int storageSizeInBytes)
        {
            int width  = MipmapHelper.GetDimension(Width, mipLevel);
            int height = MipmapHelper.GetDimension(Height, mipLevel);

            D3DTexture2D stagingTexture = new D3DTexture2D(_device, new Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                Usage             = ResourceUsage.Staging,
                BindFlags         = BindFlags.None,
                CpuAccessFlags    = CpuAccessFlags.Read,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Format            = DeviceTexture.Description.Format
            });

            // Copy the data from the GPU to the staging texture.
            _device.ImmediateContext.CopySubresourceRegion(DeviceTexture, mipLevel, null, stagingTexture.DeviceTexture, 0);

            int elementCount = width * height;
            // Copy the data to the array.
            DataBox db = _device.ImmediateContext.MapSubresource(
                stagingTexture.DeviceTexture,
                0,
                MapMode.Read,
                MapFlags.None,
                out DataStream ds);

            int pixelSizeInBytes = D3DFormats.GetPixelSize(DeviceTexture.Description.Format);
            int rowSize          = pixelSizeInBytes * width;

            // If the pitch exactly matches the row size, we can simply copy all the data.
            if (rowSize == db.RowPitch)
            {
                SharpDX.Utilities.CopyMemory(destination, db.DataPointer, elementCount * pixelSizeInBytes);
            }
            else
            {
                // The texture data may not have a pitch exactly equal to the row width.
                // This means that the pixel data is not "tightly packed" into the buffer given
                // to us, and has empty data at the end of each row.

                for (int rowNumber = 0; rowNumber < height; rowNumber++)
                {
                    int rowStartOffsetInBytes = rowNumber * width * pixelSizeInBytes;
                    ds.Read(destination, rowStartOffsetInBytes, width * pixelSizeInBytes);

                    // At the end of the row, seek the stream to skip the extra filler data,
                    // which is equal to (RowPitch - RowSize) bytes.
                    ds.Seek(db.RowPitch - rowSize, SeekOrigin.Current);
                }
            }

            stagingTexture.Dispose();
        }
Example #7
0
        public override ShaderResourceViewDescription GetShaderResourceViewDescription()
        {
            ShaderResourceViewDescription srvd = new ShaderResourceViewDescription();

            srvd.Format                      = D3DFormats.MapFormatForShaderResourceView(DeviceTexture.Description.Format);
            srvd.Dimension                   = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCube;
            srvd.TextureCube.MipLevels       = 1;
            srvd.TextureCube.MostDetailedMip = 0;

            return(srvd);
        }
Example #8
0
        public override DeviceTexture2D CreateTexture(IntPtr pixelData, int width, int height, int pixelSizeInBytes, PixelFormat format)
        {
            D3DTexture2D texture = new D3DTexture2D(
                _device,
                BindFlags.ShaderResource,
                ResourceUsage.Default,
                CpuAccessFlags.None,
                D3DFormats.ConvertPixelFormat(format),
                pixelData,
                width,
                height,
                width * pixelSizeInBytes);

            return(texture);
        }
Example #9
0
        public D3DDepthStencilState(Device device, bool isDepthEnabled, DepthComparison comparison, bool isDepthWriteEnabled)
        {
            _device             = device;
            IsDepthEnabled      = IsDepthEnabled;
            IsDepthWriteEnabled = isDepthWriteEnabled;
            DepthComparison     = comparison;

            DepthStencilStateDescription desc = DepthStencilStateDescription.Default();

            desc.DepthComparison = D3DFormats.ConvertDepthComparison(comparison);
            desc.IsDepthEnabled  = isDepthEnabled;
            desc.DepthWriteMask  = isDepthWriteEnabled ? DepthWriteMask.All : DepthWriteMask.Zero;

            _deviceState = new SharpDX.Direct3D11.DepthStencilState(device, desc);
        }
Example #10
0
        public override DeviceTexture2D CreateTexture <T>(T[] pixelData, int width, int height, int pixelSizeInBytes, PixelFormat format)
        {
            GCHandle     handle  = GCHandle.Alloc(pixelData, GCHandleType.Pinned);
            D3DTexture2D texture = new D3DTexture2D(
                _device,
                BindFlags.ShaderResource,
                ResourceUsage.Default,
                CpuAccessFlags.None,
                D3DFormats.ConvertPixelFormat(format),
                handle.AddrOfPinnedObject(),
                width,
                height,
                width * pixelSizeInBytes);

            handle.Free();
            return(texture);
        }
Example #11
0
        public D3DCubemapTexture(
            Device device,
            IntPtr pixelsFront,
            IntPtr pixelsBack,
            IntPtr pixelsLeft,
            IntPtr pixelsRight,
            IntPtr pixelsTop,
            IntPtr pixelsBottom,
            int width,
            int height,
            int pixelSizeInBytes,
            PixelFormat format)
        {
            Width  = width;
            Height = height;
            int stride = width * pixelSizeInBytes;

            DataRectangle[] dataRectangles = new DataRectangle[]
            {
                new DataRectangle(pixelsRight, stride),
                new DataRectangle(pixelsLeft, stride),
                new DataRectangle(pixelsTop, stride),
                new DataRectangle(pixelsBottom, stride),
                new DataRectangle(pixelsBack, stride),
                new DataRectangle(pixelsFront, stride),
            };

            DeviceTexture = new Texture2D(
                device,
                new Texture2DDescription()
            {
                Format            = D3DFormats.ConvertPixelFormat(format),
                ArraySize         = 6,
                MipLevels         = 1,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.ShaderResource,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.TextureCube
            },
                dataRectangles);
        }
Example #12
0
        public D3DRasterizerState(
            Device device,
            FaceCullingMode cullMode,
            TriangleFillMode fillMode,
            bool depthClipEnabled,
            bool scissorTestEnabled)
        {
            _device              = device;
            CullMode             = cullMode;
            FillMode             = fillMode;
            IsDepthClipEnabled   = depthClipEnabled;
            IsScissorTestEnabled = scissorTestEnabled;

            var desc = new RasterizerStateDescription()
            {
                IsDepthClipEnabled = IsDepthClipEnabled,
                IsScissorEnabled   = IsScissorTestEnabled,
                CullMode           = D3DFormats.ConvertCullMode(cullMode),
                FillMode           = D3DFormats.ConvertFillMode(fillMode)
            };

            _deviceState = new SharpDX.Direct3D11.RasterizerState(device, desc);
        }
Example #13
0
 public override IndexBuffer CreateIndexBuffer(int sizeInBytes, bool isDynamic, IndexFormat format)
 {
     return(new D3DIndexBuffer(_device, sizeInBytes, isDynamic, D3DFormats.ConvertIndexFormat(format)));
 }
Example #14
0
 public void SetIndices(IntPtr indices, IndexFormat format, int elementSizeInBytes, int count, int elementOffset)
 {
     SetData(indices, elementSizeInBytes * count, elementSizeInBytes * elementOffset);
     _format = D3DFormats.ConvertIndexFormat(format);
 }
Example #15
0
        private int GetRowPitch(int width)
        {
            var pixelSize = D3DFormats.GetPixelSize(DeviceTexture.Description.Format);

            return(pixelSize * width);
        }