コード例 #1
0
ファイル: WireframeShapeRenderer.cs プロジェクト: zhuowp/ge
        private void UpdateBuffers(RenderContext rc)
        {
            var factory = rc.ResourceFactory;

            _vertices.Clear();
            _indices.Clear();
            AddVerticesAndIndices();

            if (_vertices.Count > 0)
            {
                _vb.Dispose();
                _ib.Dispose();

                _vb = factory.CreateVertexBuffer(_vertices.Count * VertexPositionNormalTexture.SizeInBytes, false);
                _vb.SetVertexData(
                    _vertices.ToArray(),
                    new VertexDescriptor(
                        VertexPositionNormalTexture.SizeInBytes,
                        VertexPositionNormalTexture.ElementCount,
                        0,
                        IntPtr.Zero));
                _ib = factory.CreateIndexBuffer(sizeof(int) * _indices.Count, false);
                _ib.SetIndices(_indices.ToArray(), IndexFormat.UInt16);
            }
        }
コード例 #2
0
        public void ChangeRenderContext(RenderContext rc)
        {
            var factory = rc.ResourceFactory;

            _vertexBuffer = factory.CreateVertexBuffer(VertexPositionTexture.SizeInBytes, false);
            _vertexBuffer.SetVertexData(new VertexPositionTexture[]
            {
                new VertexPositionTexture(new Vector3(0, 0, 0), new Vector2(0, 0)),
                new VertexPositionTexture(new Vector3(1, 0, 0), new Vector2(1, 0)),
                new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 1)),
                new VertexPositionTexture(new Vector3(0, 1, 0), new Vector2(0, 1)),
            }, new VertexDescriptor(VertexPositionTexture.SizeInBytes, VertexPositionTexture.ElementCount, 0, IntPtr.Zero),
                                        0);

            _indexBuffer = factory.CreateIndexBuffer(sizeof(int) * 6, false);
            _indexBuffer.SetIndices(new int[] { 0, 1, 2, 0, 2, 3 });

            _material = factory.CreateMaterial(
                rc,
                "simple-2d-vertex",
                "simple-2d-frag",
                new MaterialVertexInput(
                    VertexPositionTexture.SizeInBytes,
                    new MaterialVertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3),
                    new MaterialVertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2)),
                new MaterialInputs <MaterialGlobalInputElement>(
                    new MaterialGlobalInputElement("WorldMatrixBuffer", MaterialInputType.Matrix4x4, _worldMatrixProvider),
                    new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, _projectionMatrixProvider)),
                MaterialInputs <MaterialPerObjectInputElement> .Empty,
                new MaterialTextureInputs(new ContextTextureInputElement("SurfaceTexture", "ShadowMap")));

            _depthDisabledState = factory.CreateDepthStencilState(false, DepthComparison.Always);
        }
コード例 #3
0
        public UpscaleStage(RenderContext rc, string stageName, DeviceTexture2D sourceTexture, Framebuffer outputBuffer)
        {
            RenderContext      = rc;
            Name               = stageName;
            _outputFramebuffer = outputBuffer;

            ResourceFactory factory = rc.ResourceFactory;

            _quadVB = factory.CreateVertexBuffer(VertexPositionTexture.SizeInBytes * 4, false);
            _quadVB.SetVertexData(
                new VertexPositionTexture[]
            {
                new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)),
                new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)),
                new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)),
                new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1))
            }, new VertexDescriptor(VertexPositionTexture.SizeInBytes, 2, 0, IntPtr.Zero));
            _quadIB = factory.CreateIndexBuffer(sizeof(int) * 6, false);
            _quadIB.SetIndices(new int[] { 0, 1, 2, 0, 2, 3 });
            _quadMaterial = factory.CreateMaterial(rc, "simple-2d-vertex", "simple-2d-frag",
                                                   new MaterialVertexInput(
                                                       20,
                                                       new MaterialVertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3),
                                                       new MaterialVertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2)),
                                                   new MaterialInputs <MaterialGlobalInputElement>(
                                                       new MaterialGlobalInputElement("WorldMatrixBuffer", MaterialInputType.Matrix4x4, _identityProvider),
                                                       new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, _identityProvider)),
                                                   MaterialInputs <MaterialPerObjectInputElement> .Empty,
                                                   new MaterialTextureInputs(new ManualTextureInput("SurfaceTexture")));

            if (sourceTexture != null)
            {
                _textureBinding = factory.CreateShaderTextureBinding(sourceTexture);
            }
        }
