示例#1
0
        public VertexBuffer(GraphicsDevice graphicsDevice, VertexDescription description, Graphics.ResourceUsage usage, DataArray data)
            : base(graphicsDevice, new StackTrace(1))
        {
            if (data.IsNull)
            {
                throw new ArgumentNullException("data.Pointer");
            }
            if (data.Size <= 0)
            {
                throw new ArgumentOutOfRangeException("data.Size", data.Size, "Size must be bigger than 0.");
            }

            this.Description = description;
            int descriptionSize = graphicsDevice.GetSizeOf(description);

            this.Usage = usage;

            if (!data.IsNull)
            {
                this.SizeBytes = data.Size;

                BufferDescription bufferDescription = new BufferDescription(SizeBytes, (SharpDX.Direct3D11.ResourceUsage)EnumConverter.Convert(usage),
                                                                            BindFlags.VertexBuffer, EnumConverter.ConvertToAccessFlag(Usage), ResourceOptionFlags.None, 0);

                this.Buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice.Device, data.Pointer, bufferDescription);
                Binding     = new VertexBufferBinding(Buffer, descriptionSize, 0);
            }
        }
示例#2
0
文件: Shader.cs 项目: K0bin/DotGame
        public Shader(GraphicsDevice graphicsDevice, string name, ShaderBytecode vertex, ShaderBytecode pixel)
            : base(graphicsDevice, new StackTrace(1))
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name is empty or whitespace.", "name");
            }
            if (vertex == null)
            {
                throw new ArgumentNullException("vertex");
            }
            if (pixel == null)
            {
                throw new ArgumentNullException("pixel");
            }

            this.Name       = name;
            this.VertexCode = vertex;
            this.PixelCode  = pixel;

            using (ShaderReflection reflection = new ShaderReflection(VertexCode))
            {
                for (int i = 0; i < reflection.Description.BoundResources; i++)
                {
                    var res = reflection.GetResourceBindingDescription(i);
                    resourcesVertex[res.Name] = res.BindPoint;

                    if (res.Type == ShaderInputType.ConstantBuffer)
                    {
                        constantBufferSizes[res.Name] = reflection.GetConstantBuffer(res.Name).Description.Size;
                    }
                }

                int inputCount     = reflection.Description.InputParameters;
                var vertexElements = new VertexElement[inputCount];
                for (int i = 0; i < inputCount; i++)
                {
                    var input = reflection.GetInputParameterDescription(i);
                    if (input.ComponentType != RegisterComponentType.Float32)
                    {
                        continue;
                    }

                    VertexElementType type;
                    if (input.UsageMask.HasFlag(RegisterComponentMaskFlags.All))
                    {
                        type = VertexElementType.Vector4;
                    }
                    else if (input.UsageMask.HasFlag(RegisterComponentMaskFlags.ComponentX | RegisterComponentMaskFlags.ComponentY | RegisterComponentMaskFlags.ComponentZ))
                    {
                        type = VertexElementType.Vector3;
                    }
                    else if (input.UsageMask.HasFlag(RegisterComponentMaskFlags.ComponentX | RegisterComponentMaskFlags.ComponentY))
                    {
                        type = VertexElementType.Vector2;
                    }
                    else if (input.UsageMask.HasFlag(RegisterComponentMaskFlags.ComponentX))
                    {
                        type = VertexElementType.Single;
                    }
                    else
                    {
                        continue;
                    }

                    VertexElementUsage usage = EnumConverter.ConvertToUsage(input.SemanticName);

                    vertexElements[i] = new VertexElement(usage, input.SemanticIndex, type);
                }
                VertexDescription = new VertexDescription(vertexElements);
            }

            using (ShaderReflection reflection = new ShaderReflection(PixelCode))
            {
                for (int i = 0; i < reflection.Description.BoundResources; i++)
                {
                    var res = reflection.GetResourceBindingDescription(i);
                    resourcesPixel[res.Name] = res.BindPoint;

                    if (res.Type == ShaderInputType.ConstantBuffer)
                    {
                        constantBufferSizes[res.Name] = reflection.GetConstantBuffer(res.Name).Description.Size;
                    }
                }
            }

            VertexShaderHandle = new VertexShader(graphicsDevice.Device, VertexCode);
            PixelShaderHandle  = new PixelShader(graphicsDevice.Device, PixelCode);

            using (MemoryStream stream = new MemoryStream())
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write("DIRECTX11");
                    writer.Write(VertexCode.Data.Length);
                    writer.Write(VertexCode.Data);
                    writer.Write(PixelCode.Data.Length);
                    writer.Write(PixelCode.Data);
                    binaryCode = stream.GetBuffer();
                }
        }
