コード例 #1
0
        void Initialize()
        {
            // 1) We create vertex shader first.
            {
                vertexShader = new ShaderCode(BindingStage.VertexShader);
                vertexShader.InputOperation.AddInput(PinComponent.Position, PinFormat.Floatx2);

                // We now extend position
                ExpandOperation expandPositionOp = new ExpandOperation(PinFormat.Floatx4, ExpandType.AddOnesAtW);
                expandPositionOp.BindInputs(vertexShader.InputOperation.PinAsOutput(PinComponent.Position));
                Pin position = expandPositionOp.Outputs[0];

                // We now output position.
                vertexShader.OutputOperation.AddComponentAndLink(PinComponent.Position, position);
            }

            vertexShader.Immutable = true;

            // 2) We create pixel shader.
            {
                pixelShader = new ShaderCode(BindingStage.PixelShader);
                pixelShader.InputOperation.AddInput(PinComponent.Position, PinFormat.Floatx2);

                ConstantOperation interfaceOp = pixelShader.CreateConstant("Composite", PinFormat.Interface, Pin.DynamicArray);

                // We use the compositing operation.
                CompositingOperation op = new CompositingOperation();
                op.BindInputs(pixelShader.InputOperation.PinAsOutput(PinComponent.Position),
                              interfaceOp.Outputs[0]);

                // Compositing is bound to output.
                pixelShader.OutputOperation.AddComponentAndLink(PinComponent.Colour, op.Outputs[0]);
            }

            pixelShader.Immutable = true;

            // 3) Initialize states.

            // Depth-stencil state.
            depthStencilState = new DepthStencilState();
            depthStencilState.DepthTestEnabled  = false;
            depthStencilState.DepthWriteEnabled = false;

            // Blend state (no blending default).
            blendState = new BlendState();


            // Rasterization state.
            rasterizationState                      = new RasterizationState();
            rasterizationState.FrontFacing          = Facing.CCW;
            rasterizationState.CullMode             = CullMode.None;
            rasterizationState.FillMode             = FillMode.Solid;
            rasterizationState.MultiSamplingEnabled = true;


            // We intern all states.
            depthStencilState  = StateManager.Intern(depthStencilState);
            blendState         = StateManager.Intern(blendState);
            rasterizationState = StateManager.Intern(rasterizationState);
        }
コード例 #2
0
        public void DAGUsageCases2()
        {
            // We first initialize our shader.
            ShaderCode code = new ShaderCode(BindingStage.VertexShader);
            {
                // We write a simple Tranform code:
                code.InputOperation.AddInput(PinComponent.Position, PinFormat.Floatx3);
                Pin positon = code.InputOperation.PinAsOutput(PinComponent.Position);

                // We first need to expand our position to float4 (adding 1 at the end).
                ExpandOperation expand = new ExpandOperation(PinFormat.Floatx4, ExpandType.AddOnesAtW);
                expand.BindInputs(positon);
                Pin expPosition = expand.Outputs[0];

                // We now create constant transform matrix.
                ConstantOperation mvpConstant = code.CreateConstant("MVP", PinFormat.Float4x4);
                Pin MVP = mvpConstant.Outputs[0];

                // We multiply matrix and pin.
                MultiplyOperation mul = new MultiplyOperation();
                mul.BindInputs(MVP, expPosition);
                Pin transPosition = mul.Outputs[0];

                // We just bind transformed position to output.
                code.OutputOperation.AddComponentAndLink(PinComponent.Position, transPosition);
            }

            // We create constant buffer manually.
            ConstantBufferLayoutBuilder builder = new ConstantBufferLayoutBuilder();

            builder.AppendElement("MVP", PinFormat.Float4x4, Pin.NotArray);
            ConstantBufferLayout layout = builder.CreateLayout();

            // We now fill the data.
            FixedShaderParameters parameters = code.FixedParameters;

            parameters.AppendLayout(layout);
            if (!parameters.IsDefined)
            {
                throw new Exception();
            }

            GraphicsDevice device = InitializeDevice();

            // We have all parameters defined, compile the shader.
            VShader shader = code.Compile(device, parameters) as VShader;

            // Shader expects data in constant buffers.
            TypelessBuffer     buffer         = new TypelessBuffer(Usage.Default, BufferUsage.ConstantBuffer, CPUAccess.Write, GraphicsLocality.DeviceOrSystemMemory, 4 * 4 * 4);
            ConstantBufferView constantBuffer = buffer.CreateConstantBuffer(layout);

            // We fill the buffer.
            constantBuffer.Map(MapOptions.Write);
            constantBuffer.SetConstant("MVP", Math.Matrix.Matrix4x4f.Identity);
            constantBuffer.UnMap();
        }
