public static ShaderInfo LoadShaderProgram(BfshaLibrary.ShaderModel shaderModel, BfshaLibrary.ShaderVariation variation)
        {
            var shaderData = variation.BinaryProgram.ShaderInfoData;

            var    vertexData = GetShaderData(shaderData.VertexShaderCode);
            var    fragData   = GetShaderData(shaderData.PixelShaderCode);
            string fragHash   = GetHashSHA1(fragData);
            string vertHash   = GetHashSHA1(vertexData);

            string key = $"{vertHash}_{fragHash}";

            if (GLShaderPrograms.ContainsKey(key))
            {
                return(new ShaderInfo()
                {
                    Program = GLShaderPrograms[key],
                    VertexConstants = GetConstants(shaderData.VertexShaderCode),
                    PixelConstants = GetConstants(shaderData.PixelShaderCode),
                    FragPath = $"ShaderCache/{fragHash}.frag",
                    VertPath = $"ShaderCache/{vertHash}.vert",
                });
            }

            if (!Directory.Exists($"ShaderCache"))
            {
                Directory.CreateDirectory("ShaderCache");
            }

            if (!File.Exists($"ShaderCache/{vertHash}.vert"))
            {
                File.WriteAllText($"ShaderCache/{vertHash}.vert",
                                  DecompileShader(BfshaLibrary.ShaderType.VERTEX, vertexData));
            }
            if (!File.Exists($"ShaderCache/{fragHash}.frag"))
            {
                File.WriteAllText($"ShaderCache/{fragHash}.frag",
                                  DecompileShader(BfshaLibrary.ShaderType.PIXEL, fragData));
            }

            //Load the source to opengl
            var program = new ShaderProgram(
                new FragmentShader(File.ReadAllText($"ShaderCache/{fragHash}.frag")),
                new VertexShader(File.ReadAllText($"ShaderCache/{vertHash}.vert")));

            GLShaderPrograms.Add(key, program);

            return(new ShaderInfo()
            {
                Program = program,
                VertexConstants = GetConstants(shaderData.VertexShaderCode),
                PixelConstants = GetConstants(shaderData.PixelShaderCode),
                FragPath = $"ShaderCache/{fragHash}.frag",
                VertPath = $"ShaderCache/{vertHash}.vert",
            });
        }
Esempio n. 2
0
 public RedCarpetNXRender(BfshaLibrary.ShaderModel shaderModel) : base(shaderModel)
 {
 }
        /// <summary>
        /// Called once when the renderer can be loaded from a given shader model and mesh.
        /// </summary>
        public void OnLoad(BfshaLibrary.ShaderModel shaderModel, FMDL model, FSHP mesh, BfresMeshAsset meshAsset)
        {
            var shapeBlock = shaderModel.UniformBlocks.Values.FirstOrDefault(x =>
                                                                             x.Type == BfshaLibrary.UniformBlock.BlockType.Shape);

            //Models may update the shape block outside the shader if the shape block is unused so update mesh matrix manually
            if (shapeBlock.Size == 0 && mesh.VertexSkinCount == 0)
            {
                mesh.UpdateVertexBuffer(true);
                meshAsset.UpdateVertexBuffer();
            }

            //Assign some necessary data
            meshAsset.MaterialAsset = this;

            //Force reload from material editing
            mesh.ShaderReload += delegate
            {
                Console.WriteLine($"Reloading shader program {meshAsset.Name}");
                this.ReloadRenderState(meshAsset);
                this.ReloadProgram(meshAsset);
                mesh.HasValidShader = this.HasValidProgram;

                Console.WriteLine($"Program Validation: {this.HasValidProgram}");
                this.UpdateShader = true;
            };

            ShaderModel  = shaderModel;
            MaterialData = mesh.Material;
            ParentModel  = model;
            //Load mesh function for loading the custom shader for the first time
            LoadMesh(meshAsset);
            ReloadRenderState(meshAsset);
            ReloadProgram(meshAsset);

            var bfresMaterial = (FMAT)this.MaterialData;

            //Remap the vertex layouts from shader model attributes
            if (!IsSwitch)
            {
                //GX2 shaders can be directly mapped via string and location searches
                Dictionary <string, string> attributeLocations = new Dictionary <string, string>();
                for (int i = 0; i < shaderModel.Attributes.Count; i++)
                {
                    string key = shaderModel.Attributes.GetKey(i);
                    attributeLocations.Add(key, $"{key}_0_0");
                }
                meshAsset.UpdateVaoAttributes(attributeLocations);
            }
            else
            {
                Dictionary <string, int> attributeLocations = new Dictionary <string, int>();
                for (int i = 0; i < shaderModel.Attributes.Count; i++)
                {
                    string key      = shaderModel.Attributes.GetKey(i);
                    int    location = shaderModel.Attributes[i].Location;
                    attributeLocations.Add(key, location);
                }
                meshAsset.UpdateVaoAttributes(attributeLocations);
            }
        }
 public BfshaRenderer(BfshaLibrary.ShaderModel shaderModel)
 {
     ShaderModel = shaderModel;
 }
 public SMORenderer(BfshaLibrary.ShaderModel shaderModel) : base(shaderModel)
 {
 }
Esempio n. 6
0
 public RedPro2NXRender(BfshaLibrary.ShaderModel shaderModel) : base(shaderModel)
 {
 }
Esempio n. 7
0
 public ACNHNXRender(BfshaLibrary.ShaderModel shaderModel) : base(shaderModel)
 {
 }