Jump to content

  • Log In with Google      Sign In   
  • Create Account


Reading a block of code into string


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 KoldGames   Members   -  Reputation: 222

Like
0Likes
Like

Posted 27 September 2013 - 04:58 PM

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



Sponsor:

#2 KoldGames   Members   -  Reputation: 222

Like
0Likes
Like

Posted 27 September 2013 - 06:30 PM

Never mind! I figured it out. Lol :)



#3 ChaosEngine   Crossbones+   -  Reputation: 2151

Like
1Likes
Like

Posted 29 September 2013 - 04:18 AM

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

#4 KoldGames   Members   -  Reputation: 222

Like
1Likes
Like

Posted 29 September 2013 - 11:37 AM

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


#5 lwm   Members   -  Reputation: 1304

Like
0Likes
Like

Posted 30 September 2013 - 10:27 AM

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, 30 September 2013 - 10:29 AM.

current project: Roa





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS