I am in the middle of implementing a skybox to my game. I have been following this tutorial http://rbwhitaker.wikidot.com/skyboxes-2. I am using MonoGame as a framework and in order to support both Windows and Windows 8 metro I need to compile the shader with pixel and vertex shader 4.

compile vs_4_0_level_9_1
compile ps_4_0_level_9_1

However some of the hlsl syntax has been updated with DX10 and DX11.

I need to update this hlsl code:

float4x4 World;
float4x4 View;
float4x4 Projection;

float3 CameraPosition;

Texture SkyBoxTexture; 
samplerCUBE SkyBoxSampler = sampler_state 
   texture = <SkyBoxTexture>; 
   magfilter = LINEAR; 
   minfilter = LINEAR; 
   mipfilter = LINEAR; 
   AddressU = Mirror; 
   AddressV = Mirror; 

struct VertexShaderInput
    float4 Position : POSITION0;

struct VertexShaderOutput
    float4 Position : POSITION0;
    float3 TextureCoordinate : TEXCOORD0;

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

    float4 VertexPosition = mul(input.Position, World);
    output.TextureCoordinate = VertexPosition - CameraPosition;

    return output;

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
    return texCUBE(SkyBoxSampler, normalize(input.TextureCoordinate));

technique Skybox
    pass Pass1
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();

I quess I need to change Texture into TextureCube, change sampler, swap texCUBE() with TextureCube.Sample() and change PixelShader return semantic to SV_Target0.

I'm very new in shader languages and any help is appreciated!

  • 1
    \$\begingroup\$ Also the vertex shader position output should be SV_Position, and the technique syntax has changed a bit (see the examples here). \$\endgroup\$ – Nathan Reed Nov 15 '12 at 19:23
  • \$\begingroup\$ Thanks for pointing this out, I wasn't exactly aware of the technique changes. \$\endgroup\$ – Jaanus Varus Nov 16 '12 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.