/// <summary> /// Bound a texture to be used with imgui, the id will be used by imgui to notify us which texture to draw, bound textures will be disposed with the ui renderer /// </summary> /// <param name="texture"></param> /// <returns></returns> public IntPtr BindTexture(CTextureSampler texture) { IntPtr id = new IntPtr(m_textureId++); m_boundTextures.Add(id, texture); return(id); }
public void InitDefaultResources(DeviceContext deviceContext) { DefaultShader = RequestShaderResource(new SHashedName("SimpleLitShader")); DepthShader = RequestShaderResource(new SHashedName("depthShader")); DepthCubeShader = RequestShaderResource(new SHashedName("depthCubeShader")); DefaultTexture = new CTextureSampler(m_graphicsDevice, deviceContext, "Resources/Textures/DefaultTexture.tga"); FallbackTexture = new CTextureSampler(m_graphicsDevice, deviceContext, "Resources/Textures/MissingTexture.tga"); WhiteTexture = new CTextureSampler(m_graphicsDevice, deviceContext, "Resources/Textures/DefaultWhite.tga"); BlackTexture = new CTextureSampler(m_graphicsDevice, deviceContext, "Resources/Textures/DefaultBlack.tga"); DefaultMaterial = CMaterial.CreateDefaultMaterial(); DefaultMaterial.ShaderResource = DefaultShader; DefaultMaterial.SetTextureParameter(new SHashedName("DiffuseTexture"), WhiteTexture); DefaultMaterial.FinishLoading(); DefaultTextureMaterial = CMaterial.CreateDefaultMaterial(); DefaultTextureMaterial.ShaderResource = DefaultShader; DefaultTextureMaterial.SetTextureParameter(new SHashedName("DiffuseTexture"), DefaultTexture); DefaultTextureMaterial.FinishLoading(); MissingTextureMaterial = CMaterial.CreateDefaultMaterial(); MissingTextureMaterial.ShaderResource = DefaultShader; MissingTextureMaterial.SetTextureParameter(new SHashedName("DiffuseTexture"), FallbackTexture); MissingTextureMaterial.FinishLoading(); }
public object ToEditorValue(object materialValue) { if (materialValue == null) { return(null); } CTextureSampler textureValue = (CTextureSampler)materialValue; CAssetReference <CTextureAsset> assetReference = CAssetRegistry.Instance.GetAsset <CTextureAsset>(textureValue.Guid); return(assetReference); }
public void EndRender() { ImGui.Render(); unsafe { ImDrawDataPtr drawData = ImGui.GetDrawData(); if (drawData.CmdListsCount <= 0) { return; } UserDefinedAnnotation annotation = m_d3DeviceContext.QueryInterface <UserDefinedAnnotation>(); annotation.BeginEvent("ImguiPass"); // Grow buffers in case they are to small if (m_vertexBufferSize < drawData.TotalVtxCount || m_indexBufferSize < drawData.TotalIdxCount) { m_indexBufferSize = (int)(drawData.TotalIdxCount * 1.5f); m_vertexBufferSize = (int)(drawData.TotalVtxCount * 1.5f); CreateVertexIndexBuffer(); } m_d3DeviceContext.MapSubresource(m_vertexBuffer, MapMode.WriteDiscard, MapFlags.None, out DataStream vertexStream); m_d3DeviceContext.MapSubresource(m_indexBuffer, MapMode.WriteDiscard, MapFlags.None, out DataStream indexStream); for (int i = 0; i < drawData.CmdListsCount; i++) { ImDrawListPtr drawList = drawData.CmdListsRange[i]; IntPtr vertexPtr = drawList.VtxBuffer.Data; vertexStream.Write(vertexPtr, 0, drawList.VtxBuffer.Size * Utilities.SizeOf <ImDrawVert>()); IntPtr indexPtr = drawList.IdxBuffer.Data; indexStream.Write(indexPtr, 0, drawList.IdxBuffer.Size * Utilities.SizeOf <ushort>()); } m_d3DeviceContext.UnmapSubresource(m_vertexBuffer, 0); m_d3DeviceContext.UnmapSubresource(m_indexBuffer, 0); float offset = 0.0f; Matrix mvpMatrix = Matrix.OrthoOffCenterLH(offset, m_imguiIO.DisplaySize.X + offset, m_imguiIO.DisplaySize.Y + offset, offset, -1.0f, 1.0f); mvpMatrix.Transpose(); SShaderParameter parameter; parameter.parameterType = EShaderParameterType.Matrix; parameter.parameterData = mvpMatrix; m_shaderParameters[new SHashedName("mvpMatrix")] = parameter; SShaderParameter fontTextureParams; fontTextureParams.parameterType = EShaderParameterType.Texture; fontTextureParams.parameterData = m_boundTextures[m_fontAtlasId]; m_shaderParameters[new SHashedName("texture")] = fontTextureParams; m_shader.SetShaderParameters(m_d3DeviceContext, m_shaderParameters); m_shader.SetActive(m_d3DeviceContext); m_d3DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_vertexBuffer, Utilities.SizeOf <ImDrawVert>(), 0)); m_d3DeviceContext.InputAssembler.SetIndexBuffer(m_indexBuffer, Format.R16_UInt, 0); m_d3DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; RawViewportF[] prevViewports = m_d3DeviceContext.Rasterizer.GetViewports <RawViewportF>(); RasterizerState prevRasterizerState = m_d3DeviceContext.Rasterizer.State; DepthStencilState prevDepthState = m_d3DeviceContext.OutputMerger.DepthStencilState; BlendState prevBlendState = m_d3DeviceContext.OutputMerger.BlendState; Rectangle[] prevScissorRects = new Rectangle[8]; m_d3DeviceContext.Rasterizer.GetScissorRectangles(prevScissorRects); Viewport viewport = new Viewport(0, 0, m_screenWidth, m_screenHeight, 0.0f, 1.0f); m_d3DeviceContext.Rasterizer.SetViewport(viewport); m_d3DeviceContext.OutputMerger.DepthStencilState = m_depthStencilState; m_d3DeviceContext.OutputMerger.BlendState = m_blendState; m_d3DeviceContext.Rasterizer.State = m_rasterizerState; int vertexOffset = 0; int indexOffset = 0; for (int i = 0; i < drawData.CmdListsCount; i++) { ImDrawListPtr drawList = drawData.CmdListsRange[i]; for (int n = 0; n < drawList.CmdBuffer.Size; n++) { ImDrawCmdPtr command = drawList.CmdBuffer[n]; if (!m_boundTextures.ContainsKey(command.TextureId)) { throw new InvalidOperationException("Requested texture was not bound to imgui, please check your binding"); } CTextureSampler textureSampler = m_boundTextures[command.TextureId]; m_d3DeviceContext.Rasterizer.SetScissorRectangle((int)command.ClipRect.X, (int)command.ClipRect.Y, (int)command.ClipRect.Z, (int)command.ClipRect.W); m_d3DeviceContext.PixelShader.SetSampler(0, textureSampler.SamplerState); m_d3DeviceContext.PixelShader.SetShaderResource(0, textureSampler.Texture.GetTexture()); m_d3DeviceContext.DrawIndexed((int)command.ElemCount, indexOffset, vertexOffset); indexOffset += (int)command.ElemCount; } vertexOffset += drawList.VtxBuffer.Size; } m_d3DeviceContext.Rasterizer.SetScissorRectangles(prevScissorRects); m_d3DeviceContext.OutputMerger.BlendState = prevBlendState; m_d3DeviceContext.OutputMerger.DepthStencilState = prevDepthState; m_d3DeviceContext.Rasterizer.State = prevRasterizerState; m_d3DeviceContext.Rasterizer.SetViewports(prevViewports); prevRasterizerState?.Dispose(); prevBlendState?.Dispose(); prevDepthState?.Dispose(); annotation.EndEvent(); annotation.Dispose(); } }