Jump to content

  • Log In with Google      Sign In   
  • Create Account


qorthos

Member Since 28 Jan 2013
Offline Last Active May 30 2013 05:32 PM

Topics I've Started

[SlimDX] Blending woes.

13 May 2013 - 02:46 PM

Hi everyone,

 

I'm having trouble getting an edge detection shader to blend with the previously rendered image.

 

Quick background:

I render a GBuffer (color, normal depth)

I then render an image with just direct and ambient light values (to the 

Finally I use the normal and depth map from the GBuffer to create my edges.

 

Everything works fine when I render just the GBuffer and lighting.  I get this:

http://imgur.com/iPxkXJc,mBCTA9P,aVi7B6U#0

 

When I render just the edge detection shader I get this:

http://imgur.com/iPxkXJc,mBCTA9P,aVi7B6U#1

 

When I add in edge detection after the lighting render I get this:

http://imgur.com/iPxkXJc,mBCTA9P,aVi7B6U#2

 

I am stumped.  I don't think its actually a blending issue.  I set the BlendState on the shader with this:

 

BlendState SrcAlphaBlend
{
   BlendEnable[0]           = TRUE;
   SrcBlend                 = SRC_ALPHA;
   DestBlend                = INV_SRC_ALPHA;
   BlendOp                  = ADD;
   SrcBlendAlpha            = ONE;
   DestBlendAlpha           = ONE;
   BlendOpAlpha             = ADD;
   RenderTargetWriteMask[0] = 0x0F;
};

 

I have a DepthStencilView set when I do the GBuffer render, but not when I do the lighting and edge detection.

 

Any ideas?


SlimDX and SSAO

18 February 2013 - 07:31 PM

Hi everyone.  I'm coming up against a wall against and need help!  I've been trying to figure out how to implement SSAO for over a week now and keep getting garbage.  A lot of the code I'm using is lifted from other places (MJP, Jose Mendez).  I think I understand it.  Mostly...

 

I'm rendering my scene to a diffuse color view, normal view (output.Normal = input.Normal * 0.5f + 0.5f) and a depth buffer (output.Depth = input.Depth.x / input.Depth.y)

 

This is the scene I'm rendering (just showing diffuse colors):

2cRVzoj.jpg

 

The cubes are 1 unit cubed.

 

This is my SSAO shader:

Texture2D DepthTexture	: register(t0);
Texture2D NormalTexture : register(t1);
Texture2D RandomTexture : register(t2);

cbuffer Once : register(cb0)
{
	float Bias;
	float Intensity;
	float Radius;
	float Scale;
}

cbuffer OncePerFrame : register(cb1)
{
	float4x4	Projection;
	float4x4	InvProjection;
	float		FarClip;
}

SamplerState sampleType
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = CLAMP;
    AddressV = CLAMP;
};

struct VS_IN
{
	float4 Position  : POSITION;
	float2 Texcoords : TEXCOORD0;
};

struct VS_OUT
{
	float4 Position	 : SV_POSITION;
	float2 Texcoords : TEXCOORD0;
};


VS_OUT VS( VS_IN input)
{
	VS_OUT output = (VS_OUT)0;
	
	output.Position = input.Position;
	output.Texcoords = input.Texcoords;

	return output;
}


float3 GetPosition(float2 vTexCoord)
{
    // Get the depth value for this pixel
    float z = DepthTexture.Sample(sampleType, vTexCoord).r;

    // Get x/w and y/w from the viewport position
    float x = vTexCoord.x * 2 - 1;
    float y = (1 - vTexCoord.y) * 2 - 1;
    float4 vProjectedPos = float4(x, y, z, 1.0f);

    // Transform by the inverse projection matrix
    float4 vPositionVS = mul(vProjectedPos, InvProjection);  

    // Divide by w to get the view-space position
    return vPositionVS.xyz / vPositionVS.w;  
}

float DoAmbientOcclusion(in float2 tcoord, in float2 uv, in float3 p, in float3 cnorm)
{
	float3 diff = GetPosition(tcoord + uv) - p;
	const float3 v = normalize(diff);
	const float d = length(diff) * Scale;
	return max(0.0, dot(cnorm, v) - Bias) * (1.0 / (1.0 + d)) * Intensity;
}


