• Advertisement
Sign in to follow this  

Reading a block of code into string

This topic is 1574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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

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
Sign in to follow this  

  • Advertisement