示例#3
0
        public IndexBuffer(GraphicsDevice graphicsDevice, IndexFormat format, Graphics.ResourceUsage usage, int indexCount)
            : base(graphicsDevice, new StackTrace(1))
        {
            if (indexCount <= 0)
            {
                throw new ArgumentOutOfRangeException("indexCount", indexCount, "indexCount must be bigger than zero.");
            }
            if (usage == ResourceUsage.Immutable)
            {
                throw new ArgumentException("data", "Immutable buffers must be initialized with data.");
            }

            this.Format    = format;
            this.SizeBytes = indexCount * graphicsDevice.GetSizeOf(format);
            this.Usage     = usage;

            BufferDescription bufferDescription = new BufferDescription(SizeBytes, (SharpDX.Direct3D11.ResourceUsage)EnumConverter.Convert(Usage),
                                                                        BindFlags.IndexBuffer, EnumConverter.ConvertToAccessFlag(Usage), ResourceOptionFlags.None, 0);

            this.Buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice.Device, bufferDescription);
        }
示例#4
0
        public VertexBuffer(GraphicsDevice graphicsDevice, int vertexCount, VertexDescription description, Graphics.ResourceUsage usage)
            : base(graphicsDevice, new StackTrace(1))
        {
            if (vertexCount <= 0)
            {
                throw new ArgumentException("vertexCount must not be smaller than/ equal to zero.", "vertexCount");
            }
            if (usage == ResourceUsage.Immutable)
            {
                throw new ArgumentException("data", "Immutable buffers must be initialized with data.");
            }

            this.Description = description;
            int descriptionSize = graphicsDevice.GetSizeOf(description);

            this.SizeBytes = descriptionSize * vertexCount;
            this.Usage     = usage;

            BufferDescription bufferDescription = new BufferDescription(SizeBytes, (SharpDX.Direct3D11.ResourceUsage)EnumConverter.Convert(Usage),
                                                                        BindFlags.VertexBuffer, EnumConverter.ConvertToAccessFlag(Usage), ResourceOptionFlags.None, 0);

            this.Buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice.Device, bufferDescription);

            Binding = new VertexBufferBinding(Buffer, descriptionSize, 0);
        }
示例#5
0
        public IndexBuffer(GraphicsDevice graphicsDevice, IndexFormat format, Graphics.ResourceUsage usage, DataArray data)
            : base(graphicsDevice, new StackTrace(1))
        {
            if (data.IsNull)
            {
                throw new ArgumentException("data.Pointer is null");
            }
            if (data.Size <= 0)
            {
                throw new ArgumentOutOfRangeException("data.Size", data.Size, "Size must be bigger than 0.");
            }

            this.Format    = format;
            this.SizeBytes = data.Size;
            this.Usage     = usage;

            if (!data.IsNull)
            {
                this.SizeBytes = data.Size;

                BufferDescription bufferDescription = new BufferDescription(SizeBytes, (SharpDX.Direct3D11.ResourceUsage)EnumConverter.Convert(usage),
                                                                            BindFlags.IndexBuffer, EnumConverter.ConvertToAccessFlag(Usage), ResourceOptionFlags.None, 0);

                this.Buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice.Device, data.Pointer, bufferDescription);
            }
        }