コード例 #3
0
        /// <summary>
        /// Creates an interface binder.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public InterfaceBinder CreateInterface([NotEmpty] string name)
        {
            ConstantOperation c = dag.CreateConstant(name, PinFormat.Interface, Pin.NotArray, null);

            return(new InterfaceBinder(c.Outputs[0], this));
        }
コード例 #4
0
        public unsafe void DAGUsageCases()
        {
            // We first initialize our shader.
            ShaderCode code = new ShaderCode(BindingStage.VertexShader);

            {
                // We write a simple Tranform code:
                code.InputOperation.AddInput(PinComponent.Position, PinFormat.Floatx3);
                Pin positon = code.InputOperation.PinAsOutput(PinComponent.Position);

                // We first need to expand our position to float4 (adding 1 at the end).
                ExpandOperation expand = new ExpandOperation(PinFormat.Floatx4, ExpandType.AddOnesAtW);
                expand.BindInputs(positon);
                Pin expPosition = expand.Outputs[0];

                // We now create constant transform matrix.
                ConstantOperation mvpConstant = code.CreateConstant("MVP", PinFormat.Float4x4);
                Pin MVP = mvpConstant.Outputs[0];

                // We multiply matrix and pin.
                MultiplyOperation mul = new MultiplyOperation();
                mul.BindInputs(MVP, expPosition);
                Pin transPosition = mul.Outputs[0];

                // We just bind transformed position to output.
                code.OutputOperation.AddComponentAndLink(PinComponent.Position, transPosition);
            }

            // Immutate it.
            code.Immutable = true;


            // We create constant buffer manually.
            ConstantBufferLayoutBuilder builder = new ConstantBufferLayoutBuilder();

            builder.AppendElement("MVP", PinFormat.Float4x4, Pin.NotArray);
            ConstantBufferLayout layout = builder.CreateLayout();

            // We now fill the data.
            FixedShaderParameters parameters = code.FixedParameters;

            parameters.AppendLayout(layout);
            if (!parameters.IsDefined)
            {
                throw new Exception();
            }



            using (GraphicsDevice device = InitializeDevice())
            {
                // We create shaders.

                // We have all parameters defined, compile the shader.
                VShader shader = code.Compile(device, parameters) as VShader;

                // Shader expects data in constant buffers.
                TypelessBuffer     tbuffer        = new TypelessBuffer(Usage.Dynamic, BufferUsage.ConstantBuffer, CPUAccess.Write, GraphicsLocality.DeviceOrSystemMemory, 4 * 4 * 4);
                ConstantBufferView constantBuffer = tbuffer.CreateConstantBuffer(layout);

                // We fill the buffer.
                constantBuffer.Map(MapOptions.Write);
                constantBuffer.SetConstant("MVP", new Math.Matrix.Matrix4x4f(1, 0, 0, 0,
                                                                             0, 1, 0, 0,
                                                                             0, 0, 1, 0,
                                                                             -0.5f, -0.5f, 0, 1));
                constantBuffer.UnMap();

                PShader pshader;
                VShader vshader = shader;
                using (ShaderCompiler compiler = device.CreateShaderCompiler())
                {
                    // And pixel shader.
                    compiler.Begin(BindingStage.PixelShader);
                    ShaderCompiler.Operand colour = compiler.CreateFixed(PinFormat.Floatx4, Pin.NotArray, new Vector4f(1, 0, 0, 1));
                    compiler.Output(colour, PinComponent.RenderTarget0);
                    pshader = compiler.End(null) as PShader;
                }

                // We create triangles.
                Geometry       geometry = new Geometry();
                TypelessBuffer buffer   = new TypelessBuffer(Usage.Static,
                                                             BufferUsage.VertexBuffer, CPUAccess.None, GraphicsLocality.DeviceOrSystemMemory, 4 * 4 * 3);

                byte[] d = buffer.Map(MapOptions.Read);
                fixed(byte *p = d)
                {
                    float *data = (float *)p;

                    // Vertex 0:
                    data[0] = -0.5f; data[1] = -0.5f; data[2] = 0.0f; data[3] = 1.0f;

                    // Vertex 1:
                    data[4] = 0.5f; data[5] = -0.5f; data[6] = 0.0f; data[7] = 1.0f;

                    // Vertex 2:
                    data[8] = 0.0f; data[9] = 0.5f; data[10] = 0.0f; data[11] = 1.0f;
                }

                buffer.UnMap();


                // Create vertex buffer and view.
                VertexBufferView vbuffer = buffer.CreateVertexBuffer(VertexFormat.Parse("P.Fx4"));

                // We construct geometry.
                geometry[0]       = vbuffer;
                geometry.Topology = Topology.Triangle;

                // Blend state.
                BlendState blendState = new BlendState();
                blendState[0] = false;

                blendState = StateManager.Intern(blendState);

                RasterizationState rastState = new RasterizationState();
                rastState.CullMode = CullMode.None;
                rastState.FillMode = FillMode.Solid;

                rastState = StateManager.Intern(rastState);

                DepthStencilState depthState = new DepthStencilState();
                depthState.DepthTestEnabled  = false;
                depthState.DepthWriteEnabled = false;

                depthState = StateManager.Intern(depthState);

                // Enter rendering loop.
                bool isClosed = false;
                window.Closed += delegate(Window w)
                {
                    isClosed = true;
                };

                for (uint i = 0; i < 1000; i++)
                {
                    window.DoEvents();

                    if (!isClosed)
                    {
                        SwapChain chain = device.SwapChain;

                        device.Enter();
                        try
                        {
                            // We just clear.
                            device.Clear(chain, Colour.Black);

                            // Set blend/rast.
                            device.SetBlendState(blendState, Colour.White, 0xFFFFFFFF);
                            device.RasterizationState = rastState;
                            device.SetDepthStencilState(depthState, 0);

                            // Sets states.
                            device.SetVertexShader(vshader, geometry, null, null, new ConstantBufferView[] { constantBuffer });
                            device.SetPixelShader(pshader, null, null, null, new RenderTargetView[] { chain }, null);

                            device.Viewport = new Region2i(0, 0, (int)chain.Width, (int)chain.Height);

                            // Render.
                            device.Draw(0, 3);
                        }
                        finally
                        {
                            device.Exit();
                        }

                        chain.Present();
                        //Thread.Sleep(10);

                        Console.WriteLine(device.DevicePerformance.CurrentFPS);
                    }
                }


                // Dispose all.
                vshader.Dispose();
                pshader.Dispose();
                geometry.Dispose();
                vbuffer.Dispose();
                buffer.Dispose();
            }
        }
