Ugliness on different hardware (solved)

Started by
9 comments, last by cephalo 9 years, 9 months ago

I'm trying a project I've been working on on a different computer than I usually use, and I'm getting extreme artifacts on this machine.

My usual computer is an older Windows 7 computer with a really bad, built in Intel vid card. My application works exactly like it is supposed to on that machine. I'm not doing anything out of the ordinary with my shaders, they are very simple solid color outputs and simple vertex adjustments. I haven't yet checked the reference device on that machine because I haven't had any problems.

This new machine is a Windows 8 machine with a much better NVidia vid card, however, in the screenshot you can see that certain parts of the screen are garbled and mixed together. They flash like crazy when the camera moves. This same problem is also evident when I use the reference device. I have ruled out problems with my texture generation by disabling the sampling in the pixel shader. The problem exists even when I don't use textures.

[attachment=22362:HexDemoProblem.jpg]

How should I go about solving this problem?

EDIT: Also, I forgot to mention I'm using SharpDX and VS2012 Express.

Advertisement

Looks like you have some z-fighting. Depth buffers might have different precision on different cards, so that might explain why it's only visible in some scenarios.

It'd be nice to see a screenshot of the game working correctly, for comparison.

z-fighting was my first thought as well when I read your post, but I'm not too certain how much variance you'd actually get between different cards if you specify the same precision of depth buffer.

Also, your screenshot doesn't totally look like z-fighting to me. Especially the bluish junk around the edges of the hexagons. What is the format of your color buffer? And what's the format of your depth buffer? Are you always clearing the depth buffer between frames? You say the screen gets garbled when the camera moves, which sounds like the type of problem that can occur if you aren't clearing your z-buffer (though I have no idea how this would work any differently on two separate video cards).

Another thought: do you clear your color buffer after creating it and before using it? If you have a floating point buffer, uninitialized data could be total garbage which would mess up your blending equations and lead to some pretty weird looking results. And I could TOTALLY imagine the contents of a color buffer being different on your two machines.

This same problem is also evident when I use the reference device

Just to be clear: You get these effects too with the reference device ?

Did you also check if vsync is enabled yes/no on both machines?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This same problem is also evident when I use the reference device

Just to be clear: You get these effects too with the reference device ?

Yes, I do. I haven't checked this on the machine that works, but I will here in a few minutes. The problem looks very much like massive z-fighting, but it is not z-fighting. It happens also when I stop drawing everything except one thing, so even when there is nothing to fight with, I get this corruption.

Ok, so I would assume the hardware driver lets you do something you're not supposed to. Double check the debug output. Dumb down the setup to the draw call(s) that provoke the artifact and then tell us more about your whole setup. Also: Use PNG or some other non-lossy format for screenshots. JPGs disturb a closer look.

Lucky guess: Some screw-up with blending, maybe in conjunction with MSAA ?

Ok, so I was able to visit the older machine and try the reference device, and the problem exists there too! Dang. First, I'll show a screenshot of what it is supposed to look like. I had just gotten rid of any undesirable z-fighting and such and was quite proud of it...

[attachment=22365:HexDemo.jpg]

This actually looks better animated, because the water surface changes transparency in a nice way and the rivers flow evenly from higher lakes to lower lakes down hills.

The corruption is happening on both the hex tops, and the rivers, but not in every case. There are no related warnings in the debug output, which surprised me. Soon I will try to pare down the draw calls to the basic problem and post what I am doing.

