protected override void OnRender(RenderContext renderContext)
        {
            /// --- set constant paramerers
            var worldMatrix = modelMatrix * renderContext.WorldMatrix;

            EffectTransforms.mWorld.SetMatrix(ref worldMatrix);

            /// --- check shadowmaps
            hasShadowMap = renderHost.IsShadowMapEnabled;
            effectMaterial.bHasShadowMapVariable.Set(hasShadowMap);
            effectMaterial.AttachMaterial(geometryInternal as MeshGeometry3D);

            /// --- check instancing
            hasInstances = (Instances != null) && (Instances.Any());
            if (bHasInstances != null)
            {
                bHasInstances.Set(hasInstances);
            }

            /// --- set context
            Device.ImmediateContext.InputAssembler.InputLayout       = vertexLayout;
            Device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            Device.ImmediateContext.InputAssembler.SetIndexBuffer(IndexBuffer.Buffer, Format.R32_UInt, 0);

            /// --- set rasterstate
            Device.ImmediateContext.Rasterizer.State = RasterState;

            if (hasInstances)
            {
                /// --- update instance buffer
                if (isInstanceChanged)
                {
                    InstanceBuffer.UploadDataToBuffer(renderContext.DeviceContext, Instances.ToArray());
                    isInstanceChanged = false;
                }

                /// --- INSTANCING: need to set 2 buffers
                Device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new[]
                {
                    new VertexBufferBinding(VertexBuffer.Buffer, VertexBuffer.StructureSize, 0),
                    new VertexBufferBinding(InstanceBuffer.Buffer, InstanceBuffer.StructureSize, 0),
                });

                /// --- render the geometry
                effectTechnique.GetPassByIndex(0).Apply(Device.ImmediateContext);
                /// --- draw
                Device.ImmediateContext.DrawIndexedInstanced(Geometry.Indices.Count, Instances.Count, 0, 0, 0);
            }
            else
            {
                /// --- bind buffer
                Device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer.Buffer, VertexBuffer.StructureSize, 0));
                /// --- render the geometry
                effectTechnique.GetPassByIndex(0).Apply(Device.ImmediateContext);
                /// --- draw
                Device.ImmediateContext.DrawIndexed(Geometry.Indices.Count, 0, 0);
            }
        }
        protected override void OnRender(RenderContext renderContext)
        {
            // --- set constant paramerers
            var worldMatrix = this.modelMatrix * renderContext.WorldMatrix;

            this.effectTransforms.mWorld.SetMatrix(ref worldMatrix);

            // --- check shadowmaps
            this.hasShadowMap = this.renderHost.IsShadowMapEnabled;
            this.effectMaterial.bHasShadowMapVariable.Set(this.hasShadowMap);

            // --- set material params
            this.effectMaterial.AttachMaterial();

            this.bHasInstances.Set(this.hasInstances);
            // --- set context
            renderContext.DeviceContext.InputAssembler.InputLayout       = this.vertexLayout;
            renderContext.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            renderContext.DeviceContext.InputAssembler.SetIndexBuffer(this.IndexBuffer.Buffer, Format.R32_UInt, 0);

            // --- set rasterstate
            renderContext.DeviceContext.Rasterizer.State = this.rasterState;
            if (this.hasInstances)
            {
                // --- update instance buffer
                if (this.isInstanceChanged)
                {
                    InstanceBuffer.UploadDataToBuffer(renderContext.DeviceContext, this.instanceInternal);
                    this.isInstanceChanged = false;
                }

                // --- INSTANCING: need to set 2 buffers
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new[]
                {
                    new VertexBufferBinding(this.VertexBuffer.Buffer, this.VertexBuffer.StructureSize, 0),
                    new VertexBufferBinding(this.InstanceBuffer.Buffer, this.InstanceBuffer.StructureSize, 0),
                });

                // --- render the geometry
                this.effectTechnique.GetPassByIndex(0).Apply(renderContext.DeviceContext);
                // --- draw
                renderContext.DeviceContext.DrawIndexedInstanced(this.geometryInternal.Indices.Count, this.instanceInternal.Count, 0, 0, 0);
                this.bHasInstances.Set(false);
            }
            else
            {
                // --- bind buffer
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.VertexBuffer.Buffer, this.VertexBuffer.StructureSize, 0));
                // --- render the geometry
                //
                var pass = this.effectTechnique.GetPassByIndex(0);
                pass.Apply(renderContext.DeviceContext);
                // --- draw
                renderContext.DeviceContext.DrawIndexed(this.geometryInternal.Indices.Count, 0, 0);
            }
        }
