Procházet zdrojové kódy

Texture update work.

bkaradzic před 13 roky
rodič
revize
28a73924b2
11 změnil soubory, kde provedl 347 přidání a 146 odebrání
  1. 17 11
      include/bgfx.h
  2. 29 2
      src/bgfx.cpp
  3. 15 4
      src/bgfx_p.h
  4. 144 79
      src/renderer_d3d11.cpp
  5. 15 3
      src/renderer_d3d11.h
  6. 14 14
      src/renderer_d3d9.cpp
  7. 1 1
      src/renderer_d3d9.h
  8. 65 23
      src/renderer_gl.cpp
  9. 1 1
      src/renderer_gl.h
  10. 1 1
      src/renderer_null.cpp
  11. 45 7
      tools/shaderc.cpp

+ 17 - 11
include/bgfx.h

@@ -9,8 +9,6 @@
 #include <stdint.h> // uint32_t
 #include <stdlib.h> // size_t
 
-namespace bgfx
-{
 #define BGFX_STATE_DEPTH_WRITE          UINT64_C(0x0000000000000001)
 
 #define BGFX_STATE_ALPHA_TEST           UINT64_C(0x0000000000000004)
@@ -79,13 +77,13 @@ namespace bgfx
 #define BGFX_STATE_NONE                 UINT64_C(0x0000000000000000)
 #define BGFX_STATE_MASK                 UINT64_C(0xffffffffffffffff)
 #define BGFX_STATE_DEFAULT (0 \
-			| BGFX_STATE_RGB_WRITE \
-			| BGFX_STATE_ALPHA_WRITE \
-			| BGFX_STATE_DEPTH_TEST_LESS \
-			| BGFX_STATE_DEPTH_WRITE \
-			| BGFX_STATE_CULL_CW \
-			| BGFX_STATE_MSAA \
-			)
+					| BGFX_STATE_RGB_WRITE \
+					| BGFX_STATE_ALPHA_WRITE \
+					| BGFX_STATE_DEPTH_TEST_LESS \
+					| BGFX_STATE_DEPTH_WRITE \
+					| BGFX_STATE_CULL_CW \
+					| BGFX_STATE_MSAA \
+					)
 
 #define BGFX_CLEAR_NONE                 UINT8_C(0x00)
 #define BGFX_CLEAR_COLOR_BIT            UINT8_C(0x01)
@@ -156,6 +154,8 @@ namespace bgfx
 
 #define BGFX_INVALID_HANDLE { bgfx::invalidHandle }
 
+namespace bgfx
+{
 	struct Fatal
 	{
 		enum Enum
@@ -432,10 +432,16 @@ namespace bgfx
 	TextureHandle createTextureCube(uint16_t _sides, uint16_t _width, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags = BGFX_TEXTURE_NONE, const Memory* _mem = NULL);
 
 	///
-	void destroyTexture(TextureHandle _handle);
+	void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
 
 	///
-	void updateTexture(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
+	void updateTexture3D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const Memory* _mem);
+
+	///
+	void updateTextureCube(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem);
+
+	///
+	void destroyTexture(TextureHandle _handle);
 
 	///
 	RenderTargetHandle createRenderTarget(uint16_t _width, uint16_t _height, uint32_t _flags = BGFX_RENDER_TARGET_COLOR_RGBA, uint32_t _textureFlags = BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);

+ 29 - 2
src/bgfx.cpp

@@ -1007,7 +1007,7 @@ namespace bgfx
 		s_ctx.destroyTexture(_handle);
 	}
 
-	void updateTexture(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem)
+	void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem)
 	{
 		if (_width == 0
 		||  _height == 0)
@@ -1016,7 +1016,34 @@ namespace bgfx
 		}
 		else
 		{
-			s_ctx.updateTexture(_handle, _mip, _x, _y, _width, _height, _mem);
+			s_ctx.updateTexture(_handle, 0, _mip, _x, _y, 0, _width, _height, 1, _mem);
+		}
+	}
+
+	void updateTexture3D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const Memory* _mem)
+	{
+		if (_width == 0
+		||  _height == 0
+		||  _depth == 0)
+		{
+			release(_mem);
+		}
+		else
+		{
+			s_ctx.updateTexture(_handle, 0, _mip, _x, _y, _z, _width, _height, _depth, _mem);
+		}
+	}
+
+	void updateTextureCube(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem)
+	{
+		if (_width == 0
+		||  _height == 0)
+		{
+			release(_mem);
+		}
+		else
+		{
+			s_ctx.updateTexture(_handle, _side, _mip, _x, _y, 0, _width, _height, 1, _mem);
 		}
 	}
 

+ 15 - 4
src/bgfx_p.h

@@ -2052,18 +2052,20 @@ namespace bgfx
 			m_submit->free(_handle);
 		}
 
-		void updateTexture(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem)
+		void updateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const Memory* _mem)
 		{
 			CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::UpdateTexture);
 			cmdbuf.write(_handle);