Ok here is the device creation code


            var form = new RenderForm("HexMap Demo");
            form.WindowState = FormWindowState.Maximized;

            // DirectX DXGI 1.1 factory
            SharpDX.DXGI.Factory1 factory1 = new SharpDX.DXGI.Factory1();

            // The 1st graphics adapter
            Adapter1 adapter1 = factory1.GetAdapter1(0);
            AdapterDescription1 adesc = adapter1.Description1;

            var description = new SwapChainDescription()
            {
                BufferCount = 1,
                Usage = Usage.RenderTargetOutput,
                OutputHandle = form.Handle,
                IsWindowed = true,
                ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags = SwapChainFlags.AllowModeSwitch,
                SwapEffect = SwapEffect.Discard
            };

            Device device;
            SwapChain swapChain;
            FeatureLevel[] featureLevels = new FeatureLevel[1];
            featureLevels[0] = FeatureLevel.Level_11_0;
            Device.CreateWithSwapChain(DriverType.Hardware,DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, description, out device, out swapChain);

            var deviceDebug = new DeviceDebug(device); // device must have been created with Debug flag
            // create a view of our render target, which is the backbuffer of the swap chain we just created
            RenderTargetView renderView;
            using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
                renderView = new RenderTargetView(device, resource);

            // setting a viewport is required if you want to actually see anything
            var context = device.ImmediateContext;
            var viewport = new ViewportF(0, 0, form.ClientSize.Width, form.ClientSize.Height);
            // Create Depth Buffer & View
            var depthBuffer = new Texture2D(device, new Texture2DDescription()
            {
                Format = Format.D32_Float_S8X24_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = form.ClientSize.Width,
                Height = form.ClientSize.Height,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.DepthStencil,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None
            });

            var depthView = new DepthStencilView(device, depthBuffer);

            context.OutputMerger.SetTargets(depthView, renderView);

            ViewportF[] viewports = new ViewportF[1];
            viewports[0] = viewport;
            context.Rasterizer.SetViewports(viewports);

Here is the render loop:


        public static void Render()
        {
            elapsedTime += GameTime.ElapsedTimeLastUpdate;
            if (maxFrameRate > 0 && ticksPerFrame > elapsedTime.Ticks)
                return;
            context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
            context.ClearRenderTargetView(renderTarget, ClearColor);

            foreach (DrawableGameComponent comp in drawableGameComponents)
            {
                if (comp.Enabled)
                    comp.Render();
            }

            swapChain.Present(0, PresentFlags.None);

            elapsedTime = TimeSpan.Zero;

        }

