Upgraded MonoGame and now I can't get my shader to compile - opengl

I upgraded my monogame to the latest version. When building the shader in the pipeline tool at first I got the error Vertex shader 'SpriteVertexShader' must be SM 4.0 level 9.1 or higher! So I changed ps_2_0 to ps_4_0_level_9_1 but now I'm getting the error:
error X3004: undeclared identifier 'SecondPassTextureSampler'
Anybody have an idea on how to fix this issue?
#include "Macros.fxh"
texture Bitmap;
sampler2D FirstPassTexture = sampler_state{
Texture = (Bitmap);
MagFilter = Linear;
MinFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
sampler2D SecondPassTexture = sampler_state{
Texture = (Bitmap);
MagFilter = Linear;
MinFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
#define KERNEL_RADIUS 7
#define KERNEL_WIDTH (2*KERNEL_RADIUS + 1)
// Uniforms
BEGIN_CONSTANTS
float kernel[KERNEL_WIDTH];
float2 offsets[KERNEL_WIDTH];
MATRIX_CONSTANTS
float4x4 MatrixTransform _vs(c0) _cb(c0);
END_CONSTANTS
struct VSOutput
{
float4 position : SV_Position;
float4 color : COLOR0;
float2 texCoord : TEXCOORD0;
};
VSOutput SpriteVertexShader( float4 position : SV_Position,
float4 color : COLOR0,
float2 texCoord : TEXCOORD0)
{
VSOutput output;
output.position = mul(position, MatrixTransform);
output.color = color;
output.texCoord = texCoord;
return output;
}
float4 gaussH(VSOutput input): SV_Target0
{
float4 color = float4(0,0,0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
color += SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(offsets[i].x, 0.0) )) * kernel[i];
return color * input.color;
}
float4 gaussV(VSOutput input): SV_Target0
{
float4 color = float4(0.0,0.0,0.0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
color += SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )) * kernel[i];
return color * input.color;
}
float4 gaussVGlow(VSOutput input): SV_Target0
{
float4 color = float4(1.0,1.0,1.0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
{
float alpha = SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )).a * kernel[i];
color.a += alpha;
}
// This will make stripes on top of the glow
/*
float m = smoothstep(0.45, 0.55, 0.5*cos(25.0*atan2(input.texCoord.y-0.5, input.texCoord.x-0.5))+0.5) * 0.5 + 0.5;
color.a *= m;
*/
color.a = pow(color.a, 0.5);
color.rgb *= color.a; // Yeah, you have to pre multiply your alpha -- either that or render with premultiply option
return color * input.color;
}
technique Blur {
pass p0 {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussH();
}
pass p1 {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussV();
}
pass p1Glow {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussVGlow();
}
}
Macros.fxh is: http://pastebin.com/y51kFfii

From the Monogame Forum:
You get an error because you use the macro SAMPLE_TEXTURE for sampling, but do not use the macro DECLARE_TEXTURE for declaring the texture.
If you want to use the macros in Macros.fxh then the texture needs to be named SecondPassTexture and the sampler needs to be named SecondPassTextureSampler. (Do not change the line where SAMPLE_TEXTURE is called.)
If you look at the macro (_Macro.fxh, line 31), the code
SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) ))
expands to
SecondPassTexture.Sample(SecondPassTextureSampler, (input.texCoord + float2(0.0, offsets[i].y) ))
But in your case (original post) it should be
Bitmap.Sample(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) ))
Here is a simpler solution, you can try:
Just replace all SAMPLE_TEXTURE with tex2D. (Leave the samplers as in the original post.)

Related

How to add opacity to objects in directX11 with shaders