+			cmdbuf.write(_side);
 			cmdbuf.write(_mip);
-
 			Rect rect;
 			rect.m_x = _x;
 			rect.m_y = _y;
 			rect.m_width = _width;
 			rect.m_height = _height;
 			cmdbuf.write(rect);
+			cmdbuf.write(_z);
+			cmdbuf.write(_depth);
 			cmdbuf.write(_mem);
 		}
 
@@ -2347,7 +2349,7 @@ namespace bgfx
 		void rendererCreateMaterial(MaterialHandle _handle, VertexShaderHandle _vsh, FragmentShaderHandle _fsh);
 		void rendererDestroyMaterial(FragmentShaderHandle _handle);
 		void rendererCreateTexture(TextureHandle _handle, Memory* _mem, uint32_t _flags);
-		void rendererUpdateTexture(TextureHandle _handle, uint8_t _mip, const Rect& _rect, const Memory* _mem);
+		void rendererUpdateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem);
 		void rendererDestroyTexture(TextureHandle _handle);
 		void rendererCreateRenderTarget(RenderTargetHandle _handle, uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags);
 		void rendererDestroyRenderTarget(RenderTargetHandle _handle);
@@ -2653,16 +2655,25 @@ namespace bgfx
 						TextureHandle handle;
 						_cmdbuf.read(handle);
 
+						uint8_t side;
+						_cmdbuf.read(side);
+
 						uint8_t mip;
 						_cmdbuf.read(mip);
 
 						Rect rect;
 						_cmdbuf.read(rect);
 
+						uint16_t zz;
+						_cmdbuf.read(zz);
+
+						uint16_t depth;
+						_cmdbuf.read(depth);
+
 						Memory* mem;
 						_cmdbuf.read(mem);
 
-						rendererUpdateTexture(handle, mip, rect, mem);
+						rendererUpdateTexture(handle, side, mip, rect, zz, depth, mem);
 
 						release(mem);
 					}

+ 144 - 79
src/renderer_d3d11.cpp

@@ -26,18 +26,18 @@ namespace bgfx
 
 	static const D3D11_BLEND s_blendFactor[][2] =
 	{
-		{ (D3D11_BLEND)0,             (D3D11_BLEND)0 }, // ignored
-		{ D3D11_BLEND_ZERO,           D3D11_BLEND_ZERO },
-		{ D3D11_BLEND_ONE,            D3D11_BLEND_ONE },
-		{ D3D11_BLEND_SRC_COLOR,      D3D11_BLEND_SRC_ALPHA },
-		{ D3D11_BLEND_INV_SRC_COLOR,  D3D11_BLEND_INV_SRC_ALPHA },
-		{ D3D11_BLEND_SRC_ALPHA,      D3D11_BLEND_SRC_ALPHA },
-		{ D3D11_BLEND_INV_SRC_ALPHA,  D3D11_BLEND_INV_SRC_ALPHA },
-		{ D3D11_BLEND_DEST_ALPHA,     D3D11_BLEND_DEST_ALPHA },
+		{ (D3D11_BLEND)0,             (D3D11_BLEND)0             }, // ignored
+		{ D3D11_BLEND_ZERO,           D3D11_BLEND_ZERO           },
+		{ D3D11_BLEND_ONE,            D3D11_BLEND_ONE            },
+		{ D3D11_BLEND_SRC_COLOR,      D3D11_BLEND_SRC_ALPHA      },
+		{ D3D11_BLEND_INV_SRC_COLOR,  D3D11_BLEND_INV_SRC_ALPHA  },
+		{ D3D11_BLEND_SRC_ALPHA,      D3D11_BLEND_SRC_ALPHA      },
+		{ D3D11_BLEND_INV_SRC_ALPHA,  D3D11_BLEND_INV_SRC_ALPHA  },
+		{ D3D11_BLEND_DEST_ALPHA,     D3D11_BLEND_DEST_ALPHA     },
 		{ D3D11_BLEND_INV_DEST_ALPHA, D3D11_BLEND_INV_DEST_ALPHA },
-		{ D3D11_BLEND_DEST_COLOR,     D3D11_BLEND_DEST_ALPHA },
+		{ D3D11_BLEND_DEST_COLOR,     D3D11_BLEND_DEST_ALPHA     },
 		{ D3D11_BLEND_INV_DEST_COLOR, D3D11_BLEND_INV_DEST_ALPHA },
-		{ D3D11_BLEND_SRC_ALPHA_SAT,  D3D11_BLEND_ONE },
+		{ D3D11_BLEND_SRC_ALPHA_SAT,  D3D11_BLEND_ONE            },
 	};
 
 	static const D3D11_COMPARISON_FUNC s_depthFunc[] =
@@ -658,31 +658,42 @@ namespace bgfx
 			D3D11_TEXTURE2D_DESC backBufferDesc;
 			backBuffer->GetDesc(&backBufferDesc);
 
