Beispiel #1
0
        public Effect(
            GraphicsDevice graphicsDevice,
            string shaderName,
            params VertexLayoutDescription[] vertexDescriptors)
        {
            GraphicsDevice = graphicsDevice;

            ID = _nextID++;

            var shaderCodeExtension = GetBytecodeExtension(graphicsDevice.BackendType);

            Shader CreateShader(ShaderStages shaderStage, string entryPoint)
            {
                var embeddedResourceName = $"OpenSage.Graphics.Shaders.Compiled.{shaderName}-{shaderStage.ToString().ToLowerInvariant()}{shaderCodeExtension}";

                using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream(embeddedResourceName))
                {
                    var shaderBytecode = shaderStream.ReadAllBytes();
                    var shader         = graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(shaderStage, shaderBytecode, entryPoint));
                    shader.Name = shaderName;
                    return(shader);
                }
            }

            _vertexShader = AddDisposable(CreateShader(ShaderStages.Vertex, "VS"));
            _pixelShader  = AddDisposable(CreateShader(ShaderStages.Fragment, "PS"));

            _cachedPipelineStates = new Dictionary <EffectPipelineStateHandle, Pipeline>();

            _vertexDescriptors = vertexDescriptors;

            var shaderDefinition = ShaderDefinitions.GetShaderDefinition(shaderName);

            _parameters      = new Dictionary <string, EffectParameter>();
            _resourceLayouts = new ResourceLayout[shaderDefinition.ResourceBindings.Count];

            for (var i = 0u; i < shaderDefinition.ResourceBindings.Count; i++)
            {
                var resourceBinding           = shaderDefinition.ResourceBindings[(int)i];
                var resourceLayoutDescription = new ResourceLayoutElementDescription(
                    resourceBinding.Name,
                    resourceBinding.Type,
                    resourceBinding.Stages);

                var parameter = AddDisposable(new EffectParameter(
                                                  graphicsDevice,
                                                  resourceBinding,
                                                  resourceLayoutDescription,
                                                  i));

                _parameters[parameter.Name] = parameter;
                _resourceLayouts[i]         = parameter.ResourceLayout;
            }
        }
Beispiel #2
0
        public Effect(
            GraphicsDevice graphicsDevice,
            string shaderName,
            VertexLayoutDescription[] vertexDescriptors)
        {
            _graphicsDevice = graphicsDevice;

            ID = _nextID++;

            const string shaderNamespace     = "OpenSage.Graphics.Shaders.Compiled";
            var          shaderCodeExtension = GetBytecodeExtension(graphicsDevice.BackendType);

            using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream($"{shaderNamespace}.{shaderName}-vertex{shaderCodeExtension}"))
            {
                var vertexShaderBytecode = shaderStream.ReadAllBytes();
                _vertexShader = AddDisposable(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytecode, "VS")));
            }

            using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream($"{shaderNamespace}.{shaderName}-fragment{shaderCodeExtension}"))
            {
                var pixelShaderBytecode = shaderStream.ReadAllBytes();
                _pixelShader = AddDisposable(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Fragment, pixelShaderBytecode, "PS")));
            }

            _cachedPipelineStates = new Dictionary <EffectPipelineStateHandle, Pipeline>();

            _vertexDescriptors = vertexDescriptors;

            var shaderDefinition = ShaderDefinitions.GetShaderDefinition(shaderName);

            _parameters      = new Dictionary <string, EffectParameter>();
            _resourceLayouts = new ResourceLayout[shaderDefinition.ResourceBindings.Length];

            for (var i = 0u; i < shaderDefinition.ResourceBindings.Length; i++)
            {
                var resourceBinding           = shaderDefinition.ResourceBindings[i];
                var resourceLayoutDescription = new ResourceLayoutElementDescription(
                    resourceBinding.Name,
                    resourceBinding.Type,
                    resourceBinding.Stages);

                var parameter = AddDisposable(new EffectParameter(
                                                  graphicsDevice,
                                                  resourceBinding,
                                                  resourceLayoutDescription,
                                                  i));

                _parameters[parameter.Name] = parameter;
                _resourceLayouts[i]         = parameter.ResourceLayout;
            }
        }