I'm new to directx and I need to set up opacity in my shaders. I don't use textures or anything and I just need to add opacity to whole objects based on one variable in shader.
I've tried to simply put this variable in shader's output color alpha channel, but it didn't work and I guess that the solution of this problem is not in shaders. This is my shader so far.
cbuffer ConstantBuffer : register(b0)
{
matrix World;
matrix View;
matrix Projection;
float4 vLigthDir[2];
float4 vLigthColor[2];
float4 vOutputColor;
float intensity;
float3 colors;
}
struct VS_INPUT
{
float4 pos : POSITION;
float3 norm : NORMAL;
};
struct PS_INPUT
{
float4 pos : SV_POSITION;
float3 norm : TEXCOORD0;
};
PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT) 0;
output.pos = mul(input.pos, World);
output.pos = mul(output.pos, View);
output.pos = mul(output.pos, Projection);
output.norm = mul(input.norm, World);
return output;
}
float4 PS(PS_INPUT input) : SV_Target
{
float4 finalColor = 0;
finalColor.r = colors.r;
finalColor.g = colors.g;
finalColor.b = colors.b;
finalColor.a = intensity;
return finalColor;
}
float4 PSSolid(PS_INPUT input) : SV_Target
{
return vOutputColor;
}
//Create BlendState
D3D11_BLEND_DESC blendDesc;
blendDesc.AlphaToCoverageEnable = false;
blendDesc.IndependentBlendEnable = false;
blendDesc.RenderTarget[0].BlendEnable = true;
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
hr = g_pd3dDevice->CreateBlendState(&blendDesc, &pBlendState);
//Using BlendState
float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
UINT sampleMask = 0xffffffff;
g_pImmediateContext->OMGetBlendState(&pBlendState, blendFactor, &sampleMask);
///////////Rendering geometry part///////////
g_pImmediateContext->OMGetBlendState(NULL, NULL, NULL);
I expect some objects to have opacity, but instead nothing happenening at all, there are just solid color.

Interpret the HLSL Shader Compiler Macro code

Profile:
WiN7 Prof SP1
Directx11
Development IDE: VS 2015
Program Language: C#
Application Type: 3D Rendering
Directx11 Wrapper: SLimDX
In my C# project I have a 3D renderer that is working.
I want to integrate a third Party HLSL Shader Code.
I have translated the code for Directx11 Feature level and it compiles with fxc.exe without errors or warnings.
The shader is loaded by my C# application and I can access all shader variables and set values of them.
Problem:
When I try to get a reference in my C# Code to access the first pass of the first technique of the Shader I get a SlimDX runtime Error.
I can reference the first technique of the shader with no runtime error but the obtained object has a flag is_valid = false showing the technique was not referenced correctly.
Clarification: I don't try to make a draw call but in preparation of the draw call I have to get a reference to the relevant pass.
Question:
I can't understand what the last compiler directives about the techniques mean exactly.
Can someone translate which HLSL Code will eventually produced from the compiler directive?
I mean the beginning from :
#define OBJECT_TEC(name, mmdpass) \
#include "../ray.conf"
#include "../ray_advanced.conf"
#include "../shader/math.fxsub"
#include "../shader/common.fxsub"
#include "../shader/gbuffer.fxsub"
#include "../shader/lighting.fxsub"
float3 LightDirection : DIRECTION < string Object = "Light"; >;
float3 LightSpecular : SPECULAR < string Object = "Light"; >;
bool ExistRay : CONTROLOBJECT<string name = "ray.x";>;
texture DiffuseMap: MATERIALTEXTURE;
sampler DiffuseMapSamp = sampler_state
{
texture = <DiffuseMap>;
MINFILTER = ANISOTROPIC;
MAGFILTER = ANISOTROPIC;
MIPFILTER = POINT;
MAXANISOTROPY = 16;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
};
float4 GetTextureColor(float4 albedo, float2 uv)
{
if (use_texture)
{
float4 TexColor = tex2D(DiffuseMapSamp, uv);
TexColor.rgb = lerp(1, TexColor * TextureMulValue + TextureAddValue, TextureMulValue.a + TextureAddValue.a).rgb;
albedo *= TexColor;
}
return srgb2linear(albedo);
}
void DrawObjectVS(
in float4 Position : POSITION,
in float3 Normal : NORMAL,
in float4 Texcoord : TEXCOORD0,
out float4 oTexcoord : TEXCOORD0,
out float3 oNormal : TEXCOORD1,
out float3 oViewdir : TEXCOORD2,
out float4 oPosition : SV_Position)
{
oNormal = Normal;
oTexcoord = Texcoord;
oViewdir = CameraPosition - Position.xyz;
oPosition = mul(Position, matWorldViewProject);
}
float4 DrawObjectPS(float4 texcoord : TEXCOORD0, float3 normal : TEXCOORD1, float3 viewdir : TEXCOORD2) : SV_Target
{
#if EXIST_RAY
#if DISCARD_ALPHA_ENABLE
float alpha = MaterialDiffuse.a;
#if DISCARD_ALPHA_MAP_ENABLE
if (use_texture) alpha *= tex2D(DiffuseMapSamp, texcoord.xy).a;
#endif
clip(alpha - DiscardAlphaThreshold);
#endif
return 0;
#else
if (ExistRay)
{
#if DISCARD_ALPHA_ENABLE
float alpha = MaterialDiffuse.a;
#if DISCARD_ALPHA_MAP_ENABLE
if (use_texture) alpha *= tex2D(DiffuseMapSamp, texcoord.xy).a;
#endif
clip(alpha - DiscardAlphaThreshold);
#endif
return 0;
}
else
{
float4 albedo = GetTextureColor(MaterialDiffuse, texcoord.xy);
float3 L = normalize(-LightDirection);
float3 V = normalize(viewdir);
float3 N = normalize(normal);
float MaterialRoughness = SmoothnessToRoughness(ShininessToSmoothness(MaterialPower));
float4 lighting = albedo;
lighting.rgb *= DiffuseBRDF(N, L, V, MaterialRoughness);
lighting.rgb += SpecularBRDF_GGX(N, L, V, MaterialRoughness, 0.04, 1.0);
lighting.rgb *= LightSpecular;
return linear2srgb(lighting);
}
#endif
}
#define OBJECT_TEC(name, mmdpass) \
technique name < string MMDPass = mmdpass;\
> { \
pass DrawObject { \
AlphaTestEnable = FALSE; AlphaBlendEnable = FALSE; \
VertexShader = compile vs_3_0 DrawObjectVS(); \
PixelShader = compile ps_3_0 DrawObjectPS(); \
} \
}
OBJECT_TEC(MainTec0, "object")
OBJECT_TEC(MainTecBS0, "object_ss")
technique EdgeTec < string MMDPass = "edge"; > {}
technique ShadowTech < string MMDPass = "shadow"; > {}
technique ZplotTec < string MMDPass = "zplot"; > {}