-			ID3D11Texture2D *texture = backBuffer;
-			if (backBufferDesc.SampleDesc.Count > 1)
-			{
-				D3D11_TEXTURE2D_DESC desc;
-				memcpy(&desc, &backBufferDesc, sizeof(desc) );
-				desc.SampleDesc.Count = 1;
-				desc.SampleDesc.Quality = 0;
-				desc.Usage = D3D11_USAGE_DEFAULT;
-				desc.BindFlags = 0;
-				desc.CPUAccessFlags = 0;
+			D3D11_TEXTURE2D_DESC desc;
+			memcpy(&desc, &backBufferDesc, sizeof(desc) );
+			desc.SampleDesc.Count = 1;
+			desc.SampleDesc.Quality = 0;
+			desc.Usage = D3D11_USAGE_STAGING;
+			desc.BindFlags = 0;
+			desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
 
-				ID3D11Texture2D* resolveTexture;
-				HRESULT hr = m_device->CreateTexture2D(&desc, NULL, &resolveTexture);
-				if (SUCCEEDED(hr) )
+			ID3D11Texture2D* texture;
+			HRESULT hr = m_device->CreateTexture2D(&desc, NULL, &texture);
+			if (SUCCEEDED(hr) )
+			{
+				if (backBufferDesc.SampleDesc.Count == 1)
 				{
-					m_deviceCtx->ResolveSubresource(resolveTexture, 0, backBuffer, 0, backBufferDesc.Format);
-					texture = resolveTexture;
+					m_deviceCtx->CopyResource(texture, backBuffer);
+				}
+				else
+				{
+					desc.Usage = D3D11_USAGE_DEFAULT;
+					desc.CPUAccessFlags = 0;
+					ID3D11Texture2D* resolve;
+					HRESULT hr = m_device->CreateTexture2D(&desc, NULL, &resolve);
+					if (SUCCEEDED(hr) )
+					{
+						m_deviceCtx->ResolveSubresource(resolve, 0, backBuffer, 0, desc.Format);
+						m_deviceCtx->CopyResource(texture, resolve);
+						DX_RELEASE(resolve, 0);
+					}
 				}
 
-				texture->GetDesc(&backBufferDesc);
-
-				// save texture
-//				saveTga( (const char*)_mem->data, m_params.BackBufferWidth, m_params.BackBufferHeight, rect.Pitch, &data[point.y*rect.Pitch+point.x*bpp]);
+				D3D11_MAPPED_SUBRESOURCE mapped;
+				DX_CHECK(m_deviceCtx->Map(texture, 0, D3D11_MAP_READ, 0, &mapped) );
+				saveTga( (const char*)_mem->data, backBufferDesc.Width, backBufferDesc.Height, mapped.RowPitch, mapped.pData);
+				m_deviceCtx->Unmap(texture, 0);
 
-				DX_RELEASE(resolveTexture, 0);
+				DX_RELEASE(texture, 0);
 			}
 
 			DX_RELEASE(backBuffer, 0);
@@ -1193,7 +1204,7 @@ namespace bgfx
 			desc.AddressW = s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT];
 			desc.MipLODBias = 0.0f;
 			desc.MaxAnisotropy = 1;
-			desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
+			desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
 			desc.BorderColor[0] = 0.0f;
 			desc.BorderColor[1] = 0.0f;
 			desc.BorderColor[2] = 0.0f;
@@ -1202,14 +1213,6 @@ namespace bgfx
 			desc.MaxLOD = D3D11_FLOAT32_MAX;
 			s_renderCtx.m_device->CreateSamplerState(&desc, &m_sampler);
 
-//			D3D11_TEXTURE_ADDRESS_WRAP
-//			D3D11_FILTER_MIN_MAG_MIP_POINT
-
-// 			desc. = s_textureFilter[(_flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT];
-// 			m_magFilter = s_textureFilter[(_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT];
-// 			m_mipFilter = s_textureFilter[(_flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT];
-// 			m_srgb = (_flags&BGFX_TEXTURE_SRGB) == BGFX_TEXTURE_SRGB;
-
 			s_renderCtx.m_samplerStateCache.add(_flags, m_sampler);
 		}
 
@@ -1224,38 +1227,24 @@ namespace bgfx
 			if (dds.m_cubeMap)
 			{
 				m_type = TextureCube;
-//				createCubeTexture(dds.m_width, dds.m_numMips, s_textureFormat[dds.m_type].m_fmt);
 			}
 			else if (dds.m_depth > 1)
 			{
 				m_type = Texture3D;
-//				createVolumeTexture(dds.m_width, dds.m_height, dds.m_depth, dds.m_numMips, s_textureFormat[dds.m_type].m_fmt);
 			}
 			else
 			{
 				m_type = Texture2D;
-//				createTexture(dds.m_width, dds.m_height, dds.m_numMips, s_textureFormat[dds.m_type].m_fmt);
 			}
 
