Exemplo n.º 1
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
        }
Exemplo n.º 2
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);
                    }
                }
            }
        }
Exemplo n.º 3
0
 private void LoadShaderMenuItem_Click(object sender, RoutedEventArgs e)
 {
     ShaderManager.Reload("asset/shader/shader.lst");
 }
Exemplo n.º 4
0
        /// <summary>
        /// キューブマップにレンダリング
        /// </summary>
        void RenderCubeMap()
        {
            var context = GraphicsCore.ImmediateContext;

            // コンスタントバッファ更新定義
            bool isInit = false;

            Shader.SetConstantBufferUpdateFunc("CB_LightParam", (s, i) => {
                if (!isInit)
                {
                    dynamic cb = i;
                    cb.g_directionalLightDir       = lightMgr_.DirectionalLightDir;
                    cb.g_directionalLightIntensity = lightMgr_.DirectionalLightIntensity;
                    cb.g_shadowMatrix = Matrix.Transpose(shadowMap_.Camera.ViewProjectionMatrix);
                    isInit            = true;
                    return(true);
                }
                return(false);
            });

            // モデルのシェーダ差し換え
            Func <Shader, Shader> overrideFunc = (o) => {
                return(ShaderManager.FindShader("ForwardRender", (uint)(Shader.VertexAttr.NORMAL | Shader.VertexAttr.TEXCOORD0) & o.NeedVertexAttr));
            };

            // 描画アクション
            Action func = () => {
                // TODO:要クリア
                //context.ClearRenderTarget(new Color4(1, 0, 0, 0));
                //context.ClearDepthStencil();
                int shadowMapBindingPoint = 4;
                context.SetShaderResourcePS(shadowMapBindingPoint, shadowMap_.Map);
                GraphicsCore.SetSamplerStatePS(shadowMapBindingPoint, shadowMap_.Map.AddressingModeU, shadowMap_.Map.AddressingModeV);
                ShaderManager.UserShaderBindHandler += overrideFunc;
                SceneDraw(context);
                ShaderManager.UserShaderBindHandler -= overrideFunc;
            };

            //globalCapture_.Capture(func, 3);

            if (localCapture_.Count > 0)
            {
                foreach (var c in localCapture_)
                {
                    c.Capture(context, func, 2);
                }

                int        size   = cubeMapInfo_.numZ * cubeMapInfo_.numY * cubeMapInfo_.numX * 4 * 2;
                DataStream data_r = new DataStream(size, true, true);
                DataStream data_g = new DataStream(size, true, true);
                DataStream data_b = new DataStream(size, true, true);
                for (int z = 0; z < cubeMapInfo_.numZ; z++)
                {
                    for (int y = 0; y < cubeMapInfo_.numY; y++)
                    {
                        for (int x = 0; x < cubeMapInfo_.numX; x++)
                        {
                            int index = (z * cubeMapInfo_.numY * cubeMapInfo_.numX) + (y * cubeMapInfo_.numX) + x;
                            var coef  = localCapture_[index].SHCoef;
                            for (int i = 0; i < 4; i++)
                            {
                                data_r.Write(Lib.Math.CPf32Tof16(coef[i].X));
                                data_g.Write(Lib.Math.CPf32Tof16(coef[i].Y));
                                data_b.Write(Lib.Math.CPf32Tof16(coef[i].Z));
                                //data_r.Write(coef[i].X);
                                //data_g.Write(coef[i].Y);
                                //data_b.Write(coef[i].Z);
                            }
                        }
                    }
                }

                var giTexDesc = new Texture.InitDesc()
                {
                    bindFlag = TextureBuffer.BindFlag.IsRenderTarget,
                    width    = cubeMapInfo_.numX,
                    height   = cubeMapInfo_.numY,
                    depth    = cubeMapInfo_.numZ,
                    format   = SlimDX.DXGI.Format.R16G16B16A16_Float,
                    //format = SlimDX.DXGI.Format.R32G32B32A32_Float,
                };
                giTexDesc.initStream = data_r;
                giTextures_[0]       = new Texture(giTexDesc);
                giTextures_[0].SaveFile("asset/gitex_r.dds");
                giTexDesc.initStream = data_g;
                giTextures_[1]       = new Texture(giTexDesc);
                giTextures_[1].SaveFile("asset/gitex_g.dds");
                giTexDesc.initStream = data_b;
                giTextures_[2]       = new Texture(giTexDesc);
                giTextures_[2].SaveFile("asset/gitex_b.dds");
                csMgr_.SetResources(6, giTextures_[0]);
                csMgr_.SetResources(7, giTextures_[1]);
                csMgr_.SetResources(8, giTextures_[2]);
            }
        }