Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Want to write my first HLSL Shader


  • Please log in to reply
3 replies to this topic

#1 Ryan 3000

Ryan 3000

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:04:04 AM

Posted 10 October 2011 - 02:55 PM

Hi. I've spent a long time writing quick games in XNA 3 and XNA 4.0 and I've always liked writing the graphical effects the most. I want to write my first HLSL shader because I know it has awesome potential for graphical effects. However, I want to write it for a 2D program. I think I can get rid of a lot of the overhead like all of the perspective matrices and such, but there's still a decent learning curve until I get my first shader written. I'm pretty sure I can handle the pixel shader part easily but I do need some help with the vertex shader. I am working with the default shader that XNA writes for me.

The language is not entirely foreign to me but there are some syntax changes from C#/Java that I'm not used to and it's throwing me off. It's difficult for me to find a lot of this documentation on Google or more precisely, it doesn't answer my specific questions. Please see my questions commented in the code below. I've numbered them so they can be easily answered in your responses. Thanks a ton.

float4x4 World;
float4x4 View;
float4x4 Projection;//1. can I delete these? I'm working in 2D


struct VertexShaderInput //2. I assume this represents all of the data that I will receive from the Shader's caller?
{
    float4 Position : POSITION0; //3. what does the ": POSITION0" part do?
    
};

struct VertexShaderOutput
{
    float4 Position : POSITION0; //what does the ": POSITION0" part do?

};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input) ]
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection); //4. don't think I need these three lines? I'm not doing 3D transformation

    return output; //5. this is passed as data to PixelShader, correct?
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 //6. again, what is the ": COLOR0" part for?
{
    // TODO: add pixel shader code here.

    return float4(1, 0, 0, 1);
}

technique Technique1
{
    pass Pass1
    {
        // No questions here.

        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

Edited by Ryan 3000, 11 October 2011 - 11:08 AM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:04 AM

Posted 12 October 2011 - 10:03 AM

This link should explain structures well enough so that you can understand them.

As far as whether or not you can delete specific lines, what about maybe just deleting them and seeing what happens? That's what I would do.

#3 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:04:04 AM

Posted 12 October 2011 - 02:00 PM

Thanks Groovicus. I understand what a struct is, I'm just generally more confused by the line:
float4 Position : POSITION0;
What is the purpose of the ": POSITION0"? My computer science professor suggested that POSITION0 is a required part of the code and you are satisfying that need by attaching the alias Position to it, but I am not sure.
No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:04 AM

Posted 14 October 2011 - 08:36 AM

According to the page I linked to, the part that comes after the colon is

interfaces (Optional)
A list that contains the interfaces implemented by the struct, all separated by commas.


So you can create a structure, and have the structure implement an interface. Do you understand how one can extend classes by implementing another class? Basically it means that all methods of that class are available to them implementing structure. So in your case, it is implementing an interface called POSITION0. I can't find any reference anywhere as to what the interface allows one to do though. Perhaps it is a custom designed interface that lives within your development environment only.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users