Ejemplo n.º 1
0
        private void UpdateReflection(ShaderBytecode shaderBytecode, EffectReflection effectReflection, LoggerResult log)
        {
            var shaderReflectionRaw     = new SharpDX.D3DCompiler.ShaderReflection(shaderBytecode);
            var shaderReflectionRawDesc = shaderReflectionRaw.Description;

            foreach (var constantBuffer in effectReflection.ConstantBuffers)
            {
                UpdateConstantBufferReflection(constantBuffer);
            }

            // Constant Buffers
            for (int i = 0; i < shaderReflectionRawDesc.ConstantBuffers; ++i)
            {
                var constantBufferRaw     = shaderReflectionRaw.GetConstantBuffer(i);
                var constantBufferRawDesc = constantBufferRaw.Description;
                if (constantBufferRawDesc.Type == SharpDX.D3DCompiler.ConstantBufferType.ResourceBindInformation)
                {
                    continue;
                }

                var linkBuffer = effectReflection.ConstantBuffers.First(buffer => buffer.Name == constantBufferRawDesc.Name);

                ValidateConstantBufferReflection(constantBufferRaw, ref constantBufferRawDesc, linkBuffer, log);
            }

            // BoundResources
            for (int i = 0; i < shaderReflectionRawDesc.BoundResources; ++i)
            {
                var boundResourceDesc = shaderReflectionRaw.GetResourceBindingDescription(i);

                string linkKeyName   = null;
                string resourceGroup = null;
                string logicalGroup  = null;
                foreach (var linkResource in effectReflection.ResourceBindings)
                {
                    if (linkResource.RawName == boundResourceDesc.Name && linkResource.Stage == ShaderStage.None)
                    {
                        linkKeyName   = linkResource.KeyInfo.KeyName;
                        resourceGroup = linkResource.ResourceGroup;
                        logicalGroup  = linkResource.LogicalGroup;
                        break;
                    }
                }

                if (linkKeyName == null)
                {
                    log.Error($"Resource [{boundResourceDesc.Name}] has no link");
                }
                else
                {
                    var binding = GetResourceBinding(boundResourceDesc, linkKeyName, log);
                    binding.Stage         = shaderBytecode.Stage;
                    binding.ResourceGroup = resourceGroup;
                    binding.LogicalGroup  = logicalGroup;

                    effectReflection.ResourceBindings.Add(binding);
                }
            }
        }
Ejemplo n.º 2
0
        CompiledShaderReader(string fn,string shaderType,ConstantBuffer[] cbs , ShaderParameterDescription[] pds,InputBindingDescription[] rbs,ShaderReflection reflec,ShaderBytecode bytec)
        {
            cBuffers = cbs;
            parameterDescriptions = pds;
            resourceBindings = rbs;
            filename = fn;
            typePrefix = shaderType;

            reflector = reflec;
            bytecode = bytec;
        }
Ejemplo n.º 3
0
        private void UpdateReflection(ShaderBytecode shaderBytecode, EffectReflection effectReflection, LoggerResult log)
        {
            var shaderReflectionRaw = new SharpDX.D3DCompiler.ShaderReflection(shaderBytecode);
            var shaderReflectionRawDesc = shaderReflectionRaw.Description;

            // Constant Buffers
            for (int i = 0; i < shaderReflectionRawDesc.ConstantBuffers; ++i)
            {
                var constantBufferRaw = shaderReflectionRaw.GetConstantBuffer(i);
                var constantBufferRawDesc = constantBufferRaw.Description;
                var linkBuffer = effectReflection.ConstantBuffers.FirstOrDefault(buffer => buffer.Name == constantBufferRawDesc.Name && buffer.Stage == ShaderStage.None);

                var constantBuffer = GetConstantBufferReflection(constantBufferRaw, ref constantBufferRawDesc, linkBuffer, log);
                constantBuffer.Stage = shaderBytecode.Stage;
                effectReflection.ConstantBuffers.Add(constantBuffer);
            }

            // BoundResources
            for (int i = 0; i < shaderReflectionRawDesc.BoundResources; ++i)
            {
                var boundResourceDesc = shaderReflectionRaw.GetResourceBindingDescription(i);

                string linkKeyName = null;
                foreach (var linkResource in effectReflection.ResourceBindings)
                {
                    if (linkResource.Param.RawName == boundResourceDesc.Name && linkResource.Stage == ShaderStage.None)
                    {
                        linkKeyName = linkResource.Param.KeyName;
                        break;
                    }

                }

                if (linkKeyName == null)
                {
                    log.Error("Resource [{0}] has no link", boundResourceDesc.Name);
                }
                else
                {

                    var binding = GetResourceBinding(boundResourceDesc, linkKeyName, log);
                    binding.Stage = shaderBytecode.Stage;

                    effectReflection.ResourceBindings.Add(binding);
                }
            }
        }