c++ DirectX lighting in pixel shader issue

I have a problem that I cant manage to figure out. I just added a point light to my project and it makes the textures go completely black. I have no idea why.
I think that it might be either the normal that is not updating correctly or it might be calculation of s.x, s.y and s.z.
I would be very happy if someone had time to take a look at it and help me. Thanks.
So. Here is my pixel shader :
Texture2D txDiffuse : register(t0);
SamplerState sampState;
cbuffer PointLight : register(b0)
{
float3 Pos;
float diff;
float amb;
float spec;
float range;
float intensity;
};
struct VS_IN
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD;
float4 Norm : NORMAL;
float4 Pos2 : POSITION;
};
float4 PS_main(VS_IN input) : SV_Target
{
float3 s = txDiffuse.Sample(sampState, input.Tex).xyz;
float3 lightPos = Pos;
float3 lightVector = lightPos - input.Pos2;
lightVector = normalize(lightVector);
float nDotL = dot(lightVector, input.Norm);
float diff1 = 0.8;
float amb1 = 0.1;
s.x = (s.x * diff * nDotL + s.x * amb);
s.y = (s.y * diff * nDotL + s.y * amb);
s.z = (s.z * diff * nDotL + s.z * amb);
return float4(s, 0.0);
};
Geometry shader :
cbuffer worldMatrix : register(b0)
{
matrix world;
}
cbuffer viewMatrix : register(b1)
{
matrix view;
}
cbuffer projectionMatrix : register(b2)
{
matrix projection;
}
struct VS_IN
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD;
};
struct VS_OUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD;
float4 Norm : NORMAL;
float4 Pos2 : POSITION;
};
[maxvertexcount(6)]
void main(triangle VS_IN input[3] : SV_POSITION, inout TriangleStream< VS_OUT > output2)
{
matrix wvp = mul(projection, mul(world, view));
matrix worldView = mul(world, view);
float4 normal = float4(cross(input[1].Pos - input[0].Pos, input[2].Pos - input[0].Pos), 0.0f);
normal = normalize(normal);
float4 rotNorm = mul(worldView, normal);
rotNorm = normalize(rotNorm);
VS_OUT output[3];
for (uint i = 0; i < 3; i++)
{
output[i].Pos = input[i].Pos;
output[i].Pos = mul(wvp, input[i].Pos);
output[i].Tex = input[i].Tex;
output[i].Norm = rotNorm;
output[i].Pos2 = mul(worldView, output[i].Pos);
output2.Append(output[i]);
}
output2.RestartStrip();
VS_OUT outputcopy[3];
for (uint i = 0; i < 3; i++)
{
outputcopy[i].Pos = input[i].Pos + (normal);
outputcopy[i].Pos = mul(wvp, outputcopy[i].Pos);
outputcopy[i].Tex = input[i].Tex;
outputcopy[i].Norm = rotNorm;
outputcopy[i].Pos2 = mul(worldView, outputcopy[i].Pos);
output2.Append(outputcopy[i]);
}
output2.RestartStrip();
}
Code to initializing the point light:
struct PointLight
{
Vector3 Pos;
float diff;
float amb;
float spec;
float range;
float intensity;
};
PointLight* pointLight = nullptr;
PointLight PL =
{
Vector3(0.0f, 0.0f, -3.0f),
0.8f,
0.2f,
0.0f,
100.0f,
1.0f
};
pointLight = &PL;
D3D11_BUFFER_DESC lightBufferDesc;
memset(&lightBufferDesc, 0, sizeof(lightBufferDesc));
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
lightBufferDesc.Usage = D3D11_USAGE_DEFAULT;
lightBufferDesc.StructureByteStride = 0;
lightBufferDesc.MiscFlags = 0;
lightBufferDesc.ByteWidth = sizeof(PointLight);
D3D11_SUBRESOURCE_DATA pointLightData;
memset(&pointLightData, 0, sizeof(pointLightData));
pointLightData.pSysMem = &PL;
gDevice->CreateBuffer(&lightBufferDesc, &pointLightData, &lightBuffer);
and in render() i run
gDeviceContext->PSSetConstantBuffers(0, 1, &lightBuffer);
Texture will be black if s.x, s.y, s.z equal to zero.
s.x = (s.x * diff * nDotL + s.x * amb);
s.y = (s.y * diff * nDotL + s.y * amb);
s.z = (s.z * diff * nDotL + s.z * amb);
Try to change diff and amb with a non-zero constant so that you can be sure that you set contant buffer correctly or not. If after you change them, it's still black then it must be nDotL and/or sampled texture that is zero. Then try with non-zero constant for texture sample. If they're still causing texture to look black then your light vector calculation is the culprit.