コード例 #5
0
        void Initialize()
        {
            // Initialize shader code.
            vertexShaderCode = new ShaderCode(BindingStage.VertexShader);

            {
                // We register inputs.
                vertexShaderCode.InputOperation.AddInput(PinComponent.Position, PinFormat.Floatx2);  //< Position.
                vertexShaderCode.InputOperation.AddInput(PinComponent.TexCoord0, PinFormat.Floatx2); //< Texture coordinate.
                vertexShaderCode.InputOperation.AddInput(PinComponent.User0, PinFormat.Floatx4);     //< Custom attribute 0.
                vertexShaderCode.InputOperation.AddInput(PinComponent.User1, PinFormat.UInteger);    //< Fill ID.


                // Position transform array (dynamically sized).
                ConstantOperation positionTransformOp = vertexShaderCode.CreateConstant("PositionTransform",
                                                                                        PinFormat.Float4x4, Pin.NotArray, null);

                // Texture transform array (dynamically sized).
                ConstantOperation textureTransformOp = vertexShaderCode.CreateConstant("TextureTransform",
                                                                                       PinFormat.Float4x4, Pin.NotArray, null);

                // We expand the position.
                ExpandOperation expandPos = new ExpandOperation(PinFormat.Floatx4, ExpandType.AddOnesAtW);
                expandPos.BindInputs(vertexShaderCode.InputOperation.PinAsOutput(PinComponent.Position));

                // We transform position by matrix.
                MultiplyOperation multiply = new MultiplyOperation();
                multiply.BindInputs(positionTransformOp.Outputs[0], expandPos.Outputs[0]);
                Pin position = multiply.Outputs[0];

                // We expand texture coordinate.
                ExpandOperation expandTex = new ExpandOperation(PinFormat.Floatx4, ExpandType.AddOnesAtW);
                expandTex.BindInputs(vertexShaderCode.InputOperation.PinAsOutput(PinComponent.TexCoord0));

                // We transform by matrix.
                MultiplyOperation multiply2 = new MultiplyOperation();
                multiply2.BindInputs(textureTransformOp.Outputs[0], expandTex.Outputs[0]);
                Pin texcoord = multiply2.Outputs[0];


                // We register outputs.
                vertexShaderCode.OutputOperation.AddComponentAndLink(PinComponent.Position, position);
                vertexShaderCode.OutputOperation.AddComponentAndLink(PinComponent.TexCoord0, texcoord);
                vertexShaderCode.OutputOperation.AddComponentAndLink(PinComponent.User0,
                                                                     vertexShaderCode.InputOperation.PinAsOutput(PinComponent.User0));
                vertexShaderCode.OutputOperation.AddComponentAndLink(PinComponent.User1,
                                                                     vertexShaderCode.InputOperation.PinAsOutput(PinComponent.User1));
            }
            vertexShaderCode.Immutable = true;

            pixelShaderCode = new ShaderCode(BindingStage.PixelShader);

            {
                // We register inputs.
                pixelShaderCode.InputOperation.AddInput(PinComponent.Position, PinFormat.Floatx4);  //< Position.
                pixelShaderCode.InputOperation.AddInput(PinComponent.TexCoord0, PinFormat.Floatx4); //< Texture coordinate.
                pixelShaderCode.InputOperation.AddInput(PinComponent.User0, PinFormat.Floatx4);     //< Custom attribute 0.
                pixelShaderCode.InputOperation.AddInput(PinComponent.User1, PinFormat.UInteger);    //< Fill ID.

                // We resgister interface constants.
                ConstantOperation interfaceConstant = pixelShaderCode.CreateConstant("Fills", PinFormat.Interface,
                                                                                     Pin.DynamicArray, null);

                // TODO: distance from border must be "evaluated".

                // We  convert position/tex coordinate.
                SwizzleOperation swizzlePos = new SwizzleOperation(SwizzleMask.XY);
                swizzlePos.BindInputs(pixelShaderCode.InputOperation.PinAsOutput(PinComponent.Position));
                Pin position = swizzlePos.Outputs[0];

                SwizzleOperation swizzleTex = new SwizzleOperation(SwizzleMask.XY);
                swizzleTex.BindInputs(pixelShaderCode.InputOperation.PinAsOutput(PinComponent.TexCoord0));
                Pin texcoord = swizzleTex.Outputs[0];

                // We now add the fill operation.
                FillElementOperation fillOperation = new FillElementOperation();
                fillOperation.BindInputs(position, texcoord, pixelShaderCode.InputOperation.PinAsOutput(PinComponent.User0),
                                         pixelShaderCode.InputOperation.PinAsOutput(PinComponent.User1), interfaceConstant.Outputs[0]);

                // The output is colour.
                pixelShaderCode.OutputOperation.AddComponentAndLink(PinComponent.RenderTarget0, fillOperation.Outputs[0]);
            }
            pixelShaderCode.Immutable = true;

            // Depth-stencil state.
            depthStencilState = new DepthStencilState();
            depthStencilState.DepthTestEnabled  = false;
            depthStencilState.DepthWriteEnabled = false;

            // Blend state
            blendState = new BlendState();
            blendState.AlphaBlendDestination = BlendOperand.One;
            blendState.AlphaBlendSource      = BlendOperand.Zero;
            blendState.AlphaBlendOperation   = BlendOperation.Add;

            blendState.BlendDestination = BlendOperand.SrcAlphaInverse;
            blendState.BlendSource      = BlendOperand.SrcAlpha;
            blendState.BlendOperation   = BlendOperation.Add;

            // We enable blending.
            blendState[0] = true;

            // Rasterization state.
            rasterizationState                      = new RasterizationState();
            rasterizationState.FrontFacing          = Facing.CCW;
            rasterizationState.CullMode             = CullMode.None;
            rasterizationState.FillMode             = FillMode.Solid;
            rasterizationState.MultiSamplingEnabled = true; //< May change that in future.


            // We intern all states.
            depthStencilState  = StateManager.Intern(depthStencilState);
            blendState         = StateManager.Intern(blendState);
            rasterizationState = StateManager.Intern(rasterizationState);
        }
