Пример #1
0
        public static Shader LoadShader(ResourceFactory factory, string setName, ShaderStages stage, string entryPoint)
        {
            Shader shader = factory.CreateShader(new ShaderDescription(stage, LoadBytecode(factory, setName, stage), entryPoint));

            shader.Name = $"{setName}-{stage.ToString()}";
            return(shader);
        }
Пример #2
0
//        public static Shader LoadShader(ResourceFactory factory, string set, ShaderStages stage, string entryPoint)
//        {
//            var name = $"{set}-{stage.ToString().ToLower()}.{GetExtension(factory.BackendType)}";
//            return factory.CreateShader(new ShaderDescription(stage, ReadEmbeddedAssetBytes(name), entryPoint));
//        }

        public static byte[] LoadShaderBytes(GraphicsBackend backend, Assembly assembly, string set, ShaderStages stage)
        {
            var allNames = assembly.GetManifestResourceNames();
            var name     = $"{set}-{stage.ToString().ToLower()}.{GetExtension(backend)}";

            return(ReadEmbeddedAssetBytes(name, assembly));
        }
Пример #3
0
 public static string GetPath(string setName, ShaderStages stage)
 {
     return(Path.Combine(
                AppContext.BaseDirectory,
                "Shaders",
                $"{setName}.{stage.ToString().ToLowerInvariant().Substring(0, 4)}"));
 }
Пример #4
0
        public static Shader LoadShader(ResourceFactory factory, string set, ShaderStages stage, string entryPoint)
        {
            string path = Path.Combine(
                AppContext.BaseDirectory,
                "Shaders",
                $"{set}-{stage.ToString().ToLower()}.{GetExtension(factory.BackendType)}");

            return(factory.CreateShader(new ShaderDescription(stage, File.ReadAllBytes(path), entryPoint)));
        }
Пример #5
0
        private Veldrid.Shader LoadShader(ResourceFactory factory, string set, ShaderStages stage, string entryPoint)
        {
            string name       = $"{set}-{stage.ToString().ToLower()}.{GetExtension(factory.BackendType)}";
            var    assetBytes = ReadEmbeddedAssetBytes(name);

            if (factory.BackendType == GraphicsBackend.Vulkan)
            {
                //Create a new compiler and new options
                var c = new ShaderCompiler();
                var o = new CompileOptions();

                //Set our compile options
                o.Language     = CompileOptions.InputLanguage.GLSL;
                o.Optimization = CompileOptions.OptimizationLevel.Performance;

                //Compile the specified vertex shader and give it a name
                var r = c.Compile(Encoding.UTF8.GetString(assetBytes), stage == ShaderStages.Vertex ? ShaderCompiler.Stage.Vertex : ShaderCompiler.Stage.Fragment, o, stage == ShaderStages.Vertex ? "VS" : "PS");

                //Check if we had any compilation errors
                if (r.CompileStatus != CompileResult.Status.Success)
                {
                    //Write the error out
                    System.Console.WriteLine(r.ErrorMessage);
                    throw new Exception("Cannot compile Vulkan shader");
                }

                //Get the produced SPV bytecode
                assetBytes = r.GetBytes();
            }

            var hash = 0l;

            using (var sha = SHA256Managed.Create())
            {
                using (var stream = new MemoryStream(assetBytes))
                {
                    var rawHash = sha.ComputeHash(stream);
                    hash = BitConverter.ToInt64(rawHash, 0);
                }
            }

            if (stage == ShaderStages.Vertex)
            {
                _vertexHash = hash;
            }
            else
            {
                if (stage == ShaderStages.Fragment)
                {
                    _fragmentHash = hash;
                }
            }

            return(factory.CreateShader(new ShaderDescription(stage, assetBytes, entryPoint)));
        }
