Reading a block of code into string

Started by
3 comments, last by lwm 10 years, 6 months ago

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 :)

Advertisement

Never mind! I figured it out. Lol :)

Ok, what was wrong? Other people might come here searching for a solution. Tell them how you fixed it.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

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;
        }

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.

current project: Roa

This topic is closed to new replies.

Advertisement