Beispiel #3
0
        public Effect(
            GraphicsDevice graphicsDevice,
            string shaderName,
            VertexLayoutDescription[] vertexDescriptors,
            bool useNewShaders = false)
        {
            _graphicsDevice = graphicsDevice;

            ID = _nextID++;

            if (useNewShaders)
            {
                using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream($"OpenSage.Graphics.Shaders.Compiled.{shaderName}-vertex.hlsl.bytes"))
                {
                    var vertexShaderBytecode = shaderStream.ReadAllBytes();
                    _vertexShader = AddDisposable(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytecode, "VS")));
                }

                using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream($"OpenSage.Graphics.Shaders.Compiled.{shaderName}-fragment.hlsl.bytes"))
                {
                    var pixelShaderBytecode = shaderStream.ReadAllBytes();
                    _pixelShader = AddDisposable(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Fragment, pixelShaderBytecode, "PS")));
                }
            }
            else
            {
                using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream($"OpenSage.Graphics.Shaders.{shaderName}.fxo"))
                    using (var shaderReader = new BinaryReader(shaderStream))
                    {
                        var vertexShaderBytecodeLength = shaderReader.ReadInt32();
                        var vertexShaderBytecode       = shaderReader.ReadBytes(vertexShaderBytecodeLength);
                        _vertexShader = AddDisposable(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytecode, "VS")));

                        var pixelShaderBytecodeLength = shaderReader.ReadInt32();
                        var pixelShaderBytecode       = shaderReader.ReadBytes(pixelShaderBytecodeLength);
                        _pixelShader = AddDisposable(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Fragment, pixelShaderBytecode, "PS")));
                    }
            }

            _cachedPipelineStates = new Dictionary <EffectPipelineStateHandle, Pipeline>();

            _vertexDescriptors = vertexDescriptors;

            var shaderDefinition = ShaderDefinitions.GetShaderDefinition(shaderName);

            _parameters      = new Dictionary <string, EffectParameter>();
            _resourceLayouts = new ResourceLayout[shaderDefinition.ResourceBindings.Length];

            for (var i = 0u; i < shaderDefinition.ResourceBindings.Length; i++)
            {
                var resourceBinding           = shaderDefinition.ResourceBindings[i];
                var resourceLayoutDescription = new ResourceLayoutElementDescription(
                    resourceBinding.Name,
                    resourceBinding.Type,
                    resourceBinding.Stages);

                var parameter = AddDisposable(new EffectParameter(
                                                  graphicsDevice,
                                                  resourceBinding,
                                                  resourceLayoutDescription,
                                                  i));

                _parameters[parameter.Name] = parameter;
                _resourceLayouts[i]         = parameter.ResourceLayout;
            }
        }
