Jump to content

  • Log In with Google      Sign In   
  • Create Account

Kjell Andersson

Member Since 28 Nov 1999
Offline Last Active Apr 24 2016 11:19 AM

Topics I've Started

IOTD broken

24 February 2016 - 01:59 PM

The right side image is always broken and never loads (404).

Best way of finding resource leaks

07 February 2016 - 05:57 AM

What is the best way of finding D3D9 resource leaks on a Windows 10 machine nowadays?

I must add that the leak is not easily reproducible also.


I find it more and more difficult to work with legacy D3D9 applications since the debug version no longer works on a modern Windows. Are there any new tools available that can help me, like nSight or GPU PerfStudio or such?

Blinn-Phong artifact in shader

09 October 2015 - 10:49 AM

I'm implementing Blinn-Phong lighting in a pixel shader and is getting an artifact that I don't like. I'm tossing out a screenshot here to ask if this is normal for Blinn-Phong or if I have gotten something wrong with my calculations.

Attached File  BlinnPhongArtifact.jpg   47.89KB   1 downloads


The light source is coming from behind the sphere and is directed towards the viewer. It is supposed to be a directional light.

On the screenshot to the left the specular light is barely visible on the rim, which is perfectly fine. The problem occurs when moving a bit further behind the sphere. The specular light then becomes a cone and is detached from the rim in an unnatural way. Moving further behind the sphere makes it fade away but it keeps moving inwards the sphere before it disappears.


Lets toss in the code here as well if you want to inspect it (beware, experimental code that is not production-worthy).

struct VertDataIn {
	float4 Position   : POSITION;
	float3 Normal     : NORMAL;
	float4 Color      : COLOR;
	float2 Texture    : TEXCOORD0;

struct VertDataOut {
	float4 Position      : POSITION;
	float4 Color         : COLOR;
	float2 Texture       : TEXCOORD0;
	float3 Normal        : TEXCOORD1;
	float3 WorldPosition : TEXCOORD2;

VertDataOut VS_Main(VertDataIn In)
	VertDataOut Out;
	Out.Texture = In.Texture.xy;
	In.Position.w = 1;
	Out.Position = mul(In.Position, g_worldViewProjectionTransform);
	Out.Color = In.Color * g_diffuseColor;
	Out.Normal = mul(In.Normal, g_worldTransform);
	Out.WorldPosition = mul(In.Position, g_worldTransform);
	return Out;

ColorPair CalculateDirectionalLightBlinnPhong(float3 N, float3 V, int i) 
	ColorPair Out; 
	float3 L = -normalize(g_directionalLights[i].dir); 
	float NdotL = dot(N, L); 
	Out.ColorDiffuse = g_directionalLights[i].ambient; 
	Out.ColorSpecular = 0; 
	if (NdotL > 0.0f) { 
		Out.ColorDiffuse += NdotL * g_directionalLights[i].diffuse; 
	float3 H = normalize(L + V);
	Out.ColorSpecular = pow(saturate(dot(H, N)), g_materialPower) * g_directionalLights[i].specular; 
	return Out; 

float4 PS_Main(VertDataOut Input) : COLOR
	float4 col = Input.Color;
	if (g_hasDiffuseTexture) {
		col *= tex2D(Sampler, Input.Texture);

	// Directional lights 
	float4 diffuseColor = float4(0, 0, 0, 0);
	float4 specularColor = float4(0, 0, 0, 0);
	for(int i = 0; i < g_numDirectionalLights; i++) { 
		ColorPair colorOut = CalculateDirectionalLightBlinnPhong(normalize(Input.Normal), normalize(g_cameraPosition - Input.WorldPosition), i); 
		diffuseColor += colorOut.ColorDiffuse; 
		specularColor += colorOut.ColorSpecular; 

	col.rgb *= (diffuseColor.rgb + specularColor.rgb);

	return col;

Shader variable register mapping in DirectX9

03 October 2015 - 07:56 AM

I'm in the process of abstracting away the D3D9 usage in an application in order to be able to slowly migrate to a newer API in the future.

The first step in this is to create a DX9 rendering back-end that supports the current functionality but without using the legacy D3DX interfaces.


My problem right now comes to the introspection of the shader code in order to map data into constant buffers, i.e. shader variables and texture input.

I'm trying to use the D3DCompile(), CreateVertexShader(), SetVertexShaderConstant() interfaces instead of the D3DX and effect framework and thus need to be able to map variable names to register numbers that can be passed to the SetVertexShaderConstant() function.


If you don't specify any registers manually in your shader, the compiler automatically assigns them, but how do you get that information out from the compiled shader?


Is there a way to do this mapping table automatically instead of manually specifying register usage in the shader and then creating a mapping table outside of the shader?

FBX import from memory

11 September 2015 - 06:19 PM

I'm trying to import an FBX file that is stored in memory with the Autodesk FBX SDK but with no luck.


I have subclassed the FbxStream class to create a memory reader and is passing it into the importer's Initialize method, but I only get a fail result with the error message "Unexpected file type".


It seems to be reading a lot before it fails since I get may calls to my overloaded Read method so that seems to be working.


Anyone here that have successfully loaded FBX files from a memory stream using the SDK?