Sign in to follow this  

DX11 DX11 - Swap Chain - Slow Engine

Recommended Posts

Migi0027    4628

Hi guys,


PS. FPS I GOT IN MY ENGINE: 50 - 70 fps, res: 1080x1920, system can run Crysis 3 at high settings, so that's not the problem.





right now I'm facing another issue, which is the performance of my Engine. Now the problem is not my architecture, well at least I don't think so, so I tried running a Profiler.


Most of the performance was taken off in the initialization of course, but then later on in the render loop then rendering of the depth, normal, diffuse maps are quite fast, relatively to other things. Now the real problem comes when the swapchain->present(0,0) is called. As i understand it takes a long time because it's waiting for the previous frame to finish, right?


I have head some rumors about that if statements in a shader are quite weird, because even though it is a negative statement, false, it will still semi-run/check all the contents inside the if statement, which makes it quite slow, is this true or just some rubbish?


Also my FPS is NOT affected if I change my regular shader for all the objects (tried to remove all so only the positions were calculated in world space and then returned a white color), but then if I changed my Post Processing shader to a very simple version which ONLY returns the diffuse map, my FPS is boosted 2x - 3x times! But why? (Shader is below if you're interested, but there are still errors).


Texture2D t_dffmap : register(t0);
Texture2D t_depthmap : register(t1);
Texture2D t_normalmap : register(t2);
Texture2D t_random : register(t3);
Texture2D t_blmextract : register(t4);
Texture2D t_megaparticles : register(t5);
Texture2D t_fractalnoise : register(t6);
Texture2D t_softp : register(t7);
Texture2D t_softp_depth : register(t8);
Texture2D t_shadowmap : register(t9);
Texture2D t_glowmap : register(t10);
SamplerState ss;

cbuffer PARAMSBUFFER : register(b0)
	float time;
	float blur;
	float bloomExtract;
	float bloom;
	float pixelDisortion;
	float pixelDisorterAmount;
	float ssao;
	float bluramount;
	float megaparticles;
	float radialblur;
	float radial_start;
	float radial_width;
	float2 radial_center;
	float fractalNoise;
	float glowmap;

	matrix view;

cbuffer BloomBuffer : register(b1)
	float BloomThreshold;
	float BloomSaturation;
	float BaseSaturation;
	float BloomIntensity;
	float BaseIntensity;

cbuffer SSAOBuffer : register(b2)
	float g_scale;
	float g_bias;
	float g_sample_rad;
	float g_intensity;
	float ssaoIterations;
	float3 pppspace;

cbuffer GODRAYBuffer : register(b3)
	float3 LightPosition;
	float pad;
	matrix WorldViewProjection;
	float GOD_Density;
	float GOD_Weight;
	float GOD_Decay;
	float GOD_Exposure;
	float GodRays;
	float2 ppad;

struct VS_Output
    float4 Pos : SV_POSITION;              
    float2 Tex : TEXCOORD0;
	float2 LightPos : TEXCOORD1;	
VS_Output VShader(uint id : SV_VertexID)
    VS_Output Output;
    Output.Tex = float2((id << 1) & 2, id & 2);
    Output.Pos = float4(Output.Tex * float2(2,-2) + float2(-1,1), 0, 1);
    return Output;

// Helper for modifying the saturation of a color.
float4 AdjustSaturation(float4 color, float saturation)
    // The constants 0.3, 0.59, and 0.11 are chosen because the
    // human eye is more sensitive to green light, and less to blue.
    float grey = dot(color, float3(0.3, 0.59, 0.11));

    return lerp(grey, color, saturation);

// Ambient Occlusion Stuff --------------------------------------------------

float3 getPosition(in float2 uv)
	return mul( t_depthmap.Sample(ss, uv).xyz, view);

float3 getNormal(in float2 uv)
	return normalize(t_normalmap.Sample(ss, uv).xyz * 2.0f - 1.0f);

float2 getRandom(in float2 uv)
	return normalize( mul(t_random.Sample(ss, float2(800, 600) * uv / float2(64, 64)).xy * 2.0f - 1.0f, view) );

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)*g_scale;
	return max(0.0,dot(cnorm,v)-g_bias)*(1.0/(1.0+d))*g_intensity;

// End

