Ejemplo n.º 1
0
        private string loadFile(byte[] bytes)
        {
            if (bytes == null)
            {
                return(null);
            }

            using (MemoryStream ms = new MemoryStream(bytes))
                using (StreamReader sr = new StreamReader(ms))
                {
                    string code = string.Empty;

                    while (sr.Peek() != -1)
                    {
                        string line = sr.ReadLine();

                        if (string.IsNullOrEmpty(line))
                        {
                            continue;
                        }

                        Match includeMatch = includeRegex.Match(line);
                        if (includeMatch.Success)
                        {
                            string includeName = includeMatch.Groups[1].Value.Trim();

                            //#if DEBUG
                            //                        byte[] rawData = null;
                            //                        if (File.Exists(includeName))
                            //                            rawData = File.ReadAllBytes(includeName);
                            //#endif
                            shaderCodes.Add(loadFile(manager.LoadRaw(includeName)));
                        }
                        else
                        {
                            code += '\n' + line;
                        }

                        Match attributeMatch = attributeRegex.Match(line);
                        if (attributeMatch.Success)
                        {
                            Attributes.Add(new AttributeInfo
                            {
                                Location = lastAttributeIndex++,
                                Name     = attributeMatch.Groups[1].Value.Trim()
                            });
                        }
                    }

                    return(code);
                }
        }
Ejemplo n.º 2
0
        private string loadFile(byte[] bytes)
        {
            if (bytes == null)
            {
                return(null);
            }

            using (MemoryStream ms = new MemoryStream(bytes))
                using (StreamReader sr = new StreamReader(ms))
                {
                    string code = string.Empty;

                    while (sr.Peek() != -1)
                    {
                        string line = sr.ReadLine();

                        Match includeMatch = includeRegex.Match(line);
                        if (includeMatch.Success)
                        {
                            string includeName = includeMatch.Groups[1].Value.Trim();

                            byte[] rawData = null;
#if DEBUG
                            if (File.Exists(includeName))
                            {
                                rawData = File.ReadAllBytes(includeName);
                            }
#endif
                            shaderCodes.Add(loadFile(manager.LoadRaw(includeName)));
                        }
                        else
                        {
                            code += '\n' + line;
                        }
                    }

                    return(code);
                }
        }
Ejemplo n.º 3
0
        private string loadFile(byte[] bytes)
        {
            if (bytes == null)
            {
                return(null);
            }

            using (MemoryStream ms = new MemoryStream(bytes))
                using (StreamReader sr = new StreamReader(ms))
                {
                    string code = string.Empty;

                    while (sr.Peek() != -1)
                    {
                        string line = sr.ReadLine();

                        if (string.IsNullOrEmpty(line))
                        {
                            continue;
                        }

                        if (line.StartsWith("#version")) // the version directive has to appear before anything else in the shader
                        {
                            shaderCodes.Add(line);
                            continue;
                        }

                        Match includeMatch = includeRegex.Match(line);
                        if (includeMatch.Success)
                        {
                            string includeName = includeMatch.Groups[1].Value.Trim();

                            //#if DEBUG
                            //                        byte[] rawData = null;
                            //                        if (File.Exists(includeName))
                            //                            rawData = File.ReadAllBytes(includeName);
                            //#endif
                            code += loadFile(manager.LoadRaw(includeName)) + '\n';
                        }
                        else
                        {
                            code += line + '\n';
                        }

                        if (Type == ShaderType.VertexShader || Type == ShaderType.VertexShaderArb)
                        {
                            Match inputMatch = shaderInputRegex.Match(line);
                            if (inputMatch.Success)
                            {
                                ShaderInputs.Add(new ShaderInputInfo
                                {
                                    Location = lastShaderInputIndex++,
                                    Name     = inputMatch.Groups[1].Value.Trim()
                                });
                            }
                        }
                    }

                    return(code);
                }
        }
Ejemplo n.º 4
0
        private string loadFile(byte[] bytes, bool mainFile)
        {
            if (bytes == null)
            {
                return(null);
            }

            using (MemoryStream ms = new MemoryStream(bytes))
                using (StreamReader sr = new StreamReader(ms))
                {
                    string code = string.Empty;

                    while (sr.Peek() != -1)
                    {
                        string line = sr.ReadLine();

                        if (string.IsNullOrEmpty(line))
                        {
                            continue;
                        }

                        if (line.StartsWith("#version")) // the version directive has to appear before anything else in the shader
                        {
                            shaderCodes.Add(line);
                            continue;
                        }

                        Match includeMatch = includeRegex.Match(line);

                        if (includeMatch.Success)
                        {
                            string includeName = includeMatch.Groups[1].Value.Trim();

                            //#if DEBUG
                            //                        byte[] rawData = null;
                            //                        if (File.Exists(includeName))
                            //                            rawData = File.ReadAllBytes(includeName);
                            //#endif
                            code += loadFile(manager.LoadRaw(includeName), false) + '\n';
                        }
                        else
                        {
                            code += line + '\n';
                        }

                        if (Type == ShaderType.VertexShader || Type == ShaderType.VertexShaderArb)
                        {
                            Match inputMatch = shaderInputRegex.Match(line);

                            if (inputMatch.Success)
                            {
                                ShaderInputs.Add(new ShaderInputInfo
                                {
                                    Location = lastShaderInputIndex++,
                                    Name     = inputMatch.Groups[1].Value.Trim()
                                });
                            }
                        }
                    }

                    if (mainFile)
                    {
                        code = loadFile(manager.LoadRaw("sh_Precision_Internal.h"), false) + "\n" + code;

                        if (isVertexShader)
                        {
                            string realMainName = "real_main_" + Guid.NewGuid().ToString("N");

                            string backbufferCode = loadFile(manager.LoadRaw("sh_Backbuffer_Internal.h"), false);

                            backbufferCode = backbufferCode.Replace("{{ real_main }}", realMainName);
                            code           = Regex.Replace(code, @"void main\((.*)\)", $"void {realMainName}()") + backbufferCode + '\n';
                        }
                    }

                    return(code);
                }
        }