Beispiel #1
0
        private static TagPreviewViewModel GetSpasPreview(ShaderPassTag spas)
        {
            var preview = new TagPreviewViewModel();

            var shaderCount = 0;

            foreach (var wrapper in spas.Wrapper1s)
            {
                foreach (var wrapper2 in wrapper.Wrapper2s)
                {
                    foreach (var shader in wrapper2.ShaderReferenceGroups1)
                    {
                        if (shader.ShaderData1?.Length > 0)
                        {
                            preview.AddItem($"Shader {shaderCount++}", shader.ShaderData1, GetShaderPreview);
                        }

                        if (shader.ShaderData2?.Length > 0)
                        {
                            preview.AddItem($"Shader {shaderCount++}", shader.ShaderData2, GetShaderPreview);
                        }

                        if (shader.ShaderData3?.Length > 0)
                        {
                            preview.AddItem($"Shader {shaderCount++}", shader.ShaderData3, GetShaderPreview);
                        }
                    }
                }
            }

            return(preview);
        }
Beispiel #2
0
        private static TagPreviewViewModel GetVertexPreview(VertexShaderTag vert)
        {
            var preview = new TagPreviewViewModel();

            var shaderCount = 0;

            foreach (var shader in vert.Shaders)
            {
                if (shader.ShaderData?.Length > 0)
                {
                    preview.AddItem($"Shader {shaderCount++}", shader.ShaderData, GetShaderPreview);
                }
            }

            return(preview);
        }
Beispiel #3
0
        private static TagPreviewViewModel GetBitmPreview(BitmapTag bitm)
        {
            var preview = new TagPreviewViewModel();

            preview.AddItem("bitmap", bitm, GetBitmapPreview);

            return(preview);

            object GetBitmapPreview(BitmapTag bitm)
            {
                // HACK: hard coding texture 0, needs texture binder rewrite to support here
                if (bitm.TextureInfos[0].Width == 0 || bitm.TextureInfos[0].Height == 0)
                {
                    return(null);
                }

                var textureBinder = new OpenGLTextureBinder();

                var settings = new GameWindowSettings();

                var nsettings = new NativeWindowSettings()
                {
                    API = ContextAPI.OpenGL,
                    AutoLoadBindings = true,
                    Size             = new OpenTK.Mathematics.Vector2i(bitm.TextureInfos[0].Width, bitm.TextureInfos[0].Height),
                    Title            = "OpenH2",
                    Flags            = ContextFlags.Debug | ContextFlags.Offscreen,
                    APIVersion       = new Version(4, 0)
                };

                var window = new GameWindow(settings, nsettings);

                window.IsVisible = false;
                window.MakeCurrent();

                GL.Enable(EnableCap.DebugOutput);
                GL.DebugMessageCallback(callback, (IntPtr.Zero));

                GL.Enable(EnableCap.DepthTest);
                GL.Enable(EnableCap.CullFace);
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

                var meshId           = UploadQuadMesh();
                var matriciesUniform = new GlobalUniform()
                {
                    ProjectionMatrix = Matrix4x4.CreateOrthographic(2f, 2f, 0, 10),
                    ViewMatrix       = Matrix4x4.Identity,
                    ViewPosition     = Vector3.Zero
                };

                var shader = ShaderCompiler.CreateShader(Shader.TextureViewer);

                var handle = textureBinder.GetOrBind(bitm, out var _);

                GL.ActiveTexture(TextureUnit.Texture0);
                GL.BindTexture(TextureTarget.Texture2D, handle);

                GL.ClearColor(0f, 0f, 0f, 1f);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                GL.UseProgram(shader);

                GL.GenBuffers(1, out uint MatriciesUniformHandle);

                GL.BindBuffer(BufferTarget.UniformBuffer, MatriciesUniformHandle);

                GL.BufferData(BufferTarget.UniformBuffer, GlobalUniform.Size, ref matriciesUniform, BufferUsageHint.DynamicDraw);

                GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, MatriciesUniformHandle);
                GL.BindBuffer(BufferTarget.UniformBuffer, 0);

                GL.BindVertexArray(meshId);

                GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0);

                GL.Flush();

                var bmp = new WriteableBitmap(new PixelSize(bitm.TextureInfos[0].Width, bitm.TextureInfos[0].Height), new Avalonia.Vector(72, 72), Avalonia.Platform.PixelFormat.Rgba8888);

                using (var buf = bmp.Lock())
                {
                    GL.ReadPixels(0, 0, bitm.TextureInfos[0].Width, bitm.TextureInfos[0].Height, PixelFormat.Rgba, PixelType.UnsignedByte, buf.Address);
                }

                window.Close();
                window.Dispose();

                return(bmp);
            }
        }