示例#6
0
        public InputLayout GetLayout(VertexDescription description, Shader shader)
        {
            InputLayout layout;

            if (inputLayoutPool.TryGetValue(description, out layout))
            {
                return(layout);
            }

            InputElement[]  dxElements = new InputElement[description.ElementCount];
            VertexElement[] elements   = description.GetElements();

            for (int i = 0; i < description.ElementCount; i++)
            {
                dxElements[i] = new InputElement(EnumConverter.Convert(elements[i].Usage), 0, EnumConverter.Convert(elements[i].Type), elements[i].UsageIndex);
            }
            layout = inputLayoutPool[description] = new InputLayout(Device, shader.VertexCode, dxElements);
            return(layout);
        }
示例#7
0
 public int GetSizeOf(IndexFormat format)
 {
     return(SharpDX.DXGI.FormatHelper.SizeOfInBytes(EnumConverter.Convert(format)));
 }
示例#8
0
 public int GetSizeOf(VertexElementType format)
 {
     return(SharpDX.DXGI.FormatHelper.SizeOfInBytes(EnumConverter.Convert(format)));
 }
示例#9
0
        private void ApplyState()
        {
            ApplyRenderTargets();

            var shader = graphicsDevice.Cast <Shader>(currentState.Shader, "currentState.Shader");

            if (stateDirty)
            {
                if (vertexCache.Shader != shader)
                {
                    Context.VertexShader.Set(shader.VertexShaderHandle);
                    vertexCache.Shader = shader;
                }
                if (pixelCache.Shader != shader)
                {
                    if (!currentState.Shader.VertexDescription.EqualsIgnoreOrder(currentVertexBuffer.Description))
                    {
                        throw new GraphicsException("Current shader VertexDescription doesn't match the description of the current VertexBuffer");
                    }

                    Context.PixelShader.Set(shader.PixelShaderHandle);
                    pixelCache.Shader = shader;
                }
                if (primitiveType != currentState.PrimitiveType)
                {
                    Context.InputAssembler.PrimitiveTopology = EnumConverter.Convert(currentState.PrimitiveType);
                    primitiveType = currentState.PrimitiveType;
                }
                if (currentRasterizer != currentState.Rasterizer)
                {
                    var rasterizer = graphicsDevice.Cast <RasterizerState>(currentState.Rasterizer, "currentState.Rasterizer");
                    Context.Rasterizer.State = rasterizer.Handle;

                    currentRasterizer = rasterizer;
                }
                if (currentDepthStencil != currentState.DepthStencil)
                {
                    var depthStencil = graphicsDevice.Cast <DepthStencilState>(currentState.DepthStencil, "currentState.DepthStencil");
                    Context.OutputMerger.DepthStencilState = depthStencil.Handle;

                    currentDepthStencil = depthStencil;
                }

                if (currentBlend != currentState.Blend)
                {
                    var blend = graphicsDevice.Cast <BlendState>(currentState.Blend, "currentState.Blend");
                    Context.OutputMerger.BlendState = blend.Handle;

                    currentBlend = blend;
                }
            }
            if (vertexBufferDirty)
            {
                if (!currentState.Shader.VertexDescription.EqualsIgnoreOrder(currentVertexBuffer.Description))
                {
                    throw new GraphicsException("Current shader VertexDescription doesn't match the description of the current VertexBuffer");
                }

                Context.InputAssembler.InputLayout = graphicsDevice.GetLayout(currentVertexBuffer.Description, shader);
                Context.InputAssembler.SetVertexBuffers(0, currentVertexBuffer.Binding);
            }
            if (indexBufferDirty)
            {
                Context.InputAssembler.SetIndexBuffer(currentIndexBuffer.Buffer, currentIndexBuffer.IndexFormat, 0);
            }

            stateDirty        = false;
            vertexBufferDirty = false;
            indexBufferDirty  = false;
        }