Przeglądaj źródła

Added ability to create texture for external use.

Branimir Karadžić 10 lat temu
rodzic
commit
445ac92eff

+ 3 - 0
include/bgfx/bgfxplatform.h

@@ -87,6 +87,9 @@ namespace bgfx
 	///
 	void setInternal(TextureHandle _handle, uintptr_t _ptr);
 
+	///
+	uintptr_t setInternal(TextureHandle _handle, uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE);
+
 } // namespace bgfx
 
 #if BX_PLATFORM_ANDROID

+ 38 - 7
src/bgfx.cpp

@@ -320,6 +320,37 @@ namespace bgfx
 		s_ctx->m_renderCtx->setInternal(_handle, _ptr);
 	}
 
+	uintptr_t setInternal(TextureHandle _handle, uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags)
+	{
+		BGFX_CHECK_RENDER_THREAD();
+
+		uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate);
+		Memory* mem   = const_cast<Memory*>(alloc(size) );
+
+		bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
+		uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
+		bx::write(&writer, magic);
+
+		TextureCreate tc;
+		tc.m_flags   = _flags;
+		tc.m_width   = _width;
+		tc.m_height  = _height;
+		tc.m_sides   = 0;
+		tc.m_depth   = 0;
+		tc.m_numMips = uint8_t(bx::uint16_max(1, _numMips) );
+		tc.m_format  = _format;
+		tc.m_cubeMap = false;
+		tc.m_mem     = NULL;
+		bx::write(&writer, tc);
+
+		s_ctx->m_renderCtx->destroyTexture(_handle);
+		s_ctx->m_renderCtx->createTexture(_handle, mem, _flags, 0);
+
+		release(mem);
+
+		return s_ctx->m_renderCtx->getInternal(_handle);
+	}
+
 	void setGraphicsDebuggerPresent(bool _present)
 	{
 		BX_TRACE("Graphics debugger is %spresent.", _present ? "" : "not ");
@@ -2841,13 +2872,6 @@ again:
 				);
 		}
 
-		uint32_t size = sizeof(uint32_t)+sizeof(TextureCreate);
-		const Memory* mem = alloc(size);
-
-		bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
-		uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
-		bx::write(&writer, magic);
-
 		if (BackbufferRatio::Count != _ratio)
 		{
 			_width  = uint16_t(s_ctx->m_resolution.m_width);
@@ -2855,6 +2879,13 @@ again:
 			getTextureSizeFromRatio(_ratio, _width, _height);
 		}
 
+		uint32_t size = sizeof(uint32_t)+sizeof(TextureCreate);
+		const Memory* mem = alloc(size);
+
+		bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
+		uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
+		bx::write(&writer, magic);
+
 		TextureCreate tc;
 		tc.m_flags   = _flags;
 		tc.m_width   = _width;

+ 1 - 0
src/bgfx_p.h

@@ -2044,6 +2044,7 @@ namespace bgfx
 		virtual void readTexture(TextureHandle _handle, void* _data) = 0;
 		virtual void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height) = 0;
 		virtual void setInternal(TextureHandle _handle, uintptr_t _ptr) = 0;
+		virtual uintptr_t getInternal(TextureHandle _handle) = 0;
 		virtual void destroyTexture(TextureHandle _handle) = 0;
 		virtual void createFrameBuffer(FrameBufferHandle _handle, uint8_t _num, const TextureHandle* _textureHandles) = 0;
 		virtual void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) = 0;

+ 5 - 0
src/renderer_d3d11.cpp

@@ -1760,6 +1760,11 @@ BX_PRAGMA_DIAGNOSTIC_POP();
 			m_textures[_handle.idx].setInternal(_ptr);
 		}
 
+		uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE
+		{
+			return uintptr_t(m_textures[_handle.idx].m_ptr);
+		}
+
 		void destroyTexture(TextureHandle _handle) BX_OVERRIDE
 		{
 			m_textures[_handle.idx].destroy();

+ 6 - 0
src/renderer_d3d12.cpp

@@ -1377,6 +1377,12 @@ namespace bgfx { namespace d3d12
 			BX_UNUSED(_handle, _ptr);
 		}
 
+		uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE
+		{
+			BX_UNUSED(_handle);
+			return 0;
+		}
+
 		void destroyTexture(TextureHandle _handle) BX_OVERRIDE
 		{
 			m_textures[_handle.idx].destroy();

+ 5 - 0
src/renderer_d3d9.cpp

@@ -1002,6 +1002,11 @@ namespace bgfx { namespace d3d9
 			BX_UNUSED(_handle, _ptr);
 		}
 
+		uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE
+		{
+			return uintptr_t(m_textures[_handle.idx].m_ptr);
+		}
+
 		void destroyTexture(TextureHandle _handle) BX_OVERRIDE
 		{
 			m_textures[_handle.idx].destroy();

+ 5 - 0
src/renderer_gl.cpp

@@ -2233,6 +2233,11 @@ namespace bgfx { namespace gl
 			BX_UNUSED(_handle, _ptr);
 		}
 
+		uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE
+		{
+			return uintptr_t(m_textures[_handle.idx].m_id);
+		}
+
 		void destroyTexture(TextureHandle _handle) BX_OVERRIDE
 		{
 			m_textures[_handle.idx].destroy();

+ 6 - 0
src/renderer_mtl.mm

@@ -683,6 +683,12 @@ namespace bgfx { namespace mtl
 			BX_UNUSED(_handle, _ptr);
 		}
 
+		uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE
+		{
+			BX_UNUSED(_handle);
+			return 0;
+		}
+
 		void destroyTexture(TextureHandle _handle) BX_OVERRIDE
 		{
 			m_textures[_handle.idx].destroy();

+ 5 - 0
src/renderer_null.cpp

@@ -125,6 +125,11 @@ namespace bgfx { namespace noop
 		{
 		}
 
+		uintptr_t getInternal(TextureHandle /*_handle*/) BX_OVERRIDE
+		{
+			return 0;
+		}
+
 		void destroyTexture(TextureHandle /*_handle*/) BX_OVERRIDE
 		{
 		}