/// <summary> /// /// </summary> /// <param name="w"></param> /// <param name="h"></param> public void ScreenSizeChanged(int w, int h) { csMgr_.ScreenSizeChanged(w, h); var bufs = new IShaderView[] { gbuffers_[0], gbuffers_[1], gbuffers_[2], GraphicsCore.DefaultDepthBuffer, lightMgr_.LightBuffer, shadowMap_.Map }; csMgr_.SetResources(bufs); // GBuffer for (int i = 0; i < gbufferDesc_.Length; i++) { gbufferDesc_[i].width = w; gbufferDesc_[i].height = h; gbuffers_[i].Dispose(); gbuffers_[i].Initialize(gbufferDesc_[i]); } deferredBuffer_.depth_stencil_ = GraphicsCore.DefaultDepthBuffer; forwardBuffer_.depth_stencil_ = GraphicsCore.DefaultDepthBuffer; uav_.Dispose(); uavDesc_.width = w; uavDesc_.height = h; uav_.Initialize(uavDesc_); hdrResolveBuffer_.Dispose(); hdrResolveDesc_.width = w; hdrResolveDesc_.height = h; hdrResolveBuffer_.Initialize(hdrResolveDesc_); prim_.GetMaterial().SetShaderViewPS(0, debugViewList_[ViewModel.ViewIndex]); // カラーバッファ作り替え fxaaPrim_.GetMaterial().SetShaderViewPS(0, hdrResolveBuffer_); }
/// <summary> /// /// </summary> /// <param name="w"></param> /// <param name="h"></param> public void ScreenSizeChanged(int w, int h) { // GBuffer for (int i = 0; i < gbufferDesc_.Length; i++) { gbufferDesc_[i].width = w; gbufferDesc_[i].height = h; gbuffers_[i].Dispose(); gbuffers_[i].Initialize(gbufferDesc_[i]); } gBufferBinder_.depth_stencil_ = MyRenderer.DefaultDepthBuffer; hdrResolveBuffer_.Dispose(); hdrResolveDesc_.width = w; hdrResolveDesc_.height = h; hdrResolveBuffer_.Initialize(hdrResolveDesc_); // カラーバッファ作り替え fxaaPrim_.GetMaterial().SetShaderViewPS(0, hdrResolveBuffer_); }
/// <summary> /// レンダーターゲット初期化 /// </summary> void InitializeTarget() { // deferred用MRTバッファ gbufferDesc_ = new Texture.InitDesc[] { new Texture.InitDesc() { bindFlag = TextureBuffer.BindFlag.IsRenderTarget, width = MyRenderer.TargetWidth, height = MyRenderer.TargetHeight, format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, }, new Texture.InitDesc() { bindFlag = TextureBuffer.BindFlag.IsRenderTarget, width = MyRenderer.TargetWidth, height = MyRenderer.TargetHeight, format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, }, }; gbuffers_ = new Texture[gbufferDesc_.Length]; for (int i = 0; i < gbuffers_.Length; i++) { gbuffers_[i] = new Texture(gbufferDesc_[i]); } gBufferBinder_ = new MyRenderer.FrameBuffer(); gBufferBinder_.color_buffer_ = gbuffers_; gBufferBinder_.depth_stencil_ = MyRenderer.DefaultDepthBuffer; hdrResolveDesc_ = new Texture.InitDesc() { bindFlag = TextureBuffer.BindFlag.IsRenderTarget, width = MyRenderer.TargetWidth, height = MyRenderer.TargetHeight, format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, }; hdrResolveBuffer_ = new Texture(hdrResolveDesc_); hdrResultBuffer_ = new MyRenderer.FrameBuffer(); hdrResultBuffer_.color_buffer_ = new Texture[] { hdrResolveBuffer_ }; hdrResultBuffer_.depth_stencil_ = null; // アンチなし用プリム Lib.Rect rect = new Lib.Rect(new Vector3(-1.0f, 1.0f, 0.0f), new Vector3(1.0f, -1.0f, 0.0f)); prim_ = new Prim("Outline", (uint)Shader.VertexAttr.TEXCOORD0); prim_.AddRect(ref rect); prim_.GetMaterial().SetShaderViewPS(0, gbuffers_[0]); prim_.GetMaterial().DepthState = RenderState.DepthState.None; // FXAA fxaaPrim_ = new Prim("FXAA", (uint)Shader.VertexAttr.TEXCOORD0); fxaaPrim_.AddRect(ref rect); fxaaPrim_.GetMaterial().SetShaderViewPS(0, hdrResolveBuffer_); fxaaPrim_.GetMaterial().DepthState = RenderState.DepthState.None; // ラインスプライト lineSpriteMaterial_ = new Material(); lineSpriteMaterial_.SetShader("LineSprite", 0); lineTexture_ = new Texture("asset/texture/circle32x32_2.png"); Lib.Shader.SetConstantBufferUpdateFunc("CB_LineSprite", (o, e) => { dynamic cb = e; cb.g_screenScale = new Vector4(hdrResolveBuffer_.Width, hdrResolveBuffer_.Height, 1.0f / hdrResolveBuffer_.Width, 1.0f / hdrResolveBuffer_.Height); return(true); }); // デバッグ表示用 debugViewList_ = new Texture[] { hdrResolveBuffer_, gbuffers_[0], gbuffers_[1], }; }
/// <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); } } } }
/// <summary> /// レンダーターゲット初期化 /// </summary> void InitializeTarget() { // deferred用MRTバッファ gbufferDesc_ = new Texture.InitDesc[] { new Texture.InitDesc() { bindFlag = TextureBuffer.BindFlag.IsRenderTarget, width = GraphicsCore.TargetWidth, height = GraphicsCore.TargetHeight, format = SlimDX.DXGI.Format.R16G16B16A16_Float, }, new Texture.InitDesc() { bindFlag = TextureBuffer.BindFlag.IsRenderTarget, width = GraphicsCore.TargetWidth, height = GraphicsCore.TargetHeight, format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, }, new Texture.InitDesc() { bindFlag = TextureBuffer.BindFlag.IsRenderTarget, width = GraphicsCore.TargetWidth, height = GraphicsCore.TargetHeight, format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, }, }; gbuffers_ = new Texture[gbufferDesc_.Length]; for (int i = 0; i < gbuffers_.Length; i++) { gbuffers_[i] = new Texture(gbufferDesc_[i]); } deferredBuffer_ = new GraphicsCore.FrameBuffer(); deferredBuffer_.color_buffer_ = gbuffers_; deferredBuffer_.depth_stencil_ = GraphicsCore.DefaultDepthBuffer; uavDesc_ = new Texture.InitDesc { bindFlag = TextureBuffer.BindFlag.IsRenderTarget | TextureBuffer.BindFlag.IsUnorderedAccess, width = GraphicsCore.TargetWidth, height = GraphicsCore.TargetHeight, format = SlimDX.DXGI.Format.R16G16B16A16_Float, }; uav_ = new Texture(uavDesc_); csMgr_.SetUAVs(new Texture[] { uav_ }); var bufs = new IShaderView[] { gbuffers_[0], gbuffers_[1], gbuffers_[2], GraphicsCore.DefaultDepthBuffer, lightMgr_.LightBuffer, shadowMap_.Map }; csMgr_.SetResources(bufs); hdrResolveDesc_ = new Texture.InitDesc() { bindFlag = TextureBuffer.BindFlag.IsRenderTarget, width = GraphicsCore.TargetWidth, height = GraphicsCore.TargetHeight, format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, }; hdrResolveBuffer_ = new Texture(hdrResolveDesc_); forwardBuffer_ = new GraphicsCore.FrameBuffer(); forwardBuffer_.color_buffer_ = new Texture[] { uav_ }; forwardBuffer_.depth_stencil_ = GraphicsCore.DefaultDepthBuffer; hdrResultBuffer_ = new GraphicsCore.FrameBuffer(); hdrResultBuffer_.color_buffer_ = new Texture[] { hdrResolveBuffer_ }; hdrResultBuffer_.depth_stencil_ = null; // キューブマップ int cubeSize = 128; globalCapture_ = new ReflectionCapture(cubeSize, new Vector3(0, 3, 0)); localCapture_ = new List <ReflectionCapture>(); giTextures_ = new Texture[3]; if (System.IO.File.Exists("asset/gitex_r.dds")) { giTextures_[0] = new Texture("asset/gitex_r.dds"); giTextures_[1] = new Texture("asset/gitex_g.dds"); giTextures_[2] = new Texture("asset/gitex_b.dds"); csMgr_.SetResources(6, giTextures_[0]); csMgr_.SetResources(7, giTextures_[1]); csMgr_.SetResources(8, giTextures_[2]); cubeMapRenderEnable_ = false; } // グリッド上で生成 if (cubeMapRenderEnable_) { Vector3 bounds = cubeMapInfo_.boundingMax - cubeMapInfo_.boundingMin; for (int z = 0; z < cubeMapInfo_.numZ; z++) { for (int y = 0; y < cubeMapInfo_.numY; y++) { for (int x = 0; x < cubeMapInfo_.numX; x++) { Vector3 add = new Vector3(bounds.X * ((float)x / (cubeMapInfo_.numX - 1)), bounds.Y * ((float)y / (cubeMapInfo_.numY - 1)), bounds.Z * ((float)z / (cubeMapInfo_.numZ - 1)) ); Vector3 pos = cubeMapInfo_.boundingMin + add; localCapture_.Add(new ReflectionCapture(cubeSize, pos)); } } } } // アンチなし用プリム Lib.Rect rect = new Lib.Rect(new Vector3(-1.0f, 1.0f, 0.0f), new Vector3(1.0f, -1.0f, 0.0f)); prim_ = new Prim("Direct", (uint)Shader.VertexAttr.TEXCOORD0); prim_.AddRect(ref rect); prim_.GetMaterial().SetShaderViewPS(0, gbuffers_[0]); prim_.GetMaterial().DepthState = RenderState.DepthState.None; // FXAA fxaaPrim_ = new Prim("FXAA", (uint)Shader.VertexAttr.TEXCOORD0); fxaaPrim_.AddRect(ref rect); fxaaPrim_.GetMaterial().SetShaderViewPS(0, hdrResolveBuffer_); fxaaPrim_.GetMaterial().DepthState = RenderState.DepthState.None; // デバッグ表示用 debugViewList_ = new Texture[] { hdrResolveBuffer_, gbuffers_[0], gbuffers_[1], gbuffers_[2], shadowMap_.Map, toneMap_.LuminanceAvgTexture, }; }
/// <summary> /// /// </summary> /// <param name="max_num"></param> public LightManager(int max_num) { int size = structSize * max_num; count_ = max_num; lightList_ = new Light[max_num]; stream_ = new DataStream(size, true, true); // 構造化バッファ生成 StructuredBuffer.BufferDesc desc = new StructuredBuffer.BufferDesc { bindUAV = true, stride = structSize, size = (int)size, initData = null, }; buffer_ = new StructuredBuffer(desc); #if true // ライトセットアップ(仮) Random rand = new Random(0); Vector3[] colorAry = new Vector3[] { new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), new Vector3(1, 1, 1), }; float scale = 0.2f; float range_x = 150 * scale; float range_y = 70 * scale; float range_z = 65 * scale; for (int i = 0; i < count_; i++) { var light = new Light(); float x = (float)(rand.NextDouble() - 0.5) * 2 * range_x; float y = (float)(rand.NextDouble() - 0.0) * 2 * range_y; float z = (float)(rand.NextDouble() - 0.5) * 2 * range_z; light.pos = new Vector3(x, y, z); light.color = colorAry[i % colorAry.Length]; light.radius = 5; light.intensity = 4.0f; SetLight(i, light); } #else var light = new Light(); light.pos = new Vector3(0, 10, 0); light.color = new Vector3(1, 1, 1); light.intensity = 80; SetLight(0, light); #endif DirectionalLightDir = new Vector3(0.3f, -1.0f, 0.2f); DirectionalLightDir.Normalize(); DirectionalLightIntensity = 2; //float primSize = 2.0f; float primSize = 0.5f; Lib.Rect rect = new Lib.Rect(new Vector3(-primSize, primSize, 0.0f), new Vector3(primSize, -primSize, 0.0f)); debugPrim_ = new Prim("DebugDrawPointLights", (uint)Shader.VertexAttr.TEXCOORD0); debugPrim_.AddRect(ref rect); debugPrim_.GetMaterial().SetShaderViewVS(0, LightBuffer); debugPrim_.GetMaterial().SetShaderViewPS(0, LightBuffer); debugPrim_.GetMaterial().BlendState = RenderState.BlendState.Add; debugPrim_.GetMaterial().DepthState = RenderState.DepthState.TestOnly; }