Here is the part where I draw things:


        void drawHexChunks()
        {

            //Take a snapshot of the camera properties so that they don't change in mid draw
            Vector2 cameraPos = new Vector2(camera.Position.X, camera.Position.Z);
            Matrix projectionView = camera.ProjectionView;
            Vector3 vecEye = camera.Position;

            DataBox box = Game.GraphicsDevice.ImmediateContext.MapSubresource(perFrameBuffer, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None);
            var cb = new ProjectionViewBuffer();
            cb.ProjectionView = projectionView;
            timeValue = (timeValue + (float)GameTime.ElapsedTimeLastUpdate.Ticks * 0.00001f) % 1.0f;
            cb.Time = timeValue;
            //cb.VecEye = new Vector4(vecEye, 1);
            //cb.LightDirection = new Vector4(0.0f, 0.6f, -0.9f, 1);
            //cb.LightColor = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);

            Utilities.Write(box.DataPointer, ref cb);
            Game.GraphicsDevice.ImmediateContext.UnmapSubresource(perFrameBuffer, 0);

            Game.GraphicsDevice.ImmediateContext.OutputMerger.SetBlendState(solidBlendState);

            Game.GraphicsDevice.ImmediateContext.OutputMerger.SetDepthStencilState(normalDepthState);
            Game.GraphicsDevice.ImmediateContext.Rasterizer.State = rasterStateSolid;


            Point mapCoords = HexTile.CalculateMapCoordsFromPosition(vecEye);
            Point cameraCoords = new Point(mapCoords.X / HexMapChunk.Size, mapCoords.Y / HexMapChunk.Size);


            var context = Game.GraphicsDevice.ImmediateContext;
            //context.InputAssembler.InputLayout = hexSidesInputLayout;
            //context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            //context.VertexShader.Set(hexSidesVS);
            //context.VertexShader.SetConstantBuffer(0, perFrameBuffer);
            //context.PixelShader.Set(hexSidesPS);
            //context.PixelShader.SetConstantBuffer(0, perFrameBuffer);
            ////context.PixelShader.SetShaderResource(0, riverTextureView);
            ////context.PixelShader.SetShaderResource(1, riverNormalView);
            ////context.PixelShader.SetSampler(0, samLinear);
            //foreach (HexMapChunk chunk in chunksInView) //chunksInView determined in update function
            //{
            //    chunk.DrawHexSides(hexSidesBuffer);
            //}

            context.InputAssembler.InputLayout = hexTopInputLayout;
            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.VertexShader.Set(hexTopVS);
            context.VertexShader.SetConstantBuffer(0, perFrameBuffer);
            context.PixelShader.Set(hexTopPS);
            context.PixelShader.SetConstantBuffer(0, perFrameBuffer);
            context.PixelShader.SetConstantBuffer(1, terrainColorsBuffer);
            context.PixelShader.SetShaderResource(0, basicTextureView);
            //context.PixelShader.SetShaderResource(1, gradientView);
            context.PixelShader.SetSampler(0, samLinear);

            foreach (HexMapChunk chunk in chunksInView) //chunksInView determined in update function
            {
                chunk.DrawHexTop(hexTopBuffer);
            }
            //Game.GraphicsDevice.ImmediateContext.Rasterizer.State = rasterStateFluid;
            //Game.GraphicsDevice.ImmediateContext.OutputMerger.SetBlendState(transparentBlendState);
            //Game.GraphicsDevice.ImmediateContext.OutputMerger.SetDepthStencilState(decalDepthState);
            //foreach (HexMapChunk chunk in chunksInView) //chunksInView determined in update function
            //{
            //    chunk.DrawHexFluid(hexTopBuffer);
            //}

            //context.InputAssembler.InputLayout = riverInputLayout;
            //context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            //context.VertexShader.Set(riverVS);
            //context.VertexShader.SetConstantBuffer(0, perFrameBuffer);
            //context.PixelShader.Set(riverPS);
            //context.PixelShader.SetConstantBuffer(0, perFrameBuffer);
            //context.PixelShader.SetShaderResource(0, gradientView);
            //context.PixelShader.SetSampler(0, samLinear);

            //foreach (HexMapChunk chunk in rivChunksInView) //chunksInView determined in update function
            //{
            //    chunk.DrawRivers(riverSideBuffer);
            //}

        }

Next is a screenshot of the result of only drawing the hex tops. I will post a png this time:

[attachment=22366:HexProb.png]

EDIT:

I forgot to post the shader for the hexTops. It's pretty basic, only that there is a constant buffer that holds the color info for each terrain type for color cycling etc. it is indexed with the texture array index. I used to draw rivers directly in this shader, but I didn't like the results:


#define MAX_TERRAINS 50

cbuffer PerFrameBuffer : register(b0)
{
	//float4x4 World; This is just the identity matrix, so not needed
	float4x4 ViewProjection;
	float time;
};

//Texture2DArray hexTextures : register(t0);
Texture2DArray hexTexture : register(t0);
//Texture1D gradTex : register(t1);
SamplerState samLinear;

struct TerrainColor
{
	float4 forecolor1;
	float4 forecolor2;
	float4 backcolor1;
	float4 backcolor2;
	float forecycle;
	float backcycle;
};

cbuffer TerrainColorBuffer : register(b1)
{
	TerrainColor TerrainColors[MAX_TERRAINS];
};

struct VertexInput
{
	//imediate data
	float4 PositionLocal		: POSITION0;
	float4 Bary					: POSITION1; //w component contains edge code for how to handle edge drawing
	//int sideIndex				: BLENDINDICES0;
	//instance data
	float4 Translation			: POSITION2; //w component contains texture index!
//	float4 RiverData[6]			: TEXCOORD0;
};

struct VertexOutput
{
	float4 PositionH	: SV_POSITION;
	float4 Bary			: POSITION1;
	float3 TexCoord		: TEXCOORD0;
	//float RivTexCoord	: TEXCOORD1;
	//float RivSize		: TEXCOORD2;
	//float RivClockwise	: TEXCOORD3;
	//float RivColor		: TEXCOORD4;
};

