コード例 #1
0
        public void CreateBufferInstance(CustomConstantBufferDefinition baseDefinition, Device device, bool newInstance)
        {
            if (!newInstance)
            {
                m_Writer.Dispose();
                m_Writer = null;

                m_DataStream.Dispose();
                m_DataStream = null;

                m_ConstantBuffer.Dispose();
                m_ConstantBuffer = null;
            }
            else
            {
                m_Values = new Dictionary <String, object>();
            }

            m_Definition     = baseDefinition;
            m_DefinitionName = baseDefinition.m_Name;
            m_DataBuffer     = new byte[baseDefinition.m_Size];
            m_Size           = baseDefinition.m_Size;
            m_DataStream     = new MemoryStream(m_DataBuffer, 0, baseDefinition.m_Size, true);
            m_Writer         = new BinaryWriter(m_DataStream);
            m_ConstantBuffer = new SlimDX.Direct3D11.Buffer(
                device,
                baseDefinition.m_Size,
                ResourceUsage.Dynamic,
                BindFlags.ConstantBuffer,
                CpuAccessFlags.Write,
                ResourceOptionFlags.None,
                0);
        }
コード例 #2
0
        public CustomConstantBufferInstance(CustomConstantBufferDefinition baseDefinition, Device device)
        {
            CreateBufferInstance(baseDefinition, device, true);

            lock (m_Lock)
            {
                if (baseDefinition.m_IsGlobal)
                {
                    // todo never destroyed
                    s_AllGlobalInstances.Add(this);
                }
                s_AllInstances.Add(this);
            }
        }