Ejemplo n.º 4
0
        private void UpdateReflection(ShaderBytecode shaderBytecode, EffectReflection effectReflection, LoggerResult log)
        {
            var shaderReflectionRaw = new SharpDX.D3DCompiler.ShaderReflection(shaderBytecode);
            var shaderReflectionRawDesc = shaderReflectionRaw.Description;

            // Constant Buffers
            for (int i = 0; i < shaderReflectionRawDesc.ConstantBuffers; ++i)
            {
                var constantBufferRaw = shaderReflectionRaw.GetConstantBuffer(i);
                var constantBufferRawDesc = constantBufferRaw.Description;
                var linkBuffer = effectReflection.ConstantBuffers.FirstOrDefault(buffer => buffer.Name == constantBufferRawDesc.Name && buffer.Stage == ShaderStage.None);

                var constantBuffer = GetConstantBufferReflection(constantBufferRaw, ref constantBufferRawDesc, linkBuffer, log);
                constantBuffer.Stage = shaderBytecode.Stage;
                effectReflection.ConstantBuffers.Add(constantBuffer);
            }

            // BoundResources
            for (int i = 0; i < shaderReflectionRawDesc.BoundResources; ++i)
            {
                var boundResourceDesc = shaderReflectionRaw.GetResourceBindingDescription(i);

                string linkKeyName = null;
                foreach (var linkResource in effectReflection.ResourceBindings)
                {
                    if (linkResource.Param.RawName == boundResourceDesc.Name && linkResource.Stage == ShaderStage.None)
                    {
                        linkKeyName = linkResource.Param.KeyName;
                        break;
                    }

                }

                if (linkKeyName == null)
                {
                    log.Error("Resource [{0}] has no link", boundResourceDesc.Name);
                }
                else
                {

                    var binding = GetResourceBinding(boundResourceDesc, linkKeyName, log);
                    binding.Stage = shaderBytecode.Stage;

                    effectReflection.ResourceBindings.Add(binding);
                }
            }
        }
Ejemplo n.º 5
0
        public static CompiledShaderReader ReadCompiledShader(string path)
        {
            if (File.Exists(path) && (Path.GetExtension(path) == ".cso"))
            {

                byte[] bytes = File.ReadAllBytes(path);
                ShaderReflection reflecter = new ShaderReflection(bytes);

                ShaderBytecode bytecode = new ShaderBytecode(bytes);
                ShaderProfile profile = bytecode.GetVersion();

                ConstantBuffer[] cbuffers = new ConstantBuffer[reflecter.Description.ConstantBuffers];
                for (int i = 0; i < reflecter.Description.ConstantBuffers; i++)
                {
                    cbuffers[i] = reflecter.GetConstantBuffer(i);//this might not work
                }

                ShaderParameterDescription[] paramdescriptions = new ShaderParameterDescription[reflecter.Description.InputParameters];
                for (int i = 0; i < reflecter.Description.InputParameters; i++)
                {
                    paramdescriptions[i] = reflecter.GetInputParameterDescription(i);
                }

                InputBindingDescription[] bindings = new InputBindingDescription[reflecter.Description.BoundResources];
                for (int i = 0; i < reflecter.Description.BoundResources; i++)
                {
                    bindings[i] = reflecter.GetResourceBindingDescription(i);
                }

                ConstantBuffer[] cbuffersCOPY = cbuffers.ToArray();

                return new CompiledShaderReader(path, profile.GetTypePrefix(), cbuffersCOPY, paramdescriptions.ToArray(), bindings.ToArray(), reflecter, bytecode);

            }
            else throw new Exception("Bad path " + path);
        }
