private LightShaderGroupDynamic FindOrCreateLightShaderGroup(LightGroupKey lightGroupKey, ProcessLightsParameters parameters) { LightShaderGroupDynamic lightShaderGroup; // Check to see if this combination of parameters has already been stored as a group: if (!lightShaderGroupPool.TryGetValue(lightGroupKey, out lightShaderGroup)) { // If a group with the same key has not already been added, create it: ILightShadowMapShaderGroupData shadowGroupData = CreateShadowMapShaderGroupData(lightGroupKey.ShadowRenderer, lightGroupKey.ShadowType); lightShaderGroup = CreateLightShaderGroup(parameters.Context, shadowGroupData); lightShaderGroup.SetViews(parameters.Views); lightShaderGroupPool.Add(lightGroupKey, lightShaderGroup); } return(lightShaderGroup); }
public override void ProcessLights(ProcessLightsParameters parameters) { if (parameters.LightCollection.Count == 0) { return; } // Check if we have a fallback renderer next in the chain, in case we don't need shadows bool hasNextRenderer = parameters.RendererIndex < (parameters.Renderers.Length - 1); ILightShadowMapRenderer currentShadowRenderer = null; LightShadowType currentShadowType = 0; // Start by filtering/sorting what can be processed shadowComparer.ShadowMapTexturesPerLight = parameters.ShadowMapTexturesPerLight; shadowComparer.Lights = parameters.LightCollection; parameters.LightIndices.Sort(0, parameters.LightIndices.Count, shadowComparer); // Loop over the number of lights + 1 where the last iteration will always flush the last batch of lights for (int j = 0; j < parameters.LightIndices.Count + 1;) { // TODO: Eventually move this loop to a separate function that returns a structure. // These variables will contain the relevant parameters of the next usable light: LightShadowType nextShadowType = 0; ILightShadowMapRenderer nextShadowRenderer = null; LightShadowMapTexture nextShadowTexture = null; RenderLight nextLight = null; // Find the next light whose attributes aren't null: if (j < parameters.LightIndices.Count) { nextLight = parameters.LightCollection[parameters.LightIndices[j]]; if (parameters.ShadowMapRenderer != null && parameters.ShadowMapTexturesPerLight.TryGetValue(nextLight, out nextShadowTexture) && nextShadowTexture.Atlas != null) // atlas could not be allocated? treat it as a non-shadowed texture { nextShadowType = nextShadowTexture.ShadowType; nextShadowRenderer = nextShadowTexture.Renderer; } } // Flush current group // If we detect that the previous light's attributes don't match the next one's, create a new group (or add to an existing one that has the same attributes): if (j == parameters.LightIndices.Count || currentShadowType != nextShadowType || currentShadowRenderer != nextShadowRenderer) // TODO: Refactor this into a little structure instead. { if (processedLights.Count > 0) { var lightGroupKey = new LightGroupKey(currentShadowRenderer, currentShadowType); LightShaderGroupDynamic lightShaderGroup = FindOrCreateLightShaderGroup(lightGroupKey, parameters); // Add view and lights to the current group: var allowedLightCount = lightShaderGroup.AddView(parameters.ViewIndex, parameters.View, processedLights.Count); for (int i = 0; i < allowedLightCount; ++i) { LightDynamicEntry light = processedLights[i]; lightShaderGroup.AddLight(light.Light, light.ShadowMapTexture); } // TODO: assign extra lights to non-shadow rendering if possible //for (int i = lightCount; i < processedLights.Count; ++i) // XXX.AddLight(processedLights[i], null); // Add the current light shader group to the collection if it hasn't already been added: var lightShaderGroupEntry = new LightShaderGroupEntry <LightGroupKey>(lightGroupKey, lightShaderGroup); if (!lightShaderGroups.Contains(lightShaderGroupEntry)) { lightShaderGroups.Add(lightShaderGroupEntry); } processedLights.Clear(); } // Start next group currentShadowType = nextShadowType; currentShadowRenderer = nextShadowRenderer; } if (j < parameters.LightIndices.Count) { // Do we need to process non shadowing lights or defer it to something else? if (nextShadowTexture == null && hasNextRenderer) { // Break out so the remaining lights can be handled by the next renderer break; } parameters.LightIndices.RemoveAt(j); processedLights.Add(new LightDynamicEntry(nextLight, nextShadowTexture)); } else { j++; } } processedLights.Clear(); }
public LightShaderGroupEntry(LightGroupKey key, LightShaderGroup value) { Key = key; Value = value; }
public override void ProcessLights(ProcessLightsParameters parameters) { if (parameters.LightCollection.Count == 0) { return; } ILightShadowMapRenderer currentShadowRenderer = null; LightShadowType currentShadowType = 0; // Start by filtering/sorting what can be processed shadowComparer.ShadowMapTexturesPerLight = parameters.ShadowMapTexturesPerLight; parameters.LightCollection.Sort(parameters.LightStart, parameters.LightEnd - parameters.LightStart, shadowComparer); for (int index = parameters.LightStart; index <= parameters.LightEnd; index++) { LightShadowType nextShadowType = 0; ILightShadowMapRenderer nextShadowRenderer = null; LightShadowMapTexture nextShadowTexture = null; LightComponent nextLight = null; if (index < parameters.LightEnd) { nextLight = parameters.LightCollection[index]; if (parameters.ShadowMapRenderer != null && parameters.ShadowMapTexturesPerLight.TryGetValue(nextLight, out nextShadowTexture) && nextShadowTexture.Atlas != null) // atlas could not be allocated? treat it as a non-shadowed texture { nextShadowType = nextShadowTexture.ShadowType; nextShadowRenderer = nextShadowTexture.Renderer; } } // Flush current group if (index == parameters.LightEnd || currentShadowType != nextShadowType || currentShadowRenderer != nextShadowRenderer) { if (processedLights.Count > 0) { var lightGroupKey = new LightGroupKey(currentShadowRenderer, currentShadowType); LightShaderGroupDynamic lightShaderGroup; if (!lightShaderGroupPool.TryGetValue(lightGroupKey, out lightShaderGroup)) { ILightShadowMapShaderGroupData shadowGroupData = null; if (currentShadowRenderer != null) { shadowGroupData = currentShadowRenderer.CreateShaderGroupData(currentShadowType); } lightShaderGroup = CreateLightShaderGroup(parameters.Context, shadowGroupData); lightShaderGroup.SetViews(parameters.Views); lightShaderGroupPool.Add(lightGroupKey, lightShaderGroup); } // Add view and lights var allowedLightCount = lightShaderGroup.AddView(parameters.ViewIndex, parameters.View, processedLights.Count); for (int i = 0; i < allowedLightCount; ++i) { var light = processedLights[i]; lightShaderGroup.AddLight(light.Light, light.ShadowMapTexture); } // TODO: assign extra lights to non-shadow rendering if possible //for (int i = lightCount; i < processedLights.Count; ++i) // XXX.AddLight(processedLights[i], null); var lightShaderGroupEntry = new LightShaderGroupEntry(lightGroupKey, lightShaderGroup); if (!lightShaderGroups.Contains(lightShaderGroupEntry)) { lightShaderGroups.Add(lightShaderGroupEntry); } processedLights.Clear(); } // Start next group currentShadowType = nextShadowType; currentShadowRenderer = nextShadowRenderer; } if (index < parameters.LightEnd) { // Do we need to process non shadowing lights or defer it to something else? if (nextShadowTexture == null && NonShadowRenderer != null) { parameters.LightStart = index; NonShadowRenderer.ProcessLights(parameters); break; } processedLights.Add(new LightDynamicEntry(nextLight, nextShadowTexture)); } } processedLights.Clear(); }
public override void ProcessLights(ProcessLightsParameters parameters) { if (parameters.LightCollection.Count == 0) { return; } // Check if we have a fallback renderer next in the chain, in case we don't need shadows bool hasNextRenderer = parameters.RendererIndex < (parameters.Renderers.Length - 1); ILightShadowMapRenderer currentShadowRenderer = null; LightShadowType currentShadowType = 0; // Start by filtering/sorting what can be processed shadowComparer.ShadowMapTexturesPerLight = parameters.ShadowMapTexturesPerLight; shadowComparer.Lights = parameters.LightCollection; parameters.LightIndices.Sort(0, parameters.LightIndices.Count, shadowComparer); // Loop over the number of lights + 1 where the last iteration will always flush the last batch of lights for (int j = 0; j < parameters.LightIndices.Count + 1;) { LightShadowType nextShadowType = 0; ILightShadowMapRenderer nextShadowRenderer = null; LightShadowMapTexture nextShadowTexture = null; LightComponent nextLight = null; if (j < parameters.LightIndices.Count) { nextLight = parameters.LightCollection[parameters.LightIndices[j]]; if (parameters.ShadowMapRenderer != null && parameters.ShadowMapTexturesPerLight.TryGetValue(nextLight, out nextShadowTexture) && nextShadowTexture.Atlas != null) // atlas could not be allocated? treat it as a non-shadowed texture { nextShadowType = nextShadowTexture.ShadowType; nextShadowRenderer = nextShadowTexture.Renderer; } } // Flush current group if (j == parameters.LightIndices.Count || currentShadowType != nextShadowType || currentShadowRenderer != nextShadowRenderer) { if (processedLights.Count > 0) { var lightGroupKey = new LightGroupKey(currentShadowRenderer, currentShadowType); LightShaderGroupDynamic lightShaderGroup; if (!lightShaderGroupPool.TryGetValue(lightGroupKey, out lightShaderGroup)) { ILightShadowMapShaderGroupData shadowGroupData = null; if (currentShadowRenderer != null) { shadowGroupData = currentShadowRenderer.CreateShaderGroupData(currentShadowType); } lightShaderGroup = CreateLightShaderGroup(parameters.Context, shadowGroupData); lightShaderGroup.SetViews(parameters.Views); lightShaderGroupPool.Add(lightGroupKey, lightShaderGroup); } // Add view and lights var allowedLightCount = lightShaderGroup.AddView(parameters.ViewIndex, parameters.View, processedLights.Count); for (int i = 0; i < allowedLightCount; ++i) { var light = processedLights[i]; lightShaderGroup.AddLight(light.Light, light.ShadowMapTexture); } // TODO: assign extra lights to non-shadow rendering if possible //for (int i = lightCount; i < processedLights.Count; ++i) // XXX.AddLight(processedLights[i], null); var lightShaderGroupEntry = new LightShaderGroupEntry(lightGroupKey, lightShaderGroup); if (!lightShaderGroups.Contains(lightShaderGroupEntry)) { lightShaderGroups.Add(lightShaderGroupEntry); } processedLights.Clear(); } // Start next group currentShadowType = nextShadowType; currentShadowRenderer = nextShadowRenderer; } if (j < parameters.LightIndices.Count) { // Do we need to process non shadowing lights or defer it to something else? if (nextShadowTexture == null && hasNextRenderer) { // Break out so the remaining lights can be handled by the next renderer break; } parameters.LightIndices.RemoveAt(j); processedLights.Add(new LightDynamicEntry(nextLight, nextShadowTexture)); } else { j++; } } processedLights.Clear(); }