DirectX10, Rendering to a Rendertarget - No Alpha

My Problem is, that when I render to a rendertarget, which has a texture binded to it, the rendered information cannot be seen, because the alpha is 0.
If I turn the alpha value within the shader to 1 when displaying the rendertarget, then I can see it clear.
If I save the texture to a file, everywhere where there is nothing rendered, it is the clearcolor. Everywhere, where something is rendered, there's alpha = 0.
My blendstate:
D3D10_BLEND_DESC BlendState;
ZeroMemory(&BlendState, sizeof(D3D10_BLEND_DESC));
BlendState.AlphaToCoverageEnable = FALSE;
for (int32 i=0;i<this->m_TextureCount;i++)
BlendState.BlendEnable[i] = TRUE;
BlendState.SrcBlend = D3D10_BLEND_SRC_ALPHA;
BlendState.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
BlendState.BlendOp = D3D10_BLEND_OP_ADD;
BlendState.SrcBlendAlpha = D3D10_BLEND_ZERO;
BlendState.DestBlendAlpha = D3D10_BLEND_ZERO;
BlendState.BlendOpAlpha = D3D10_BLEND_OP_ADD;
for (int32 i = 0; i<this->m_TextureCount; i++)
BlendState.RenderTargetWriteMask[i] = D3D10_COLOR_WRITE_ENABLE_ALL;
this->m_pDevice->CreateBlendState(&BlendState, &this->m_pBlendState);
Before rendering:
this->m_pDevice->OMSetBlendState(this->m_pBlendState, 0, 0xffffffff);
Shadercode to draw the bricks:
float4x4 View;
float4x4 Projection;
Texture2D Diffuse;
SamplerState TextureSampler
{
Filter = MIN_MAG_MIP_Linear;
AddressU = WRAP;
AddressV = WRAP;
};
struct VertexShaderInput
{
float3 Position : POSITION0;
float2 UV : TEXCOORD0;
float3 Color : COLOR0;
float3 Normal : NORMAL0;
row_major float4x4 Offset : MATRIX;
};
struct VertexShaderOutput
{
float4 PositionOut : SV_POSITION;
float4 Position : POSITION0;
float2 UV : TEXCOORD0;
float3 Color : COLOR0;
float3 Normal : NORMAL0;
};
struct PixelShaderOutput
{
float4 colorMap: SV_TARGET0;
float4 normalMap: SV_TARGET1;
float4 positionMap: SV_TARGET2;
};
VertexShaderOutput VS_MAIN(VertexShaderInput input)
{
VertexShaderOutput Output = (VertexShaderOutput)0;
float4 pos = float4(input.Position.xyz, 1);
float4x4 Model = input.Offset;
float4x4 MVP = mul(mul(Model,View),Projection);
Output.Position = mul(pos,MVP);
Output.PositionOut = mul(pos,MVP);
Output.UV = input.UV;
Output.Color = input.Color;
Output.Normal = normalize(mul(input.Normal,(float3x3)Model));
return Output;
}
PixelShaderOutput PS_MAIN(VertexShaderOutput input)
{
PixelShaderOutput output;
output.colorMap = float4(1,0,0,1); // This is the texture above
output.normalMap = float4(input.Normal,1);
output.positionMap = float4(input.Position.xyz,1);
return output;
}
technique10 Main
{
pass p0
{
SetVertexShader(CompileShader(vs_4_0, VS_MAIN()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_4_0, PS_MAIN()));
}
};
changed Rendertarget's Blendstate to
D3D10_BLEND_DESC BlendState;
ZeroMemory(&BlendState, sizeof(D3D10_BLEND_DESC));
BlendState.AlphaToCoverageEnable = true;
for (int32 i=0;i<this->m_TextureCount;i++)
BlendState.BlendEnable[i] = TRUE;
BlendState.SrcBlend = D3D10_BLEND_SRC_ALPHA;
BlendState.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
BlendState.BlendOp = D3D10_BLEND_OP_ADD;
BlendState.SrcBlendAlpha = D3D10_BLEND_SRC_ALPHA;
BlendState.DestBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA;
BlendState.BlendOpAlpha = D3D10_BLEND_OP_ADD;
for (int32 i = 0; i<this->m_TextureCount; i++)
BlendState.RenderTargetWriteMask[i] = D3D10_COLOR_WRITE_ENABLE_ALL;
this->m_pDevice->CreateBlendState(&BlendState, &this->m_pBlendState);
And it works now.

