Skip to content

Commit 708cb73

Browse files
committed
TextureComparer: multiple slots per WindowData
Signed-off-by: Xarblu <xarblu@protonmail.com>
1 parent f85e6aa commit 708cb73

4 files changed

Lines changed: 62 additions & 30 deletions

File tree

src/blur_cache.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "texture_comparer.hpp"
88

99
#include <epoxy/gl.h>
10-
#include <qloggingcategory.h>
1110
#include <scene/scene.h>
1211
#include <sys/types.h>
1312

@@ -128,7 +127,7 @@ BBDX::BlurCacheEntry* BBDX::BlurCacheLRU::get() {
128127
return m_entry.get();
129128
}
130129

131-
const BBDX::TextureComparer::WindowData* BBDX::BlurCacheLRU::textureCompareWindowData() {
130+
BBDX::TextureComparer::WindowData* BBDX::BlurCacheLRU::textureCompareWindowData() {
132131
// alloc only happens once per Window+RenderView combination
133132
if (!m_textureCompareWindowData) [[unlikely]] {
134133
m_textureCompareWindowData = TextureComparer::WindowData::create();
@@ -305,23 +304,25 @@ void BBDX::BlurCache::prepareCache(BBDX::BlurCacheLRU &cache) {
305304
return;
306305
}
307306

308-
const auto textureCompareWindowData = cache.textureCompareWindowData();
307+
auto textureCompareWindowData = cache.textureCompareWindowData();
309308
if (!textureCompareWindowData) [[unlikely]] {
310309
// GL resource alloc failed
311310
return;
312311
}
313312

313+
const auto textureCompareWindowDataSlot = textureCompareWindowData->getSlot();
314+
314315
const auto newTexture = m_paintData.blitFramebuffer->colorAttachment();
315316
const auto cachedTexture = cacheEntry->blitTexture.get();
316317

317-
m_textureComparer->compareAndUpdate(textureCompareWindowData,
318+
m_textureComparer->compareAndUpdate(textureCompareWindowDataSlot,
318319
newTexture,
319320
cachedTexture,
320321
cacheEntry->localDirtyRegionGL(*m_paintData.dirtyRegion),
321322
m_paintData.window);
322323

323324
// await the query from TextureComparer::compareAndUpdate()
324-
glBeginConditionalRender(textureCompareWindowData->query, GL_QUERY_BY_REGION_WAIT);
325+
glBeginConditionalRender(textureCompareWindowDataSlot.second, GL_QUERY_BY_REGION_WAIT);
325326
m_paintData.glBeginConditionalRenderCalled = true;
326327

327328
// *if* the texture changed we need to ensure it's fully flushed

src/blur_cache.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class BlurCacheLRU {
128128
* Return const pointer to the contained texture compare
129129
* region and lazily create it if needed
130130
*/
131-
const TextureComparer::WindowData* textureCompareWindowData();
131+
TextureComparer::WindowData* textureCompareWindowData();
132132

133133
/**
134134
* Add an entry to the cache, potentially removing the already existing entry.

src/texture_comparer.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <memory>
1717
#include <unordered_map>
18+
#include <array>
1819

1920
Q_LOGGING_CATEGORY(BBDX_TEXTURE_COMPARER, "kwin_effect_better_blur_dx.texture_comparer", QtInfoMsg)
2021

@@ -48,28 +49,34 @@ static inline const char* glslFormatString(GLenum internalFormat) {
4849
std::unique_ptr<BBDX::TextureComparer::WindowData> BBDX::TextureComparer::WindowData::create() {
4950
std::unique_ptr<WindowData> windowData{new WindowData{}};
5051

51-
glGenBuffers(1, &windowData->counterBuffer);
52+
glGenBuffers(SLOTS, windowData->m_counterBuffers.data());
53+
glGenQueries(SLOTS, windowData->m_queries.data());
5254

5355
// allocate a single GLuint (the change counter)
54-
glBindBuffer(GL_SHADER_STORAGE_BUFFER, windowData->counterBuffer);
55-
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLuint), nullptr, GL_DYNAMIC_DRAW);
56+
for (const auto &buffer : windowData->m_counterBuffers) {
57+
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
58+
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLuint), nullptr, GL_DYNAMIC_DRAW);
59+
}
5660
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
5761

58-
glGenQueries(1, &windowData->query);
59-
6062
return windowData;
6163
}
6264

6365
BBDX::TextureComparer::WindowData::~WindowData() {
64-
if (counterBuffer > 0) {
65-
glDeleteBuffers(1, &counterBuffer);
66-
}
66+
glDeleteBuffers(SLOTS, m_counterBuffers.data());
67+
glDeleteQueries(SLOTS, m_queries.data());
68+
}
69+
6770

68-
if (query > 0) {
69-
glDeleteQueries(1, &query);
71+
std::pair<GLuint, GLuint> BBDX::TextureComparer::WindowData::getSlot() {
72+
const int slot = m_nextSlot;
73+
74+
if (++m_nextSlot >= SLOTS) {
75+
m_nextSlot = 0;
7076
}
71-
}
7277

78+
return {m_counterBuffers[slot], m_queries[slot]};
79+
}
7380

7481
std::unique_ptr<BBDX::TextureComparer::ComputeShader> BBDX::TextureComparer::buildComputeShader(GLenum textureFormat) {
7582
qCDebug(BBDX_TEXTURE_COMPARER) << "Creating texture compare instance for" << glslFormatString(textureFormat);
@@ -180,11 +187,14 @@ std::unique_ptr<BBDX::TextureComparer> BBDX::TextureComparer::create() {
180187
return textureComparer;
181188
}
182189

183-
void BBDX::TextureComparer::compareAndUpdate(const WindowData *windowData, KWin::GLTexture *freshBlit, KWin::GLTexture *cachedBlit, const KWin::Region &localDirtyRegionGL, const KWin::EffectWindow *window) {
190+
void BBDX::TextureComparer::compareAndUpdate(const std::pair<GLuint, GLuint> &windowDataSlot, KWin::GLTexture *freshBlit, KWin::GLTexture *cachedBlit, const KWin::Region &localDirtyRegionGL, const KWin::EffectWindow *window) {
184191
#if !defined(BBDX_DEBUG)
185192
Q_UNUSED(window);
186193
#endif
187194

195+
const GLuint counterBuffer{windowDataSlot.first};
196+
const GLuint query{windowDataSlot.second};
197+
188198
const auto textureFormat = freshBlit->internalFormat();
189199

190200
// lazily create compute shader instances in case we need
@@ -212,10 +222,10 @@ void BBDX::TextureComparer::compareAndUpdate(const WindowData *windowData, KWin:
212222

213223
// reset and bind counter
214224
const GLuint zero = 0;
215-
glBindBuffer(GL_SHADER_STORAGE_BUFFER, windowData->counterBuffer);
225+
glBindBuffer(GL_SHADER_STORAGE_BUFFER, counterBuffer);
216226
glClearBufferSubData(GL_SHADER_STORAGE_BUFFER, GL_R32UI, 0, sizeof(GLuint), GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
217227
// slot 2 - matching compute shader
218-
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, windowData->counterBuffer);
228+
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, counterBuffer);
219229

220230
// prepare compute shader
221231
GLint prevProgram{};
@@ -262,7 +272,7 @@ void BBDX::TextureComparer::compareAndUpdate(const WindowData *windowData, KWin:
262272
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
263273
glDepthMask(GL_FALSE);
264274

265-
glBeginQuery(GL_ANY_SAMPLES_PASSED, windowData->query);
275+
glBeginQuery(GL_ANY_SAMPLES_PASSED, query);
266276
glDrawArrays(GL_POINTS, 0, 1);
267277
glEndQuery(GL_ANY_SAMPLES_PASSED);
268278

src/texture_comparer.hpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <memory>
1313
#include <unordered_map>
14+
#include <array>
1415

1516
namespace BBDX {
1617

@@ -19,20 +20,33 @@ class TextureComparer {
1920
/**
2021
* Per Window (BlurCacheLRU) data
2122
*/
22-
struct WindowData {
23-
private:
23+
class WindowData {
24+
/**
25+
* Amount of slots per window
26+
*/
27+
static constexpr int SLOTS{3};
28+
29+
/**
30+
* Next slot to return
31+
*/
32+
int m_nextSlot{0};
33+
34+
/**
35+
* SSBO counting changed blocks
36+
*/
37+
std::array<GLuint, SLOTS> m_counterBuffers{};
38+
39+
/**
40+
* GL query object used for conditional render
41+
*/
42+
std::array<GLuint, SLOTS> m_queries{};
43+
2444
/**
2545
* Use create()
2646
*/
2747
WindowData() = default;
2848

2949
public:
30-
// SSBO counting changed blocks
31-
GLuint counterBuffer{0};
32-
33-
// GL query object used for conditional render
34-
GLuint query{0};
35-
3650
/**
3751
* Create WindowData
3852
* nullptr on error
@@ -43,6 +57,11 @@ class TextureComparer {
4357
* Cleanup GL resources
4458
*/
4559
~WindowData();
60+
61+
/**
62+
* Get a matching {counterBuffer, query} tuple
63+
*/
64+
std::pair<GLuint, GLuint> getSlot();
4665
};
4766

4867
private:
@@ -91,13 +110,15 @@ class TextureComparer {
91110
* Compare and update cachedBlit with freshBlit
92111
* within the localDirtyRegion (in GL coords)
93112
*
113+
* windowDataSlot is a pair as returned by WindowData::getSlot()
114+
*
94115
* The EffectWindow is optional and only used
95116
* for extra logging in the debug build (BBDX_DEBUG)
96117
*
97118
* The result of the comparison can be found using the
98119
* query object returned by queryObject()
99120
*/
100-
void compareAndUpdate(const WindowData *windowData, KWin::GLTexture *freshBlit, KWin::GLTexture *cachedBlit, const KWin::Region &localDirtyRegionGL, const KWin::EffectWindow *window = nullptr);
121+
void compareAndUpdate(const std::pair<GLuint, GLuint> &windowDataSlot, KWin::GLTexture *freshBlit, KWin::GLTexture *cachedBlit, const KWin::Region &localDirtyRegionGL, const KWin::EffectWindow *window = nullptr);
101122
};
102123

103124
}

0 commit comments

Comments
 (0)