-			D3D11_TEXTURE2D_DESC desc;
-			desc.Width = dds.m_width;
-			desc.Height = dds.m_height;
-			desc.MipLevels = dds.m_numMips;
-			desc.ArraySize = 1;
-			desc.Format = s_textureFormat[dds.m_type].m_fmt;
-			desc.SampleDesc.Count = 1;
-			desc.SampleDesc.Quality = 0;
-			desc.Usage = D3D11_USAGE_DEFAULT;
-			desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
-			desc.CPUAccessFlags = 0;
-			desc.MiscFlags = 0;
-
 			uint32_t numSrd = dds.m_numMips*(dds.m_cubeMap ? 6 : 1);
 			D3D11_SUBRESOURCE_DATA* srd = (D3D11_SUBRESOURCE_DATA*)alloca(numSrd*sizeof(D3D11_SUBRESOURCE_DATA) );
 
 			uint32_t kk = 0;
 			bool convert = TextureFormat::XRGB8 == dds.m_type;
 
+			m_numMips = dds.m_numMips;
+
 			if (decompress
 			||  TextureFormat::Unknown < dds.m_type)
 			{
@@ -1267,7 +1256,7 @@ namespace bgfx
 					uint32_t height = dds.m_height;
 					uint32_t depth = dds.m_depth;
 
-					for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
+					for (uint32_t lod = 0, num = m_numMips; lod < num; ++lod)
 					{
 						width = uint32_max(1, width);
 						height = uint32_max(1, height);
@@ -1283,16 +1272,15 @@ namespace bgfx
 
 								srd[kk].pSysMem = temp;
 								srd[kk].SysMemPitch = mip.m_width*bpp;
-								srd[kk].SysMemSlicePitch = 0;
-								++kk;
 							}
 							else
 							{
 								srd[kk].pSysMem = mip.m_data;
 								srd[kk].SysMemPitch = mip.m_width*mip.m_bpp;
-								srd[kk].SysMemSlicePitch = 0;
-								++kk;
 							}
+
+							srd[kk].SysMemSlicePitch = mip.m_height*srd[kk].SysMemPitch;
+							++kk;
 						}
 
 						width >>= 1;
@@ -1305,36 +1293,90 @@ namespace bgfx
 			{
 				for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
 				{
-					for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
+					for (uint32_t lod = 0, num = m_numMips; lod < num; ++lod)
 					{
 						Mip mip;
-						if (getRawImageData(dds, 0, lod, _mem, mip) )
+						if (getRawImageData(dds, side, lod, _mem, mip) )
 						{
 							srd[kk].pSysMem = mip.m_data;
 							if (TextureFormat::Unknown > dds.m_type)
 							{
 								srd[kk].SysMemPitch = (mip.m_width/4)*mip.m_blockSize;
+								srd[kk].SysMemSlicePitch = (mip.m_height/4)*srd[kk].SysMemPitch;
 							}
 							else
 							{
 								srd[kk].SysMemPitch = mip.m_width*mip.m_bpp;
+								srd[kk].SysMemSlicePitch = mip.m_height*srd[kk].SysMemPitch;
 							}
 
-							srd[kk].SysMemSlicePitch = 0;
 							++kk;
 						}
 					}
 				}
 			}
 
