void HandleDrawCallChange(ulong key) { SortItem.VPLayer vpLayer = SortItem.GetVPLayer(key); SortItem.Viewport vp = SortItem.GetViewport(key); SortItem.Translucency trans = SortItem.GetTranslucency(key); ushort VB = SortItem.GetVBID(key); ushort IB = SortItem.GetIBID(key); if (vpLayer != currentVPLayer) { if (currentVPLayer == SortItem.VPLayer.SKYBOX3D) { //Camera.position = originalPosition; //Camera.PositionCamera(); Camera.RotateForViewer(); // Resetting will just be a call to rotateforviewer now Matrix worldview = device.GetTransform(TransformState.View) * Camera.World; effect.SetValue("WorldViewProj", worldview * Camera.Projection); //device.Clear(ClearFlags.ZBuffer, Color.FromArgb(0, Color.CornflowerBlue), 1.0f, 0); } else if (currentVPLayer == SortItem.VPLayer.SKYBOX) { device.SetRenderState(RenderState.ZWriteEnable, true); device.SetRenderState(RenderState.ZEnable, true); } else if (currentVPLayer == SortItem.VPLayer.HUD) { device.SetRenderState(RenderState.ZEnable, true); } else if (currentVPLayer == SortItem.VPLayer.EFFECT) { device.SetRenderState(RenderState.ZWriteEnable, true); } currentVPLayer = vpLayer; // Layer change. switch (vpLayer) { case SortItem.VPLayer.SKYBOX3D: if (effect.Technique != technique) effect.Technique = "Sky3d"; if (SourceMap != null) { SourceMap.SetupVertexBuffer(device); Vector3 oldPos = Camera.position; Vector3 pos = SourceMap.skybox3d.GetSkyboxPosition(Camera.position); Renderer.Instance.viewParams.Origin.origin = pos; //Camera.position = pos; Camera.RotateForViewer(); // Resetting will just be a call to rotateforviewer now Renderer.Instance.viewParams.Origin.origin = oldPos; Matrix worldview = device.GetTransform(TransformState.View) * Camera.World; effect.SetValue("WorldViewProj", worldview * Camera.Projection); //device.SetRenderState(RenderState.z } break; case SortItem.VPLayer.SKYBOX: effect.Technique = "Sky"; device.SetRenderState(RenderState.ZWriteEnable, false); if (SourceMap != null) SourceMap.skybox.SetupRender(device); break; case SortItem.VPLayer.HUD: device.SetRenderState(RenderState.ZEnable, false); if (trans != SortItem.Translucency.OPAQUE) effect.Technique = "GUIAlpha"; else effect.Technique = "FinalPass_RGBE8"; break; case SortItem.VPLayer.WORLD: if (effect.Technique != technique) effect.Technique = technique; if (SourceMap != null) SourceMap.SetupVertexBuffer(device); break; case SortItem.VPLayer.EFFECT: effect.Technique = "PositionColorAlpha"; device.SetRenderState(RenderState.ZWriteEnable, false); break; } } // Handle viewport change if (vp != currentViewport) { if (currentViewport == SortItem.Viewport.INSTANCED) { // Disable frequency instancing device.ResetStreamSourceFrequency(0); device.SetStreamSource(1, null, 0, 0); //device.SetTexture(1, null); effect.Technique = technique; } else if (currentViewport == SortItem.Viewport.FOUR) effect.Technique = technique; currentViewport = vp; switch (vp) { case SortItem.Viewport.INSTANCED: Matrix worldview = device.GetTransform(TransformState.View) * Camera.World; effect.SetValue("WorldViewProj", worldview * Camera.Projection); effect.Technique = "TexturedInstaced"; //device.SetTexture(1, SourceMap.ambientLightTexture); break; case SortItem.Viewport.FOUR: effect.Technique = "PositionTexturedColor"; break; } } // Handle transparency change if (trans != currentTrans) { if (currentTrans == SortItem.Translucency.NORMAL || currentTrans == SortItem.Translucency.ADDITIVE || currentTrans == SortItem.Translucency.SUBSTRACTIVE) { device.SetRenderState(RenderState.AlphaBlendEnable, false); } currentTrans = trans; switch (trans) { case SortItem.Translucency.OPAQUE: if (vpLayer == SortItem.VPLayer.SKYBOX3D) effect.Technique = "Sky3d"; else if (vpLayer != SortItem.VPLayer.HUD && currentViewport != SortItem.Viewport.INSTANCED && currentViewport != SortItem.Viewport.FOUR) effect.Technique = technique; else if (currentViewport != SortItem.Viewport.INSTANCED && currentViewport != SortItem.Viewport.FOUR) effect.Technique = "FinalPass_RGBE8"; break; case SortItem.Translucency.ADDITIVE: case SortItem.Translucency.SUBSTRACTIVE: case SortItem.Translucency.NORMAL: if (vpLayer == SortItem.VPLayer.SKYBOX3D) { effect.Technique = "Sky3d"; } else if (vpLayer != SortItem.VPLayer.HUD && currentViewport != SortItem.Viewport.INSTANCED && vpLayer != SortItem.VPLayer.EFFECT && currentViewport != SortItem.Viewport.FOUR) effect.Technique = "TexturedLightmapAlpha"; else if (currentViewport != SortItem.Viewport.INSTANCED && vpLayer == SortItem.VPLayer.HUD) effect.Technique = "GUIAlpha"; else if (vpLayer == SortItem.VPLayer.EFFECT) { effect.Technique = "PositionColorAlpha"; } break; } } // Vertex buffer change if (VB != currentVB && VB != 0) { device.SetStreamSource(0, VertexBuffers[VB].VB, 0, D3DX.GetFVFVertexSize(VertexBuffers[VB].VF)); device.VertexDeclaration = VertexBuffers[VB].VD; currentVB = VB; } // Index buffer change if (IB != currentIB && IB != 0) { device.Indices = IndexBuffers[IB].IB; currentIB = IB; } }
private void RenderDrawCallsNew() { // Reset stuff currentVPLayer = SortItem.VPLayer.EFFECT; currentViewport = SortItem.Viewport.EIGHT; currentTrans = SortItem.Translucency.OPAQUE; currentIB = 65535; currentVB = 65535; lastMaterial = 0; setMaterial = true; //Vector3 originalPosition = Camera.position; //nSameMaterial = 0; int drawOffset = 0; int nDrawGroup = 0; // Issue drawcalls from last frame ulong oldKey = 0; uint oldMaterial = 0; int endcase = currentdrawCalls.Count; ulong key = 0; for (int i = 0; i < currentdrawCalls.Count+1; i++) { if (i == endcase) key = 0; else key = currentdrawCalls[i].Key; // Check for statechange - ignore material/depth change if ((uint)(key>>32) != (uint)(oldKey>>32) || nDrawGroup == 1023) { int numpasses = effect.Begin(FX.None); for (int j = 0; j < numpasses; j++) { effect.BeginPass(j); { for (int h = 0; h < nDrawGroup; h++) { currentdrawCalls[drawOffset+h].Value.Invoke(effect, device, setmaterialbatch[h]); } } effect.EndPass(); } effect.End(); // Draw grouped calls HandleDrawCallChange(key); // Reset key oldKey = key; setmaterialbatch[0] = true; nDrawGroup = 1; drawOffset = i; } else { // Save result for material change uint materialID = SortItem.GetMaterial(key); setmaterialbatch[nDrawGroup] = materialID != oldMaterial; oldMaterial = materialID; // Store up calls nDrawGroup++; } oldKey = key; } if (currentViewport == SortItem.Viewport.INSTANCED) { // Disable frequency instancing device.ResetStreamSourceFrequency(0); device.SetStreamSource(1, null, 0, 0); effect.Technique = technique; } }