diff --git a/examples/08-update/update.cpp b/examples/08-update/update.cpp index 8ac79e8d..11597a7e 100644 --- a/examples/08-update/update.cpp +++ b/examples/08-update/update.cpp @@ -141,7 +141,7 @@ int _main_(int /*_argc*/, char** /*_argv*/) loadTexture("texture_compression_bc1.dds", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP), loadTexture("texture_compression_bc2.dds", BGFX_TEXTURE_U_CLAMP), loadTexture("texture_compression_bc3.dds", BGFX_TEXTURE_V_CLAMP), - loadTexture("texture_compression_etc1.ktx", BGFX_TEXTURE_U_BORDER|BGFX_TEXTURE_V_BORDER), + loadTexture("texture_compression_etc1.ktx", BGFX_TEXTURE_U_BORDER|BGFX_TEXTURE_V_BORDER|BGFX_TEXTURE_BORDER_COLOR(1) ), loadTexture("texture_compression_etc2.ktx"), loadTexture("texture_compression_ptc12.pvr"), loadTexture("texture_compression_ptc14.pvr"), @@ -243,6 +243,9 @@ int _main_(int /*_argc*/, char** /*_argv*/) while (!entry::processEvents(width, height, debug, reset) ) { + float borderColor[4] = { float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f }; + bgfx::setPaletteColor(1, borderColor); + // Set view 0 and 1 viewport. bgfx::setViewRect(0, 0, 0, width, height); bgfx::setViewRect(1, 0, 0, width, height); diff --git a/examples/19-oit/oit.cpp b/examples/19-oit/oit.cpp index f673a9ab..7457364e 100644 --- a/examples/19-oit/oit.cpp +++ b/examples/19-oit/oit.cpp @@ -311,11 +311,11 @@ int _main_(int /*_argc*/, char** /*_argv*/) bgfx::setViewTransform(0, view, proj); - // Set clear color palette for index 0 - bgfx::setClearColor(0, 0.0f, 0.0f, 0.0f, 0.0f); + // Set palette color for index 0 + bgfx::setPaletteColor(0, 0.0f, 0.0f, 0.0f, 0.0f); - // Set clear color palette for index 1 - bgfx::setClearColor(1, 1.0f, 1.0f, 1.0f, 1.0f); + // Set palette color for index 1 + bgfx::setPaletteColor(1, 1.0f, 1.0f, 1.0f, 1.0f); bgfx::setViewClear(0 , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH diff --git a/examples/21-deferred/deferred.cpp b/examples/21-deferred/deferred.cpp index 8bd68c44..019b8920 100644 --- a/examples/21-deferred/deferred.cpp +++ b/examples/21-deferred/deferred.cpp @@ -228,11 +228,11 @@ class Deferred : public entry::AppI // Enable m_debug text. bgfx::setDebug(m_debug); - // Set clear color palette for index 0 - bgfx::setClearColor(0, UINT32_C(0x00000000) ); + // Set palette color for index 0 + bgfx::setPaletteColor(0, UINT32_C(0x00000000) ); - // Set clear color palette for index 1 - bgfx::setClearColor(1, UINT32_C(0x303030ff) ); + // Set palette color for index 1 + bgfx::setPaletteColor(1, UINT32_C(0x303030ff) ); // Set geometry pass view clear state. bgfx::setViewClear(RENDER_PASS_GEOMETRY_ID diff --git a/include/bgfx.c99.h b/include/bgfx.c99.h index f2f49ffa..23849bb5 100644 --- a/include/bgfx.c99.h +++ b/include/bgfx.c99.h @@ -638,7 +638,7 @@ BGFX_C_API bgfx_uniform_handle_t bgfx_create_uniform(const char* _name, bgfx_uni BGFX_C_API void bgfx_destroy_uniform(bgfx_uniform_handle_t _handle); /**/ -BGFX_C_API void bgfx_set_clear_color(uint8_t _index, const float _rgba[4]); +BGFX_C_API void bgfx_set_palette_color(uint8_t _index, const float _rgba[4]); /**/ BGFX_C_API void bgfx_set_view_name(uint8_t _id, const char* _name); diff --git a/include/bgfx.h b/include/bgfx.h index d84698a9..2bdc5b2e 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -1503,32 +1503,32 @@ namespace bgfx /// void destroyUniform(UniformHandle _handle); - /// Set clear color palette value. + /// Set palette color value. /// /// @param[in] _index Index into palette. /// @param[in] _rgba Packed 32-bit RGBA value. /// - /// @attention C99 equivalent is `bgfx_set_clear_color`. + /// @attention C99 equivalent is `bgfx_set_palette_color`. /// - void setClearColor(uint8_t _index, uint32_t _rgba); + void setPaletteColor(uint8_t _index, uint32_t _rgba); - /// Set clear color palette value. + /// Set palette color value. /// /// @param[in] _index Index into palette. /// @param[in] _r, _g, _b, _a RGBA floating point values. /// - /// @attention C99 equivalent is `bgfx_set_clear_color`. + /// @attention C99 equivalent is `bgfx_set_palette_color`. /// - void setClearColor(uint8_t _index, float _r, float _g, float _b, float _a); + void setPaletteColor(uint8_t _index, float _r, float _g, float _b, float _a); - /// Set clear color palette value. + /// Set palette color value. /// /// @param[in] _index Index into palette. /// @param[in] _rgba RGBA floating point value. /// - /// @attention C99 equivalent is `bgfx_set_clear_color`. + /// @attention C99 equivalent is `bgfx_set_palette_color`. /// - void setClearColor(uint8_t _index, const float _rgba[4]); + void setPaletteColor(uint8_t _index, const float _rgba[4]); /// Set view name. /// diff --git a/include/bgfxdefines.h b/include/bgfxdefines.h index 459ce6a3..4fd6120e 100644 --- a/include/bgfxdefines.h +++ b/include/bgfxdefines.h @@ -308,8 +308,12 @@ #define BGFX_TEXTURE_COMPARE_MASK UINT32_C(0x000f0000) //!< #define BGFX_TEXTURE_COMPUTE_WRITE UINT32_C(0x00100000) //!< #define BGFX_TEXTURE_SRGB UINT32_C(0x00200000) //!< -#define BGFX_TEXTURE_RESERVED_SHIFT 24 //!< -#define BGFX_TEXTURE_RESERVED_MASK UINT32_C(0xff000000) //!< +#define BGFX_TEXTURE_BORDER_COLOR_SHIFT 24 //!< +#define BGFX_TEXTURE_BORDER_COLOR_MASK UINT32_C(0x0f000000) //!< +#define BGFX_TEXTURE_RESERVED_SHIFT 28 //!< +#define BGFX_TEXTURE_RESERVED_MASK UINT32_C(0xf0000000) //!< + +#define BGFX_TEXTURE_BORDER_COLOR(_index) ( (_index << BGFX_TEXTURE_BORDER_COLOR_SHIFT) & BGFX_TEXTURE_BORDER_COLOR_MASK) #define BGFX_TEXTURE_SAMPLER_BITS_MASK (0 \ | BGFX_TEXTURE_U_MASK \ diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 4c16482c..df8541aa 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -1289,7 +1289,7 @@ namespace bgfx if (m_clearColorDirty > 0) { --m_clearColorDirty; - memcpy(m_submit->m_clearColor, m_clearColor, sizeof(m_clearColor) ); + memcpy(m_submit->m_colorPalette, m_clearColor, sizeof(m_clearColor) ); } m_submit->finish(); @@ -2891,7 +2891,7 @@ again: s_ctx->destroyUniform(_handle); } - void setClearColor(uint8_t _index, uint32_t _rgba) + void setPaletteColor(uint8_t _index, uint32_t _rgba) { BGFX_CHECK_MAIN_THREAD(); @@ -2907,20 +2907,20 @@ again: bb * 1.0f/255.0f, aa * 1.0f/255.0f, }; - s_ctx->setClearColor(_index, rgba); + s_ctx->setPaletteColor(_index, rgba); } - void setClearColor(uint8_t _index, float _r, float _g, float _b, float _a) + void setPaletteColor(uint8_t _index, float _r, float _g, float _b, float _a) { BGFX_CHECK_MAIN_THREAD(); float rgba[4] = { _r, _g, _b, _a }; - s_ctx->setClearColor(_index, rgba); + s_ctx->setPaletteColor(_index, rgba); } - void setClearColor(uint8_t _index, const float _rgba[4]) + void setPaletteColor(uint8_t _index, const float _rgba[4]) { BGFX_CHECK_MAIN_THREAD(); - s_ctx->setClearColor(_index, _rgba); + s_ctx->setPaletteColor(_index, _rgba); } bool checkView(uint8_t _id) @@ -3781,9 +3781,9 @@ BGFX_C_API void bgfx_destroy_uniform(bgfx_uniform_handle_t _handle) bgfx::destroyUniform(handle.cpp); } -BGFX_C_API void bgfx_set_clear_color(uint8_t _index, const float _rgba[4]) +BGFX_C_API void bgfx_set_palette_color(uint8_t _index, const float _rgba[4]) { - bgfx::setClearColor(_index, _rgba); + bgfx::setPaletteColor(_index, _rgba); } BGFX_C_API void bgfx_set_view_name(uint8_t _id, const char* _name) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 002a71ce..8cca5e45 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -1602,7 +1602,7 @@ namespace bgfx uint8_t m_viewRemap[BGFX_CONFIG_MAX_VIEWS]; FrameBufferHandle m_fb[BGFX_CONFIG_MAX_VIEWS]; Clear m_clear[BGFX_CONFIG_MAX_VIEWS]; - float m_clearColor[BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE][4]; + float m_colorPalette[BGFX_CONFIG_MAX_COLOR_PALETTE][4]; Rect m_rect[BGFX_CONFIG_MAX_VIEWS]; Rect m_scissor[BGFX_CONFIG_MAX_VIEWS]; Matrix4 m_view[BGFX_CONFIG_MAX_VIEWS]; @@ -3179,11 +3179,11 @@ namespace bgfx cmdbuf.write(_filePath, len); } - BGFX_API_FUNC(void setClearColor(uint8_t _index, const float _rgba[4]) ) + BGFX_API_FUNC(void setPaletteColor(uint8_t _index, const float _rgba[4]) ) { - BX_CHECK(_index < BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE, "Clear color palette index out of bounds %d (max: %d)." + BX_CHECK(_index < BGFX_CONFIG_MAX_COLOR_PALETTE, "Color palette index out of bounds %d (max: %d)." , _index - , BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE + , BGFX_CONFIG_MAX_COLOR_PALETTE ); memcpy(&m_clearColor[_index][0], _rgba, 16); m_clearColorDirty = 2; @@ -3684,7 +3684,7 @@ namespace bgfx FrameBufferHandle m_fb[BGFX_CONFIG_MAX_VIEWS]; Clear m_clear[BGFX_CONFIG_MAX_VIEWS]; - float m_clearColor[BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE][4]; + float m_clearColor[BGFX_CONFIG_MAX_COLOR_PALETTE][4]; Rect m_rect[BGFX_CONFIG_MAX_VIEWS]; Rect m_scissor[BGFX_CONFIG_MAX_VIEWS]; Matrix4 m_view[BGFX_CONFIG_MAX_VIEWS]; diff --git a/src/config.h b/src/config.h index 55656c33..199fc144 100644 --- a/src/config.h +++ b/src/config.h @@ -277,7 +277,7 @@ #endif // BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT #ifndef BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE -# define BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE 16 +# define BGFX_CONFIG_MAX_COLOR_PALETTE 16 #endif // BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE #define BGFX_CONFIG_DRAW_INDIRECT_STRIDE 32 diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index a4af202c..e7b7be0b 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -52,6 +52,7 @@ namespace bgfx { namespace d3d11 ID3D11ShaderResourceView* m_srv[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT]; ID3D11SamplerState* m_sampler[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT]; uint32_t m_zero[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT]; + float m_zerof[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT]; }; BX_PRAGMA_DIAGNOSTIC_PUSH(); @@ -1844,7 +1845,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); setShaderUniform(flags, predefined.m_loc, proj, 4); commitShaderConstants(); - m_textures[_blitter.m_texture.idx].commit(0); + m_textures[_blitter.m_texture.idx].commit(0, BGFX_SAMPLER_DEFAULT_FLAGS, NULL); commitTextureStage(); } @@ -2500,7 +2501,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); { _state &= BGFX_STATE_CULL_MASK|BGFX_STATE_MSAA; _state |= _wireframe ? BGFX_STATE_PT_LINES : BGFX_STATE_NONE; - _state |= _scissor ? BGFX_STATE_RESERVED_MASK : 0; + _state |= _scissor ? BGFX_STATE_RESERVED_MASK : 0; ID3D11RasterizerState* rs = m_rasterizerStateCache.find(_state); if (NULL == rs) @@ -2527,37 +2528,76 @@ BX_PRAGMA_DIAGNOSTIC_POP(); m_deviceCtx->RSSetState(rs); } - ID3D11SamplerState* getSamplerState(uint32_t _flags) + ID3D11SamplerState* getSamplerState(uint32_t _flags, const float _rgba[4]) { + const uint32_t index = (_flags & BGFX_TEXTURE_BORDER_COLOR_MASK) >> BGFX_TEXTURE_BORDER_COLOR_SHIFT; _flags &= BGFX_TEXTURE_SAMPLER_BITS_MASK; - ID3D11SamplerState* sampler = m_samplerStateCache.find(_flags); + + uint32_t hash; + ID3D11SamplerState* sampler; + if (BGFX_TEXTURE_U_BORDER != (_flags & BGFX_TEXTURE_U_BORDER) + && BGFX_TEXTURE_V_BORDER != (_flags & BGFX_TEXTURE_V_BORDER) + && BGFX_TEXTURE_W_BORDER != (_flags & BGFX_TEXTURE_W_BORDER) ) + { + bx::HashMurmur2A murmur; + murmur.begin(); + murmur.add(_flags); + murmur.add(-1); + hash = murmur.end(); + _rgba = s_zero.m_zerof; + + sampler = m_samplerStateCache.find(hash); + } + else + { + bx::HashMurmur2A murmur; + murmur.begin(); + murmur.add(_flags); + murmur.add(index); + hash = murmur.end(); + _rgba = NULL == _rgba ? s_zero.m_zerof : _rgba; + + sampler = m_samplerStateCache.find(hash); + if (NULL != sampler) + { + D3D11_SAMPLER_DESC sd; + sampler->GetDesc(&sd); + if (0 != memcmp(_rgba, sd.BorderColor, 16) ) + { + // Sampler will be released when updated sampler + // is added to cache. + sampler = NULL; + } + } + } + if (NULL == sampler) { - const uint32_t cmpFunc = (_flags&BGFX_TEXTURE_COMPARE_MASK)>>BGFX_TEXTURE_COMPARE_SHIFT; - const uint8_t minFilter = s_textureFilter[0][(_flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT]; - const uint8_t magFilter = s_textureFilter[1][(_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT]; - const uint8_t mipFilter = s_textureFilter[2][(_flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT]; - const uint8_t filter = 0 == cmpFunc ? 0 : D3D11_COMPARISON_FILTERING_BIT; + const uint32_t cmpFunc = (_flags&BGFX_TEXTURE_COMPARE_MASK)>>BGFX_TEXTURE_COMPARE_SHIFT; + const uint8_t minFilter = s_textureFilter[0][(_flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT]; + const uint8_t magFilter = s_textureFilter[1][(_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT]; + const uint8_t mipFilter = s_textureFilter[2][(_flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT]; + const uint8_t filter = 0 == cmpFunc ? 0 : D3D11_COMPARISON_FILTERING_BIT; D3D11_SAMPLER_DESC sd; - sd.Filter = (D3D11_FILTER)(filter|minFilter|magFilter|mipFilter); - sd.AddressU = s_textureAddress[(_flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT]; - sd.AddressV = s_textureAddress[(_flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT]; - sd.AddressW = s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT]; - sd.MipLODBias = 0.0f; + sd.Filter = (D3D11_FILTER)(filter|minFilter|magFilter|mipFilter); + sd.AddressU = s_textureAddress[(_flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT]; + sd.AddressV = s_textureAddress[(_flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT]; + sd.AddressW = s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT]; + sd.MipLODBias = 0.0f; sd.MaxAnisotropy = m_maxAnisotropy; sd.ComparisonFunc = 0 == cmpFunc ? D3D11_COMPARISON_NEVER : s_cmpFunc[cmpFunc]; - sd.BorderColor[0] = 0.0f; - sd.BorderColor[1] = 0.0f; - sd.BorderColor[2] = 0.0f; - sd.BorderColor[3] = 0.0f; + sd.BorderColor[0] = _rgba[0]; + sd.BorderColor[1] = _rgba[1]; + sd.BorderColor[2] = _rgba[2]; + sd.BorderColor[3] = _rgba[3]; sd.MinLOD = 0; sd.MaxLOD = D3D11_FLOAT32_MAX; m_device->CreateSamplerState(&sd, &sampler); DX_CHECK_REFCOUNT(sampler, 1); - m_samplerStateCache.add(_flags, sampler); + m_samplerStateCache.add(hash, sampler); } return sampler; @@ -3035,7 +3075,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); float mrtClear[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS][4]; for (uint32_t ii = 0; ii < numMrt; ++ii) { - uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE-1, _clear.m_index[ii]); + uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[ii]); memcpy(mrtClear[ii], _palette[index], 16); } @@ -3662,8 +3702,6 @@ BX_PRAGMA_DIAGNOSTIC_POP(); void TextureD3D11::create(const Memory* _mem, uint32_t _flags, uint8_t _skip) { - m_sampler = s_renderD3D11->getSamplerState(_flags); - ImageContainer imageContainer; if (imageParse(imageContainer, _mem->data, _mem->size) ) @@ -3953,14 +3991,18 @@ BX_PRAGMA_DIAGNOSTIC_POP(); } } - void TextureD3D11::commit(uint8_t _stage, uint32_t _flags) + void TextureD3D11::commit(uint8_t _stage, uint32_t _flags, const float _palette[][4]) { TextureStage& ts = s_renderD3D11->m_textureStage; ts.m_srv[_stage] = m_srv; - ts.m_sampler[_stage] = 0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) - ? s_renderD3D11->getSamplerState(_flags) - : m_sampler + uint32_t flags = 0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) + ? _flags + : m_flags ; + uint32_t index = (flags & BGFX_TEXTURE_BORDER_COLOR_MASK) >> BGFX_TEXTURE_BORDER_COLOR_SHIFT; + ts.m_sampler[_stage] = s_renderD3D11->getSamplerState(flags + , _palette[index]) + ; } void TextureD3D11::resolve() @@ -4451,7 +4493,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); if (BGFX_CLEAR_NONE != (clr.m_flags & BGFX_CLEAR_MASK) ) { - clearQuad(_clearQuad, viewState.m_rect, clr, _render->m_clearColor); + clearQuad(_clearQuad, viewState.m_rect, clr, _render->m_colorPalette); prim = s_primInfo[BX_COUNTOF(s_primName)]; // Force primitive type update after clear quad. } } @@ -4555,7 +4597,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); ? texture.m_srv : s_renderD3D11->getCachedSrv(texture.getHandle(), bind.m_un.m_compute.m_mip) ; - sampler[ii] = texture.m_sampler; + sampler[ii] = s_renderD3D11->getSamplerState(texture.m_flags, NULL); } } break; @@ -4819,7 +4861,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); if (invalidHandle != bind.m_idx) { TextureD3D11& texture = m_textures[bind.m_idx]; - texture.commit(stage, bind.m_un.m_draw.m_flags); + texture.commit(stage, bind.m_un.m_draw.m_flags, _render->m_colorPalette); } else { diff --git a/src/renderer_d3d11.h b/src/renderer_d3d11.h index 228d9e7e..d6c716f7 100644 --- a/src/renderer_d3d11.h +++ b/src/renderer_d3d11.h @@ -217,7 +217,6 @@ namespace bgfx { namespace d3d11 : m_ptr(NULL) , m_srv(NULL) , m_uav(NULL) - , m_sampler(NULL) , m_numMips(0) { } @@ -225,7 +224,7 @@ namespace bgfx { namespace d3d11 void create(const Memory* _mem, uint32_t _flags, uint8_t _skip); void destroy(); void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem); - void commit(uint8_t _stage, uint32_t _flags = BGFX_SAMPLER_DEFAULT_FLAGS); + void commit(uint8_t _stage, uint32_t _flags, const float _palette[][4]); void resolve(); TextureHandle getHandle() const; @@ -238,7 +237,6 @@ namespace bgfx { namespace d3d11 ID3D11ShaderResourceView* m_srv; ID3D11UnorderedAccessView* m_uav; - ID3D11SamplerState* m_sampler; uint32_t m_flags; uint8_t m_type; uint8_t m_requestedFormat; diff --git a/src/renderer_d3d12.cpp b/src/renderer_d3d12.cpp index e80bc46a..a2efa2e2 100644 --- a/src/renderer_d3d12.cpp +++ b/src/renderer_d3d12.cpp @@ -4351,7 +4351,7 @@ data.NumQualityLevels = 0; { Rect clearRect = rect; clearRect.intersect(rect, viewScissorRect); - clearQuad(clearRect, clr, _render->m_clearColor); + clearQuad(clearRect, clr, _render->m_colorPalette); } prim = s_primInfo[BX_COUNTOF(s_primName)]; // Force primitive type update. diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 2bd0a80e..3c4504ed 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -1083,7 +1083,7 @@ namespace bgfx { namespace d3d9 uint8_t flags = predefined.m_type; setShaderUniform(flags, predefined.m_loc, proj, 4); - m_textures[_blitter.m_texture.idx].commit(0); + m_textures[_blitter.m_texture.idx].commit(0, BGFX_SAMPLER_DEFAULT_FLAGS, NULL); } void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) BX_OVERRIDE @@ -1420,7 +1420,7 @@ namespace bgfx { namespace d3d9 } } - static void setSamplerState(IDirect3DDevice9* _device, DWORD _stage, D3DSAMPLERSTATETYPE _type,DWORD _value) + static void setSamplerState(IDirect3DDevice9* _device, DWORD _stage, D3DSAMPLERSTATETYPE _type, DWORD _value) { DX_CHECK(_device->SetSamplerState(_stage, _type, _value) ); if (4 > _stage) @@ -1429,7 +1429,7 @@ namespace bgfx { namespace d3d9 } } - void setSamplerState(uint8_t _stage, uint32_t _flags) + void setSamplerState(uint8_t _stage, uint32_t _flags, const float _rgba[4]) { const uint32_t flags = _flags&( (~BGFX_TEXTURE_RESERVED_MASK) | BGFX_TEXTURE_SAMPLER_BITS_MASK | BGFX_TEXTURE_SRGB); BX_CHECK(_stage < BX_COUNTOF(m_samplerFlags), ""); @@ -1444,7 +1444,6 @@ namespace bgfx { namespace d3d9 D3DTEXTUREFILTERTYPE magFilter = s_textureFilter[(_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT]; D3DTEXTUREFILTERTYPE mipFilter = s_textureFilter[(_flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT]; - setSamplerState(device, _stage, D3DSAMP_ADDRESSU, tau); setSamplerState(device, _stage, D3DSAMP_ADDRESSU, tau); setSamplerState(device, _stage, D3DSAMP_ADDRESSV, tav); setSamplerState(device, _stage, D3DSAMP_ADDRESSW, taw); @@ -1453,6 +1452,20 @@ namespace bgfx { namespace d3d9 setSamplerState(device, _stage, D3DSAMP_MIPFILTER, mipFilter); setSamplerState(device, _stage, D3DSAMP_MAXANISOTROPY, m_maxAnisotropy); setSamplerState(device, _stage, D3DSAMP_SRGBTEXTURE, 0 != (flags & BGFX_TEXTURE_SRGB) ); + if (NULL != _rgba) + { + if (BGFX_TEXTURE_U_BORDER == (_flags & BGFX_TEXTURE_U_BORDER) + || BGFX_TEXTURE_V_BORDER == (_flags & BGFX_TEXTURE_V_BORDER) + || BGFX_TEXTURE_W_BORDER == (_flags & BGFX_TEXTURE_W_BORDER) ) + { + DWORD bc = D3DCOLOR_COLORVALUE(_rgba[0], _rgba[1], _rgba[2], _rgba[3]); + setSamplerState(device + , _stage + , D3DSAMP_BORDERCOLOR + , bc + ); + } + } } } @@ -1671,7 +1684,7 @@ namespace bgfx { namespace d3d9 { if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags) { - uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE-1, _clear.m_index[0]); + uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[0]); const float* rgba = _palette[index]; const float rr = rgba[0]; const float gg = rgba[1]; @@ -1810,7 +1823,7 @@ namespace bgfx { namespace d3d9 float mrtClear[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS][4]; for (uint32_t ii = 0; ii < numMrt; ++ii) { - uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE-1, _clear.m_index[ii]); + uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[ii]); memcpy(mrtClear[ii], _palette[index], 16); } @@ -2547,8 +2560,6 @@ namespace bgfx { namespace d3d9 void TextureD3D9::create(const Memory* _mem, uint32_t _flags, uint8_t _skip) { - m_flags = _flags; - ImageContainer imageContainer; if (imageParse(imageContainer, _mem->data, _mem->size) ) @@ -2560,6 +2571,7 @@ namespace bgfx { namespace d3d9 const uint32_t textureWidth = bx::uint32_max(blockInfo.blockWidth, imageContainer.m_width >>startLod); const uint32_t textureHeight = bx::uint32_max(blockInfo.blockHeight, imageContainer.m_height>>startLod); + m_flags = _flags; m_requestedFormat = imageContainer.m_format; m_textureFormat = imageContainer.m_format; @@ -2692,7 +2704,7 @@ namespace bgfx { namespace d3d9 { uint32_t slicePitch; s_renderD3D9->m_updateTextureSide = _side; - s_renderD3D9->m_updateTextureMip = _mip; + s_renderD3D9->m_updateTextureMip = _mip; s_renderD3D9->m_updateTextureBits = lock(_side, _mip, s_renderD3D9->m_updateTexturePitch, slicePitch); } @@ -2743,11 +2755,15 @@ namespace bgfx { namespace d3d9 unlock(s_renderD3D9->m_updateTextureSide, s_renderD3D9->m_updateTextureMip); } - void TextureD3D9::commit(uint8_t _stage, uint32_t _flags) + void TextureD3D9::commit(uint8_t _stage, uint32_t _flags, const float _palette[][4]) { - s_renderD3D9->setSamplerState(_stage - , 0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) ? _flags : m_flags - ); + uint32_t flags = 0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) + ? _flags + : m_flags + ; + uint32_t index = (flags & BGFX_TEXTURE_BORDER_COLOR_MASK) >> BGFX_TEXTURE_BORDER_COLOR_SHIFT; + s_renderD3D9->setSamplerState(_stage, flags, _palette[index]); + IDirect3DDevice9* device = s_renderD3D9->m_device; DX_CHECK(device->SetTexture(_stage, m_ptr) ); if (4 > _stage) @@ -3267,7 +3283,7 @@ namespace bgfx { namespace d3d9 if (BGFX_CLEAR_NONE != (clear.m_flags & BGFX_CLEAR_MASK) ) { - clearQuad(_clearQuad, viewState.m_rect, clear, _render->m_clearColor); + clearQuad(_clearQuad, viewState.m_rect, clear, _render->m_colorPalette); prim = s_primInfo[BX_COUNTOF(s_primName)]; // Force primitive type update after clear quad. } @@ -3536,7 +3552,7 @@ namespace bgfx { namespace d3d9 { if (invalidHandle != bind.m_idx) { - m_textures[bind.m_idx].commit(stage, bind.m_un.m_draw.m_flags); + m_textures[bind.m_idx].commit(stage, bind.m_un.m_draw.m_flags, _render->m_colorPalette); } else { diff --git a/src/renderer_d3d9.h b/src/renderer_d3d9.h index ab4c9256..58676cec 100644 --- a/src/renderer_d3d9.h +++ b/src/renderer_d3d9.h @@ -334,7 +334,7 @@ namespace bgfx { namespace d3d9 void updateBegin(uint8_t _side, uint8_t _mip); void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem); void updateEnd(); - void commit(uint8_t _stage, uint32_t _flags = BGFX_SAMPLER_DEFAULT_FLAGS); + void commit(uint8_t _stage, uint32_t _flags, const float _palette[][4]); void resolve() const; void preReset(); diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index ff321b0d..c7855013 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -2519,21 +2519,55 @@ namespace bgfx { namespace gl } } - void setSamplerState(uint32_t _stage, uint32_t _numMips, uint32_t _flags) + void setSamplerState(uint32_t _stage, uint32_t _numMips, uint32_t _flags, const float _rgba[4]) { if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) ) { if (0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) ) { + const uint32_t index = (_flags & BGFX_TEXTURE_BORDER_COLOR_MASK) >> BGFX_TEXTURE_BORDER_COLOR_SHIFT; + _flags &= ~BGFX_TEXTURE_RESERVED_MASK; _flags &= BGFX_TEXTURE_SAMPLER_BITS_MASK; _flags |= _numMips<>BGFX_TEXTURE_U_SHIFT]) ); @@ -4210,8 +4263,10 @@ namespace bgfx { namespace gl if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || s_renderGL->m_borderColorSupport) { - const float color[] = { 0.0f, 0.0f, 0.0f, 0.0f }; - GL_CHECK(glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, color) ); + if (hasBorderColor) + { + GL_CHECK(glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _rgba) ); + } } if (0 != (flags & (BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC) ) @@ -4235,12 +4290,18 @@ namespace bgfx { namespace gl } } - m_currentFlags = flags; + m_currentSamplerHash = hash; } } - void TextureGL::commit(uint32_t _stage, uint32_t _flags) + void TextureGL::commit(uint32_t _stage, uint32_t _flags, const float _palette[][4]) { + const uint32_t flags = 0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) + ? _flags + : m_flags + ; + const uint32_t index = (flags & BGFX_TEXTURE_BORDER_COLOR_MASK) >> BGFX_TEXTURE_BORDER_COLOR_SHIFT; + GL_CHECK(glActiveTexture(GL_TEXTURE0+_stage) ); GL_CHECK(glBindTexture(m_target, m_id) ); @@ -4248,7 +4309,7 @@ namespace bgfx { namespace gl && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30) ) { // GLES2 doesn't have support for sampler object. - setSamplerState(_flags); + setSamplerState(flags, _palette[index]); } else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL < 31) ) @@ -4256,17 +4317,17 @@ namespace bgfx { namespace gl // In case that GL 2.1 sampler object is supported via extension. if (s_renderGL->m_samplerObjectSupport) { - s_renderGL->setSamplerState(_stage, m_numMips, _flags); + s_renderGL->setSamplerState(_stage, m_numMips, flags, _palette[index]); } else { - setSamplerState(_flags); + setSamplerState(flags, _palette[index]); } } else { // Everything else has sampler object. - s_renderGL->setSamplerState(_stage, m_numMips, _flags); + s_renderGL->setSamplerState(_stage, m_numMips, flags, _palette[index]); } } @@ -5188,7 +5249,7 @@ namespace bgfx { namespace gl if (BGFX_CLEAR_NONE != (clear.m_flags & BGFX_CLEAR_MASK) ) { - clearQuad(_clearQuad, viewState.m_rect, clear, height, _render->m_clearColor); + clearQuad(_clearQuad, viewState.m_rect, clear, height, _render->m_colorPalette); } GL_CHECK(glDisable(GL_STENCIL_TEST) ); @@ -5660,7 +5721,7 @@ namespace bgfx { namespace gl if (invalidHandle != bind.m_idx) { TextureGL& texture = m_textures[bind.m_idx]; - texture.commit(stage, bind.m_un.m_draw.m_flags); + texture.commit(stage, bind.m_un.m_draw.m_flags, _render->m_colorPalette); } } diff --git a/src/renderer_gl.h b/src/renderer_gl.h index fcc8b954..72476fa6 100644 --- a/src/renderer_gl.h +++ b/src/renderer_gl.h @@ -1074,7 +1074,7 @@ namespace bgfx { namespace gl , m_fmt(GL_ZERO) , m_type(GL_ZERO) , m_flags(0) - , m_currentFlags(UINT32_MAX) + , m_currentSamplerHash(UINT32_MAX) , m_numMips(0) { } @@ -1083,8 +1083,8 @@ namespace bgfx { namespace gl void create(const Memory* _mem, uint32_t _flags, uint8_t _skip); void destroy(); void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem); - void setSamplerState(uint32_t _flags); - void commit(uint32_t _stage, uint32_t _flags); + void setSamplerState(uint32_t _flags, const float _rgba[4]); + void commit(uint32_t _stage, uint32_t _flags, const float _palette[][4]); GLuint m_id; GLuint m_rbo; @@ -1092,7 +1092,7 @@ namespace bgfx { namespace gl GLenum m_fmt; GLenum m_type; uint32_t m_flags; - uint32_t m_currentFlags; + uint32_t m_currentSamplerHash; uint32_t m_width; uint32_t m_height; uint32_t m_depth;