-			DX_CHECK(s_renderCtx.m_device->CreateTexture2D(&desc, srd, &m_ptr) );
+			D3D11_SHADER_RESOURCE_VIEW_DESC srvd;
+			memset(&srvd, 0, sizeof(srvd) );
+			srvd.Format = s_textureFormat[dds.m_type].m_fmt;
+
+			switch (m_type)
+			{
+			case Texture2D:
+			case TextureCube:
+				{
+					D3D11_TEXTURE2D_DESC desc;
+					desc.Width = dds.m_width;
+					desc.Height = dds.m_height;
+					desc.MipLevels = dds.m_numMips;
+					desc.Format = s_textureFormat[dds.m_type].m_fmt;
+					desc.SampleDesc.Count = 1;
+					desc.SampleDesc.Quality = 0;
+					desc.Usage = D3D11_USAGE_IMMUTABLE;
+					desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
+					desc.CPUAccessFlags = 0;
+
+					if (dds.m_cubeMap)
+					{
+						desc.ArraySize = 6;
+						desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
+						srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
+						srvd.TextureCube.MipLevels = dds.m_numMips;
+					}
+					else
+					{
+						desc.ArraySize = 1;
+						desc.MiscFlags = 0;
+						srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
+						srvd.Texture2D.MipLevels = dds.m_numMips;
+					}
 
-			D3D11_SHADER_RESOURCE_VIEW_DESC srv;
-			memset(&srv, 0, sizeof(srv) );
-			srv.Format = s_textureFormat[dds.m_type].m_fmt;
-			srv.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
-			srv.Texture2D.MipLevels = dds.m_numMips;
-			DX_CHECK(s_renderCtx.m_device->CreateShaderResourceView(m_ptr, &srv, &m_srv) );
+					DX_CHECK(s_renderCtx.m_device->CreateTexture2D(&desc, srd, &m_texture2d) );
+				}
+				break;
+
+			case Texture3D:
+				{
+					D3D11_TEXTURE3D_DESC desc;
+					desc.Width = dds.m_width;
+					desc.Height = dds.m_height;
+					desc.Depth = dds.m_depth;
+					desc.MipLevels = dds.m_numMips;
+					desc.Format = s_textureFormat[dds.m_type].m_fmt;
+					desc.Usage = D3D11_USAGE_IMMUTABLE;
+					desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
+					desc.CPUAccessFlags = 0;
+					desc.MiscFlags = 0;
+
+					srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
+					srvd.Texture3D.MipLevels = dds.m_numMips;
+
+					DX_CHECK(s_renderCtx.m_device->CreateTexture3D(&desc, srd, &m_texture3d) );
+				}
+				break;
+			}
+
+			DX_CHECK(s_renderCtx.m_device->CreateShaderResourceView(m_ptr, &srvd, &m_srv) );
 
 			if (convert)
 			{
@@ -1369,13 +1411,16 @@ namespace bgfx
 				desc.Format = s_textureFormat[ti.m_type].m_fmt;
 				desc.SampleDesc.Count = 1;
 				desc.SampleDesc.Quality = 0;
-				desc.Usage = D3D11_USAGE_DEFAULT;
 				desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
 				desc.CPUAccessFlags = 0;
 				desc.MiscFlags = 0;
 
+				m_numMips = ti.m_numMips;
+
 				if (NULL != ti.m_mem)
 				{
+					desc.Usage = D3D11_USAGE_IMMUTABLE;
+
 					D3D11_SUBRESOURCE_DATA* srd = (D3D11_SUBRESOURCE_DATA*)alloca(ti.m_numMips*sizeof(D3D11_SUBRESOURCE_DATA) );
 					uint32_t bpp = s_textureFormat[ti.m_type].m_bpp;
 					uint8_t* data = ti.m_mem->data;
@@ -1401,13 +1446,15 @@ namespace bgfx
 						}
 					}
 
-					DX_CHECK(s_renderCtx.m_device->CreateTexture2D(&desc, srd, &m_ptr) );
+					DX_CHECK(s_renderCtx.m_device->CreateTexture2D(&desc, srd, &m_texture2d) );
 
 					release(ti.m_mem);
 				}
 				else
 				{
-					DX_CHECK(s_renderCtx.m_device->CreateTexture2D(&desc, NULL, &m_ptr) );
+					desc.Usage = D3D11_USAGE_DEFAULT;
+
+					DX_CHECK(s_renderCtx.m_device->CreateTexture2D(&desc, NULL, &m_texture2d) );
 				}
 
 				D3D11_SHADER_RESOURCE_VIEW_DESC srv;
@@ -1429,7 +1476,7 @@ namespace bgfx
 		s_renderCtx.m_textureStage.m_sampler[_stage] = m_sampler;
 	}
 
-	void Texture::update(uint8_t _mip, const Rect& _rect, const Memory* _mem)
+	void Texture::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
 	{
 		ID3D11DeviceContext* deviceCtx = s_renderCtx.m_deviceCtx;
 
@@ -1438,9 +1485,27 @@ namespace bgfx
 		box.top = _rect.m_y;
 		box.right = box.left + _rect.m_width;
 		box.bottom = box.top + _rect.m_height;
-		box.front = 0;
-		box.back = 1;
-		deviceCtx->UpdateSubresource(m_ptr, 0, &box, _mem->data, _rect.m_width, 0); 
+		box.front = _z;
+		box.back = box.front + _depth;
+
+		uint32_t subres = _mip + (_side * m_numMips);
+#if 0
+		D3D11_MAPPED_SUBRESOURCE mapped;
+		DX_CHECK(deviceCtx->Map(m_ptr, 0, D3D11_MAP_WRITE, D3D11_MAP_FLAG_DO_NOT_WAIT, &mapped) );
+		memcpy( (uint8_t*)mapped.pData + subres*mapped.DepthPitch, _mem->data, _mem->size);
+		deviceCtx->Unmap(m_ptr, 0);
+
+		deviceCtx->CopySubresourceRegion(m_ptr
+			, subres
+			, _rect.m_x
+			, _rect.m_y
+			, _rect.m_z
+			, staging // D3D11_USAGE_STAGING
+			, ...
+			);
+#else
+		deviceCtx->UpdateSubresource(m_ptr, subres, &box, _mem->data, _rect.m_width, 0); 
+#endif // 0
 	}
 
 	void RenderTarget::create(uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags)
@@ -1648,9 +1713,9 @@ namespace bgfx
 		s_renderCtx.m_textures[_handle.idx].create(_mem, _flags);
 	}
 