float4 PS(VS_OUT input) : SV_Target
{
	const float2 vec[4] = 
	{
		float2(1, 0),float2(-1, 0),
		float2(0, 1),float2(0, -1)
	};
	
	float3 p = GetPosition(input.Texcoords);
	float3 n = NormalTexture.Sample(sampleType, input.Texcoords).xyz * 2 - 1;
	float2 rand = RandomTexture.Sample(sampleType, input.Texcoords).xy;
	
	float ao = 0.0f;
	float rad = Radius / p.z;
	
	//**SSAO Calculation**//

	int iterations = 4;
	for (int j = 0; j < iterations; ++j)
	{
		float2 coord1 = reflect(vec[j], rand) * rad;
		float2 coord2 = float2(coord1.x * 0.707 - coord1.y * 0.707,
								coord1.x * 0.707 + coord1.y * 0.707);
		
		ao += DoAmbientOcclusion(input.Texcoords, coord1 * 0.25, p, n);
		ao += DoAmbientOcclusion(input.Texcoords, coord2 * 0.50, p, n);
		ao += DoAmbientOcclusion(input.Texcoords, coord1 * 0.75, p, n);
		ao += DoAmbientOcclusion(input.Texcoords, coord2 * 1.00, p, n);
	}

	ao /= (float)iterations * 4.0;
	
	//**END**//

	return float4(1 - ao, 1-ao, 1-ao, 1);
}

 

 

I've gone through and debugged the shader, all of my settings are being set.  I'm reasonably sure that I'm getting position from depth correctly and decoding the normal right.  

 

Is the problem with the SSAO settings?  I'm using this:

 

            float bias = 0.01f;
            float intensity = 0.5f;
            float radius = 1.1f;
            float scale = 0.5f;
 
Which seems like are right...  Aggh.  I'm pulling my hair out here.

 


Writing to a dynamic vertex buffer

04 February 2013 - 12:25 PM

Hello everyone, I'm currently instancing a set of primitives (cubes, spheres, etc) with modulo arithmetic and I'm having wit the DataStream.WriteRange<> method.

 

I'm using this to write to the InstanceBuffer:

DataStream dsInstances = instanceBuffer.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None);

            if (instancesStart + instancesCount < instancesMax)
            {
                dsInstances.Position = instancesStart;
                dsInstances.WriteRange<VertexPrimitiveInstance>(instances, instancesStart, instancesCount);
            }
            else
            {
                dsInstances.Position = instancesStart;
                dsInstances.WriteRange<VertexPrimitiveInstance>(instances, instancesStart, instancesMax - instancesStart);

                dsInstances.Position = 0;
                dsInstances.WriteRange<VertexPrimitiveInstance>(instances, 0, instancesCount - (instancesMax - instancesStart));
            }

            instanceBuffer.Unmap();

 

 

I see nothing the first loop around from 0 to InstancesMax.  However, I start seeing my objects once I've written all values to the buffer at least once.  Then I get some very strange flicker and occasionally deformed geometry.

 

If I change the above to write the entire array of VertexPrimitiveInstances to the buffer, I get no flicker.


            DataStream dsInstances = instanceBuffer.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None);

            dsInstances.Position = 0;
            dsInstances.WriteRange<VertexPrimitiveInstance>(instances);

            instanceBuffer.Unmap();

 

 

 

I presume that the second method is far slower than the first (if if worked) and is thus undesirable.  Am I using WriteRange wrong, or am I wrong in thinking I should only set the vertices that are changing (like I did in XNA)?


[SlimDX] Instanced Triangles

30 January 2013 - 07:35 PM