VertexOutput VS(VertexInput vin)
{
	VertexOutput vout;

	//float3 positionWorld = vin.PositionLocal + Translation.xyz;
	float3 positionWorld = vin.PositionLocal.xyz;
	positionWorld.x += vin.Translation.x;
	positionWorld.y += vin.Translation.y;
	positionWorld.z += vin.Translation.z;
	vout.PositionH = mul(float4(positionWorld,1.0f),ViewProjection);

	vout.TexCoord.x = vin.PositionLocal.x * 0.5f + 0.5f;
	vout.TexCoord.y = vin.PositionLocal.z * 0.5f + 0.55f;
	vout.TexCoord.z = vin.Translation.w;

	vout.Bary = vin.Bary;

	//vout.RivTexCoord = max(vin.Bary.z,vin.Bary.x);

	//vout.RivSize = vin.RiverData[vin.sideIndex].x;
	//vout.RivClockwise = vin.RiverData[vin.sideIndex].y;
	return vout;
}

float4 PS(VertexOutput input ) : SV_Target
{

	static const float PI = 3.14159265f;
	float4 samplecolor = hexTexture.Sample(samLinear,input.TexCoord);
	int terrainIndex = (int)input.TexCoord.z;
	float sample = samplecolor.r;
	//float sample = 1.0f;
	float inversesample = 1.0f - sample;
	float foredrift = sin((time * PI * TerrainColors[terrainIndex].forecycle * 2.0f)) * 0.5f + 0.5f;
//	float inverseforedrift = 1.0f - foredrift;
	float backdrift = sin((time * PI * TerrainColors[terrainIndex].backcycle * 2.0f)) * 0.5f + 0.5f;
//	float inversebackdrift = 1.0f - backdrift;

	float4 back = lerp(TerrainColors[terrainIndex].backcolor1,TerrainColors[terrainIndex].backcolor2,backdrift);
	float4 fore = lerp(TerrainColors[terrainIndex].forecolor1,TerrainColors[terrainIndex].forecolor2,foredrift);

	float4 color = back * sample + fore * inversesample;

	float shade = 1.0f;
//	float4 rivColor = float4(0.6f,0.6f,1.0f,0.0f);

	if (input.Bary.w > 1.0) //color the tips of the triangle
	{
		shade = min(min(saturate((1.0f - input.Bary.x) * 10.0f),saturate((1.0f - input.Bary.y) * 10.0f)),saturate((1.0f - input.Bary.z) * 10.0f));
	}
	else if(input.Bary.w > 0.0) //color two edges of the triangle
	{
		shade = min(saturate(input.Bary.x * 10.0f),saturate(input.Bary.z * 10.0f));

		////do possible river depiction
		//float rivTexCoord;
		//if(input.Bary.x < input.RivSize)
		//{
		//	if(input.RivClockwise == 1.0f)
		//		rivTexCoord = 1.0 - input.RivTexCoord; //clockwise
		//	else
		//		rivTexCoord = input.RivTexCoord; //counter-clockwise

		//	shade = gradTex.Sample(samLinear,rivTexCoord + time);
		//	rivColor = rivColor * shade;
		//	rivColor.a = 1.0f;
		//}

	}
	float4 FinalColor;
	float4 grey = float4(shade, shade, shade, 1.0f);
	FinalColor = color * grey;
	return FinalColor;
}

Looking at your shader, the thing that jumps out the most is you're choosing a terrain color based on an interpolated value (input.TexCoord.z). Do your screenshots look like the "fighting" is between two different sets of terrain colors? What happens if you change your shader to always sample from terrainIndex 0, for instance? If that fixes it, maybe you need to round() your input.TexCoord.z, or use terrain indices that are a little bit a above whole numbers (I thinking maybe, for instance, maybe when the rasterizer interpolates between 4.0 and 4.0 it occasionally produces 3.9999999?).

This topic is closed to new replies.

Advertisement