Sign in to follow this  
goldie83

Pixel shader - simple color

Recommended Posts

Hi,

i don't want pass a color to the vertex shader -> only positions. I've have only one material without textures per triangle list.

 

How to pass the color for the pixel shader? Is there an another way to do that than not passing for each vertex a position and color?

Share this post


Link to post
Share on other sites

Hi, thank you for the Tip!.
Here is my implementation. Can be it done in a better way?

 

HLSL:

[source=HLSL]

// constant buffer for scene
cbuffer sceneData :register(b0)
{
    float4x4 viewProj;
};

// constant buffer for pixel shader
cbuffer pixelData :register(b1)
{
    float4 color;
};

// vertex shader in structure
struct VS_IN
{
    float4 position      : POSITION;
    float4x4 instancePos : INSTANCEPOSITION;
};

// pixel shader in structure
struct PS_IN
{
    float4 position      : SV_POSITION;
};

// vertex shader
PS_IN VS(VS_IN input)
{
    PS_IN output = (PS_IN)0;
    float4x4 x = mul(viewProj, input.instancePos);
    output.position = mul(x, input.position);
    return output;
}

// pixel shader
float4 PS(PS_IN input) : SV_Target
{
    return color;
}
[/source]

 

c#:
[source=C#]

using System;
using System.Windows.Forms;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Windows;
using System.Collections.Generic;
using SharpHelper;

using Buffer11 = SharpDX.Direct3D11.Buffer;

namespace SharpDXRenderer
{
    static class Program
    {
        /// <summary>
        /// constant scene buffer structure
        /// </summary>
        struct SceneData
        {
            public Matrix ViewProjection;
        }

        /// <summary>
        /// constant pixel buffer structure
        /// </summary>
        struct PixelData
        {
            public Vector4 Color;
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //render form
            RenderForm form = new RenderForm();
            form.Text = "Tests";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();

            using (SharpDevice device = new SharpDevice(form))
            {
                // init mesh
                SharpMesh mesh = SharpMesh.CreateFromObj(device, @"Resources\inputMesh.obj");

                // create shader from file and create input layout
                SharpShader shader = new SharpShader(device, "../../HLSL.hlsl",
                    new SharpShaderDescription() { VertexShaderFunction = "VS", PixelShaderFunction = "PS" },
                    new InputElement[] {
                        new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                        new InputElement("INSTANCEPOSITION",0,Format.R32G32B32A32_Float,0,1,InputClassification.PerInstanceData,1),
                        new InputElement("INSTANCEPOSITION",1,Format.R32G32B32A32_Float,16,1,InputClassification.PerInstanceData,1),
                        new InputElement("INSTANCEPOSITION",2,Format.R32G32B32A32_Float,32,1,InputClassification.PerInstanceData,1),
                        new InputElement("INSTANCEPOSITION",3,Format.R32G32B32A32_Float,48,1,InputClassification.PerInstanceData,1)
                   });

                // create instance buffer
                List<Matrix> transforms = new List<Matrix>() { new Matrix(), new Matrix() };
                SharpInstanceBuffer<Matrix> instanceBuffer = new SharpInstanceBuffer<Matrix>(device, transforms.ToArray());

                // create constant buffer
                Buffer11 sceneBuffer = shader.CreateBuffer<SceneData>();
                Buffer11 pixelBuffer = shader.CreateBuffer<PixelData>();

                fpsCounter.Reset();

                // main loop
                RenderLoop.Run(form, () =>
                {
                    // resizing
                    if (device.MustResize)
                        device.Resize();

                    // apply states
                    device.UpdateAllStates();

                    // clear color
                    device.Clear(Color.CornflowerBlue);

                    // apply shader
                    shader.Apply();

                    // set transformation matrix
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1F, 1000.0F);
                    Matrix view = Matrix.LookAtLH(new Vector3(0, 2, -5), new Vector3(), Vector3.UnitY);

                    // update the transfoms
                    List<Matrix> instanceTransforms = new List<Matrix>();
                    instanceTransforms.Add(Matrix.RotationY(Environment.TickCount / 1000.0F) * new Matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -2.5f, 0, 2, 1));
                    instanceTransforms.Add(Matrix.RotationY(-Environment.TickCount / 2000.0F) * new Matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2.5f, 0, 2, 1));
                    instanceBuffer.UpdateBuffer(instanceTransforms.ToArray());

                    // write data inside constant buffer and apply to shader
                    SceneData sceneInformation = new SceneData()
                    {
                        ViewProjection = view * projection,
                    };
                    device.UpdateData<SceneData>(sceneBuffer, sceneInformation);
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, sceneBuffer);

                    // draw mesh
                    mesh.Begin();
                    for (int i = 0; i < mesh.SubSets.Count; i++)
                    {
                        // write data to pixel shader buffer and apply to shader
                        PixelData pixelData = new PixelData() { Color = mesh.SubSets[i].DiffuseColor };
                        device.UpdateData(pixelBuffer, pixelData);
                        device.DeviceContext.PixelShader.SetConstantBuffer(1, pixelBuffer);

                        // draw instances
                        instanceBuffer.DrawInstance(2, mesh.SubSets[i].IndexCount, mesh.SubSets[i].StartIndex);
                    }

                    // begin drawing text
                    device.Font.Begin();

                    // draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);

                    // flush text to view
                    device.Font.End();

                    // present
                    device.Present();
                });


                //release resources
                mesh.Dispose();
                sceneBuffer.Dispose();
                pixelBuffer.Dispose();
                instanceBuffer.Dispose();
            }
        }
    }
}
[/source]

Edited by goldie83

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this