BioGTK  6.5.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
SlideGLArea.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.InteropServices;
5using BruTile;
6using Gtk;
7using OpenTK.Graphics.OpenGL;
8using SkiaSharp;
9using AForge;
10using PixelFormat = OpenTK.Graphics.OpenGL.PixelFormat;
11
12namespace BioGTK
13{
19 public class SlideGLArea : GLArea
20 {
21 // ============================================================================
22 // GL Resources
23 // ============================================================================
24
25 private bool _glInitialized;
26 private int _vao;
27 private int _vbo;
28 private int _shaderProgram;
29
30 // Uniform locations (cached for performance)
31 private int _locPos;
32 private int _locSize;
33 private int _locViewportSize;
34 private int _locTex;
35
36 // ============================================================================
37 // Texture Cache - tiles uploaded to GPU
38 // ============================================================================
39
40 private Dictionary<TileIndex, int> _textureCache = new();
41 private const int MAX_CACHED_TEXTURES = 500;
42
43 // ============================================================================
44 // Skia GL Backend for annotations
45 // ============================================================================
46
47 private GRContext _grContext;
48 private GRBackendRenderTarget _renderTarget;
49 private SKSurface _skSurface;
50
51 // ============================================================================
52 // Rendering State
53 // ============================================================================
54
55 public List<TileRenderInfo> TilesToRender { get; } = new();
56 public bool NeedsRedraw { get; set; } = true;
57
61 public event Action<SKCanvas, int, int> OnSkiaRender;
62
63 // ============================================================================
64 // Shaders
65 // ============================================================================
66
67 private const string VertexShaderSource = @"
68#version 330 core
69
70layout(location=0) in vec2 aPos;
71layout(location=1) in vec2 aUV;
72
73uniform vec2 pos; // pixel-space top-left of tile
74uniform vec2 size; // pixel-space size of tile
75uniform vec2 viewportSize; // (width, height) in pixels
76
77out vec2 uv;
78
79void main()
80{
81 // aPos is in [0,1] x [0,1]
82 vec2 pixelPos = aPos * size + pos;
83
84 // Convert from pixel coords to NDC (-1..1), with Y flipped for GL
85 vec2 ndc;
86 ndc.x = (pixelPos.x / viewportSize.x) * 2.0 - 1.0;
87 ndc.y = 1.0 - (pixelPos.y / viewportSize.y) * 2.0;
88
89 gl_Position = vec4(ndc, 0.0, 1.0);
90 uv = aUV;
91}
92";
93
94 private const string FragmentShaderSource = @"
95#version 330 core
96
97in vec2 uv;
98out vec4 FragColor;
99uniform sampler2D tex;
100
101void main()
102{
103 // Fix: Removed '1.0 - uv.y'.
104 // Image data is usually Top-Down. OpenGL Textures are Bottom-Up.
105 // This implicit flip means UV(0,0) (Top-Left Quad) maps to Texture Bottom (Image Top).
106 // This is the correct orientation for Top-Left origin rendering.
107 FragColor = texture(tex, vec2(uv.x, uv.y));
108}
109";
110
111 // ============================================================================
112 // Construction
113 // ============================================================================
114
115 public SlideGLArea()
116 {
117 HasDepthBuffer = false;
118 HasStencilBuffer = false;
119 AutoRender = false; // We control when to render
120
121 Realized += OnRealized;
122 //Unrealize += OnUnrealize;
123 Render += OnRender;
124 Resize += OnResized;
125 }
126 // ============================================================================
127 // GLArea Lifecycle
128 // ============================================================================
129
130 private void OnRealized(object sender, EventArgs e)
131 {
132 MakeCurrent();
133
134 if (Error != 0)
135 {
136 Console.WriteLine($"GLArea error on realize.");
137 return;
138 }
139
140 InitializeGL();
141 InitializeSkia();
142
143 _glInitialized = true;
144 }
145
146 private void OnResized(object sender, EventArgs e)
147 {
148 if (!_glInitialized) return;
149
150 MakeCurrent();
151
152 // Recreate Skia surface for new size
153 CleanupSkia();
154 InitializeSkia();
155
156 NeedsRedraw = true;
157 }
158
159 // ============================================================================
160 // GL Initialization
161 // ============================================================================
162
163 private void InitializeGL()
164 {
165 // Compile shaders
166 int vertexShader = CompileShader(ShaderType.VertexShader, VertexShaderSource);
167 int fragmentShader = CompileShader(ShaderType.FragmentShader, FragmentShaderSource);
168
169 _shaderProgram = GL.CreateProgram();
170 GL.AttachShader(_shaderProgram, vertexShader);
171 GL.AttachShader(_shaderProgram, fragmentShader);
172 GL.LinkProgram(_shaderProgram);
173
174 GL.GetProgram(_shaderProgram, GetProgramParameterName.LinkStatus, out int linkStatus);
175 if (linkStatus == 0)
176 {
177 string infoLog = GL.GetProgramInfoLog(_shaderProgram);
178 throw new Exception($"Shader link failed: {infoLog}");
179 }
180
181 GL.DeleteShader(vertexShader);
182 GL.DeleteShader(fragmentShader);
183
184 // Cache uniform locations
185 _locPos = GL.GetUniformLocation(_shaderProgram, "pos");
186 _locSize = GL.GetUniformLocation(_shaderProgram, "size");
187 _locViewportSize = GL.GetUniformLocation(_shaderProgram, "viewportSize");
188 _locTex = GL.GetUniformLocation(_shaderProgram, "tex");
189
190 // Create quad VAO/VBO for tile rendering
191 float[] quadVertices =
192 {
193 // pos uv
194 0, 0, 0, 0,
195 1, 0, 1, 0,
196 1, 1, 1, 1,
197
198 0, 0, 0, 0,
199 1, 1, 1, 1,
200 0, 1, 0, 1
201 };
202
203 _vao = GL.GenVertexArray();
204 _vbo = GL.GenBuffer();
205
206 GL.BindVertexArray(_vao);
207 GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo);
208 GL.BufferData(BufferTarget.ArrayBuffer, quadVertices.Length * sizeof(float),
209 quadVertices, BufferUsageHint.StaticDraw);
210
211 // Position attribute
212 GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0);
213 GL.EnableVertexAttribArray(0);
214
215 // UV attribute
216 GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2 * sizeof(float));
217 GL.EnableVertexAttribArray(1);
218
219 GL.BindVertexArray(0);
220 }
221
222 private int CompileShader(ShaderType type, string source)
223 {
224 int shader = GL.CreateShader(type);
225 GL.ShaderSource(shader, source);
226 GL.CompileShader(shader);
227
228 GL.GetShader(shader, ShaderParameter.CompileStatus, out int status);
229 if (status == 0)
230 {
231 string infoLog = GL.GetShaderInfoLog(shader);
232 throw new Exception($"Shader compilation failed ({type}): {infoLog}");
233 }
234
235 return shader;
236 }
237
238 private void CleanupGL()
239 {
240 if (_shaderProgram != 0)
241 {
242 GL.DeleteProgram(_shaderProgram);
243 _shaderProgram = 0;
244 }
245
246 if (_vbo != 0)
247 {
248 GL.DeleteBuffer(_vbo);
249 _vbo = 0;
250 }
251
252 if (_vao != 0)
253 {
254 GL.DeleteVertexArray(_vao);
255 _vao = 0;
256 }
257
259 }
260
261 // ============================================================================
262 // Skia Initialization
263 // ============================================================================
264
265 private void InitializeSkia()
266 {
267 _grContext = GRContext.CreateGl();
268 // Get the current FBO ID (usually 0 in GLArea, but not always)
269 GL.GetInteger(GetPName.FramebufferBinding, out int fbo);
270
271 // Ensure the format matches what Gtk.GLArea provides (usually GL_RGBA8)
272 var framebufferInfo = new GRGlFramebufferInfo((uint)fbo, 0x8058); // GL_RGBA8
273
274 _renderTarget = new GRBackendRenderTarget(
275 AllocatedWidth * ScaleFactor,
276 AllocatedHeight * ScaleFactor,
277 0, 8, framebufferInfo);
278
279 _skSurface = SKSurface.Create(_grContext, _renderTarget, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
280 }
281
282 private void CleanupSkia()
283 {
284 _skSurface?.Dispose();
285 _skSurface = null;
286
287 _renderTarget?.Dispose();
288 _renderTarget = null;
289
290 _grContext?.Dispose();
291 _grContext = null;
292 }
293
294 // ============================================================================
295 // Rendering
296 // ============================================================================
297
298 private void OnRender(object sender, RenderArgs args)
299 {
300 if (!_glInitialized) return;
301
302 MakeCurrent();
303
304 int width = AllocatedWidth;
305 int height = AllocatedHeight;
306
307 if (width <= 0 || height <= 0) return;
308
309 // Phase 1: Render tiles with OpenGL
310 RenderTiles(width, height);
311
312 // Phase 2: Render annotations with Skia
313 RenderSkiaOverlay(width, height);
314
315 // Flush Skia to ensure all drawing is done
316 _grContext?.Flush();
317 }
318
319 private void RenderTiles(int width, int height)
320 {
321 // Multiply by ScaleFactor to handle High-DPI screens correctly
322 int scale = ScaleFactor;
323 GL.Viewport(0, 0, width * scale, height * scale);
324 GL.ClearColor(0.2f, 0.2f, 0.2f, 1f);
325 GL.Clear(ClearBufferMask.ColorBufferBit);
326
327 if (TilesToRender.Count == 0)
328 {
329 Console.WriteLine("WARNING: No tiles to render!");
330 return;
331 }
332
333 Console.WriteLine($"Rendering {TilesToRender.Count} tiles at viewport {width}x{height}");
334
335 GL.Enable(EnableCap.Blend);
336 GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
337
338 GL.UseProgram(_shaderProgram);
339 GL.BindVertexArray(_vao);
340
341 // Set viewport size uniform (constant for all tiles this frame)
342 GL.Uniform2(_locViewportSize, (float)width, (float)height);
343 GL.Uniform1(_locTex, 0);
344
345 GL.ActiveTexture(TextureUnit.Texture0);
346
347 int renderedCount = 0;
348 foreach (var tile in TilesToRender)
349 {
350 if (!_textureCache.TryGetValue(tile.Index, out int texId))
351 {
352 Console.WriteLine($"WARNING: Tile {tile.Index} not in texture cache!");
353 continue;
354 }
355
356 GL.Uniform2(_locPos, tile.ScreenX, tile.ScreenY);
357 GL.Uniform2(_locSize, tile.ScreenWidth, tile.ScreenHeight);
358
359 Console.WriteLine($"Rendering tile {tile.Index}: pos=({tile.ScreenX}, {tile.ScreenY}), size=({tile.ScreenWidth}, {tile.ScreenHeight})");
360
361 GL.BindTexture(TextureTarget.Texture2D, texId);
362 GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
363 renderedCount++;
364 }
365
366 Console.WriteLine($"Successfully rendered {renderedCount} tiles");
367
368 GL.BindVertexArray(0);
369 GL.UseProgram(0);
370 GL.Disable(EnableCap.Blend);
371 }
372
373 private void RenderSkiaOverlay(int width, int height)
374 {
375 if (_skSurface == null || OnSkiaRender == null) return;
376 InitializeSkia();
377 var canvas = _skSurface.Canvas;
378 // Fire event for annotation drawing
379 OnSkiaRender?.Invoke(canvas, width, height);
380 _grContext.Flush();
381 }
382
383 // ============================================================================
384 // Texture Management
385 // ============================================================================
386
390 public void UploadTileTexture(TileIndex index, byte[] pixelData, int tileWidth, int tileHeight)
391 {
392 if (!_glInitialized)
393 {
394 Console.WriteLine("WARNING: GL not initialized, cannot upload texture");
395 return;
396 }
397
398 if (pixelData == null || pixelData.Length == 0)
399 {
400 Console.WriteLine($"WARNING: Empty pixel data for tile {index}");
401 return;
402 }
403
404 MakeCurrent();
405
406 // Check if already cached
407 if (_textureCache.ContainsKey(index))
408 {
409 Console.WriteLine($"Tile {index} already cached");
410 return;
411 }
412
413 // Evict old textures if cache is full
414 if (_textureCache.Count >= MAX_CACHED_TEXTURES)
415 {
416 EvictOldestTextures(MAX_CACHED_TEXTURES / 4);
417 }
418
419 // Create and upload texture
420 int tex = GL.GenTexture();
421 GL.BindTexture(TextureTarget.Texture2D, tex);
422
423 GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
424
425 // CRITICAL FIX: Use Rgba instead of Bgra for internal format consistency
426 GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
427 tileWidth, tileHeight, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixelData);
428
429 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
430 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
431 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
432 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
433
434 GL.BindTexture(TextureTarget.Texture2D, 0);
435
436 _textureCache[index] = tex;
437 Console.WriteLine($"Uploaded tile {index} as texture {tex} ({tileWidth}x{tileHeight}, {pixelData.Length} bytes)");
438 }
439
443 public bool HasTileTexture(TileIndex index)
444 {
445 return _textureCache.ContainsKey(index);
446 }
447
451 public void ReleaseTileTexture(TileIndex index)
452 {
453 if (_textureCache.TryGetValue(index, out int tex))
454 {
455 MakeCurrent();
456 GL.DeleteTexture(tex);
457 _textureCache.Remove(index);
458 }
459 }
460
464 public void ReleaseLevelTextures(int level)
465 {
466 if (!_glInitialized) return;
467
468 MakeCurrent();
469
470 var toRemove = _textureCache.Where(kvp => kvp.Key.Level == level).ToList();
471 foreach (var kvp in toRemove)
472 {
473 GL.DeleteTexture(kvp.Value);
474 _textureCache.Remove(kvp.Key);
475 }
476 }
477
481 public void ClearTextureCache()
482 {
483 if (!_glInitialized) return;
484
485 MakeCurrent();
486
487 foreach (var tex in _textureCache.Values)
488 {
489 GL.DeleteTexture(tex);
490 }
491 _textureCache.Clear();
492 }
493
494 private void EvictOldestTextures(int count)
495 {
496 // Simple FIFO eviction - could be improved with LRU tracking
497 var toRemove = _textureCache.Take(count).ToList();
498 foreach (var kvp in toRemove)
499 {
500 GL.DeleteTexture(kvp.Value);
501 _textureCache.Remove(kvp.Key);
502 }
503 }
504
505 // ============================================================================
506 // Public API
507 // ============================================================================
508
512 public void RequestRedraw()
513 {
514 NeedsRedraw = true;
515 QueueRender();
516 }
517
521 public void SetTilesToRender(IEnumerable<TileRenderInfo> tiles)
522 {
523 TilesToRender.Clear();
524 TilesToRender.AddRange(tiles);
525 Console.WriteLine($"Set {TilesToRender.Count} tiles to render");
526 }
527
532 public byte[] ReadPixels()
533 {
534 if (!_glInitialized) return null;
535
536 MakeCurrent();
537
538 int width = AllocatedWidth;
539 int height = AllocatedHeight;
540
541 byte[] pixels = new byte[width * height * 4];
542 GL.ReadPixels(0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, pixels);
543
544 return pixels;
545 }
546 }
547
548 // ============================================================================
549 // Supporting Types
550 // ============================================================================
551
555 public struct TileRenderInfo
556 {
557 public TileIndex Index;
558
559 // Screen-space position and size in pixels
560 public float ScreenX;
561 public float ScreenY;
562 public float ScreenWidth;
563 public float ScreenHeight;
564
565 public TileRenderInfo(TileIndex index, float x, float y, float w, float h)
566 {
567 Index = index;
568 ScreenX = x;
569 ScreenY = y;
570 ScreenWidth = w;
571 ScreenHeight = h;
572 }
573 }
574}
A GTK GLArea widget that renders pyramidal slide tiles directly on the GPU. Eliminates ReadPixels ove...
void RequestRedraw()
Request a redraw of the GLArea.
void UploadTileTexture(TileIndex index, byte[] pixelData, int tileWidth, int tileHeight)
Upload a tile texture to the GPU. Call from the main thread.
bool HasTileTexture(TileIndex index)
Check if a tile texture is already in the GPU cache.
byte[] ReadPixels()
Read pixels from the current framebuffer (for export/save operations). This is slow - only use for ex...
Action< SKCanvas, int, int > OnSkiaRender
Event fired during Skia rendering phase for annotation drawing.
void ClearTextureCache()
Clear all cached textures.
void ReleaseLevelTextures(int level)
Release all textures for a specific pyramid level.
void SetTilesToRender(IEnumerable< TileRenderInfo > tiles)
Prepare tiles for rendering. Call before RequestRedraw().
void ReleaseTileTexture(TileIndex index)
Release a specific tile texture.
Definition ML.cs:17
Information needed to render a single tile.