Example #1
0
        /// <summary>
        /// Draws indexed, instanced geometry.
        /// </summary>
        /// <param name="primType">Type of the primitives to draw.</param>
        /// <param name="indexCountPerInstance">Number of indices per instance to draw.</param>
        /// <param name="startIndex">Starting index in the index buffer at which to read vertex indices from.</param>
        /// <param name="baseVertex">Offset to add to each vertex index in the index buffer.</param>
        /// <param name="instanceCount">Number of instances to draw.</param>
        public override void DrawIndexedInstanced(PrimitiveType primType, int indexCountPerInstance, int startIndex, int baseVertex, int instanceCount)
        {
            D3D10Helper.CheckDisposed(_graphicsDevice);
            if (indexCountPerInstance <= 0)
            {
                throw new ArgumentOutOfRangeException("vertexCount", "Index count must be greater than zero.");
            }
            if (instanceCount <= 0)
            {
                throw new ArgumentOutOfRangeException("instanceCount", "Number of instances to draw must be greater than zero.");
            }

            if (_currentIndexBuffer == null)
            {
                throw new ArgumentNullException("DrawIndexedInstanced requires a valid index buffer.");
            }
            else
            {
                D3D10Helper.CheckDisposed(_currentIndexBuffer);
            }

            //Send any remaining texture/sampler changes to the device.
            FlushStates();

            _graphicsDevice.InputAssembler.SetInputLayout(_currentPass.InputLayoutMap.GetOrCreateLayout(_currentVertexBuffers, _currentVertexBufferCount));
            _graphicsDevice.InputAssembler.SetPrimitiveTopology(D3D10Helper.ToD3DPrimitiveType(primType));
            _graphicsDevice.DrawIndexedInstanced(indexCountPerInstance, instanceCount, startIndex, startIndex, 0);
        }
        private void render()
        {
            clearRenderTargets();

            RenderObjects objs = extractor.Scene;

            extractor.ExtractNext = true;
            foreach (var obj in objs.Objs)
            {
                using (MeshResource mesh = (MeshResource)obj.Key.Acquire())
                {
                    device.InputAssembler.SetPrimitiveTopology(mesh.primitiveTopology);

                    device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(mesh.vertexBuffer, mesh.elementSize, 0));
                    device.InputAssembler.SetVertexBuffers(1, new VertexBufferBinding(instanceBuffer, 16 * 4 + sizeof(float), 0));
                    if (mesh.indexed)
                    {
                        device.InputAssembler.SetIndexBuffer(mesh.indexBuffer, mesh.indexFormat, 0);
                    }

                    foreach (var matRes in obj.Value)
                    {
                        using (MaterialResource material = (MaterialResource)matRes.Key.Acquire())
                        {
                            var vertData = from RenderObject mat in matRes.Value
                                           select new { model = mat.model * objs.Camera, mat.frame };

                            var matArray = vertData.ToArray();

                            /*
                             * for(int i = 0; i < posArray.Length; ++i)
                             * {
                             *  posArray[i].model = posArray[i].model * objs.Camera;
                             * }
                             *
                             */
                            using (var effect = material.AcquireEffect())
                            {
                                foreach (var constant in material.Constants)
                                {
                                    dynamic value    = constant.Value;
                                    var     variable = effect.Value.GetVariableByName(constant.Key);
                                    if (value is float)
                                    {
                                        variable.AsScalar().Set(value);
                                    }
                                    else if (value is Vector2 || value is Vector3 || value is Vector4)
                                    {
                                        variable.AsVector().Set(value);
                                    }

                                    /*
                                     * switch ((string)constant.Value.GetType().Name)
                                     * {
                                     *  default:
                                     *      throw new NotSupportedException("Constant type: " + constant.Value.GetType() + )
                                     *  break;
                                     * }
                                     * .*/
                                }
                                //effect.Value.GetVariableByName("frameDimensions").AsVector().Set(material.frameDimensions);

                                using (var textures = material.AcquireTextures())
                                {
                                    foreach (var texture in textures)
                                    {
                                        effect.Value.GetVariableByName(texture.Key).AsResource().SetResource(texture.Value.texture);
                                    }

                                    EffectTechnique tech = effect.Value.GetTechniqueByName("Full");
                                    for (int i = 0; i < tech.Description.PassCount; ++i)
                                    {
                                        EffectPass pass = tech.GetPassByIndex(i);

                                        List <InputElement> elems = new List <InputElement>(mesh.inputLayout);
                                        elems.AddRange(elem);

                                        using (InputLayout layout = new InputLayout(device, pass.Description.Signature, elems.ToArray()))
                                        {
                                            device.InputAssembler.SetInputLayout(layout);

                                            for (int j = 0; j < matArray.Length; j += InstanceCount)
                                            {
                                                int curInstanceCount;
                                                using (DataStream stream = instanceBuffer.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None))
                                                {
                                                    curInstanceCount = Math.Min(InstanceCount, matArray.Length - j);

                                                    for (int k = 0; k < curInstanceCount; ++k)
                                                    {
                                                        stream.Write <Matrix>(matArray[j + k].model);
                                                        stream.Write <float>(matArray[j + k].frame);
                                                    }
                                                    //    stream.WriteRange<Matrix>(posArray, j, curInstanceCount);
                                                    instanceBuffer.Unmap();
                                                }

                                                pass.Apply();

                                                if (mesh.indexed)
                                                {
                                                    device.DrawIndexedInstanced(mesh.indexCount, curInstanceCount, 0, 0, 0);
                                                }
                                                else
                                                {
                                                    device.DrawInstanced(mesh.elementCount, curInstanceCount, 0, 0);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            calcPerformanceMetrics();
            renderDebugOutput();

            swapChain.Present(0, PresentFlags.None);
        }