-	void Context::rendererUpdateTexture(TextureHandle _handle, uint8_t _mip, const Rect& _rect, const Memory* _mem)
+	void Context::rendererUpdateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
 	{
-		s_renderCtx.m_textures[_handle.idx].update(_mip, _rect, _mem);
+		s_renderCtx.m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _mem);
 	}
 
 	void Context::rendererDestroyTexture(TextureHandle _handle)

+ 15 - 3
src/renderer_d3d11.h

@@ -6,6 +6,7 @@
 #ifndef __RENDERER_D3D11_H__
 #define __RENDERER_D3D11_H__
 
+#define D3D11_NO_HELPERS
 #include <d3d11.h>
 #include "renderer_d3d.h"
 
@@ -214,7 +215,11 @@ namespace bgfx
 		};
 
 		Texture()
-			: m_srv(NULL)
+			: m_ptr(NULL)
+			, m_srv(NULL)
+			, m_sampler(NULL)
+			, m_srgb(false)
+			, m_numMips(0)
 		{
 		}
 
@@ -230,14 +235,21 @@ namespace bgfx
 			DX_RELEASE(m_ptr, 1);
 		}
 
+		void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem);
 		void commit(uint8_t _stage);
-		void update(uint8_t _mip, const Rect& _rect, const Memory* _mem);
 
-		ID3D11Texture2D* m_ptr;
+		union
+		{
+			ID3D11Resource* m_ptr;
+			ID3D11Texture2D* m_texture2d;
+			ID3D11Texture3D* m_texture3d;
+		};
+
 		ID3D11ShaderResourceView* m_srv;
 		ID3D11SamplerState* m_sampler;
 		Enum m_type;
 		bool m_srgb;
+		uint8_t m_numMips;
 	};
 
 	struct RenderTarget

+ 14 - 14
src/renderer_d3d9.cpp

@@ -44,18 +44,18 @@ namespace bgfx
 
 	static const D3DBLEND s_blendFactor[][2] =
 	{
-		{ (D3DBLEND)0,           (D3DBLEND)0 }, // ignored
-		{ D3DBLEND_ZERO,         D3DBLEND_ZERO },
-		{ D3DBLEND_ONE,          D3DBLEND_ONE },
-		{ D3DBLEND_SRCCOLOR,     D3DBLEND_SRCCOLOR },
-		{ D3DBLEND_INVSRCCOLOR,  D3DBLEND_INVSRCCOLOR },
-		{ D3DBLEND_SRCALPHA,     D3DBLEND_SRCALPHA },
-		{ D3DBLEND_INVSRCALPHA,  D3DBLEND_INVSRCALPHA },
-		{ D3DBLEND_DESTALPHA,    D3DBLEND_DESTALPHA },
+		{ (D3DBLEND)0,           (D3DBLEND)0           }, // ignored
+		{ D3DBLEND_ZERO,         D3DBLEND_ZERO         },
+		{ D3DBLEND_ONE,          D3DBLEND_ONE          },
+		{ D3DBLEND_SRCCOLOR,     D3DBLEND_SRCCOLOR     },
+		{ D3DBLEND_INVSRCCOLOR,  D3DBLEND_INVSRCCOLOR  },
+		{ D3DBLEND_SRCALPHA,     D3DBLEND_SRCALPHA     },
+		{ D3DBLEND_INVSRCALPHA,  D3DBLEND_INVSRCALPHA  },
+		{ D3DBLEND_DESTALPHA,    D3DBLEND_DESTALPHA    },
 		{ D3DBLEND_INVDESTALPHA, D3DBLEND_INVDESTALPHA },
-		{ D3DBLEND_DESTCOLOR,    D3DBLEND_DESTCOLOR },
+		{ D3DBLEND_DESTCOLOR,    D3DBLEND_DESTCOLOR    },
 		{ D3DBLEND_INVDESTCOLOR, D3DBLEND_INVDESTCOLOR },
-		{ D3DBLEND_SRCALPHASAT,  D3DBLEND_ONE },
+		{ D3DBLEND_SRCALPHASAT,  D3DBLEND_ONE          },
 	};
 
 	static const D3DCMPFUNC s_depthFunc[] =
@@ -1384,7 +1384,7 @@ namespace bgfx
 		}
 	}
 
-	void Texture::update(uint8_t _mip, const Rect& _rect, const Memory* _mem)
+	void Texture::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
 	{
 		uint32_t pitch;
 		uint32_t slicePitch;
@@ -1623,7 +1623,7 @@ namespace bgfx
 			_type* value = (_type*)data; \
 			s_renderCtx.m_device->SetPixelShaderConstant##_dxsuffix(loc, value, num); \
 		} \
-		break;
+		break
 
 			switch ((int32_t)type)
 			{
@@ -1823,9 +1823,9 @@ namespace bgfx
 		s_renderCtx.m_textures[_handle.idx].create(_mem, _flags);
 	}
 
-	void Context::rendererUpdateTexture(TextureHandle _handle, uint8_t _mip, const Rect& _rect, const Memory* _mem)
+	void Context::rendererUpdateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
 	{
-		s_renderCtx.m_textures[_handle.idx].update(_mip, _rect, _mem);
+		s_renderCtx.m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _mem);
 	}
 
 	void Context::rendererDestroyTexture(TextureHandle _handle)