コード例 #6
0
        void Initialize()
        {
            // 1) We create vertex shader first.
            {
                vertexShader = new ShaderCode(BindingStage.VertexShader);
                vertexShader.InputOperation.AddInput(PinComponent.Position, PinFormat.Floatx2);



                // We now extend position
                ExpandOperation expandPositionOp = new ExpandOperation(PinFormat.Floatx4, ExpandType.AddOnesAtW);
                expandPositionOp.BindInputs(vertexShader.InputOperation.PinAsOutput(PinComponent.Position));
                Pin position = expandPositionOp.Outputs[0];

                // We now output position.
                vertexShader.OutputOperation.AddComponentAndLink(PinComponent.Position, position);
            }

            vertexShader.Immutable = true;

            // 2) We create pixel shader.
            {
                pixelShader = new ShaderCode(BindingStage.PixelShader);
                pixelShader.InputOperation.AddInput(PinComponent.Position, PinFormat.Floatx4);

                // We first use only XY.
                SwizzleOperation swizzleOp = new SwizzleOperation(SwizzleMask.XY);
                swizzleOp.BindInputs(pixelShader.InputOperation.PinAsOutput(PinComponent.Position));

                ConstantOperation constant = vertexShader.CreateConstant("Offset", PinFormat.Floatx2);

                // We first offset.
                SubstractOperation subOp = new SubstractOperation();
                subOp.BindInputs(swizzleOp.Outputs[0], constant.Outputs[0]);

                ConstantOperation interfaceOp = pixelShader.CreateConstant("Composite", PinFormat.Interface, Pin.DynamicArray);

                // We use the compositing operation.
                CompositingOperation op = new CompositingOperation();
                op.BindInputs(subOp.Outputs[0],
                              interfaceOp.Outputs[0]);

                // Compositing is bound to output.
                pixelShader.OutputOperation.AddComponentAndLink(PinComponent.RenderTarget0, op.Outputs[0]);
            }

            pixelShader.Immutable = true;

            // 3) Initialize states.

            // Depth-stencil state.
            depthStencilState = new DepthStencilState();
            depthStencilState.DepthTestEnabled  = false;
            depthStencilState.DepthWriteEnabled = false;

            // Blend state (no blending default).
            blendState = new BlendState();


            // Rasterization state.
            rasterizationState                      = new RasterizationState();
            rasterizationState.FrontFacing          = Facing.CCW;
            rasterizationState.CullMode             = CullMode.None;
            rasterizationState.FillMode             = FillMode.Solid;
            rasterizationState.MultiSamplingEnabled = true;


            // We intern all states.
            depthStencilState  = StateManager.Intern(depthStencilState);
            blendState         = StateManager.Intern(blendState);
            rasterizationState = StateManager.Intern(rasterizationState);

            // 4) We create geometry.
            alignedQuad = new Geometry(device);
            alignedQuad.AssociateBuffers = true;

            {
                // We create vertex buffer
                VertexBufferView vertexBuffer = VertexBufferView.Create(device, vertexFormat, Usage.Default,
                                                                        CPUAccess.None, GraphicsLocality.DeviceOrSystemMemory,
                                                                        new Vector2f(-1.0f, -1.0f), new Vector2f(1.0f, -1.0f),
                                                                        new Vector2f(1.0f, 1.0f), new Vector2f(-1.0f, -1.0f),
                                                                        new Vector2f(1.0f, 1.0f), new Vector2f(-1.0f, 1.0f));

                alignedQuad[0] = vertexBuffer;
            }

            // 5) We create pixel typeless buffer for constant buffer.
            pixelTypelessConstantBuffer = new TypelessBuffer(Usage.Dynamic, BufferUsage.ConstantBuffer,
                                                             CPUAccess.Write, GraphicsLocality.DeviceOrSystemMemory, ConstantBufferView.MaxSize);
        }