Sign in to follow this  
KoldGames

Reading a block of code into string

Recommended Posts

KoldGames    222

Hello! I have a .fx file:

cbuffer MatrixBuffer
{
	matrix worldMatrix;
	matrix viewMatrix;
	matrix projectionMatrix;
};

struct VertexInputType
{
	float4 position : POSITION;
	float4 color : COLOR;
};

struct PixelInputType
{
	float4 position : SV_POSITION;
	float4 color : COLOR;
};

PixelInputType VS(VertexInputType input)
{
	PixelInputType output;

	input.position.w = 1.0f;

	output.position = mul(input.position, worldMatrix);
	output.position = mul(output.position, viewMatrix);
	output.position = mul(output.position, projectionMatrix);

	output.color = input.color;

	return output;
}

float4 PS(PixelInputType input) : SV_TARGET
{
	return input.color;
}

What I'm trying to do is get my VS method into one string and my PS method into another so that the result is like this:

 

Vertex Shader Code String:

PixelInputType VS(VertexInputType input)
{
	PixelInputType output;

	input.position.w = 1.0f;

	output.position = mul(input.position, worldMatrix);
	output.position = mul(output.position, viewMatrix);
	output.position = mul(output.position, projectionMatrix);

	output.color = input.color;

	return output;
}

Pixel Shader Code String:

float4 PS(PixelInputType input) : SV_TARGET
{
	return input.color;
}

But I've tried forever and cannot seem to get it to work:

string vsCode = null, psCode = null;
            bool receivingVSCode = false, receivingPSCode = false;
            bool receivedVSCode = false, receivedPSCode = false;

            foreach (string line in code)
            {
                if (!receivedVSCode)
                {
                    if (line.Contains(block.VertexShaderEntry))
                    {
                        receivingVSCode = true;
                    }

                    if (receivingVSCode)
                    {
                        vsCode += line;
                    }

                    if(line.StartsWith("}"))
                    {
                        receivedVSCode = true;
                    }
                }

                if (!receivedPSCode)
                {
                    if (line.Contains(block.PixelShaderEntry))
                    {
                        receivingPSCode = true;
                    }

                    if (receivingPSCode)
                    {
                        psCode += line;
                    }

                    if (line.StartsWith("}"))
                    {
                        receivedPSCode = true;
                    }
                }
            }

How would I do this correctly? Thanks :)

Share this post


Link to post
Share on other sites
KoldGames    222

Here you go:

static string GetCode(string entry, string[] code)
        {
            string block = "";
            bool reading = false;
            foreach (string line in code)
            {
                if (line.Contains(entry))
                    reading = true;

                if (reading)
                {
                    if (!line.Contains('}'))
                        block += line;
                    else
                        reading = false;
                }
            }

            return block;
        }

Share this post


Link to post
Share on other sites
lwm    2518

But what if your pixel shader looks like this:

float4 PS(PixelInputType input) : SV_TARGET
{
	if(input.color.r == 0)
	{
		discard;
	}
	return input.color;
}

Unless you know this is never going to happen, you should probably at least count the number of curly brackets. Add one when it's an opening bracket, subtract one when it's a closing bracket. You've reached the end of the function when the counter reaches zero again.

Edited by lwm

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