コード例 #4
0
ファイル: Skybox.cs プロジェクト: zhuowp/ge
        public void InitializeContextObjects(AssetDatabase ad, RenderContext rc)
        {
            var factory = rc.ResourceFactory;

            _vb = factory.CreateVertexBuffer(s_vertices.Length * VertexPosition.SizeInBytes, false);
            _vb.SetVertexData(s_vertices, new VertexDescriptor(VertexPosition.SizeInBytes, 1, 0, IntPtr.Zero));

            _ib = factory.CreateIndexBuffer(s_indices.Length * sizeof(int), false);
            _ib.SetIndices(s_indices);

            _material = rc.ResourceFactory.CreateMaterial(rc, "skybox-vertex", "skybox-frag",
                                                          new MaterialVertexInput(12,
                                                                                  new MaterialVertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3)),
                                                          new MaterialInputs <MaterialGlobalInputElement>(
                                                              new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "ProjectionMatrix")),
                                                          new MaterialInputs <MaterialPerObjectInputElement>(
                                                              new MaterialPerObjectInputElement("ViewMatrixBuffer", MaterialInputType.Matrix4x4, 16)),
                                                          new MaterialTextureInputs(new ManualTextureInput("Skybox")));

            _perObjectInput = rc.GetNamedGlobalBufferProviderPair("ViewMatrix").DataProvider;

            RecreateCubemapTexture();

            _rasterizerState   = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, false, false);
            _depthStencilState = factory.CreateDepthStencilState(true, DepthComparison.LessEqual, false);
            _initialized       = true;
        }
コード例 #5
0
ファイル: TextBuffer.cs プロジェクト: zhuowp/ge
        private unsafe void EnsureIndexCapacity(int capacity)
        {
            int needed = capacity - _filledIndexCount;

            if (needed > 0)
            {
                ushort[] indices = new ushort[needed * 6];
                for (int i = 0, v = _filledIndexCount * 4; i < needed; i++, v += 4)
                {
                    indices[i * 6 + 0] = (ushort)(v + 0);
                    indices[i * 6 + 1] = (ushort)(v + 2);
                    indices[i * 6 + 2] = (ushort)(v + 1);
                    indices[i * 6 + 3] = (ushort)(v + 2);
                    indices[i * 6 + 4] = (ushort)(v + 0);
                    indices[i * 6 + 5] = (ushort)(v + 3);
                }

                _ib.SetIndices(indices, IndexFormat.UInt16, 0, _filledIndexCount * 6);
                _filledIndexCount = capacity;
            }
        }
コード例 #6
0
ファイル: MeshRenderer.cs プロジェクト: nureyev/ge
        private unsafe void SortTransparentTriangles()
        {
            int[] indices = GetMeshIndices();

            if (_triIndices == null || _triIndices.Length < indices.Length / 3)
            {
                _triIndices = new TriangleIndices[indices.Length / 3];
                for (int i = 0; i < _triIndices.Length; i++)
                {
                    _triIndices[i] = new TriangleIndices(indices[i * 3], indices[i * 3 + 1], indices[i * 3 + 2]);
                }
            }

            _triangleComparer.WorldMatrix    = Transform.GetWorldMatrix();
            _triangleComparer.CameraPosition = _gs.MainCamera.Transform.Position;
            _triangleComparer.Positions      = GetMeshVertexPositions();

            Array.Sort(_triIndices, _triangleComparer);

            fixed(TriangleIndices *indicesPtr = _triIndices)
            {
                _ib.SetIndices(new IntPtr(indicesPtr), IndexFormat.UInt32, sizeof(uint), indices.Length);
            }
        }
