// Note: We only support a single user uniform buffer per shader void CreateBuffers() { m_VSConstantBuffers = new SharpDX.Direct3D11.Buffer[m_VSUniformBuffers.Count + (m_VSUserUniformBuffer != null ? 1 : 0)]; foreach (ShaderUniformBufferDeclaration decl in m_VSUniformBuffers) { var desc = new BufferDescription { SizeInBytes = decl.Size, Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write }; m_VSConstantBuffers[decl.Register] = new SharpDX.Direct3D11.Buffer(Context.Instance.Dev, desc); } if (m_VSUserUniformBuffer != null) { ShaderUniformBufferDeclaration decl = m_VSUserUniformBuffer; var desc = new BufferDescription { SizeInBytes = decl.Size, Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write }; m_VSConstantBuffers[decl.Register] = new SharpDX.Direct3D11.Buffer(Context.Instance.Dev, desc); } m_PSConstantBuffers = new SharpDX.Direct3D11.Buffer[m_PSUniformBuffers.Count + (m_PSUserUniformBuffer != null ? 1 : 0)]; foreach (ShaderUniformBufferDeclaration decl in m_PSUniformBuffers) { var desc = new BufferDescription { SizeInBytes = decl.Size, Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write }; m_PSConstantBuffers[decl.Register] = new SharpDX.Direct3D11.Buffer(Context.Instance.Dev, desc); } if (m_PSUserUniformBuffer != null) { ShaderUniformBufferDeclaration decl = m_PSUserUniformBuffer; var desc = new BufferDescription { SizeInBytes = decl.Size, Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write }; m_PSConstantBuffers[decl.Register] = new SharpDX.Direct3D11.Buffer(Context.Instance.Dev, desc); } }
void ParseCBuffer(string block) { string[] tokens = block.Split(new[] { " ", "\t", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); int index = 1; string bufferName = tokens[index++]; int reg = 0; if (tokens[index++] == ":") // Register specified { reg = int.Parse(Regex.Match(tokens[index++], @"\d+").Value); } ShaderUniformBufferDeclaration buffer = null; ShaderType shaderType = ShaderType.NONE; if (bufferName.StartsWith("VS")) { shaderType = ShaderType.VERTEX; } else if (bufferName.StartsWith("PS")) { shaderType = ShaderType.PIXEL; } else { Debug.Assert(false, "CBuffer no shader type"); } index++; // { while (!tokens[index].Equals("}")) { string type = tokens[index++]; string name = tokens[index++]; // Strip ; from name if present name = name.Replace(";", ""); if (buffer == null) { buffer = new ShaderUniformBufferDeclaration(bufferName, reg, shaderType); if (name.StartsWith("sys_")) { switch (shaderType) { case ShaderType.VERTEX: m_VSUniformBuffers.Add(buffer); break; case ShaderType.PIXEL: m_PSUniformBuffers.Add(buffer); break; } } else { switch (shaderType) { case ShaderType.VERTEX: Debug.Assert(m_VSUserUniformBuffer == null); m_VSUserUniformBuffer = buffer; break; case ShaderType.PIXEL: Debug.Assert(m_PSUserUniformBuffer == null); m_PSUserUniformBuffer = buffer; break; } } } ShaderUniformType t = ShaderUniformTypeMethods.StringToUniformType(type); ShaderUniformDeclaration decl = null; if (t == ShaderUniformType.NONE) { ShaderStruct s = FindStruct(type); decl = new ShaderUniformDeclaration(s, name); } else { decl = new ShaderUniformDeclaration(t, name); } buffer.PushUniform(decl); } buffer.Align(); }