Hi everyone, I'm back!  This time I have an equally simple problem as my previous problem.  I've got constant buffers, indexbuffers and view/projection matrices worked out (who woulda though I'd need to transpose the matrices!).  Now I'm having a spot of bother trying to instance my triangle.

 

 

I'm trying to set up the InputElement array and the InputLayout and getting a gibberish error message (I'm using visual studio express and can't see the native code exceptions and DebugView tells me nothing) 

 

Here's my shader code:

cbuffer OncePerDraw : register(cb0)
{
	float4		color;
	float4x4	world;	
}

cbuffer OncePerFrame : register(cb1)
{
	float4x4	view;
	float4x4	projection;	
}


struct VS_INPUT
{
	float4 Position : POSITION;
	float3 Offset	: TEXCOORD0;
};

struct VS_OUTPUT
{
	float4 Position : SV_POSITION;
	float4 Color	: COLOR0;
};


VS_OUTPUT VShader(VS_INPUT input)
{
	VS_OUTPUT output = (VS_OUTPUT)0;
	
	float4 worldPosition = mul(input.Position, world);
	worldPosition += float4(input.Offset, 0);
    float4 viewPosition = mul(worldPosition, view);
    output.Position = mul(viewPosition, projection);
		
	output.Color = color;
	//output.Color = float4(1, 1, 1, 1);

	return output;
}

float4 PShader(VS_OUTPUT input) : SV_Target
{
	float4 color = input.Color;
	return color;
}

 

 

 

And a snippet of trying to set up the InputElement array:

            using (var bytecode = ShaderBytecode.CompileFromFile("colored.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
            {
                isColored = ShaderSignature.GetInputSignature(bytecode);
                vsColored = new VertexShader(device, bytecode);
            }

            primitiveElements = new[] 
            { 
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerInstanceData, 1)
            };          
                        
            ilPrimitiveColored = new InputLayout(device, isColored, primitiveElements);

 

 

The error is thrown on the line that creates the InputLayout.  I'm a bit baffled, as changing the InputClassification of the TEXCOORD0 semantic to PerVertexData and the stepRate to 0 solves the problem.


[SlimDX] Shader Constants and their registers

28 January 2013 - 02:54 PM

Hi everyone.  I've just started transitioning from XNA to SlimDX (and DirectX11) and boy am I confused!  I have a deferred shader and a good way to instance primitives (cubes cones, spheres, etc) in XNA and I'm trying currently trying to learn enough of SlimDX and Directx11 to recreate those.

 

My brain already hurts.  I started off by trying to expand the 3rd SlimDX tutorial: simple triangle.  I modified the tutorial effect file to this:

 

cbuffer ColorBuffer : register(cb0)
{
	float4	color;
}

cbuffer WorldBuffer : register(cb1)
{
	float4x4	world;	
}

struct VS_INPUT
{
	float4 Position : POSITION;
};

struct VS_OUTPUT
{
	float4 Position : SV_POSITION;
	float4 Color	: COLOR0;
};


VS_OUTPUT VShader(VS_INPUT input)
{
	VS_OUTPUT output = (VS_OUTPUT)0;

	output.Position = mul(input.Position, world);
	output.Color = color;

	return output;
}

float4 PShader(VS_OUTPUT input) : SV_Target
{
	return input.Color;
}

 

It's simple enough.  

 

I then attempt to set the shader constants in the SlimDX MessagePump:

            MessagePump.Run(form, () =>
            {
                context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));

                System.DateTime now = System.DateTime.Now;
                System.TimeSpan span = System.TimeSpan.FromTicks(now.Ticks - start.Ticks);

                world = Matrix.RotationZ((float)span.TotalSeconds);

                worldStream.Position = 0;
                worldStream.Write(world);
                worldStream.Position = 0;
                
                worldBuffer = new Buffer(
                    device,
                    worldStream,
                    64,
                    ResourceUsage.Dynamic,
                    BindFlags.ConstantBuffer,
                    CpuAccessFlags.Write,
                    ResourceOptionFlags.None,
                    4);

                color = new Color4(1, 1, 1, 1);
                colorStream.Position = 0;
                colorStream.Write(color);
                colorStream.Position = 0;
                context.VertexShader.SetConstantBuffer(worldBuffer, 0);

                colorBuffer = new Buffer(
                    device,
                    colorStream,
                    16,
                    ResourceUsage.Dynamic,
                    BindFlags.ConstantBuffer,
                    CpuAccessFlags.Write,
                    ResourceOptionFlags.None,
                    4);

                context.VertexShader.SetConstantBuffer(colorBuffer, 1);
                
                

                // draw the triangle
                context.Draw(3, 0);
                swapChain.Present(0, PresentFlags.None);
            });

 

The world matrix is set correctly, but the output behaves as if the color constant was never set.  And to add to my puzzlement, I don't understand why context.VertexShader.SetConstantBuffer(worldBuffer, 0) sets the second ConstantBuffer in the shader file.  

 

What am I doing wrong?


PARTNERS