Hi, I’m trying to implement this shader in Monogame http://blog.nostatic.org/2009/09/fractal-rendering-on-gpu-mandelbrot-and.html but all I’m getting is a black screen.
Here’s are the relevant code pieces:
_spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone, effect);
_spriteBatch.Draw(screenTexture, Vector2.Zero, Color.White);
_spriteBatch.End();
screenTexture
is just an orange texture that takes up the whole window.
and the effects file:
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
Texture2D SpriteTexture;
sampler2D SpriteTextureSampler = sampler_state
{
Texture = (SpriteTexture);
// AddressU = CLAMP;
// AddressV = CLAMP;
// MagFilter = LINEAR;
// MinFilter = LINEAR;
// Mipfilter = LINEAR;
};
struct VertexShaderOutput
{
float4 Position : POSITION;
float4 Color : COLOR0;
float2 TextureCoordinates : TEXCOORD0;
};
int Iterations = 128;
float2 Pan = float2(0.5, 0);
float Zoom = 3;
float Aspect = 0.5f;
float2 JuliaSeed = float2(0.39, -0.2);
float3 ColorScale = float3(4, 5, 6);
float ComputeValue(float2 v, float2 offset)
{
float vxsquare = 0;
float vysquare = 0;
int iteration = 0;
int lastIteration = Iterations;
do
{
vxsquare = v.x * v.x;
vysquare = v.y * v.y;
v = float2(vxsquare - vysquare, v.x * v.y * 2) + offset;
iteration++;
if ((lastIteration == Iterations) && (vxsquare + vysquare) > 4.0)
{
lastIteration = iteration + 1;
}
} while (iteration < lastIteration);
return (float(iteration) - (log(log(sqrt(vxsquare + vysquare))) / log(2.0))) / float(Iterations);
}
float4 Mandelbrot_PixelShader(float2 texCoord : TEXCOORD0) : COLOR0
{
float2 v = (texCoord - 0.5) * Zoom * float2(1, Aspect) - Pan;
float val = ComputeValue(v, v);
return float4(sin(val * ColorScale.x ), sin(val * ColorScale.y), sin(val * ColorScale.z), 1);
}
technique SpriteDrawing
{
pass P0
{
PixelShader = compile PS_SHADERMODEL Mandelbrot_PixelShader();
}
};
I’m new to HLSL and shaders in general so maybe there’s a small error somewhere?
edit: I was able to fix this using this tutorial instead https://digitalerr0r.net/2010/12/13/fundamentals-of-fractals-5-the-mandelbrot-set/