Пример #6
0
        public static Shader LoadShader(string name, ShaderStages stage, GraphicsDevice graphicsDevice)
        {
            string extension = null;

            switch (graphicsDevice.BackendType)
            {
            case GraphicsBackend.OpenGL:
                extension = "glsl";
                break;

            case GraphicsBackend.Metal:
                extension = "metallib";
                break;

            default: throw new System.InvalidOperationException();
            }

            string entryPoint = string.Empty;

            switch (stage)
            {
            case ShaderStages.Vertex:
                entryPoint = "VS";
                break;

            case ShaderStages.Geometry:
                entryPoint = "GS";
                break;

            case ShaderStages.Fragment:
                entryPoint = "FS";
                break;
            }
            string shaderName = String.Concat(name, stage.ToString());

            string path = Path.Combine(System.AppContext.BaseDirectory, "Shaders", $"{shaderName}.{extension}");

            //TODO: it can happend due to some effects using Geometry shaders and some dont, that a shader will not exist.
            //TODO: Think of a better solution
            if (File.Exists(path))
            {
                byte[] shaderBytes = File.ReadAllBytes(path);
                return(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(stage, shaderBytes, entryPoint)));
            }
            else
            {
                return(null);
            }
        }
Пример #7
0
		private byte[] LoadSpirvBytes(ShaderStages stage)
		{
			string name = $"VertexColor-{stage.ToString().ToLowerInvariant()}.450.glsl";
			string full = $"TestEtoVeldrid.shaders.{name}";

			// Precompiled SPIR-V bytecode can speed up program start by saving
			// the need to load text files and compile them before converting
			// the result to the final backend shader format. If they're not
			// available, though, the plain .glsl files will do just fine. Look
			// up glslangValidator to learn how to compile SPIR-V binary files.

			using (var stream = GetType().Assembly.GetManifestResourceStream(full))
			using (var reader = new BinaryReader(stream))
			{
				return reader.ReadBytes((int)stream.Length);
			}
		}
Пример #8
0
        private static byte[] LoadBytecode(ResourceFactory factory, string setName, ShaderStages stage)
        {
            string          name    = setName + "-" + stage.ToString().ToLower();
            GraphicsBackend backend = factory.BackendType;

            if (backend == GraphicsBackend.Vulkan || backend == GraphicsBackend.Direct3D11)
            {
                string bytecodeExtension = GetBytecodeExtension(backend);
                string bytecodePath      = AssetHelper.GetPath(Path.Combine("Shaders.Generated", name + bytecodeExtension));
                if (File.Exists(bytecodePath))
                {
                    return(File.ReadAllBytes(bytecodePath));
                }
            }

            string extension = GetSourceExtension(backend);
            string path      = AssetHelper.GetPath(Path.Combine("Shaders.Generated", name + extension));

            return(File.ReadAllBytes(path));
        }
Пример #9
0
        public Shader LoadShader(ResourceFactory factory, string set, ShaderStages stage, string entryPoint)
        {
            string name = $"{set}-{stage.ToString().ToLower()}.{GetExtension(factory.BackendType)}";

            return(factory.CreateShader(new ShaderDescription(stage, ReadEmbeddedAssetBytes(name), entryPoint)));
        }
        private Shader LoadShader(ShaderStages stage)
        {
            string extension = null;

            switch (GraphicsDevice.BackendType)
            {
            case GraphicsBackend.Direct3D11:
                extension = "hlsl.bytes";
                break;

            case GraphicsBackend.Vulkan:
                extension = "spv";
                break;

            case GraphicsBackend.OpenGL:
                extension = "glsl";
                break;

            case GraphicsBackend.Metal:
                extension = "metallib";
                break;

            default: throw new System.InvalidOperationException();
            }

            string entryPoint = stage == ShaderStages.Vertex ? "VS" : "FS";
            string path       = Path.Combine(System.AppContext.BaseDirectory, "Shaders", $"{stage.ToString()}.{extension}");

            byte[] shaderBytes = File.ReadAllBytes(path);
            return(GraphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(stage, shaderBytes, entryPoint)));
        }
Пример #11
0
 static Shader LoadShader(ShaderStages stage)
 {
     return(LoadShader(stage.ToString(), stage));
 }