Example #3
0
        protected override void OnRender(RenderContext renderContext)
        {
            this.bHasInstances.Set(this.hasInstances);
            // --- set constant paramerers
            var worldMatrix = this.modelMatrix * renderContext.worldMatrix;

            this.EffectTransforms.mWorld.SetMatrix(ref worldMatrix);

            // --- check shadowmaps
            this.hasShadowMap = this.renderHost.IsShadowMapEnabled;
            this.effectMaterial.bHasShadowMapVariable.Set(this.hasShadowMap);
            this.effectMaterial.AttachMaterial(geometryInternal as MeshGeometry3D);
            // --- set context
            renderContext.DeviceContext.InputAssembler.InputLayout       = this.vertexLayout;
            renderContext.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            renderContext.DeviceContext.InputAssembler.SetIndexBuffer(this.IndexBuffer.Buffer, Format.R32_UInt, 0);

            // --- set rasterstate
            renderContext.DeviceContext.Rasterizer.State = this.RasterState;
            renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.VertexBuffer.Buffer, this.VertexBuffer.StructureSize, 0));
            hasBonesVar.Set(hasBoneParameter && hasBoneMatrices);

            if (hasBoneMatrices)
            {
                boneMatricesVar.SetMatrix(mBones.Bones);
            }
            if (this.hasBoneParameter)
            {
                if (isBoneParamChanged && this.VertexBoneIds.Count >= geometryInternal.Positions.Count)
                {
                    vertexBoneParamsBuffer.UploadDataToBuffer(renderContext.DeviceContext, this.VertexBoneIds);
                    this.isBoneParamChanged = false;
                }
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(1, new VertexBufferBinding(this.vertexBoneParamsBuffer.Buffer, this.vertexBoneParamsBuffer.StructureSize, 0));
            }
            if (this.hasInstances)
            {
                // --- update instance buffer
                if (this.isInstanceChanged)
                {
                    InstanceBuffer.UploadDataToBuffer(renderContext.DeviceContext, this.instanceInternal);
                    this.isInstanceChanged = false;
                }
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(2, new VertexBufferBinding(this.InstanceBuffer.Buffer, this.InstanceBuffer.StructureSize, 0));
                OnInstancedDrawCall(renderContext);
            }
            else
            {
                // --- bind buffer
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.VertexBuffer.Buffer, this.VertexBuffer.StructureSize, 0));
                OnDrawCall(renderContext);
            }
        }
Example #4
0
        protected override void OnRender(RenderContext renderContext)
        {
            // --- set constant paramerers
            var worldMatrix = this.modelMatrix * renderContext.worldMatrix;

            this.EffectTransforms.mWorld.SetMatrix(ref worldMatrix);

            // --- check shadowmaps
            this.hasShadowMap = this.renderHost.IsShadowMapEnabled;
            this.effectMaterial.bHasShadowMapVariable.Set(this.hasShadowMap);

            // --- set material params
            this.effectMaterial.AttachMaterial(geometryInternal as MeshGeometry3D);
            this.bInvertNormalVar.Set(invertNormal);
            this.bHasInstances.Set(this.hasInstances);
            // --- set context
            renderContext.DeviceContext.InputAssembler.InputLayout       = this.vertexLayout;
            renderContext.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            renderContext.DeviceContext.InputAssembler.SetIndexBuffer(this.IndexBuffer.Buffer, Format.R32_UInt, 0);

            // --- set rasterstate
            renderContext.DeviceContext.Rasterizer.State = this.RasterState;
            if (this.hasInstances)
            {
                // --- update instance buffer
                if (this.isInstanceChanged)
                {
                    InstanceBuffer.UploadDataToBuffer(renderContext.DeviceContext, this.instanceInternal);
                    this.isInstanceChanged = false;
                }

                // --- INSTANCING: need to set 2 buffers
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new[]
                {
                    new VertexBufferBinding(this.VertexBuffer.Buffer, this.VertexBuffer.StructureSize, 0),
                    new VertexBufferBinding(this.InstanceBuffer.Buffer, this.InstanceBuffer.StructureSize, 0),
                });
                OnInstancedDrawCall(renderContext);
                this.bHasInstances.Set(false);
            }
            else
            {
                // --- bind buffer
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.VertexBuffer.Buffer, this.VertexBuffer.StructureSize, 0));

                OnDrawCall(renderContext);
            }
            this.bInvertNormalVar.Set(false);
        }