Ejemplo n.º 6
0
        public void Initialize(Device Device)
        {
            _shaderSolution=ROD_core.ShaderBinding.GetCompatibleShader(this);
            layout = new InputLayout(Device, _shaderSolution.shaders_bytecode[Shaders.VertexShader], mesh._vertexStream.vertexDefinition.GetInputElements());

            mesh.Load(Device);
            material.LoadTextures(Device);
            sampler = new SamplerState(Device, new SamplerStateDescription()
            {
                Filter = Filter.MinMagMipLinear,
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                BorderColor = new SharpDX.Color4(0,0,0,1),
                ComparisonFunction = Comparison.Never,
                MaximumAnisotropy = 16,
                MipLodBias = 0,
                MinimumLod = -float.MaxValue,
                MaximumLod = float.MaxValue
            });

            List<Shaders> actual_shaders = (from sh in _shaderSolution.shaders_bytecode select sh.Key).ToList<Shaders>();
            foreach (Shaders sh in actual_shaders)
            {
                ShaderReflection _shaderReflection = new ShaderReflection(_shaderSolution.shaders_bytecode[sh]);
                int buffers_count = _shaderReflection.Description.ConstantBuffers;
                SharpDX.Direct3D11.Buffer[] _buffers = new SharpDX.Direct3D11.Buffer[buffers_count];
                for (int i = 0; i < buffers_count; i++)
                {
                    ConstantBuffer cb_buffer = _shaderReflection.GetConstantBuffer(i);
                    _buffers[i] = new SharpDX.Direct3D11.Buffer(Device, cb_buffer.Description.Size, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
                }
                _shaderSolution.shaders_buffers[sh] = _buffers;
            }
        }
		public void ShaderReflectionMatchesDirect3DReflection(string file)
		{
			// Arrange.
			var binaryFileBytes = File.ReadAllBytes(file + ".o");
			using (var shaderBytecode = ShaderBytecode.FromStream(new MemoryStream(binaryFileBytes)))
			using (var shaderReflection = new ShaderReflection(shaderBytecode))
			{
				var desc = shaderReflection.Description;

				// Act.
				var container = BytecodeContainer.Parse(binaryFileBytes);

				// Assert.
				Assert.AreEqual(shaderReflection.BitwiseInstructionCount, 0); // TODO
				Assert.AreEqual(shaderReflection.ConditionalMoveInstructionCount, container.Statistics.MovCInstructionCount);
				Assert.AreEqual(shaderReflection.ConversionInstructionCount, container.Statistics.ConversionInstructionCount);
				Assert.AreEqual((int) shaderReflection.GeometryShaderSInputPrimitive, (int) container.Statistics.InputPrimitive);
				Assert.AreEqual(shaderReflection.InterfaceSlotCount, container.ResourceDefinition.InterfaceSlotCount);
				Assert.AreEqual((bool) shaderReflection.IsSampleFrequencyShader, container.Statistics.IsSampleFrequencyShader);
				Assert.AreEqual(shaderReflection.MoveInstructionCount, container.Statistics.MovInstructionCount);
				//Assert.AreEqual(shaderReflection.RequiresFlags, 0); // TODO

				int expectedSizeX, expectedSizeY, expectedSizeZ;
				uint actualSizeX, actualSizeY, actualSizeZ;
				shaderReflection.GetThreadGroupSize(out expectedSizeX, out expectedSizeY, out expectedSizeZ);
				container.Shader.GetThreadGroupSize(out actualSizeX, out actualSizeY, out actualSizeZ);
				Assert.AreEqual(expectedSizeX, actualSizeX);
				Assert.AreEqual(expectedSizeY, actualSizeY);
				Assert.AreEqual(expectedSizeZ, actualSizeZ);

				//Assert.AreEqual((int) shaderReflection.MinFeatureLevel, 0); // TODO

				Assert.AreEqual(desc.ArrayInstructionCount, container.Statistics.ArrayInstructionCount);
				Assert.AreEqual(desc.BarrierInstructions, container.Statistics.BarrierInstructions);
				Assert.AreEqual(desc.BoundResources, container.ResourceDefinition.ResourceBindings.Count);
				Assert.AreEqual(desc.ConstantBuffers, container.ResourceDefinition.ConstantBuffers.Count);
				Assert.AreEqual(desc.ControlPoints, container.Statistics.ControlPoints);
				Assert.AreEqual(desc.Creator, container.ResourceDefinition.Creator);
				Assert.AreEqual(desc.CutInstructionCount, container.Statistics.CutInstructionCount);
				Assert.AreEqual(desc.DeclarationCount, container.Statistics.DeclarationCount);
				Assert.AreEqual(desc.DefineCount, container.Statistics.DefineCount);
				Assert.AreEqual(desc.DynamicFlowControlCount, container.Statistics.DynamicFlowControlCount);
				Assert.AreEqual(desc.EmitInstructionCount, container.Statistics.EmitInstructionCount);
				Assert.AreEqual((int) desc.Flags, (int) container.ResourceDefinition.Flags);
				Assert.AreEqual(desc.FloatInstructionCount, container.Statistics.FloatInstructionCount);
				Assert.AreEqual(desc.GeometryShaderInstanceCount, container.Statistics.GeometryShaderInstanceCount);
				Assert.AreEqual(desc.GeometryShaderMaxOutputVertexCount, container.Statistics.GeometryShaderMaxOutputVertexCount);
				Assert.AreEqual((int) desc.GeometryShaderOutputTopology, (int) container.Statistics.GeometryShaderOutputTopology);
				Assert.AreEqual((int) desc.HullShaderOutputPrimitive, (int) container.Statistics.HullShaderOutputPrimitive);
				Assert.AreEqual((int) desc.HullShaderPartitioning, (int) container.Statistics.HullShaderPartitioning);
				Assert.AreEqual(desc.InputParameters, container.InputSignature.Parameters.Count);
				Assert.AreEqual((int) desc.InputPrimitive, (int) container.Statistics.InputPrimitive);
				Assert.AreEqual(desc.InstructionCount, container.Statistics.InstructionCount);
				Assert.AreEqual(desc.InterlockedInstructions, container.Statistics.InterlockedInstructions);
				Assert.AreEqual(desc.IntInstructionCount, container.Statistics.IntInstructionCount);
				Assert.AreEqual(desc.MacroInstructionCount, container.Statistics.MacroInstructionCount);
				Assert.AreEqual(desc.OutputParameters, container.OutputSignature.Parameters.Count);
				Assert.AreEqual(desc.PatchConstantParameters, (container.PatchConstantSignature != null)
					? container.PatchConstantSignature.Parameters.Count
					: 0);
				Assert.AreEqual(desc.StaticFlowControlCount, container.Statistics.StaticFlowControlCount);
				Assert.AreEqual(desc.TempArrayCount, container.Statistics.TempArrayCount);
				Assert.AreEqual(desc.TempRegisterCount, container.Statistics.TempRegisterCount);
				Assert.AreEqual((int) desc.TessellatorDomain, (int) container.Statistics.TessellatorDomain);
				Assert.AreEqual(desc.TextureBiasInstructions, container.Statistics.TextureBiasInstructions);
				Assert.AreEqual(desc.TextureCompInstructions, container.Statistics.TextureCompInstructions);
				Assert.AreEqual(desc.TextureGradientInstructions, container.Statistics.TextureGradientInstructions);
				Assert.AreEqual(desc.TextureLoadInstructions, container.Statistics.TextureLoadInstructions);
				Assert.AreEqual(desc.TextureNormalInstructions, container.Statistics.TextureNormalInstructions);
				Assert.AreEqual(desc.TextureStoreInstructions, container.Statistics.TextureStoreInstructions);
				Assert.AreEqual(desc.UintInstructionCount, container.Statistics.UIntInstructionCount);
				//Assert.AreEqual(desc.Version, container.ResourceDefinition.Target); // TODO

				for (int i = 0; i < shaderReflection.Description.ConstantBuffers; i++)
					CompareConstantBuffer(shaderReflection.GetConstantBuffer(i),
						container.ResourceDefinition.ConstantBuffers[i]);

				for (int i = 0; i < shaderReflection.Description.BoundResources; i++)
					CompareResourceBinding(shaderReflection.GetResourceBindingDescription(i),
						container.ResourceDefinition.ResourceBindings[i]);

				for (int i = 0; i < shaderReflection.Description.InputParameters; i++)
					CompareParameter(shaderReflection.GetInputParameterDescription(i),
						container.InputSignature.Parameters[i]);

				for (int i = 0; i < shaderReflection.Description.OutputParameters; i++)
					CompareParameter(shaderReflection.GetOutputParameterDescription(i),
						container.OutputSignature.Parameters[i]);

				for (int i = 0; i < shaderReflection.Description.PatchConstantParameters; i++)
					CompareParameter(shaderReflection.GetPatchConstantParameterDescription(i),
						container.PatchConstantSignature.Parameters[i]);
			}
		}