float4 PShader(VS_Output input) : SV_TARGET
	if (bloomExtract == 1)
		// Look up the original image color.
		float4 c = t_dffmap.Sample(ss, input.Tex);

		// Adjust it to keep only values brighter than the specified threshold.
		return saturate((c - BloomThreshold) / (1 - BloomThreshold));

	float4 color = float4(1.0f, 1.0f, 1.0f, 1.0f);
	if (pixelDisortion == 1)
		// Distortion factor
		float NoiseX = pixelDisorterAmount * (time/1000) * sin(input.Tex.x * input.Tex.y+time/1000);
		NoiseX=fmod(NoiseX,8) * fmod(NoiseX,4); 

		// Use our distortion factor to compute how much it will affect each
		// texture coordinate
		float DistortX = fmod(NoiseX,5);
		float DistortY = fmod(NoiseX,5+0.002);
		// Create our new texture coordinate based on our distortion factor
		input.Tex = float2(DistortX,DistortY);

	if (fractalNoise == 1)
		float offset = saturate((t_fractalnoise.Sample(ss, input.Tex) / 10.0f));

		input.Tex += 1 * (t_fractalnoise.Sample(ss, input.Tex).xy - 0.5)/15;

	float4 dffMAP = t_dffmap.Sample(ss, input.Tex);

	if (megaparticles == 1)
		dffMAP.a = 0.0f;

		dffMAP += t_megaparticles.Sample(ss, input.Tex);

	color = dffMAP;

	if(bloom == 1)
		// Look up the bloom and original base image colors.
		float4 cbloom = t_blmextract.Sample(ss, input.Tex);
		float4 base = color;
		// Adjust color saturation and intensity.
		cbloom = AdjustSaturation(cbloom, BloomSaturation) * BloomIntensity;
		base = AdjustSaturation(base, BaseSaturation) * BaseIntensity;
		// Darken down the base image in areas where there is a lot of bloom,
		// to prevent things looking excessively burned-out.
		base *= (1 - saturate(cbloom));
		// Combine the two images.
		color = base + cbloom;

	if (blur == 1)
		float hblur[17] = {0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, -13, 14, -15, 16};

		int i = 0;
		for(;i < bluramount;)
			color += t_dffmap.Sample(ss, input.Tex + float2(0.002f * hblur[i+1],0.002f * hblur[i]));


		color = color / i;
	else if (radialblur == 1)
		const int nsamples = 16;

		input.Tex -= 0.5;
		for(int i=0; i<nsamples; i++) 
    		float scale = radial_start + radial_width*(i/(float) (nsamples-1));
    		color += t_dffmap.Sample(ss, input.Tex.xy*scale + radial_center );
   		color /= nsamples;
	if (ssao == 1)
		// Apply SSAO

		const float2 vec[4] = {float2(1,0),float2(-1,0),

		float3 p = getPosition(input.Tex);
		float3 n = getNormal(input.Tex);
		float2 rand = getRandom(input.Tex);

		float ao = 0.0f;
		float rad = g_sample_rad/p.z; // g_s_r

		//**SSAO Calculation**//
		int iterations = ssaoIterations;
		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.Tex,coord1*0.25, p, n);
		  ao += doAmbientOcclusion(input.Tex,coord2*0.5, p, n);
		  ao += doAmbientOcclusion(input.Tex,coord1*0.75, p, n);
		  ao += doAmbientOcclusion(input.Tex,coord2, p, n);
		color.rgb *= ao;

	// Soft Particles
	float pDepth = t_softp_depth.Sample(ss, input.Tex);
	float wDepth = t_depthmap.Sample(ss, input.Tex);
	float twD = t_depthmap.Sample(ss, input.Tex);

	if ((wDepth < pDepth) || twD == 0)
		float4 pColor = t_softp.Sample(ss, input.Tex);

		if (twD != 0)
			pColor.a = saturate((wDepth - pDepth) * 1);

		color += pColor;
	// End

	if (glowmap == 1)
		color += t_glowmap.Sample(ss, input.Tex);

	if (GodRays == 1)
		float2 deltaTexCoord = (input.Tex - input.LightPos.xy);  
		deltaTexCoord *= GOD_Density / GOD_NUM_SAMPLES;  
		//float4 color = t_dffmap.Sample(ss, input.Tex);
		float illuminationDecay = 1.0f;  
		for (int i = 0; i < GOD_NUM_SAMPLES; i++)  
			input.Tex -= deltaTexCoord;  
			float4 sample = t_dffmap.Sample(ss, input.Tex);
			sample *= illuminationDecay * GOD_Weight;  
			color += sample;  
			illuminationDecay *= GOD_Decay;  

		return color * GOD_Exposure;  

	return color;



