diff options
Diffstat (limited to '3rdparty/bgfx/src/renderer_d3d9.cpp')
-rw-r--r-- | 3rdparty/bgfx/src/renderer_d3d9.cpp | 270 |
1 files changed, 134 insertions, 136 deletions
diff --git a/3rdparty/bgfx/src/renderer_d3d9.cpp b/3rdparty/bgfx/src/renderer_d3d9.cpp index 919a952260b..8b52ff2d6b6 100644 --- a/3rdparty/bgfx/src/renderer_d3d9.cpp +++ b/3rdparty/bgfx/src/renderer_d3d9.cpp @@ -215,6 +215,10 @@ namespace bgfx { namespace d3d9 { D3DFMT_UNKNOWN }, // RG32I { D3DFMT_UNKNOWN }, // RG32U { D3DFMT_G32R32F }, // RG32F + { D3DFMT_UNKNOWN }, // RGB8 + { D3DFMT_UNKNOWN }, // RGB8I + { D3DFMT_UNKNOWN }, // RGB8U + { D3DFMT_UNKNOWN }, // RGB8S { D3DFMT_UNKNOWN }, // RGB9E5F { D3DFMT_A8R8G8B8 }, // BGRA8 { D3DFMT_UNKNOWN }, // RGBA8 @@ -340,10 +344,10 @@ namespace bgfx { namespace d3d9 m_params.BackBufferHeight = rect.bottom-rect.top; m_d3d9dll = bx::dlopen("d3d9.dll"); - BX_WARN(NULL != m_d3d9dll, "Failed to load d3d9.dll."); if (NULL == m_d3d9dll) { + BX_TRACE("Failed to load d3d9.dll."); goto error; } @@ -370,16 +374,25 @@ namespace bgfx { namespace d3d9 && NULL != Direct3DCreate9Ex) { Direct3DCreate9Ex(D3D_SDK_VERSION, &m_d3d9ex); - DX_CHECK(m_d3d9ex->QueryInterface(IID_IDirect3D9, (void**)&m_d3d9) ); - m_pool = D3DPOOL_DEFAULT; + HRESULT hr = m_d3d9ex->QueryInterface(IID_IDirect3D9, (void**)&m_d3d9); + if (FAILED(hr) ) + { + BX_TRACE("Failed to query D3D9 interface 0x%08x.", hr); + DX_RELEASE(m_d3d9ex, 0); + } + else + { + m_pool = D3DPOOL_DEFAULT; + } } - else + + if (NULL == m_d3d9) { Direct3DCreate9 = (Direct3DCreate9Fn)bx::dlsym(m_d3d9dll, "Direct3DCreate9"); - BX_WARN(NULL != Direct3DCreate9, "Function Direct3DCreate9 not found."); if (NULL == Direct3DCreate9) { + BX_TRACE("Function Direct3DCreate9 not found."); goto error; } @@ -387,10 +400,9 @@ namespace bgfx { namespace d3d9 m_pool = D3DPOOL_MANAGED; } - BX_WARN(NULL != m_d3d9, "Unable to create Direct3D."); - if (NULL == m_d3d9) { + BX_TRACE("Unable to create Direct3D."); goto error; } @@ -487,10 +499,9 @@ namespace bgfx { namespace d3d9 } } - BX_WARN(NULL != m_device, "Unable to create Direct3D9 device."); - if (NULL == m_device) { + BX_TRACE("Unable to create Direct3D9 device."); goto error; } @@ -1317,21 +1328,30 @@ namespace bgfx { namespace d3d9 // If frame buffer has only depth attachment D3DFMT_NULL // render target is created. - uint32_t fbnum = bx::uint32_max(1, frameBuffer.m_num); + const uint32_t fbnum = bx::uint32_max(2, frameBuffer.m_numTh); + const uint8_t dsIdx = frameBuffer.m_dsIdx; + DX_CHECK(m_device->SetDepthStencilSurface(UINT8_MAX == dsIdx + ? m_backBufferDepthStencil + : frameBuffer.m_surface[dsIdx] + ) ); + + uint32_t rtIdx = 0; for (uint32_t ii = 0; ii < fbnum; ++ii) { - DX_CHECK(m_device->SetRenderTarget(ii, frameBuffer.m_color[ii]) ); + IDirect3DSurface9* surface = frameBuffer.m_surface[ii]; + if (ii != dsIdx) + { + DX_CHECK(m_device->SetRenderTarget(rtIdx, surface) ); + ++rtIdx; + } } - for (uint32_t ii = fbnum, num = g_caps.maxFBAttachments; ii < num; ++ii) + for (uint32_t ii = rtIdx, num = g_caps.maxFBAttachments; ii < num; ++ii) { DX_CHECK(m_device->SetRenderTarget(ii, NULL) ); } - IDirect3DSurface9* depthStencil = frameBuffer.m_depthStencil; - DX_CHECK(m_device->SetDepthStencilSurface(NULL != depthStencil ? depthStencil : m_backBufferDepthStencil) ); - DX_CHECK(m_device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE) ); } @@ -2819,16 +2839,11 @@ namespace bgfx { namespace d3d9 m_height = textureHeight; m_depth = imageContainer.m_depth; m_numMips = numMips; - m_requestedFormat = - m_textureFormat = uint8_t(imageContainer.m_format); + m_requestedFormat = uint8_t(imageContainer.m_format); + m_textureFormat = uint8_t(getViableTextureFormat(imageContainer) ); + const bool convert = m_textureFormat != m_requestedFormat; - const TextureFormatInfo& tfi = s_textureFormat[m_requestedFormat]; uint8_t bpp = getBitsPerPixel(TextureFormat::Enum(m_textureFormat) ); - if (D3DFMT_UNKNOWN == tfi.m_fmt) - { - m_textureFormat = (uint8_t)TextureFormat::BGRA8; - bpp = 32; - } if (imageContainer.m_cubeMap) { @@ -2871,8 +2886,6 @@ namespace bgfx { namespace d3d9 && imageContainer.m_format != TextureFormat::BC5 ; - const bool convert = m_textureFormat != m_requestedFormat; - for (uint8_t side = 0, numSides = imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side) { uint32_t width = textureWidth; @@ -2925,7 +2938,20 @@ namespace bgfx { namespace d3d9 else { uint32_t size = useMipSize ? mip.m_size : mipSize; - memcpy(bits, mip.m_data, size); + switch (m_textureFormat) + { + case TextureFormat::RGB5A1: + imageConvert(bits, 16, packBgr5a1, mip.m_data, unpackRgb5a1, size); + break; + + case TextureFormat::RGBA4: + imageConvert(bits, 16, packBgra4, mip.m_data, unpackRgba4, size); + break; + + default: + memcpy(bits, mip.m_data, size); + break; + } } unlock(side, lod); @@ -2974,7 +3000,20 @@ namespace bgfx { namespace d3d9 uint8_t* dst = bits; for (uint32_t yy = 0, height = _rect.m_height; yy < height; ++yy) { - memcpy(dst, src, rectpitch); + switch (m_textureFormat) + { + case TextureFormat::RGB5A1: + imageConvert(dst, 16, packBgr5a1, src, unpackRgb5a1, rectpitch); + break; + + case TextureFormat::RGBA4: + imageConvert(dst, 16, packBgra4, src, unpackRgba4, rectpitch); + break; + + default: + memcpy(dst, src, rectpitch); + break; + } src += srcpitch; dst += dstpitch; } @@ -3064,50 +3103,53 @@ namespace bgfx { namespace d3d9 void FrameBufferD3D9::create(uint8_t _num, const Attachment* _attachment) { - for (uint32_t ii = 0; ii < BX_COUNTOF(m_color); ++ii) + for (uint32_t ii = 0; ii < BX_COUNTOF(m_surface); ++ii) { - m_color[ii] = NULL; + m_surface[ii] = NULL; } - m_depthStencil = NULL; - m_num = 0; + m_dsIdx = UINT8_MAX; + m_num = 0; + m_numTh = _num; m_needResolve = false; + memcpy(m_attachment, _attachment, _num*sizeof(Attachment) ); + for (uint32_t ii = 0; ii < _num; ++ii) { - TextureHandle handle = _attachment[ii].handle; + TextureHandle handle = m_attachment[ii].handle; if (isValid(handle) ) { const TextureD3D9& texture = s_renderD3D9->m_textures[handle.idx]; + if (NULL != texture.m_surface) + { + m_surface[ii] = texture.m_surface; + m_surface[ii]->AddRef(); + } + else + { + m_surface[ii] = texture.getSurface(uint8_t(m_attachment[ii].layer), uint8_t(m_attachment[ii].mip) ); + } + + if (0 == m_num) + { + m_width = texture.m_width; + m_height = texture.m_height; + } + if (isDepth( (TextureFormat::Enum)texture.m_textureFormat) ) { - m_depthHandle = handle; - if (NULL != texture.m_surface) - { - m_depthStencil = texture.m_surface; - m_depthStencil->AddRef(); - } - else - { - m_depthStencil = texture.getSurface(); - } + m_dsIdx = uint8_t(ii); } else { - m_colorHandle[m_num] = handle; - if (NULL != texture.m_surface) - { - m_color[m_num] = texture.m_surface; - m_color[m_num]->AddRef(); - } - else - { - m_color[m_num] = texture.getSurface(uint8_t(_attachment[ii].layer) ); - } - m_num++; + ++m_num; } - m_needResolve |= (NULL != texture.m_surface) && (NULL != texture.m_texture2d); + m_needResolve |= true + && (NULL != texture.m_surface) + && (NULL != texture.m_texture2d) + ; } } @@ -3132,7 +3174,7 @@ namespace bgfx { namespace d3d9 params.BackBufferHeight = m_height; DX_CHECK(s_renderD3D9->m_device->CreateAdditionalSwapChain(¶ms, &m_swapChain) ); - DX_CHECK(m_swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &m_color[0]) ); + DX_CHECK(m_swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &m_surface[0]) ); DX_CHECK(s_renderD3D9->m_device->CreateDepthStencilSurface( params.BackBufferWidth @@ -3141,11 +3183,11 @@ namespace bgfx { namespace d3d9 , params.MultiSampleType , params.MultiSampleQuality , FALSE - , &m_depthStencil + , &m_surface[1] , NULL ) ); - m_colorHandle[0].idx = invalidHandle; + m_dsIdx = 1; m_denseIdx = _denseIdx; m_num = 1; m_needResolve = false; @@ -3155,44 +3197,29 @@ namespace bgfx { namespace d3d9 { if (NULL != m_hwnd) { - DX_RELEASE(m_depthStencil, 0); - DX_RELEASE(m_color[0], 0); - DX_RELEASE(m_swapChain, 0); + DX_RELEASE(m_surface[0], 0); + DX_RELEASE(m_surface[1], 0); + DX_RELEASE(m_swapChain, 0); } else { - for (uint32_t ii = 0, num = m_num; ii < num; ++ii) - { - m_colorHandle[ii].idx = invalidHandle; + uint32_t num = m_numTh; + num += uint32_t(0 < m_numTh && 0 == m_num); - IDirect3DSurface9* ptr = m_color[ii]; + for (uint32_t ii = 0; ii < num; ++ii) + { + IDirect3DSurface9* ptr = m_surface[ii]; if (NULL != ptr) { ptr->Release(); - m_color[ii] = NULL; + m_surface[ii] = NULL; } } - - if (NULL != m_depthStencil) - { - if (0 == m_num) - { - IDirect3DSurface9* ptr = m_color[0]; - if (NULL != ptr) - { - ptr->Release(); - m_color[0] = NULL; - } - } - - m_depthStencil->Release(); - m_depthStencil = NULL; - } } - m_hwnd = NULL; - m_num = 0; - m_depthHandle.idx = invalidHandle; + m_hwnd = NULL; + m_num = 0; + m_numTh = 0; uint16_t denseIdx = m_denseIdx; m_denseIdx = UINT16_MAX; @@ -3209,15 +3236,9 @@ namespace bgfx { namespace d3d9 { if (m_needResolve) { - if (isValid(m_depthHandle) ) - { - const TextureD3D9& texture = s_renderD3D9->m_textures[m_depthHandle.idx]; - texture.resolve(); - } - - for (uint32_t ii = 0, num = m_num; ii < num; ++ii) + for (uint32_t ii = 0, num = m_numTh; ii < num; ++ii) { - const TextureD3D9& texture = s_renderD3D9->m_textures[m_colorHandle[ii].idx]; + const TextureD3D9& texture = s_renderD3D9->m_textures[m_attachment[ii].handle.idx]; texture.resolve(); } } @@ -3227,28 +3248,19 @@ namespace bgfx { namespace d3d9 { if (NULL != m_hwnd) { - DX_RELEASE(m_color[0], 0); - DX_RELEASE(m_depthStencil, 0); + DX_RELEASE(m_surface[0], 0); + DX_RELEASE(m_surface[1], 0); DX_RELEASE(m_swapChain, 0); } else { - for (uint32_t ii = 0, num = m_num; ii < num; ++ii) - { - m_color[ii]->Release(); - m_color[ii] = NULL; - } + uint32_t num = m_numTh; + num += uint32_t(0 < m_numTh && 0 == m_num); - if (isValid(m_depthHandle) ) + for (uint32_t ii = 0; ii < num; ++ii) { - if (0 == m_num) - { - m_color[0]->Release(); - m_color[0] = NULL; - } - - m_depthStencil->Release(); - m_depthStencil = NULL; + m_surface[ii]->Release(); + m_surface[ii] = NULL; } } } @@ -3263,69 +3275,55 @@ namespace bgfx { namespace d3d9 params.BackBufferHeight = m_height; DX_CHECK(s_renderD3D9->m_device->CreateAdditionalSwapChain(¶ms, &m_swapChain) ); - DX_CHECK(m_swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &m_color[0]) ); + DX_CHECK(m_swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &m_surface[0]) ); DX_CHECK(s_renderD3D9->m_device->CreateDepthStencilSurface(params.BackBufferWidth , params.BackBufferHeight , params.AutoDepthStencilFormat , params.MultiSampleType , params.MultiSampleQuality , FALSE - , &m_depthStencil + , &m_surface[1] , NULL ) ); } - else + else if (0 < m_numTh) { - for (uint32_t ii = 0, num = m_num; ii < num; ++ii) + for (uint32_t ii = 0, num = m_numTh; ii < num; ++ii) { - TextureHandle th = m_colorHandle[ii]; + TextureHandle th = m_attachment[ii].handle; if (isValid(th) ) { TextureD3D9& texture = s_renderD3D9->m_textures[th.idx]; if (NULL != texture.m_surface) { - m_color[ii] = texture.m_surface; - m_color[ii]->AddRef(); + m_surface[ii] = texture.m_surface; + m_surface[ii]->AddRef(); } else { - m_color[ii] = texture.getSurface(); + m_surface[ii] = texture.getSurface(uint8_t(m_attachment[ii].layer), uint8_t(m_attachment[ii].mip) ); } } } - if (isValid(m_depthHandle) ) + if (0 == m_num) { - TextureD3D9& texture = s_renderD3D9->m_textures[m_depthHandle.idx]; - if (NULL != texture.m_surface) - { - m_depthStencil = texture.m_surface; - m_depthStencil->AddRef(); - } - else - { - m_depthStencil = texture.getSurface(); - } - - if (0 == m_num) - { - createNullColorRT(); - } + createNullColorRT(); } } } void FrameBufferD3D9::createNullColorRT() { - const TextureD3D9& texture = s_renderD3D9->m_textures[m_depthHandle.idx]; - DX_CHECK(s_renderD3D9->m_device->CreateRenderTarget(texture.m_width - , texture.m_height + DX_CHECK(s_renderD3D9->m_device->CreateRenderTarget( + m_width + , m_height , D3DFMT_NULL , D3DMULTISAMPLE_NONE , 0 , false - , &m_color[0] + , &m_surface[1] , NULL ) ); } |