Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            isRun_ = false;
            renderTask_.Wait();

            MyGPUProfiler.Dispose();
            scene_.Dispose();
            ShaderManager.Dispose();
            GraphicsCore.Dispose();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// GPUにセットアップをかける
        /// </summary>
        public void Setup(GraphicsContext context)
        {
            // シェーダ
            ShaderManager.BindShader(context, shader_inst_);

            // マテリアルパラメータ
            ShaderManager.SetMaterialParam(this);

            // テクスチャ
            for (int i = 0; i < psShaderViews_.Length; i++)
            {
                if (psShaderViews_[i] == null)
                {
                    break;
                }
                context.SetShaderResourcePS(i, psShaderViews_[i]);
                Texture tex = psShaderViews_[i] as Texture;
                if (tex != null)
                {
                    GraphicsCore.SetSamplerStatePS(i, tex.AddressingModeU, tex.AddressingModeV);
                }
            }
            for (int i = 0; i < vsShaderViews_.Length; i++)
            {
                if (vsShaderViews_[i] == null)
                {
                    break;
                }
                context.SetShaderResourceVS(i, vsShaderViews_[i]);
                Texture tex = vsShaderViews_[i] as Texture;
                if (tex != null)
                {
                    GraphicsCore.SetSamplerStateVS(i, tex.AddressingModeU, tex.AddressingModeV);
                }
            }

            // レンダーステート
            GraphicsCore.SetDepthState(DepthState);
            GraphicsCore.SetBlendState(BlendState);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        public void InitializeRenderer()
        {
#if true
            GraphicsCore.Initialize(renderCtrl);
            Input.Initialize(renderCtrl);
#else
            int width  = (int)RenderImage.DesiredSize.Width;
            int height = (int)RenderImage.DesiredSize.Height;
            width  = (width < 128) ? 128 : width;
            height = (height < 128) ? 128 : height;
            GraphicsCore.InitializeForWPF(RenderImage);
            Input.Initialize(RenderImage);
#endif
            // シェーダ
            ShaderManager.Initialize("asset/shader/shader.lst");
            ShaderManager.DefaultShader = "Unlit";

            // プロファイラ
            MyGPUProfiler.Initialize();

            // ライト
            GraphicsCore.LightPos = new Vector4(0, 100000, 0, 1);

            // シーン
            scene_ = new GameScene();
            //scene_ = new TestScene();
            DataContext = scene_.ViewModel;

#if true
            // リサイズコールバック
            bool requireResize = false;
            renderCtrl.SizeChanged += (o, e) => {
                if (renderCtrl.Width == 0 || renderCtrl.Height == 0)
                {
                    return;
                }
                requireResize = true;
            };

            // レンダースレッド
            isRun_      = true;
            renderTask_ = new Task(() => {
                while (isRun_)
                {
                    // リサイズ
                    if (requireResize)
                    {
                        GraphicsCore.ResizeTargetPanel((int)renderCtrl.Width, (int)renderCtrl.Height);
                        scene_.ScreenSizeChanged((int)renderCtrl.Width, (int)renderCtrl.Height);
                        requireResize = false;
                    }

                    // 描画
                    var context = GraphicsCore.ImmediateContext;
                    scene_.Update();
                    MyGPUProfiler.BeginFrameProfiling(context);
                    scene_.Draw();
                    MyGPUProfiler.EndFrameProfiling(context);
                    GraphicsCore.Present();
                    UpdateProfiler();
                }
                ;
            });
            renderTask_.Start();
#else
            // リサイズコールバック
            RenderImage.SizeChanged += (o, e) => {
                if (e.NewSize.Width == 0 || e.NewSize.Height == 0)
                {
                    return;
                }
                GraphicsCore.ResizeTargetPanel((int)e.NewSize.Width, (int)e.NewSize.Height);
                scene_.ScreenSizeChanged((int)e.NewSize.Width, (int)e.NewSize.Height);
            };
            // 初回描画後に一回テクスチャを更新
            ContentRendered += (o, e) => {
                GraphicsCore.ResizeTargetPanel((int)RenderImage.DesiredSize.Width, (int)RenderImage.DesiredSize.Height);
                scene_.ScreenSizeChanged((int)RenderImage.DesiredSize.Width, (int)RenderImage.DesiredSize.Height);
            };

            // 描画コールバック設定
            //ComponentDispatcher.ThreadIdle += (o, e) => {
            CompositionTarget.Rendering += (o, e) => {
                scene_.Update();
                MyGPUProfiler.BeginFrameProfiling(GraphicsCore.D3dCurrentContext);
                scene_.Draw();
                MyGPUProfiler.EndFrameProfiling(GraphicsCore.D3dCurrentContext);
                GraphicsCore.Present();
                UpdateProfiler();
            };
#endif
        }
Ejemplo n.º 4
0
        /// <summary>
        /// キューブマップにレンダリング
        /// </summary>
        public static void RenderingCubeMap(GraphicsContext context, Texture cubemap, Texture depth, Vector3 position, Action sceneDrawFunc)
        {
            GraphicsCore.FrameBuffer frameBuffer = new GraphicsCore.FrameBuffer()
            {
                color_buffer_ = new Texture[1] {
                    cubemap
                },
                depth_stencil_   = depth,
                is_array_buffer_ = true,
            };

            Camera backupCamera = GraphicsCore.CurrentDrawCamera;
            Camera camera       = new Camera();
            // 右手対応のためx反転している
            Matrix projection = Matrix.PerspectiveFovRH((float)System.Math.PI / 2.0f, 1.0f, 0.1f, 1000.0f) * Matrix.Scaling(-1, 1, 1);
            Matrix view       = Matrix.LookAtRH(position, Vector3.Zero, Vector3.UnitZ);

            camera.InitializeExternal(view, projection);
            GraphicsCore.CurrentDrawCamera = camera;
            GraphicsCore.SetRasterizerState(RenderState.RasterizerState.CullFront);

            // 各面をレンダリング
            for (int i = 0; i < 6; ++i)
            {
                Vector3 lookAt = new Vector3();
                Vector3 upVec  = new Vector3();
                switch (i)
                {
                case 0:
                    lookAt = new Vector3(1.0f, 0.0f, 0.0f);
                    upVec  = new Vector3(0.0f, 1.0f, 0.0f);
                    break;

                case 1:
                    lookAt = new Vector3(-1.0f, 0.0f, 0.0f);
                    upVec  = new Vector3(0.0f, 1.0f, 0.0f);
                    break;

                case 2:
                    lookAt = new Vector3(0.0f, 1.0f, 0.0f);
                    upVec  = new Vector3(0.0f, 0.0f, -1.0f);
                    break;

                case 3:
                    lookAt = new Vector3(0.0f, -1.0f, 0.0f);
                    upVec  = new Vector3(0.0f, 0.0f, 1.0f);
                    break;

                case 4:
                    lookAt = new Vector3(0.0f, 0.0f, 1.0f);
                    upVec  = new Vector3(0.0f, 1.0f, 0.0f);
                    break;

                case 5:
                    lookAt = new Vector3(0.0f, 0.0f, -1.0f);
                    upVec  = new Vector3(0.0f, 1.0f, 0.0f);
                    break;
                }

                frameBuffer.array_buffer_index_ = i;
                view = Matrix.LookAtRH(position, position + lookAt, upVec);
                camera.SetViewMatrix(ref view);
                camera.Update();

                // TODO:要対応
                //GraphicsCore.BeginRender(frameBuffer);
                //sceneDrawFunc();
                //GraphicsCore.EndRender();
            }
            GraphicsCore.CurrentDrawCamera = backupCamera;
            GraphicsCore.SetRasterizerState(RenderState.RasterizerState.CullBack);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 描画
        /// </summary>
        public void Draw()
        {
            var context = GraphicsCore.ImmediateContext;

            UpdateDraw();

            {
                // GBuffer
                using (new ksGpuProfilePoint(context, "Render GBuffer")) {
                    MyRenderer.CurrentDrawCamera = MyRenderer.Camera3D;
                    RenderGBuffer();
                }

                var framebuffer = new MyRenderer.FrameBuffer();
                var edgeBuffer  = hdrResolveBuffer_;
                // アウトライン検出
                using (new ksGpuProfilePoint(context, "Outline")) {
                    framebuffer.color_buffer_ = new Texture[] { hdrResolveBuffer_ };
                    context.SetRenderTargets(framebuffer.color_buffer_, framebuffer.depth_stencil_);
                    prim_.GetMaterial().SetShader("Outline");
                    prim_.GetMaterial().SetShaderViewPS(0, gbuffers_[0]);
                    prim_.GetMaterial().SetShaderViewPS(1, gbuffers_[1]);
                    prim_.GetMaterial().BlendState = RenderState.BlendState.None;
                    prim_.Draw(context);

                    //framebuffer.color_buffer_ = new Texture[] { gbuffers_[1] };
                    //ksRenderer.BeginRender(framebuffer);
                    //fxaaPrim_.GetMaterial().SetShaderViewPS(0, hdrResolveBuffer_);
                    //fxaaPrim_.Draw();
                    //ksRenderer.EndRender();
                    //edgeBuffer = gbuffers_[1];
                }

                // vertexIDによるスプライト描画
                using (new ksGpuProfilePoint(context, "LineSprite")) {
                    framebuffer.color_buffer_ = new Texture[] { gbuffers_[0] };
                    context.SetRenderTargets(framebuffer.color_buffer_, framebuffer.depth_stencil_);
                    if (test)
                    {
                        MyRenderer.D3D11ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding());
                        lineSpriteMaterial_.SetShaderViewVS(0, edgeBuffer);
                        lineSpriteMaterial_.SetShaderViewPS(0, lineTexture_);
                        lineSpriteMaterial_.DepthState = RenderState.DepthState.None;
                        lineSpriteMaterial_.BlendState = RenderState.BlendState.Normal;
                        lineSpriteMaterial_.Setup(context);
                        Matrix ident = Matrix.Identity;
                        ShaderManager.SetUniformParams(ref ident);
                        MyRenderer.SetRasterizerState(RenderState.RasterizerState.CullNone);
                        int instance = hdrResolveBuffer_.Width * hdrResolveBuffer_.Height;
                        MyRenderer.D3D11ImmediateContext.DrawInstanced(6, instance, 0, 0);
                    }
                    else
                    {
                        prim_.GetMaterial().SetShader("Direct");
                        prim_.GetMaterial().SetShaderViewPS(0, hdrResolveBuffer_);
                        prim_.GetMaterial().BlendState = RenderState.BlendState.Normal;
                        prim_.Draw(context);
                    }
                }


                // 最終レンダリング
                MyRenderer.BeginRender();
                {
                    // ライティング結果
                    using (new ksGpuProfilePoint(context, "FXAA")) {
                        fxaaPrim_.GetMaterial().SetShaderViewPS(0, gbuffers_[0]);
                        fxaaPrim_.GetMaterial().BlendState = RenderState.BlendState.None;
                        fxaaPrim_.Draw(context);
                    }
                }
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 描画関連の更新
 /// </summary>
 public void UpdateDraw()
 {
     // レンダラ更新
     MyRenderer.Update();
 }