Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 18 Sep 2012
Offline Last Active Jul 22 2013 12:25 PM

Topics I've Started

Why is this 128Bit Color Format being converted to 32Bit (HLSL/SLIMDX-9)

11 October 2012 - 07:50 AM

Note: Sorry I know I have been posting tons of newbie questions on here lately but I really don't understand this problem.

I am trying to write an HLSL pixel shader for a project I am working on. Basically want I want to do is if a texture has a pixel with a float value of 0.52 (on scale of 0-255 is 132.6) I want to output 133 60% of the time and output 132 40% of the time. Write now I am just trying to output the fractional remainder of the RGB value (i.e. the chance to bump the pixel up) however I always get a value of zero) I think this is because the colors are getting quantized to a 0-255 scale before they reach the shader but I don't know why this would be because I am useing the A32B32G32R32f format which should be able to store plenty of information about the colors. Here is my very simple shader code. I am using SlimDX (DX9) if that matters.

[source lang="cpp"]sampler2D Tex0 : register(s0);float4 DitherByChance(float2 coords : TEXCOORD0) : COLOR{ float4 newColor = float4(0, 0, 0, 0); // The pixel color to return double4 oldColor = tex2D(Tex0, coords); double scale = 0.0039215686274509803921568627451; // 1 / 255 double rPercentChance = frac(oldColor.r / scale); //Chance to round red channel up double gPercentChance = frac(oldColor.g / scale); //Chance to round green channel up double bPercentChance = frac(oldColor.b / scale); //Chance to round blue channel up newColor.r = rPercentChance; newColor.g = gPercentChance; newColor.b = bPercentChance; newColor.a = 1; return newColor;}technique DitherViaChance { pass Pass1 { PixelShader = compile ps_2_0 DitherByChance(); } }[/source]

Here is the relevant VB code
[source lang="vb"] Public Overrides Sub RenderScene() 'ditherBC.Technique = ditherBC.GetTechnique("DitherViaChance") Me.device.BeginScene() spriteRender.Begin(SpriteFlags.None) ditherBC.Begin() ditherBC.BeginPass(0) 'NOTE tex IS CREATED WITH createCharacterTexture()' spriteRender.Draw(tex, New Rectangle(0, 0, 50, 50), New Color4(1, 1, 1)) spriteRender.End() ditherBC.EndPass() ditherBC.End() Me.device.EndScene() End Sub Public Function createCharacterTexture() As Texture Dim RtsHelper As RenderToSurface = New RenderToSurface(device, 100, 100, Format.A32B32G32R32F) Dim texture As Texture = New Texture(device, 100, 100, 1, Usage.RenderTarget, Format.A32B32G32R32F, Pool.Default) Dim sloanFont As Font = New Font(device, 98, 98, FontWeight.Normal, 1, False, CharacterSet.Default, Precision.Default, FontQuality.ClearTypeNatural, PitchAndFamily.Default, "Sloan") RtsHelper.BeginScene(texture.GetSurfaceLevel(0), New Viewport(0, 0, 100, 100)) sloanFont.DrawString(Nothing, "A", 1, 1, New SlimDX.Color4()) RtsHelper.EndScene(Filter.None) Font.Dispose() RtsHelper.Dispose() Return texture End Function[/source]

How to populate a DX texture with random RGB values.

09 October 2012 - 09:24 AM

I am trying get 3 new random floats into my pixel shader for each pixel. Based on what I have read here, here, and also here, I believe that I need to generate a large texture containing random RGB values and then during each draw call randomly generate a couple of texture coordinate offset values to produce a pseudo-random effect. Is the only way to do this through the LockRect and UnlockRect API? I hope not. Also when do the floating point color values get quantized. For example if I do a ColorFill on a texture and set it too a RGB of ( 0.52, 0.52, 0.52 )
which on a 0-255 scale is ( 132.6, 132.6, 132. 6 ) when I access that texture in my pixel shader will I get a value of 0.52 or 0.52156....i.e. (133).

Can a HLSL guru please help me with this very simple pixel shader!

04 October 2012 - 04:13 PM

I am writing I SlimDX application to test color contrast sensitivity thresholds of the human visual system. The subject is presented with a large letter that is at low contrast with with the background and asked to identify the letter. However I need the ability to display more colors then is available with a simple 8bit per channel color depth. (i.e. rgb values 0-255). The way I plan to due this is through a simple "dithering" algorithm implemented as an HLSL pixel shader. Basically if request slimDX to render the text to a surface with a color of Color4( 0.55f, 0.55f, 0.55f ) which corresponds to a rgb value of ( 140.25 , 140.25 , 140.25 ) I want each color channel of each pixel to have a 25% chance of being set to 141 and a 75% chance of getting set to 140. This should (in the limit of many pixels) lead to a letter that appears as a shade of gray one 1/4 of the way in-between 140 and 141.
I am however getting errors in my prototype shader code when trying to compile with fxc. I am getting an Illegal character code when I compile and I don't know why. Also if you are a HLSL guru please look over my code and make any comments that strike you. Note I got the HLSL random function from an answer to this question.

Below is my shader code. Please forgive me if there a many mistakes as its my first HLSL code:

float4 DitherByChance(float2 coords : TEXCOORD0) : COLOR
  float4 newColor; // The pixel color to return
  float4 oldColor = tex2D(s0, coords);

  // Note I know that as the code stands now rCutOff = gCutOff = bCutOff I will sort a fix for this out later
  float rCutOff = random(coords);  // A random float that determines if the red channel will be rounded up or down
  float gCutOff = random(coords);  // A random float that determines if the green channel will be rounded up or down
  float bCutOff = random(coords);	 // A random float that determines if the blue channel will be rounded up or down
  float rPercentChance = frac(oldColor.r * 255); //Chance to round red channel up
  float gPercentChance = frac(oldColor.g * 255); //Chance to round green channel up
  float bPercentChance = frac(oldColor.b * 255); //Chance to round blue channel up

  //In the code below (1/255) is the floating point represntaion of an incress of one on the 0-255 RGB scale

  if (rCutOff <= rPercentChance) newColor.r = oldColor.r + ((1 - rPercentChance) / 255); //Bump up one r value
  else newColor.r = oldColor.r - rPercentChance * (1 / 255);	 //Bump down to ensure r is not rounded up

  if (gCutOff <= gPercentChance) newColor.g = oldColor.g + ((1 - gPercentChance) / 255); //Bump up one g value
  else newColor.b = oldColor.b - bPercentChance * (1 / 255);	 //Bump down to ensure g is not rounded up

  if (bCutOff <= bPercentChance) newColor.b = oldColor.b + ((1 - bPercentChance) / 255); //Bump up one b value
  else newColor.b = oldColor.b - bPercentChance * (1 / 255);	 //Bump down to ensure b is not rounded up

  return newColor;
// Input: It uses texture coords as the random number seed.
// Output: Random number: [0,1), that is between 0.0 and 0.999999... inclusive.
// Author: Michael Pohoreski
// Copyright: Copyleft 2012 :-)
float random( vec2 p )
   // We need irrationals for pseudo randomness.
   // Most (all?) known transcendental numbers will (generally) work.
   const vec2 r = vec2(
  23.1406926327792690,  // e^pi (Gelfond's constant)
   2.6651441426902251); // 2^sqrt(2) (Gelfond–Schneider constant)
   return fract( cos( mod( 123456789., 1e-7 + 256. * dot(p,r) ) ) );

How to dither to a 24/32 bit surface in DirectX/SlimDX

02 October 2012 - 04:18 PM

I Basically what I am trying to do is be able to display a red (or green or blue) letter with more then 256 possible shades. For example normally if I have a blue letter 'A' with an rgb value of (0 , 0 , 100) the next blue level up would be an rgb (0, 0 , 101). I want to be to show an 'A' that "APPEARS" to have an rgb value of (0, 0, 100.25). Basically I want to do this by making 3/4th of the pixels in the "A" have (0,0,100) and 1/4th have a value of (0,0,101). I noticed that there is a dither filter in DX but from reading about it I don't think it is quite what I want but I am not sure. The reason I don't think I can use the built in dithering is because from what I have read here: http://www.toymaker.info/Games/html/render_states.html#blends the built in dithering is legacy code that only works when rendering to a 16bit surface. I need a way to dither from a pixel format that is > 8bits per channel down to 8bits per channel. Does any one know of a way I could implement this dithering. I don't know anything about writing pixel shaders but do you think that might be the right approach? If I could figure out a way to determine which pixels made up the letter I would could try a simple dithering technique where I just go through each pixel and based on a 25% probability bump the color up one but this would be less then ideal. Does anyone have any experience with dithering in DirectX and know of any techniques that would make this possible.

NOTE: I am using SlimDX (DX9) but could switch to DX10/11 if needed

Is it possible to disable colorKey when loading an Image?

27 September 2012 - 02:21 PM

I am trying to get the MDX9 Surface.FromBitmap() functionality with SlimDX. I think I need to use the SlimDX.DX9.Surface.FromFile() method but all of the overloads of that method take a colorKey argument. What do I do if I want to render all of the colors?