コード例 #7
0
        private unsafe void RenderImDrawData(DrawData *draw_data, RenderContext rc)
        {
            VertexDescriptor descriptor = new VertexDescriptor((byte)sizeof(DrawVert), 3, 0, IntPtr.Zero);

            int vertexOffsetInVertices = 0;
            int indexOffsetInElements  = 0;

            if (draw_data->CmdListsCount == 0)
            {
                return;
            }

            for (int i = 0; i < draw_data->CmdListsCount; i++)
            {
                NativeDrawList *cmd_list = draw_data->CmdLists[i];

                _vertexBuffer.SetVertexData(new IntPtr(cmd_list->VtxBuffer.Data), descriptor, cmd_list->VtxBuffer.Size, vertexOffsetInVertices);
                _indexBuffer.SetIndices(new IntPtr(cmd_list->IdxBuffer.Data), IndexFormat.UInt16, sizeof(ushort), cmd_list->IdxBuffer.Size, indexOffsetInElements);

                vertexOffsetInVertices += cmd_list->VtxBuffer.Size;
                indexOffsetInElements  += cmd_list->IdxBuffer.Size;
            }

            // Setup orthographic projection matrix into our constant buffer
            {
                var io = ImGui.GetIO();

                Matrix4x4 mvp = Matrix4x4.CreateOrthographicOffCenter(
                    0f,
                    io.DisplaySize.X,
                    io.DisplaySize.Y,
                    0.0f,
                    -1.0f,
                    1.0f);

                _projectionMatrixProvider.Data = mvp;
            }

            BlendState previousBlendState = rc.BlendState;

            rc.SetBlendState(_blendState);
            rc.SetDepthStencilState(_depthDisabledState);
            RasterizerState previousRasterizerState = rc.RasterizerState;

            rc.SetRasterizerState(_rasterizerState);
            rc.SetVertexBuffer(_vertexBuffer);
            rc.SetIndexBuffer(_indexBuffer);
            rc.SetMaterial(_material);

            ImGui.ScaleClipRects(draw_data, ImGui.GetIO().DisplayFramebufferScale);

            // Render command lists
            int vtx_offset = 0;
            int idx_offset = 0;

            for (int n = 0; n < draw_data->CmdListsCount; n++)
            {
                NativeDrawList *cmd_list = draw_data->CmdLists[n];
                for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
                {
                    DrawCmd *pcmd = &(((DrawCmd *)cmd_list->CmdBuffer.Data)[cmd_i]);
                    if (pcmd->UserCallback != IntPtr.Zero)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        if (pcmd->TextureId != IntPtr.Zero)
                        {
                            if (pcmd->TextureId == new IntPtr(_fontAtlasID))
                            {
                                _material.UseTexture(0, _fontTextureBinding);
                            }
                            else
                            {
                                ShaderTextureBinding binding = ImGuiImageHelper.GetShaderTextureBinding(pcmd->TextureId);
                                _material.UseTexture(0, binding);
                            }
                        }

                        // TODO: This doesn't take into account viewport coordinates.
                        rc.SetScissorRectangle(
                            (int)pcmd->ClipRect.X,
                            (int)pcmd->ClipRect.Y,
                            (int)pcmd->ClipRect.Z,
                            (int)pcmd->ClipRect.W);

                        rc.DrawIndexedPrimitives((int)pcmd->ElemCount, idx_offset, vtx_offset);
                    }

                    idx_offset += (int)pcmd->ElemCount;
                }
                vtx_offset += cmd_list->VtxBuffer.Size;
            }

            rc.ClearScissorRectangle();
            rc.SetBlendState(previousBlendState);
            rc.SetDepthStencilState(rc.DefaultDepthStencilState);
            rc.SetRasterizerState(previousRasterizerState);
        }