Пример #1
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);
        }
Пример #2
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);
        }