コード例 #3
0
        private static void ParseFile(string path)
        {
            List<ShaderWrapper> localShaders = new List<ShaderWrapper>();
            List<Tuple<string, int, int, int>> computeRegisters = new List<Tuple<String, int, int, int>>();
            ShaderFile sf = m_AllShaderFiles[GetFileNameFromPath(path)];
            sf.m_DirectlyIncludedFiles.Clear();
            sf.m_FlattenedFiles.Clear();

            using (StreamReader sr = new StreamReader(path))
            {
                while (!sr.EndOfStream)
                {
                    String line = sr.ReadLine();
                    Match matchShaderRegex = m_RegexWrapper.shaderRegex.Match(line);
                    Match matchCbufferRegex = m_RegexWrapper.cbufferRegex.Match(line);
                    Match matchSamplerRegex = m_RegexWrapper.samplerRegex.Match(line);
                    Match matchNumThreadsRegex = m_RegexWrapper.numThreadsRegex.Match(line);
                    Match matchGlobalDefineRegex = m_RegexWrapper.globalDefineRegex.Match(line);
                    Match matchIncludeRegex = m_RegexWrapper.includeRegex.Match(line);

                    if (matchIncludeRegex.Success)
                    {
                        string includeName = matchIncludeRegex.Groups[1].Value;
                        sf.m_DirectlyIncludedFiles.Add(includeName);
                    }

                    if (matchGlobalDefineRegex.Success)
                    {
                        string defineName = matchGlobalDefineRegex.Groups[1].Value;
                        float value = Single.Parse(matchGlobalDefineRegex.Groups[2].Value, CultureInfo.InvariantCulture);

                        if (m_GlobalDefineValues.ContainsKey(defineName))
                        {
                            m_GlobalDefineValues[defineName] = value;
                        }
                        else
                        {
                            m_GlobalDefineValues.Add(defineName, value);
                        }
                    }

                    if (matchCbufferRegex.Success)
                    {
                        Match globalBufferMatch = m_RegexWrapper.globalBufferRegex.Match(line);
                        Match registerMatch = m_RegexWrapper.registerRegex.Match(line);
                        if (!registerMatch.Success)
                        {
                            throw new Exception("Unable to find register for constant buffer");
                        }
                        int cbufferRegister = Int32.Parse(registerMatch.Groups[1].Value);

                        // We have a new cbuffer
                        string cbufferName = matchCbufferRegex.Groups[1].Value;

                        string cbufferText = "";
                        while (!sr.EndOfStream)
                        {
                            line = sr.ReadLine();
                            if (line.Contains('{'))
                                continue;

                            if (line.Contains('}'))
                            {
                                if (m_ConstantBuffers.ContainsKey(cbufferName))
                                {
                                    m_ConstantBuffers[cbufferName].ParseConstantBuffer(cbufferText, cbufferRegister, globalBufferMatch.Success);
                                }
                                else
                                {
                                    CustomConstantBufferDefinition myNewConstantBuffer =
                                        new CustomConstantBufferDefinition(
                                            cbufferName,
                                            cbufferText,
                                            cbufferRegister,
                                            globalBufferMatch.Success,
                                            path);

                                    m_ConstantBuffers.Add(cbufferName, myNewConstantBuffer);

                                }
                                break;
                            }

                            cbufferText += line.Trim() + "\n";
                        }

                        continue;
                    }

                    if (matchShaderRegex.Success)
                    {
                        // We have a new shader
                        string shaderType = matchShaderRegex.Groups[1].Value;
                        string shaderName = matchShaderRegex.Groups[2].Value;
                        string shaderEntry = matchShaderRegex.Groups[3].Value;
                        string shaderDefines = matchShaderRegex.Groups[4].Value;
                        ShaderType type = ShaderType.PixelShader;

                        switch (shaderType.ToLower())
                        {
                            case "pixel": type = ShaderType.PixelShader;
                                break;
                            case "vertex": type = ShaderType.VertexShader;
                                break;
                            case "compute": type = ShaderType.ComputeShader;
                                break;
                            case "geometry": type = ShaderType.GeometryShader;
                                break;
                        }

                        HashSet<string> defines = new HashSet<String>();

                        if (shaderDefines.Length > 0)
                        {
                            var tokens = shaderDefines.Split(new String[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries);
                            for (int i = 1; i < tokens.Length; ++i)
                            {
                                defines.Add(tokens[i]);
                            }
                        }

                        ShaderWrapper newShader = new ShaderWrapper()
                        {
                            m_ShaderFile = sf,
                            m_ShaderName = shaderName,
                            m_ShaderType = type,
                            m_ShaderEntry = shaderEntry,
                            m_Defines = defines
                        };

                        localShaders.Add(newShader);
                    }

                    if (matchNumThreadsRegex.Success)
                    {
                        int threadsX = Int32.Parse(matchNumThreadsRegex.Groups[1].Value);
                        int threadsY = Int32.Parse(matchNumThreadsRegex.Groups[2].Value);
                        int threadsZ = Int32.Parse(matchNumThreadsRegex.Groups[3].Value);

                        string nextLine = sr.ReadLine();
                        var tokens = nextLine.Split(new String[] { " ", "(" }, StringSplitOptions.RemoveEmptyEntries);

                        computeRegisters.Add(new Tuple<String, int, int, int>(tokens[1], threadsX, threadsY, threadsZ));
                    }

                    if (matchSamplerRegex.Success)
                    {
                        string samplerType = matchSamplerRegex.Groups[1].Value;
                        string samplerName = matchSamplerRegex.Groups[2].Value;
                        string samplerRegister = matchSamplerRegex.Groups[3].Value;

                        m_SamplerStates[Int32.Parse(samplerRegister)] = SamplerStates.GetSamplerStateForName(samplerName);
                    }

                }
            }

            foreach (var shader in localShaders)
            {
                if (m_Shaders.ContainsKey(shader.m_ShaderName))
                {
                    m_Shaders[shader.m_ShaderName] = shader;
                }
                else
                {
                    m_Shaders.Add(shader.m_ShaderName, shader);
                }

                // CompileShader(shader);
                if (shader.m_ShaderCompilationTask != null)
                    throw new Exception("Already compiling");

                shader.m_ShaderCompilationTask = Task.Factory.StartNew(() => CompileShader(shader));
            }

            sf.m_FlattenedFiles.Add(sf.m_FileName);
            sf.m_FlattenedFiles.UnionWith(sf.m_DirectlyIncludedFiles);

            foreach (var registers in computeRegisters)
            {
                var shaderFit = localShaders.Where
                (shader => shader.m_ShaderEntry == registers.Item1);

                foreach (var fittingShader in shaderFit)
                {
                    fittingShader.m_ThreadsX = registers.Item2;
                    fittingShader.m_ThreadsY = registers.Item3;
                    fittingShader.m_ThreadsZ = registers.Item4;
                }
            }
        }
コード例 #4
0
ファイル: ShaderManager.cs プロジェクト: shoff/CSharpRenderer
        private static void ParseFile(Device device, string path)
        {
            List <ShaderWrapper> localShaders = new List <ShaderWrapper>();
            List <Tuple <string, int, int, int> > computeRegisters = new List <Tuple <String, int, int, int> >();

            using (StreamReader sr = new StreamReader(path))
            {
                while (!sr.EndOfStream)
                {
                    String line                   = sr.ReadLine();
                    Match  matchShaderRegex       = m_RegexWrapper.shaderRegex.Match(line);
                    Match  matchCbufferRegex      = m_RegexWrapper.cbufferRegex.Match(line);
                    Match  matchSamplerRegex      = m_RegexWrapper.samplerRegex.Match(line);
                    Match  matchNumThreadsRegex   = m_RegexWrapper.numThreadsRegex.Match(line);
                    Match  matchGlobalDefineRegex = m_RegexWrapper.globalDefineRegex.Match(line);

                    if (matchGlobalDefineRegex.Success)
                    {
                        string defineName = matchGlobalDefineRegex.Groups[1].Value;
                        float  value      = Single.Parse(matchGlobalDefineRegex.Groups[2].Value, CultureInfo.InvariantCulture);

                        if (m_GlobalDefineValues.ContainsKey(defineName))
                        {
                            m_GlobalDefineValues[defineName] = value;
                        }
                        else
                        {
                            m_GlobalDefineValues.Add(defineName, value);
                        }
                    }

                    if (matchCbufferRegex.Success)
                    {
                        Match globalBufferMatch = m_RegexWrapper.globalBufferRegex.Match(line);
                        Match registerMatch     = m_RegexWrapper.registerRegex.Match(line);
                        if (!registerMatch.Success)
                        {
                            throw new Exception("Unable to find register for constant buffer");
                        }
                        int cbufferRegister = Int32.Parse(registerMatch.Groups[1].Value);

                        // We have a new cbuffer
                        string cbufferName = matchCbufferRegex.Groups[1].Value;

                        string cbufferText = "";
                        while (!sr.EndOfStream)
                        {
                            line = sr.ReadLine();
                            if (line.Contains('{'))
                            {
                                continue;
                            }

                            if (line.Contains('}'))
                            {
                                if (m_ConstantBuffers.ContainsKey(cbufferName))
                                {
                                    m_ConstantBuffers[cbufferName].ParseConstantBuffer(cbufferText, cbufferRegister, globalBufferMatch.Success);
                                }
                                else
                                {
                                    CustomConstantBufferDefinition myNewConstantBuffer =
                                        new CustomConstantBufferDefinition(
                                            cbufferName,
                                            cbufferText,
                                            cbufferRegister,
                                            globalBufferMatch.Success,
                                            path);

                                    m_ConstantBuffers.Add(cbufferName, myNewConstantBuffer);
                                }
                                break;
                            }

                            cbufferText += line.Trim() + "\n";
                        }

                        continue;
                    }

                    if (matchShaderRegex.Success)
                    {
                        // We have a new shader
                        string     shaderType    = matchShaderRegex.Groups[1].Value;
                        string     shaderName    = matchShaderRegex.Groups[2].Value;
                        string     shaderEntry   = matchShaderRegex.Groups[3].Value;
                        string     shaderDefines = matchShaderRegex.Groups[4].Value;
                        ShaderType type          = ShaderType.PixelShader;

                        switch (shaderType.ToLower())
                        {
                        case "pixel": type = ShaderType.PixelShader;
                            break;

                        case "vertex": type = ShaderType.VertexShader;
                            break;

                        case "compute": type = ShaderType.ComputeShader;
                            break;

                        case "geometry": type = ShaderType.GeometryShader;
                            break;
                        }

                        HashSet <string> defines = new HashSet <String>();

                        if (shaderDefines.Length > 0)
                        {
                            var tokens = shaderDefines.Split(new String[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries);
                            for (int i = 1; i < tokens.Length; ++i)
                            {
                                defines.Add(tokens[i]);
                            }
                        }

                        ShaderWrapper newShader = new ShaderWrapper()
                        {
                            m_FilePath     = path,
                            m_ShaderName   = shaderName,
                            m_ShaderType   = type,
                            m_ShaderEntry  = shaderEntry,
                            m_UsedIncludes = new HashSet <String>(),
                            m_Defines      = defines
                        };

                        localShaders.Add(newShader);
                    }

                    if (matchNumThreadsRegex.Success)
                    {
                        int threadsX = Int32.Parse(matchNumThreadsRegex.Groups[1].Value);
                        int threadsY = Int32.Parse(matchNumThreadsRegex.Groups[2].Value);
                        int threadsZ = Int32.Parse(matchNumThreadsRegex.Groups[3].Value);

                        string nextLine = sr.ReadLine();
                        var    tokens   = nextLine.Split(new String[] { " ", "(" }, StringSplitOptions.RemoveEmptyEntries);

                        computeRegisters.Add(new Tuple <String, int, int, int>(tokens[1], threadsX, threadsY, threadsZ));
                    }

                    if (matchSamplerRegex.Success)
                    {
                        string samplerType     = matchSamplerRegex.Groups[1].Value;
                        string samplerName     = matchSamplerRegex.Groups[2].Value;
                        string samplerRegister = matchSamplerRegex.Groups[3].Value;

                        m_SamplerStates[Int32.Parse(samplerRegister)] = SamplerStates.GetSamplerStateForName(samplerName);
                    }
                }
            }

            foreach (var shader in localShaders)
            {
                CompileShader(shader, device);
            }

            foreach (var registers in computeRegisters)
            {
                var shaderFit = localShaders.Where
                                    (shader => shader.m_ShaderEntry == registers.Item1);

                foreach (var fittingShader in shaderFit)
                {
                    fittingShader.m_ThreadsX = registers.Item2;
                    fittingShader.m_ThreadsY = registers.Item3;
                    fittingShader.m_ThreadsZ = registers.Item4;
                }
            }
        }
コード例 #5
0
 private void BufferWriteType(CustomConstantBufferDefinition.ConstantType type, Object value)
 {
     switch (type)
     {
         case CustomConstantBufferDefinition.ConstantType.ConstantType_Float:
             m_Writer.Write((Single)value);
             break;
         case CustomConstantBufferDefinition.ConstantType.ConstantType_Int:
             m_Writer.Write((Int32)value);
             break;
         default:
             m_Writer.Write(StructTools.RawSerialize(value));
             break;
     }
 }
コード例 #6
0
        public void CreateBufferInstance(CustomConstantBufferDefinition baseDefinition, Device device, bool newInstance)
        {
            if(!newInstance)
            {
                m_Writer.Dispose();
                m_Writer = null;

                m_DataStream.Dispose();
                m_DataStream = null;

                m_ConstantBuffer.Dispose();
                m_ConstantBuffer = null;
            }
            else
            {
                m_Values = new Dictionary<String, object>();
            }

            m_Definition = baseDefinition;
            m_DefinitionName = baseDefinition.m_Name;
            m_DataBuffer = new byte[baseDefinition.m_Size];
            m_Size = baseDefinition.m_Size;
            m_DataStream = new MemoryStream(m_DataBuffer, 0, baseDefinition.m_Size, true);
            m_Writer = new BinaryWriter(m_DataStream);
            m_ConstantBuffer = new SlimDX.Direct3D11.Buffer(
                device,
                baseDefinition.m_Size,
                ResourceUsage.Dynamic,
                BindFlags.ConstantBuffer,
                CpuAccessFlags.Write,
                ResourceOptionFlags.None,
                0);
            m_LuaContext = new Lua();
        }
コード例 #7
0
        public CustomConstantBufferInstance(CustomConstantBufferDefinition baseDefinition, Device device)
        {
            CreateBufferInstance(baseDefinition, device, true);

            lock(m_Lock)
            {
                if (baseDefinition.m_IsGlobal)
                {
                    // todo never destroyed
                    s_AllGlobalInstances.Add(this);
                }
                s_AllInstances.Add(this);
            }
        }