Now I understand that in GameDev there are many experienced programmers in graphics, so now I ask them/you, what can speed up an engine, what should I avoid? Tricks?


And that's basically it, thanks for taking interest! happy.png

Share this post

Link to post
Share on other sites
VladR    722

1. You are most obviously fillrate-bound. Post the FPS in lower resolutions (e.g. 640x480).


2. You took a shorter / simpler route and created an uber-shader. That's great during debugging/development, but as you noticed, it has a negative impact.


3. It is very easy to check the impact of conditions. Just create a separate technique/shader pair that will have only single codepath (say - just bloom), without any conditions whatsoever.


4. Make sure VSync is off.  With 52 , it doesn't look like VSync is On, but better safe than sorry...


Of course, as always, there will be 10 other things that impact FPS (many of them on the CPU side - AI, pathfinding, ...) , but let's first address those that have biggest impact.

Share this post

Link to post
Share on other sites
Adam_42    3629

You can put them in the same hlsl file, as long as you're willing to compile it more than once.


You simply swap tests like "if (blur == 1) { ... }" for "#if defined(BLUR) ... #endif" and compile once with "/D BLUR" on the command line, once with "/D SSAO", etc. You can also set the defines in code if you're compiling shaders at runtime.


You then pick the correct shader to use, instead of setting a constant.

Share this post

Link to post
Share on other sites
mightypigeon    576

SSAO and god rays can be very pixel heavy effects and I'm guessing you're doing them at full resolution (1920x1080).  Even full screen blurs can put a fair amount of pressure on fill rate at high resolutions.  When you take into account that more than likely every one of your branches is being evaluated even if the conditions are false, this could be adding up to make a very expensive shader.


A lot of these effects are rendered to smaller render targets, such as something like 1/4 size of the backbuffer (experiment with the size to get a good image quality vs. performance trade off).  And as mentioned above, even though it's 2013 we still really need to be using the preprocessor for our branches rather than if statements.  My recommendation would be render SSAO to a small target by itself, then god rays to another small target by themselves, then have your big post process shader at the end composite those effects along with blurs and distortion etc. using #defines to turn effects on and off as needed.

Share this post

Link to post
Share on other sites
Adam_42    3629

If statements can be cheap in hlsl, if there's no branch involved and the code you execute is simple. For example this if statement is a cheap one:


if (x > 7) x = 7;


They are also free if the condition can be evaluated at compile time.


They get expensive when the extra code that gets executed is significant, because the compiler will generally execute the code anyway and multiply the result by either 0 or 1 depending on the result of the if.


You can also ask the compiler to [branch] instead of evaluating the whole thing and throw away the result. The expense of that depends on things like what pattern of pixels goes down each path, but it can be beneficial if you avoid executing the extra code a lot of the time. While the compiler will sometimes automatically decide to do a real branch, you're best off specifying it yourself as you get extra errors back if if it can't do a branch due to texturing issues (i.e. tex2D vs tex2Dlod).


Your best option when optimizing is to use a tool like GPU Shader Analyzer to see what instructions get generated, as well as profiling the performance yourself, because if statement performance depends on the input data.

Share this post

Link to post
Share on other sites
Migi0027    4628

Ok, now I've improved the frame rate a bit, so basically what I do is that I have an individual material for each mesh, (a shader) which can be modified by the user on creation. So this also helped me to escape the fixed shadings. Now the only problem is that I need to write a class that can parse any kind of shader with it's needs, because some shaders needs a specific input and some don't, and the class needs to detect that.


And a funny note, whilst doing this I lost some shader data, basically my whole post processing shader, because I closed Visual Studio without undoing, but then I realized that I had a copy here in this forum ohmy.png

Share this post

Link to post
Share on other sites
VladR    722

Did you, or did you not, yet, create a separate shader with just one effect (Bloom) as I proposed above, to check the actual difference in performance ?


Easiest way would be to just add  a different shader technique to a same HLSL file. If you are not familiar with techniques, and are not willing to learn them - then just create a separate shader HLSL file with the new shorter pixel shader.