Beispiel #4
0
        public Effect(
            GraphicsDevice graphicsDevice,
            string shaderName,
            params VertexLayoutDescription[] vertexDescriptors)
        {
            GraphicsDevice = graphicsDevice;

            ID = _nextID++;

            var shaderBytecodeExtension = GetBytecodeExtension(graphicsDevice.BackendType);
            var shaderTextExtension     = GetTextExtension(graphicsDevice.BackendType);
            var resources = typeof(Effect).Assembly.GetManifestResourceNames();

            Shader CreateShader(ShaderStages shaderStage, string entryPoint)
            {
                var    basename           = $"OpenSage.Graphics.Shaders.Compiled.{shaderName}-{shaderStage.ToString().ToLowerInvariant()}";
                var    bytecodeShaderName = basename + shaderBytecodeExtension;
                var    textShaderName     = basename + shaderTextExtension;
                Stream shaderStream       = null;

                //Check if we have a binary shader
                if (Array.Find(resources, s => s.Equals(bytecodeShaderName)) != null)
                {
                    shaderStream = typeof(Effect).Assembly.GetManifestResourceStream(bytecodeShaderName);
                }
                //We only have a text shader
                else
                {
                    shaderStream = typeof(Effect).Assembly.GetManifestResourceStream(textShaderName);
                }

#if DEBUG
                const bool debug = true;
#else
                const bool debug = false;
#endif

                var shaderBytes = shaderStream.ReadAllBytes();
                shaderStream.Dispose();
                var shader = graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(shaderStage, shaderBytes, entryPoint, debug));
                shader.Name = shaderName;
                return(shader);
            }

            _vertexShader = AddDisposable(CreateShader(ShaderStages.Vertex, "VS"));
            _pixelShader  = AddDisposable(CreateShader(ShaderStages.Fragment, "PS"));

            _cachedPipelineStates = new Dictionary <EffectPipelineStateHandle, Pipeline>();

            _vertexDescriptors = vertexDescriptors;

            var shaderDefinition = ShaderDefinitions.GetShaderDefinition(shaderName);

            _parameters      = new Dictionary <string, EffectParameter>();
            _resourceLayouts = new ResourceLayout[shaderDefinition.ResourceBindings.Count];

            for (var i = 0u; i < shaderDefinition.ResourceBindings.Count; i++)
            {
                var resourceBinding           = shaderDefinition.ResourceBindings[(int)i];
                var resourceLayoutDescription = new ResourceLayoutElementDescription(
                    resourceBinding.Name,
                    resourceBinding.Type,
                    resourceBinding.Stages);

                var parameter = AddDisposable(new EffectParameter(
                                                  graphicsDevice,
                                                  resourceBinding,
                                                  resourceLayoutDescription,
                                                  i));

                _parameters[parameter.Name] = parameter;
                _resourceLayouts[i]         = parameter.ResourceLayout;
            }
        }
Beispiel #5
0
        public Effect(
            GraphicsDevice graphicsDevice,
            string shaderName,
            PrimitiveTopology primitiveTopology,
            params VertexLayoutDescription[] vertexDescriptors)
        {
            _primitiveTopology = primitiveTopology;

            GraphicsDevice = graphicsDevice;

            ID = _nextID++;

#if DEBUG
            const bool debug = true;
#else
            const bool debug = false;
#endif

            ShaderDefinition shaderDefinition;

            var resources = typeof(Effect).Assembly.GetManifestResourceNames();

            byte[] ReadShader(string shaderType)
            {
                var bytecodeShaderName = $"OpenSage.Assets.Shaders.{shaderName}.{shaderType}.spv";

                using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream(bytecodeShaderName))
                {
                    return(shaderStream.ReadAllBytes());
                }
            }

            var vsBytes = ReadShader("vert");
            var fsBytes = ReadShader("frag");

            var shaders = graphicsDevice.ResourceFactory.CreateFromSpirv(
                new ShaderDescription(ShaderStages.Vertex, vsBytes, "main", debug),
                new ShaderDescription(ShaderStages.Fragment, fsBytes, "main", debug),
                new CrossCompileOptions());

            _vertexShader = AddDisposable(shaders[0]);
            _pixelShader  = AddDisposable(shaders[1]);

            _vertexShader.Name = $"{shaderName}.vert";
            _pixelShader.Name  = $"{shaderName}.frag";

            shaderDefinition = ShaderDefinitions.GetShaderDefinition(shaderName);

            _cachedPipelineStates = new Dictionary <EffectPipelineStateHandle, Pipeline>();

            _vertexDescriptors = vertexDescriptors;

            _parameters      = new Dictionary <string, EffectParameter>();
            _resourceLayouts = new ResourceLayout[shaderDefinition.ResourceBindings.Count];

            for (var i = 0u; i < shaderDefinition.ResourceBindings.Count; i++)
            {
                var resourceBinding           = shaderDefinition.ResourceBindings[(int)i];
                var resourceLayoutDescription = new ResourceLayoutElementDescription(
                    resourceBinding.Name,
                    resourceBinding.Kind,
                    resourceBinding.Stages);

                var parameter = AddDisposable(new EffectParameter(
                                                  graphicsDevice,
                                                  resourceBinding,
                                                  resourceLayoutDescription,
                                                  i));

                _parameters[parameter.Name] = parameter;
                _resourceLayouts[i]         = parameter.ResourceLayout;
            }
        }