+ 1 - 1
src/renderer_d3d9.h

@@ -315,7 +315,7 @@ namespace bgfx
 			DX_RELEASE(m_ptr, 0);
 		}
 
-		void update(uint8_t _mip, const Rect& _rect, const Memory* _mem);
+		void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem);
 		void commit(uint8_t _stage);
 	
 		union

+ 65 - 23
src/renderer_gl.cpp

@@ -665,18 +665,18 @@ namespace bgfx
 
 	static const GLenum s_blendFactor[][2] =
 	{
-		{ 0,                      0 }, // ignored
-		{ GL_ZERO,                GL_ZERO },
-		{ GL_ONE,                 GL_ONE },
-		{ GL_SRC_COLOR,           GL_SRC_COLOR },
+		{ 0,                      0                      }, // ignored
+		{ GL_ZERO,                GL_ZERO                },
+		{ GL_ONE,                 GL_ONE                 },
+		{ GL_SRC_COLOR,           GL_SRC_COLOR           },
 		{ GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR },
-		{ GL_SRC_ALPHA,           GL_SRC_ALPHA },
+		{ GL_SRC_ALPHA,           GL_SRC_ALPHA           },
 		{ GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
-		{ GL_DST_ALPHA,           GL_DST_ALPHA },
+		{ GL_DST_ALPHA,           GL_DST_ALPHA           },
 		{ GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA },
-		{ GL_DST_COLOR,           GL_DST_COLOR },
+		{ GL_DST_COLOR,           GL_DST_COLOR           },
 		{ GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR },
-		{ GL_SRC_ALPHA_SATURATE,  GL_ONE },
+		{ GL_SRC_ALPHA_SATURATE,  GL_ONE                 },
 	};
 
 	static const GLenum s_depthFunc[] =
@@ -1186,7 +1186,7 @@ namespace bgfx
 						depth = uint32_max(1, depth);
 
 						Mip mip;
-						if (getRawImageData(dds, 0, lod, _mem, mip) )
+						if (getRawImageData(dds, side, lod, _mem, mip) )
 						{
 							mip.decode(bits);
 
@@ -1261,7 +1261,7 @@ namespace bgfx
 						depth = uint32_max(1, depth);
 
 						Mip mip;
-						if (getRawImageData(dds, 0, ii, _mem, mip) )
+						if (getRawImageData(dds, side, ii, _mem, mip) )
 						{
 #if BGFX_CONFIG_RENDERER_OPENGL
 							if (m_target == GL_TEXTURE_3D)
@@ -1451,20 +1451,62 @@ namespace bgfx
 		}
 	}
 
-	void Texture::update(uint8_t _mip, const Rect& _rect, const Memory* _mem)
+	void Texture::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
 	{
 		GL_CHECK(glBindTexture(m_target, m_id) );
 		GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1) );
-		GL_CHECK(glTexSubImage2D(m_target
-			, _mip
-			, _rect.m_x
-			, _rect.m_y
-			, _rect.m_width
-			, _rect.m_height
-			, m_fmt
-			, m_type
-			, _mem->data
-			) );
+
+		switch (m_target)
+		{
+		case GL_TEXTURE_2D:
+			{
+				GL_CHECK(glTexSubImage2D(m_target
+					, _mip
+					, _rect.m_x
+					, _rect.m_y
+					, _rect.m_width
+					, _rect.m_height
+					, m_fmt
+					, m_type
+					, _mem->data
+					) );
+			}
+			break;
+
+		case GL_TEXTURE_CUBE_MAP:
+			{
+				GL_CHECK(glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+_side
+					, _mip
+					, _rect.m_x
+					, _rect.m_y
+					, _rect.m_width
+					, _rect.m_height
+					, m_fmt
+					, m_type
+					, _mem->data
+					) );
+			}
+			break;
+
+#if BGFX_CONFIG_RENDERER_OPENGL
+		case GL_TEXTURE_3D:
+			{
+				GL_CHECK(glTexSubImage3D(m_target
+					, _mip
+					, _rect.m_x
+					, _rect.m_y
+					, _z
+					, _rect.m_width
+					, _rect.m_height
+					, _depth
+					, m_fmt
+					, m_type
+					, _mem->data
+					) );
+			}
+			break;
+		}
+#endif // BGFX_CONFIG_RENDERER_OPENGL
 	}
 
 	void RenderTarget::create(uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags)
@@ -1904,9 +1946,9 @@ namespace bgfx
 		s_renderCtx.m_textures[_handle.idx].create(_mem, _flags);
 	}
 
-	void Context::rendererUpdateTexture(TextureHandle _handle, uint8_t _mip, const Rect& _rect, const Memory* _mem)
+	void Context::rendererUpdateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
 	{
-		s_renderCtx.m_textures[_handle.idx].update(_mip, _rect, _mem);
+		s_renderCtx.m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _mem);
 	}
 
 	void Context::rendererDestroyTexture(TextureHandle _handle)