Then post the FPS here.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By isu diss
      HRESULT FBXLoader::Open(HWND hWnd, char* Filename) { HRESULT hr = S_OK; if (FBXM) { FBXIOS = FbxIOSettings::Create(FBXM, IOSROOT); FBXM->SetIOSettings(FBXIOS); FBXI = FbxImporter::Create(FBXM, ""); if (!(FBXI->Initialize(Filename, -1, FBXIOS))) MessageBox(hWnd, (wchar_t*)FBXI->GetStatus().GetErrorString(), TEXT("ALM"), MB_OK); FBXS = FbxScene::Create(FBXM, "MCS"); if (!FBXS) MessageBox(hWnd, TEXT("Failed to create the scene"), TEXT("ALM"), MB_OK); if (!(FBXI->Import(FBXS))) MessageBox(hWnd, TEXT("Failed to import fbx file content into the scene"), TEXT("ALM"), MB_OK); if (FBXI) FBXI->Destroy(); FbxNode* MainNode = FBXS->GetRootNode(); int NumKids = MainNode->GetChildCount(); FbxNode* ChildNode = NULL; for (int i=0; i<NumKids; i++) { ChildNode = MainNode->GetChild(i); FbxNodeAttribute* NodeAttribute = ChildNode->GetNodeAttribute(); if (NodeAttribute->GetAttributeType() == FbxNodeAttribute::eMesh) { FbxMesh* Mesh = ChildNode->GetMesh(); NumVertices = Mesh->GetControlPointsCount();//number of vertices MyV = new FBXVTX[NumVertices]; for (DWORD j = 0; j < NumVertices; j++) { FbxVector4 Vertex = Mesh->GetControlPointAt(j);//Gets the control point at the specified index. MyV[j].Position = XMFLOAT3((float)Vertex.mData[0], (float)Vertex.mData[1], (float)Vertex.mData[2]); } NumIndices = Mesh->GetPolygonVertexCount();//number of indices; for cube 20 MyI = new DWORD[NumIndices]; MyI = (DWORD*)Mesh->GetPolygonVertices();//index array NumFaces = Mesh->GetPolygonCount(); MyF = new FBXFACEX[NumFaces]; for (int l=0;l<NumFaces;l++) { MyF[l].Vertices[0] = MyI[4*l]; MyF[l].Vertices[1] = MyI[4*l+1]; MyF[l].Vertices[2] = MyI[4*l+2]; MyF[l].Vertices[3] = MyI[4*l+3]; } UV = new XMFLOAT2[NumIndices]; for (int i = 0; i < Mesh->GetPolygonCount(); i++)//polygon(=mostly rectangle) count { FbxLayerElementArrayTemplate<FbxVector2>* uvVertices = NULL; Mesh->GetTextureUV(&uvVertices); for (int j = 0; j < Mesh->GetPolygonSize(i); j++)//retrieves number of vertices in a polygon { FbxVector2 uv = uvVertices->GetAt(Mesh->GetTextureUVIndex(i, j)); UV[4*i+j] = XMFLOAT2((float)uv.mData[0], (float)uv.mData[1]); } } } } } else MessageBox(hWnd, TEXT("Failed to create the FBX Manager"), TEXT("ALM"), MB_OK); return hr; } I've been trying to load fbx files(cube.fbx) into my programme. but I get this. Can someone pls help me?

    • By lonewolff
      Hi Guys,
      I am having a bit of a problem with a dynamic texture.
      It is creating without error and I am attempting to initialize the first pixel to white to make sure I am mapping correctly. But when I draw the texture to the quad it displays the whole quad white (instead of just one pixel).
      This is how I am creating, mapping, and setting the first pixel to white. But as mentioned, when I draw the quad, the entire quad is white.
      // Create dynamic texture D3D11_TEXTURE2D_DESC textureDesc = { 0 }; textureDesc.Width = 2048; textureDesc.Height = 2048; textureDesc.MipLevels = 1; textureDesc.ArraySize = 1; textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; textureDesc.SampleDesc.Count = 1; textureDesc.Usage = D3D11_USAGE_DYNAMIC; textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; textureDesc.MiscFlags = 0; HRESULT result = d3dDevice->CreateTexture2D(&textureDesc, NULL, &textureDynamic); if (FAILED(result)) return -1; result = d3dDevice->CreateShaderResourceView(textureDynamic, 0, &textureRV); if (FAILED(result)) return -2; D3D11_MAPPED_SUBRESOURCE resource; if (FAILED(d3dContext->Map(textureDynamic, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource))) return -1; memset(resource.pData, 255, 4); d3dContext->Unmap(textureDynamic, 0);  
      Hopefully I have just made an oversight somewhere.
      Any assistance would be greatly appreciated
      (If I change the 255 value to 128 the quad then turns grey, so the mapping is definitely doing something. Just can't work out why it is colouring the whole quad and not the first pixel)
    • By KaiserJohan
      Just a really quick question - is there any overhead to using DrawIndexedInstanced even for geometry you just render once vs using DrawIndexed? Or is the details obfuscated by the graphics driver?
      I would assume no but you never know  
    • By isu diss
       I'm trying to code Rayleigh part of Nishita's model (Display Method of the Sky Color Taking into Account Multiple Scattering). I get black screen no colors. Can anyone find the issue for me?
      #define InnerRadius 6320000 #define OutterRadius 6420000 #define PI 3.141592653 #define Isteps 20 #define Ksteps 10 static float3 RayleighCoeffs = float3(6.55e-6, 1.73e-5, 2.30e-5); RWTexture2D<float4> SkyColors : register (u0); cbuffer CSCONSTANTBUF : register( b0 ) { float fHeight; float3 vSunDir; } float Density(float Height) { return exp(-Height/8340); } float RaySphereIntersection(float3 RayOrigin, float3 RayDirection, float3 SphereOrigin, float Radius) { float t1, t0; float3 L = SphereOrigin - RayOrigin; float tCA = dot(L, RayDirection); if (tCA < 0) return -1; float lenL = length(L); float D2 = (lenL*lenL) - (tCA*tCA); float Radius2 = (Radius*Radius); if (D2<=Radius2) { float tHC = sqrt(Radius2 - D2); t0 = tCA-tHC; t1 = tCA+tHC; } else return -1; return t1; } float RayleighPhaseFunction(float cosTheta) { return ((3/(16*PI))*(1+cosTheta*cosTheta)); } float OpticalDepth(float3 StartPosition, float3 EndPosition) { float3 Direction = normalize(EndPosition - StartPosition); float RayLength = RaySphereIntersection(StartPosition, Direction, float3(0, 0, 0), OutterRadius); float SampleLength = RayLength / Isteps; float3 tmpPos = StartPosition + 0.5 * SampleLength * Direction; float tmp; for (int i=0; i<Isteps; i++) { tmp += Density(length(tmpPos)-InnerRadius); tmpPos += SampleLength * Direction; } return tmp*SampleLength; } static float fExposure = -2; float3 HDR( float3 LDR) { return 1.0f - exp( fExposure * LDR ); } [numthreads(32, 32, 1)] //disptach 8, 8, 1 it's 256 by 256 image void ComputeSky(uint3 DTID : SV_DispatchThreadID) { float X = ((2 * DTID.x) / 255) - 1; float Y = 1 - ((2 * DTID.y) / 255); float r = sqrt(((X*X)+(Y*Y))); float Theta = r * (PI); float Phi = atan2(Y, X); static float3 Eye = float3(0, 10, 0); float ViewOD = 0, SunOD = 0, tmpDensity = 0; float3 Attenuation = 0, tmp = 0, Irgb = 0; //if (r<=1) { float3 ViewDir = normalize(float3(sin(Theta)*cos(Phi), cos(Theta),sin(Theta)*sin(Phi) )); float ViewRayLength = RaySphereIntersection(Eye, ViewDir, float3(0, 0, 0), OutterRadius); float SampleLength = ViewRayLength / Ksteps; //vSunDir = normalize(vSunDir); float cosTheta = dot(normalize(vSunDir), ViewDir); float3 tmpPos = Eye + 0.5 * SampleLength * ViewDir; for(int k=0; k<Ksteps; k++) { float SunRayLength = RaySphereIntersection(tmpPos, vSunDir, float3(0, 0, 0), OutterRadius); float3 TopAtmosphere = tmpPos + SunRayLength*vSunDir; ViewOD = OpticalDepth(Eye, tmpPos); SunOD = OpticalDepth(tmpPos, TopAtmosphere); tmpDensity = Density(length(tmpPos)-InnerRadius); Attenuation = exp(-RayleighCoeffs*(ViewOD+SunOD)); tmp += tmpDensity*Attenuation; tmpPos += SampleLength * ViewDir; } Irgb = RayleighCoeffs*RayleighPhaseFunction(cosTheta)*tmp*SampleLength; SkyColors[DTID.xy] = float4(Irgb, 1); } }  
    • By Endurion
      I have a gaming framework with an renderer interface. Those support DX8, DX9 and latest, DX11. Both DX8 and DX9 use fixed function pipeline, while DX11 obviously uses shaders. I've got most of the parts working fine, as in I can switch renderers and notice almost no difference. The most advanced features are 2 directional lights with a single texture  
      My last problem is lighting; albeit there's documentation on the D3D lighting model I still can't get the behaviour right. My mistake shows most prominently in the dark side opposite the lights. I'm pretty sure the ambient calculation is off, but that one's supposed to be the most simple one and should be hard to get wrong.
      Interestingly I've been searching high and low, and have yet to find a resource that shows how to build a HLSL shader where diffuse, ambient and specular are used together with material properties. I've got various shaders for all the variations I'm supporting. I stepped through the shader with the graphics debugger, but the calculation seems to do what I want. I'm just not sure the formula is correct.
      This one should suffice though, it's doing two directional lights, texture modulated with vertex color and a normal. Maybe someone can spot one (or more mistakes). And yes, this is in the vertex shader and I'm aware lighting will be as "bad" as in fixed function; that's my goal currently.
      // A constant buffer that stores the three basic column-major matrices for composing geometry. cbuffer ModelViewProjectionConstantBuffer : register(b0) { matrix model; matrix view; matrix projection; matrix ortho2d; }; struct DirectionLight { float3 Direction; float PaddingL1; float4 Ambient; float4 Diffuse; float4 Specular; }; cbuffer LightsConstantBuffer : register( b1 ) { float4 Ambient; float3 EyePos; float PaddingLC1; DirectionLight Light[8]; }; struct Material { float4 MaterialEmissive; float4 MaterialAmbient; float4 MaterialDiffuse; float4 MaterialSpecular; float MaterialSpecularPower; float3 MaterialPadding; }; cbuffer MaterialConstantBuffer : register( b2 ) { Material _Material; }; // Per-vertex data used as input to the vertex shader. struct VertexShaderInput { float3 pos : POSITION; float3 normal : NORMAL; float4 color : COLOR0; float2 tex : TEXCOORD0; }; // Per-pixel color data passed through the pixel shader. struct PixelShaderInput { float4 pos : SV_POSITION; float2 tex : TEXCOORD0; float4 color : COLOR0; }; // Simple shader to do vertex processing on the GPU. PixelShaderInput main(VertexShaderInput input) { PixelShaderInput output; float4 pos = float4( input.pos, 1.0f ); // Transform the vertex position into projected space. pos = mul(pos, model); pos = mul(pos, view); pos = mul(pos, projection); output.pos = pos; // pass texture coords output.tex = input.tex; // Calculate the normal vector against the world matrix only. //set required lighting vectors for interpolation float3 normal = mul( input.normal, ( float3x3 )model ); normal = normalize( normal ); float4 ambientEffect = Ambient; float4 diffuseEffect = float4( 0, 0, 0, 0 ); float4 specularEffect = float4( 0, 0, 0, 0 ); for ( int i = 0; i < 2; ++i ) { // Invert the light direction for calculations. float3 lightDir = -Light[i].Direction; float lightFactor = max( dot( lightDir, input.normal ), 0 ); ambientEffect += Light[i].Ambient * _Material.MaterialAmbient; diffuseEffect += saturate( Light[i].Diffuse * dot( normal, lightDir ) );// * _Material.MaterialDiffuse; //specularEffect += Light[i].Specular * dot( normal, halfangletolight ) * _Material.MaterialSpecularPower; } specularEffect *= _Material.MaterialSpecular; //ambientEffect.w = 1.0; ambientEffect = normalize( ambientEffect ); /* Ambient effect: (L1.ambient + L2.ambient) * object ambient color Diffuse effect: (L1.diffuse * Dot(VertexNormal, Light1.Direction) + L2.diffuse * Dot(VertexNormal, Light2.Direction)) * object diffuse color Specular effect: (L1.specular * Dot(VertexNormal, HalfAngleToLight1) * Object specular reflection power + L2.specular * Dot(VertexNormal, HalfAngleToLight2) * Object specular reflection power ) * object specular color Resulting color = Ambient effect + diffuse effect + specular effect*/ float4 totalFactor = ambientEffect + diffuseEffect + specularEffect; totalFactor.w = 1.0; output.color = input.color * totalFactor; return output; }   Edit: This message editor is driving me nuts (Arrrr!) - I don't write code in Word.
  • Popular Now