private void InitializeConstantBuffers(TSource source, InitializerParameters parameters) { var technique = parameters.Technique; var referenceTable = from shaderObject in technique.Mapping.Shaders from cb in shaderObject.ConstantBuffers where parameters.Selector(cb) && ValidateConstantBuffer(cb) select cb; string effectName = technique.Name; foreach (var cbDesc in referenceTable) { if (!technique[cbDesc.ShaderType].HasConstantBuffer(cbDesc.Index, effectName, parameters.EntityId)) { technique[cbDesc.ShaderType].AddConstantBuffer(parameters.EntityId, new ConstantBuffer(device, cbDesc, effectName)); } ConstantBuffer cb = technique[cbDesc.ShaderType].GetConstantBuffer(cbDesc.Index, effectName, parameters.EntityId); var validReferences = from kvp in cbDesc.References let reference = kvp.Value where AcceptedReferences.Any(r => string.Equals(r.Value, reference.Value)) select kvp; foreach (var kvp in validReferences) { var reference = kvp.Value; if (cbDesc.UpdateFrequency != UpdateType.InstanceFrame && cbDesc.IsParsed(effectName, reference)) { continue; } var effectParameters = CreateParameter(cbDesc, source, kvp.Key, reference.Value, parameters); foreach (IParameter parameter in effectParameters) { cb.AddParameter(kvp.Key, parameter); } cbDesc.MarkAsParsed(effectName, reference); } } }
private List <ConstantBuffer> ReflectConstantBuffers(ShaderReflection reflection) { var constantBuffers = new List <ConstantBuffer>(); for (int cBufferIndex = 0; cBufferIndex < reflection.Description.ConstantBuffers; cBufferIndex++) { var cb = reflection.GetConstantBuffer(cBufferIndex); var bindingDesc = reflection.GetResourceBindingDescription(cb.Description.Name); var buf = new Buffer(renderer.Device, cb.Description.Size, ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, sizeof(float)); var constantBuffer = new ConstantBuffer(buf, bindingDesc.BindPoint); for (int i = 0; i < cb.Description.VariableCount; i++) { var refVar = cb.GetVariable(i); var type = refVar.GetVariableType(); switch (type.Description.Type) { case ShaderVariableType.Float: if (type.Description.RowCount == 4 && type.Description.ColumnCount == 4) { var matParam = new MatrixParameter(refVar.Description.StartOffset); if (matParam.Size != refVar.Description.Size) { throw CargoEngineException.Create("Error ConstantBufferParamtersize"); } constantBuffer.AddParameter(refVar.Description.Name, matParam); } if (type.Description.RowCount == 1) { switch (type.Description.ColumnCount) { case 2: var vec2Param = new Vector2Parameter(refVar.Description.StartOffset); if (vec2Param.Size != refVar.Description.Size) { throw CargoEngineException.Create("Error ConstantBufferParamtersize"); } constantBuffer.AddParameter(refVar.Description.Name, vec2Param); break; case 3: var vec3Param = new Vector3Parameter(refVar.Description.StartOffset); if (vec3Param.Size != refVar.Description.Size) { throw CargoEngineException.Create("Error ConstantBufferParamtersize"); } constantBuffer.AddParameter(refVar.Description.Name, vec3Param); break; case 4: var vec4Param = new Vector4Parameter(refVar.Description.StartOffset); if (vec4Param.Size != refVar.Description.Size) { throw CargoEngineException.Create("Error ConstantBufferParamtersize"); } constantBuffer.AddParameter(refVar.Description.Name, vec4Param); break; } } break; } constantBuffers.Add(constantBuffer); } } return(constantBuffers); }