+ 1 - 1
src/renderer_gl.h

@@ -310,7 +310,7 @@ namespace bgfx
 		void createColor(uint32_t _width, uint32_t _height, GLenum _min, GLenum _mag);
 		void createDepth(uint32_t _width, uint32_t _height);
 		void destroy();
-		void update(uint8_t _mip, const Rect& _rect, const Memory* _mem);
+		void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem);
 
 		GLuint m_id;
 		GLenum m_target;

+ 1 - 1
src/renderer_null.cpp

@@ -124,7 +124,7 @@ namespace bgfx
 		}
 	}
 
-	void Context::rendererUpdateTexture(TextureHandle /*_handle*/, uint8_t /*_mip*/, const Rect& /*_rect*/, const Memory* /*_mem*/)
+	void Context::rendererUpdateTexture(TextureHandle /*_handle*/, uint8_t /*_side*/, uint8_t /*_mip*/, const Rect& /*_rect*/, uint16_t /*_z*/, uint16_t /*_depth*/, const Memory* /*_mem*/)
 	{
 	}
 

+ 45 - 7
tools/shaderc.cpp

@@ -873,7 +873,7 @@ bool compileHLSLShaderDx11(CommandLine& _cmdLine, const std::string& _code, IStr
 						ConstantType::Enum type = findConstantTypeDx11(constDesc, varDesc.Size);
 
 						if (ConstantType::Count != type
-						&&  0 != (varDesc.uFlags & D3D10_SVF_USED) )
+						&&  0 != (varDesc.uFlags & D3D_SVF_USED) )
 						{
 							Uniform un;
 							un.name = varDesc.Name;
@@ -897,6 +897,26 @@ bool compileHLSLShaderDx11(CommandLine& _cmdLine, const std::string& _code, IStr
 		}
 	}
 
+	BX_TRACE("Bound:");
+	for (uint32_t ii = 0; ii < desc.BoundResources; ++ii)
+	{
+		D3D11_SHADER_INPUT_BIND_DESC bindDesc;
+
+		hr = reflect->GetResourceBindingDesc(ii, &bindDesc);
+		if (SUCCEEDED(hr) )
+		{
+//			if (bindDesc.Type == D3D_SIT_SAMPLER)
+			{
+				BX_TRACE("\t%s, %d, %d, %d"
+					, bindDesc.Name
+					, bindDesc.Type
+					, bindDesc.BindPoint
+					, bindDesc.BindCount
+					);
+			}
+		}
+	}
+
 	uint16_t count = (uint16_t)uniforms.size();
 	_stream.write(count);
 
@@ -917,7 +937,7 @@ bool compileHLSLShaderDx11(CommandLine& _cmdLine, const std::string& _code, IStr
 		BX_TRACE("%s, %s, %d, %d, %d"
 			, un.name.c_str()
 			, s_constantTypeName[un.type]
-		, un.num
+			, un.num
 			, un.regIndex
 			, un.regCount
 			);
@@ -1173,6 +1193,24 @@ int main(int _argc, const char* _argv[])
 		return EXIT_FAILURE;
 	}
 
+	uint32_t hlsl = 2;
+	const char* profile = cmdLine.findOption('p');
+	if (NULL != profile)
+	{
+		if (0 == strncmp(&profile[1], "s_3", 3) )
+		{
+			hlsl = 3;
+		}
+		else if (0 == strncmp(&profile[1], "s_4", 3) )
+		{
+			hlsl = 4;
+		}
+		else if (0 == strncmp(&profile[1], "s_5", 3) )
+		{
+			hlsl = 5;
+		}
+	}
+
 	const char* bin2c = NULL;
 	if (cmdLine.hasArg("bin2c") )
 	{
@@ -1252,12 +1290,14 @@ int main(int _argc, const char* _argv[])
 	else if (0 == _stricmp(platform, "windows") )
 	{
 		preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
-		preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=1");
+		char temp[256];
+		_snprintf(temp, sizeof(temp), "BGFX_SHADER_LANGUAGE_HLSL=%d", hlsl);
+		preprocessor.setDefine(temp);
 	}
 	else if (0 == _stricmp(platform, "xbox360") )
 	{
 		preprocessor.setDefine("BX_PLATFORM_XBOX360=1");
-		preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=1");
+		preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=3");
 	}
 	else
 	{
@@ -1341,9 +1381,7 @@ int main(int _argc, const char* _argv[])
 			}
 			else
 			{
-				const char* profile = cmdLine.findOption('p');
-				if (0 == strncmp(&profile[1], "s_4", 3)
-				||  0 == strncmp(&profile[1], "s_5", 3) )
+				if (hlsl > 3)
 				{
 					compiled = compileHLSLShaderDx11(cmdLine, preprocessor.m_preprocessed, *stream);
 				}