Problem generating perlin noise using HLSL shader

I'm relatively new to HLSL and shaders in general, I've been trying to get a perlin noise shader working and I think I'm almost there, however the rendered noise is covered in banding or seam artifacts and I can't figure out why. heres a picture of the problem http://www.sharpoblunto.com/resources/news/perlin.jpg
I'm using c++ and directx 9, I'm generating a noise permutation texture and a gradient texture as inputs to the shader on the CPU using the following code
IDirect3DTexture9 *ImprovedPerlinNoise::GeneratePermTexture(IDirect3DDevice9 *device)
{
IDirect3DTexture9 *tex;
device->CreateTexture(256,256,1,NULL,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&tex,NULL);
D3DSURFACE_DESC surfaceDesc;
tex->GetLevelDesc(0,&surfaceDesc);
D3DLOCKED_RECT lockedRect;
tex->LockRect(0,&lockedRect,NULL,NULL);
DWORD *imageData = (DWORD*)lockedRect.pBits;
for (int y=0;y<surfaceDesc.Height;++y)
{
for (int x=0;x<surfaceDesc.Width;++x)
{
int A = Perm2d(x) + y;
int AA = Perm2d(A);
int AB = Perm2d(A + 1);
int B = Perm2d(x + 1) + y;
int BA = Perm2d(B);
int BB = Perm2d(B + 1);
int index =y*lockedRect.Pitch / 4 + x;
imageData[index] = D3DCOLOR_ARGB(BB,BA,AB,AA);
}
}
tex->UnlockRect(0);
return tex;
}
IDirect3DTexture9 *ImprovedPerlinNoise::GenerateGradientTexture(IDirect3DDevice9 *device)
{
IDirect3DTexture9 *tex;
device->CreateTexture(256,1,1,NULL,D3DFMT_Q8W8V8U8,D3DPOOL_MANAGED,&tex,NULL);
D3DSURFACE_DESC surfaceDesc;
tex->GetLevelDesc(0,&surfaceDesc);
D3DLOCKED_RECT lockedRect;
tex->LockRect(0,&lockedRect,NULL,NULL);
DWORD *imageData = (DWORD*)lockedRect.pBits;
for (int y=0;y<surfaceDesc.Height;++y)
{
for (int x=0;x<surfaceDesc.Width;++x)
{
int index =y*lockedRect.Pitch / 4 + x;
int q = _gradients[_permutation[x] % 16][0] * 127;
int w = _gradients[_permutation[x] % 16][1] * 127;
int v = _gradients[_permutation[x] % 16][2] * 127;
int u = 1 * 127;
imageData[index] = D3DCOLOR_ARGB(q,w,v,u);
}
}
tex->UnlockRect(0);
return tex;
}
The actual shader code is as follows
float4x4 World;
float4x4 View;
float4x4 Projection;
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 texCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 texCoord : TEXCOORD0;
float4 wPosition: TEXCOORD1;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.wPosition = mul(input.Position, World);
output.texCoord = input.texCoord;
return output;
}
texture permTexture2d;
texture permGradTexture;
sampler permSampler2d = sampler_state
{
texture = <permTexture2d>;
AddressU = Wrap;
AddressV = Wrap;
MAGFILTER = POINT;
MINFILTER = POINT;
MIPFILTER = NONE;
};
sampler permGradSampler = sampler_state
{
texture = <permGradTexture>;
AddressU = Wrap;
AddressV = Clamp;
MAGFILTER = POINT;
MINFILTER = POINT;
MIPFILTER = NONE;
};
float3 fade(float3 t)
{
return t * t * t * (t * (t * 6 - 15) + 10); // new curve
}
float4 perm2d(float2 p)
{
return tex2D(permSampler2d, p);
}
float gradperm(float x, float3 p)
{
float3 sample = tex1D(permGradSampler, x);
return dot(sample, p);
}
float inoise(float3 p)
{
float3 P = fmod(floor(p), 256.0); // FIND UNIT CUBE THAT CONTAINS POINT
p -= floor(p); // FIND RELATIVE X,Y,Z OF POINT IN CUBE.
float3 f = fade(p); // COMPUTE FADE CURVES FOR EACH OF X,Y,Z.
P = P / 256.0;
const float one = 1.0 / 256.0;
// HASH COORDINATES OF THE 8 CUBE CORNERS
float4 AA = perm2d(P.xy) + P.z;
// AND ADD BLENDED RESULTS FROM 8 CORNERS OF CUBE
return lerp( lerp( lerp( gradperm(AA.x, p ),
gradperm(AA.z, p + float3(-1, 0, 0) ), f.x),
lerp( gradperm(AA.y, p + float3(0, -1, 0) ),
gradperm(AA.w, p + float3(-1, -1, 0) ), f.x), f.y),
lerp( lerp( gradperm(AA.x+one, p + float3(0, 0, -1) ),
gradperm(AA.z+one, p + float3(-1, 0, -1) ), f.x),
lerp( gradperm(AA.y+one, p + float3(0, -1, -1) ),
gradperm(AA.w+one, p + float3(-1, -1, -1) ), f.x), f.y), f.z);
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float3 p = input.wPosition;
float inz = inoise(p)*0.5+0.5;
return float4(inz,inz,inz,1);
}
technique PerlinNoise
{
pass Pass1
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}
If anyone could point me in the right direction that would be fantastic, I really would like to be able to generate 3d noise in hlsl as its so much faster than doing it on the CPU (I have a working CPU implementation but it takes around 10 seconds to render a perlin cubemap for an object, the shader as I have it renders at 60FPS no problems at all)
Thanks in advance