Ejemplo n.º 1
0
        protected override AcceleratedImagingKernel CreateOutputKernel()
        {
            var kernelWriter = new KernelSourceWriter();

            kernelWriter.Write(@"

				float4 main(float2 uv  : POSITION) : SV_Target {

					return float4(
						uv.x < 0.5 && uv.y < 0.5f ? 1 : 0,
						uv.x >= 0.5f && uv.y < 0.5f ? 1 : 0,
						uv.x < 0.5f && uv.y >= 0.5f ? 1 : 0,
						uv.x >= 0.5f && uv.y >= 0.5f ? 0 : 1);
				}

			"            );

            return(AcceleratedImagingKernel.FromSource(ComputationProvider, kernelWriter).TextureSize(512, 512));
        }
Ejemplo n.º 2
0
        private static AcceleratedComputationKernel GetKernel(AccelerationDevice device)
        {
            var kernelWriter = new KernelSourceWriter();

            kernelWriter.Write(@"

				cbuffer cb : register(b0) {
					int x;
				}

				StructuredBuffer<int> b_in : register(t0);
				RWStructuredBuffer<int> b_out : register(u0);

				[numthreads(10, 1, 1)]
				void main(uint3 id : SV_DispatchThreadId) {
					b_out[id.x] = x * b_in[id.x];
				}

			"            );

            return(AcceleratedComputationKernel.FromSource(device, kernelWriter));
        }
        public static AcceleratedComputationKernel FromSource([NotNull] AccelerationDevice device, [NotNull] KernelSourceWriter source, CompilerContext context = null)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var kernel = new AcceleratedComputationKernel(device);

            context = context ?? new CompilerContext();
            kernel.Compile(source, context);
            return(kernel);
        }
Ejemplo n.º 4
0
        protected void Compile([NotNull] KernelSourceWriter source, [NotNull] CompilerContext context)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            mBytecode = ShaderBytecode.Compile(
                source.SourceCode, KernelSourceWriter.EntryPointName, GetProfileName(),
                ShaderFlags.PackMatrixRowMajor | ShaderFlags.OptimizationLevel3,
                EffectFlags.None, null, null, out var output);

            var parseRegex = new Regex(
                @"^(?:(?:\b[a-z]:|\\\\[a-z0-9 %._-]+\\[a-z0-9 $%._-]+)\\|\\?[^\\/:*?""<>|\x00-\x1F]+\\?)(?:[^\\/:*?""<>"
                + @"|\x00-\x1F]+\\)*[^\\/:*?""<>|\x00-\x1F]*Shader@0x[a-f0-9]{8}\((\d+),(\d+)\):\s((?:warning)|(?:error))\"
                + @"sX(\d+):\s(.*?)$", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            var matches  = parseRegex.Matches(output).OfType <Match>();
            var messages =
                from match in matches

                let lineNumber = match.Groups[1].Value.TryDeserialize <int>()
                                 let columnNumber                                                        = match.Groups[2].Value.TryDeserialize <int>()
                                                                          let type                       = match.Groups[3].Value.TryDeserialize <LogMessageClass>()
                                                                                                let code = match.Groups[4].Value.TryDeserialize <uint>()
                                                                                                           let message = match.Groups[5].Value

                                                                                                                         select new
            {
                Type = type,
                Text = message                         //match.Groups[0].Value
            };

            var hasError = false;
            var errors   = new List <string>();

            foreach (var message in messages)
            {
                context.Writer.Write(message.Text, message.Type);

                if (message.Type == LogMessageClass.Error)
                {
                    hasError = true;
                    errors.Add(message.Text);
                }
            }

            if (!hasError)
            {
                OnBytecodeLoaded();
                return;
            }

            mBytecode?.Dispose();
            mBytecode = null;

            throw new Exception($"The source code contains errors:\r\n{string.Join("\r\n", errors)}");
        }
Ejemplo n.º 5
0
        public static IAcceleratedImagingKernelConfiguration FromSource([NotNull] AccelerationDevice device, [NotNull] KernelSourceWriter source, CompilerContext context = null)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            context = context ?? new CompilerContext();
            return(new AcceleratedImagingKernelConfiguration(device, k => k.Compile(source, context)));
        }