Esempio n. 1
0
            public DeviceResourceContext(GraphicsDevice device, Effect effect, VertexDeclaration declaration, ResourceBufferInfo resourceBufferInfo, int indexStructSize)
            {
                var vertexSize = declaration.CalculateSize();

                VertexCount          = resourceBufferInfo.VertexCount;
                IndexCount           = resourceBufferInfo.IndexCount;
                IsIndexBufferDynamic = resourceBufferInfo.IsIndexBufferDynamic;

                VertexBuffer = Buffer.Vertex.New(device, VertexCount * vertexSize, GraphicsResourceUsage.Dynamic).DisposeBy(this);

                if (IsIndexBufferDynamic)
                {
                    IndexBuffer = Buffer.Index.New(device, IndexCount * indexStructSize, GraphicsResourceUsage.Dynamic).DisposeBy(this);
                }
                else
                {
                    IndexBuffer        = Buffer.Index.New(device, resourceBufferInfo.StaticIndices).DisposeBy(this);
                    IndexBuffer.Reload = graphicsResource => ((Buffer)graphicsResource).Recreate(resourceBufferInfo.StaticIndices);
                }

                var indexBufferBinding  = new IndexBufferBinding(IndexBuffer, indexStructSize == sizeof(int), IndexBuffer.Description.SizeInBytes / indexStructSize);
                var vertexBufferBinding = new VertexBufferBinding(VertexBuffer, declaration, VertexCount, vertexSize);

                // Creates a VAO
                VertexArrayObject = VertexArrayObject.New(device, effect.InputSignature, indexBufferBinding, vertexBufferBinding).DisposeBy(this);
            }
Esempio n. 2
0
            public Description(EffectInputSignature shaderSignature, VertexBufferBinding[] vertexBuffers, IndexBufferBinding indexBuffer)
            {
                ShaderSignature = shaderSignature;
                VertexBuffers   = vertexBuffers ?? emptyVertexBufferBindings;
                IndexBuffer     = indexBuffer;

                // Precompute hash code
                hashCode = 0;
                hashCode = ComputeHashCode();
            }
        private VertexArrayObject(GraphicsDevice graphicsDevice, EffectInputSignature shaderSignature, IndexBufferBinding indexBufferBinding, VertexBufferBinding[] vertexBufferBindings)
            : base(graphicsDevice)
        {
            this.vertexBufferBindings    = vertexBufferBindings;
            this.indexBufferBinding      = indexBufferBinding;
            this.preferredInputSignature = shaderSignature;

            CreateAttributes();
        }
Esempio n. 4
0
        public static VertexArrayObject New(GraphicsDevice graphicsDevice, EffectInputSignature shaderSignature, IndexBufferBinding indexBufferBinding, params VertexBufferBinding[] vertexBufferBindings)
        {
            // Store SamplerState in a cache (D3D seems to have quite bad concurrency when using CreateSampler while rendering)
            VertexArrayObject vertexArrayObject;
            var description = new Description(shaderSignature, vertexBufferBindings, indexBufferBinding);

            lock (graphicsDevice.CachedVertexArrayObjects)
            {
                if (graphicsDevice.CachedVertexArrayObjects.TryGetValue(description, out vertexArrayObject))
                {
                    // TODO: Appropriate destroy
                    vertexArrayObject.AddReferenceInternal();
                }
                else
                {
                    vertexArrayObject = new VertexArrayObject(graphicsDevice, shaderSignature, indexBufferBinding, vertexBufferBindings);

                    // For now store description as is to avoid having to recreate it on Destroy.
                    // It would probably save little bit of memory space to try to reuse existing fields and add only what's missing.
                    vertexArrayObject.description = description;

                    graphicsDevice.CachedVertexArrayObjects.Add(description, vertexArrayObject);
                }
            }

            return(vertexArrayObject);
        }
Esempio n. 5
0
 public static VertexArrayObject New(GraphicsDevice graphicsDevice, IndexBufferBinding indexBufferBinding, params VertexBufferBinding[] vertexBufferBindings)
 {
     return(New(graphicsDevice, null, indexBufferBinding, vertexBufferBindings));
 }
Esempio n. 6
0
        private VertexArrayObject(GraphicsDevice graphicsDevice, EffectInputSignature shaderSignature, IndexBufferBinding indexBufferBinding, VertexBufferBinding[] vertexBufferBindings)
            : base(graphicsDevice)
        {
            this.vertexBufferBindings = vertexBufferBindings;
            this.indexBufferBinding   = indexBufferBinding;
            this.EffectInputSignature = shaderSignature;

            // Calculate Direct3D11 InputElement
            int inputElementCount = vertexBufferBindings.Sum(t => t.Declaration.VertexElements.Length);
            var inputElements     = new InputElement[inputElementCount];

            int j = 0;

            for (int i = 0; i < vertexBufferBindings.Length; i++)
            {
                var declaration = vertexBufferBindings[i].Declaration;
                vertexBufferBindings[i].Buffer.AddReferenceInternal();
                foreach (var vertexElementWithOffset in declaration.EnumerateWithOffsets())
                {
                    var vertexElement = vertexElementWithOffset.VertexElement;
                    inputElements[j++] = new InputElement
                    {
                        Slot              = i,
                        SemanticName      = vertexElement.SemanticName,
                        SemanticIndex     = vertexElement.SemanticIndex,
                        AlignedByteOffset = vertexElementWithOffset.Offset,
                        Format            = (SharpDX.DXGI.Format)vertexElement.Format,
                    };
                }
            }

            Layout = VertexArrayLayout.GetOrCreateLayout(new VertexArrayLayout(inputElements));

            if (indexBufferBinding != null)
            {
                indexBufferBinding.Buffer.AddReferenceInternal();
                indexBufferOffset = indexBufferBinding.Offset;
                indexFormat       = (indexBufferBinding.Is32Bit ? SharpDX.DXGI.Format.R32_UInt : SharpDX.DXGI.Format.R16_UInt);
            }

            CreateResources();
        }
Esempio n. 7
0
        private VertexArrayObject(GraphicsDevice graphicsDevice, EffectInputSignature shaderSignature, IndexBufferBinding indexBufferBinding, VertexBufferBinding[] vertexBufferBindings)
            : base(graphicsDevice)
        {
            this.vertexBufferBindings    = vertexBufferBindings;
            this.indexBufferBinding      = indexBufferBinding;
            this.preferredInputSignature = shaderSignature;

            // Increase the reference count on the provided buffers -> we do not want to take the ownership
            foreach (VertexBufferBinding vertexBufferBinding in vertexBufferBindings)
            {
                vertexBufferBinding.Buffer.AddReferenceInternal();
            }

            if (indexBufferBinding != null)
            {
                indexBufferBinding.Buffer.AddReferenceInternal();
            }

            CreateAttributes();
        }
Esempio n. 8
0
 private VertexArrayObject(GraphicsDevice graphicsDevice, object shaderSignature, IndexBufferBinding indexBufferBinding, VertexBufferBinding[] vertexBuffers)
 {
     throw new NotImplementedException();
 }