Example #5
0
        protected override void OnRender(RenderContext renderContext)
        {
            // --- since these values are changed only per window resize, we set them only once here
            //if (this.isResized || renderContext.Camera != this.lastCamera)
            {
                //this.isResized = false;
                //this.lastCamera = renderContext.Camera;

                if (renderContext.Camera is ProjectionCamera)
                {
                    var c = renderContext.Camera as ProjectionCamera;
                    // viewport: W,H,0,0
                    var viewport = new Vector4((float)renderContext.Canvas.ActualWidth, (float)renderContext.Canvas.ActualHeight, 0, 0);
                    var ar       = viewport.X / viewport.Y;
                    this.vViewport.Set(ref viewport);

                    // Actually, we don't really need vFrustum because we already know the depth of the projected line.
                    //var fov = 100.0; // this is a fake value, since the line shader does not use it!
                    //var zn = c.NearPlaneDistance > 0 ? c.NearPlaneDistance : 0.1;
                    //var zf = c.FarPlaneDistance + 0.0;
                    // frustum: FOV,AR,N,F
                    //var frustum = new Vector4((float)fov, (float)ar, (float)zn, (float)zf);
                    //this.vFrustum.Set(ref frustum);
                }
            }
            // --- set transform paramerers
            var worldMatrix = this.modelMatrix * renderContext.worldMatrix;

            this.EffectTransforms.mWorld.SetMatrix(ref worldMatrix);

            // --- set effect per object const vars
            this.vLineParams.Set(lineParams);

            // --- set context
            renderContext.DeviceContext.InputAssembler.InputLayout = this.vertexLayout;
            renderContext.DeviceContext.InputAssembler.SetIndexBuffer(this.IndexBuffer.Buffer, Format.R32_UInt, 0);
            renderContext.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;

            this.bHasInstances.Set(this.hasInstances);

            // --- set rasterstate
            renderContext.DeviceContext.Rasterizer.State = this.RasterState;

            if (this.hasInstances)
            {
                // --- update instance buffer
                if (this.isInstanceChanged)
                {
                    InstanceBuffer.UploadDataToBuffer(renderContext.DeviceContext, this.instanceInternal);
                    this.isInstanceChanged = false;
                }

                // --- INSTANCING: need to set 2 buffers
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new[]
                {
                    new VertexBufferBinding(this.VertexBuffer.Buffer, this.VertexBuffer.StructureSize, 0),
                    new VertexBufferBinding(this.InstanceBuffer.Buffer, this.InstanceBuffer.StructureSize, 0),
                });

                // --- render the geometry
                for (int i = 0; i < this.effectTechnique.Description.PassCount; i++)
                {
                    this.effectTechnique.GetPassByIndex(i).Apply(renderContext.DeviceContext);
                    renderContext.DeviceContext.DrawIndexedInstanced(this.geometryInternal.Indices.Count, this.instanceInternal.Count, 0, 0, 0);
                }
                this.bHasInstances.Set(false);
            }
            else
            {
                // --- bind buffer
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.VertexBuffer.Buffer, this.VertexBuffer.StructureSize, 0));

                // --- render the geometry
                this.effectTechnique.GetPassByIndex(0).Apply(renderContext.DeviceContext);
                renderContext.DeviceContext.DrawIndexed(this.geometryInternal.Indices.Count, 0, 0);
            }
        }
        protected override void OnRender(RenderContext renderContext)
        {
            // --- check to render the model
            var geometry = geometryInternal as IBillboardText;

            if (geometry == null)
            {
                throw new System.Exception("Geometry must implement IBillboardText");
            }
            this.bHasInstances.Set(this.hasInstances);
            this.hasInstanceParamVar.Set(this.hasInstanceParams);
            // --- set constant paramerers
            var worldMatrix = modelMatrix * renderContext.worldMatrix;

            EffectTransforms.mWorld.SetMatrix(ref worldMatrix);
            this.bFixedSizeVariable.Set(FixedSize);
            // --- check shadowmaps
            //this.hasShadowMap = this.renderHost.IsShadowMapEnabled;
            //this.effectMaterial.bHasShadowMapVariable.Set(this.hasShadowMap);

            // --- set context
            renderContext.DeviceContext.InputAssembler.InputLayout       = vertexLayout;
            renderContext.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;

            // --- set rasterstate
            renderContext.DeviceContext.Rasterizer.State = RasterState;

            // --- bind buffer
            renderContext.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer.Buffer, VertexBuffer.StructureSize, 0));
            // --- render the geometry
            this.bHasBillboardTexture.Set(geometry.Texture != null);
            if (geometry.Texture != null)
            {
                billboardTextureVariable.SetResource(billboardTextureView);
            }

            this.bHasBillboardAlphaTexture.Set(geometry.AlphaTexture != null);
            if (geometry.AlphaTexture != null)
            {
                billboardAlphaTextureVariable.SetResource(billboardAlphaTextureView);
            }
            var vertexCount = geometryInternal.Positions.Count;

            if (this.hasInstances)
            {
                if (this.isInstanceChanged)
                {
                    InstanceBuffer.UploadDataToBuffer(renderContext.DeviceContext, this.instanceInternal);
                    this.isInstanceChanged = false;
                }
                renderContext.DeviceContext.InputAssembler.SetVertexBuffers(1, new VertexBufferBinding(this.InstanceBuffer.Buffer, this.InstanceBuffer.StructureSize, 0));
                if (this.hasInstanceParams)
                {
                    if (instanceParamArrayChanged)
                    {
                        instanceParamBuffer.UploadDataToBuffer(renderContext.DeviceContext, this.InstanceParamArray);
                        this.instanceParamArrayChanged = false;
                    }
                    renderContext.DeviceContext.InputAssembler.SetVertexBuffers(2, new VertexBufferBinding(this.instanceParamBuffer.Buffer, this.instanceParamBuffer.StructureSize, 0));
                }

                switch (billboardType)
                {
                case BillboardType.SingleImage:
                    // Use foreground shader to draw text
                    effectTechnique.GetPassByIndex(2).Apply(renderContext.DeviceContext);

                    // --- draw text, foreground vertex is beginning from 0.
                    renderContext.DeviceContext.DrawInstanced(vertexCount, this.instanceInternal.Count, 0, 0);
                    break;

                case BillboardType.SingleText:
                    if (vertexCount == 8)
                    {
                        var half = vertexCount / 2;
                        // Use background shader to draw background first
                        effectTechnique.GetPassByIndex(1).Apply(renderContext.DeviceContext);
                        // --- draw background, background vertex is beginning from middle. <see cref="BillboardSingleText3D"/>
                        renderContext.DeviceContext.DrawInstanced(half, this.instanceInternal.Count, half, 0);

                        // Use foreground shader to draw text
                        effectTechnique.GetPassByIndex(0).Apply(renderContext.DeviceContext);

                        // --- draw text, foreground vertex is beginning from 0.
                        renderContext.DeviceContext.DrawInstanced(half, this.instanceInternal.Count, 0, 0);
                    }
                    break;
                }
            }
            this.bHasInstances.Set(false);
            this.hasInstanceParamVar.Set(false);
        }