1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkVolumeTexture.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/**
* @class vtkVolumeTexture
* @brief Creates and manages the volume texture rendered by
* vtkOpenGLGPUVolumeRayCastMapper.
*
* Wraps a vtkTextureObject for which it selects the appropriate format
* (depending on the input vtkDataArray type, number of components, etc.) and
* loads input data. The class maintains a set of members of interest to the
* parent mapper, such as:
*
* * Active vtkDataArray scalar range.
* * Volume's scale and bias (pixel transfer functions).
* * HandleLargeDataType flag.
* * Texture to data transformations.
* * Block extents
* * Block loaded bounds
*
* This class supports streaming the volume data in separate blocks to make it
* fit in graphics memory (sometimes referred to as bricking). The data is split
* into a user-defined number of blocks in such a way that a single sub-block
* (brick) fits completely into GPU memory. A stride is passed to OpenGL so
* that it can access the underlying vtkDataArray adequately for each of the
* blocks to be streamed into GPU memory (back-to-front for correct
* composition).
*
* Streaming the volume as separate texture bricks certainly imposes a
* performance trade-off but acts as a graphics memory expansion scheme for
* devices that would not be able to render higher resoulution volumes
* otherwise.
*
* @warning There are certain caveats when texture streaming is enabled, given
* the locality constraint that rendering a single block imposes.
*
* - Quality might suffer near the block seams with ShadeOn() (gradient
* computation at the boundaries needs adjustment).
*
* - Not all of the features supported by the mapper currently work correctly.
* This is a list of known issues:
* - Blending modes such as average and additive might compute a different
* value near the edges.
*
* - Future work will extend the API to be able to compute an ideal number of
* partitions and extents based on the platform capabilities.
*
* @warning This is an internal class of vtkOpenGLGPUVolumeRayCastMapper. It
* assumes there is an active OpenGL context in methods involving GL calls
* (MakeCurrent() is expected to be called in the mapper beforehand).
*/
#ifndef vtkVolumeTexture_h
#define vtkVolumeTexture_h
#include <map> // For ImageDataBlockMap
#include <vector> // For ImageDataBlocks
#include "vtkMatrix4x4.h" // For vtkMatrix4
#include "vtkNew.h" // For vtkNew
#include "vtkObject.h"
#include "vtkRenderingVolumeOpenGL2Module.h" // For export macro
#include "vtkSmartPointer.h" // For SmartPointer
#include "vtkTimeStamp.h" // For UploadTime
#include "vtkTuple.h" // For Size6 and Size3
VTK_ABI_NAMESPACE_BEGIN
class vtkDataArray;
class vtkDataSet;
class vtkImageData;
class vtkRenderer;
class vtkTextureObject;
class vtkVolumeProperty;
class vtkWindow;
class VTKRENDERINGVOLUMEOPENGL2_EXPORT vtkVolumeTexture : public vtkObject
{
typedef vtkTuple<int, 6> Size6;
typedef vtkTuple<int, 3> Size3;
public:
static vtkVolumeTexture* New();
struct VolumeBlock
{
VolumeBlock(vtkDataSet* dataset, vtkTextureObject* tex, Size3 const& texSize)
{
// Block extent is stored in vtkDataSet
DataSet = dataset;
TextureObject = tex;
TextureSize = texSize;
TupleIndex = 0;
this->Extents[0] = VTK_INT_MAX;
this->Extents[1] = VTK_INT_MIN;
this->Extents[2] = VTK_INT_MAX;
this->Extents[3] = VTK_INT_MIN;
this->Extents[4] = VTK_INT_MAX;
this->Extents[5] = VTK_INT_MIN;
}
vtkDataSet* DataSet;
vtkTextureObject* TextureObject;
Size3 TextureSize;
vtkIdType TupleIndex;
vtkNew<vtkMatrix4x4> TextureToDataset;
vtkNew<vtkMatrix4x4> TextureToDatasetInv;
float CellStep[3];
double DatasetStepSize[3];
/**
* LoadedBounds are corrected for cell-data (if that is the case). So they
* are not equivalent to vtkDataSet::GetBounds().
*/
double LoadedBounds[6];
double LoadedBoundsAA[6];
double VolumeGeometry[24];
int Extents[6];
};
vtkTypeMacro(vtkVolumeTexture, vtkObject);
void PrintSelf(ostream& os, vtkIndent indent) override;
/**
* Set a number of blocks per axis.
*/
void SetPartitions(int const x, int const y, int const z);
const Size3& GetPartitions();
/**
* Loads the data array into the texture in the case only a single block is
* is defined. Does not load when the input data is divided in multiple blocks
* (in which case they will be loaded into GPU memory by GetNextBlock()).
* Requires an active OpenGL context.
*/
bool LoadVolume(vtkRenderer* ren, vtkDataSet* data, vtkDataArray* scalars, int const isCell,
int const interpolation);
/**
* It currently only calls SetInterpolation internally. Requires an active OpenGL
* context.
*/
void UpdateVolume(vtkVolumeProperty* property);
/**
* If streaming the data array as separate blocks, sort them back to front.
* This method does nothing if there is a single block.
*/
void SortBlocksBackToFront(vtkRenderer* ren, vtkMatrix4x4* volumeMat);
/**
* Return the next volume block to be rendered and load its data. If the
* current block is the last one, it will return nullptr.
*/
VolumeBlock* GetNextBlock();
/**
* Return the currently loaded block.
*/
VolumeBlock* GetCurrentBlock();
/**
* Clean-up acquired graphics resources.
*/
void ReleaseGraphicsResources(vtkWindow* win);
/**
* Get the scale and bias values given a VTK scalar type and a finite range.
* The scale and bias values computed using this method can be useful for
* custom shader code. For example, when looking up color values through the
* transfer function texture, the scalar value must be scaled and offset.
*/
static void GetScaleAndBias(const int scalarType, float* scalarRange, float& scale, float& bias);
vtkDataArray* GetLoadedScalars();
bool HandleLargeDataTypes;
float Scale[4];
float Bias[4];
float ScalarRange[4][2];
float CellSpacing[3];
int InterpolationType;
vtkTimeStamp UploadTime;
int IsCellData = 0;
vtkNew<vtkMatrix4x4> CellToPointMatrix;
float AdjustedTexMin[4];
float AdjustedTexMax[4];
vtkSmartPointer<vtkTextureObject> CoordsTex;
int CoordsTexSizes[3];
float CoordsScale[3];
float CoordsBias[3];
vtkSmartPointer<vtkTextureObject> BlankingTex;
protected:
vtkVolumeTexture();
~vtkVolumeTexture() override;
private:
vtkVolumeTexture(const vtkVolumeTexture&) = delete;
void operator=(const vtkVolumeTexture&) = delete;
/**
* Load an image block as defined in volBlock into GPU memory.
* Requires an active OpenGL context.
*/
bool LoadTexture(int const interpolation, VolumeBlock* volBlock);
/**
* Divide the image data in NxMxO user-defined blocks.
*/
void SplitVolume(vtkImageData* imageData, Size3 const& part);
void CreateBlocks(unsigned int const format, unsigned int const internalFormat, int const type);
void AdjustExtentForCell(Size6& extent);
Size3 ComputeBlockSize(int* extent);
/**
* Defines OpenGL's texture type, format and internal format based on the
* vtkDataArray type (scalarType) and the number of array components.
*/
void SelectTextureFormat(unsigned int& format, unsigned int& internalFormat, int& type,
int const scalarType, int const noOfComponents);
/**
* Clean-up any acquired host side resources (image blocks, etc.).
*/
void ClearBlocks();
/**
* Computes loaded bounds in data-coordinates.
*/
// can be combined into one call
void ComputeBounds(VolumeBlock* block);
void UpdateTextureToDataMatrix(VolumeBlock* block);
/**
* Compute transformation from cell texture-coordinates to point texture-coords
* (CTP). Cell data maps correctly to OpenGL cells, point data does not (VTK
* defines points at the cell corners). To set the point data in the center of the
* OpenGL texels, a translation of 0.5 texels is applied, and the range is rescaled
* to the point range.
*
* delta = TextureExtentsMax - TextureExtentsMin;
* min = vec3(0.5) / delta;
* max = (delta - vec3(0.5)) / delta;
* range = max - min
*
* CTP = translation * Scale
* CTP = range.x, 0, 0, min.x
* 0, range.y, 0, min.y
* 0, 0, range.z, min.z
* 0, 0, 0, 1.0
*/
void ComputeCellToPointMatrix(int extents[6]);
///@{
/**
* @brief Helper functions to catch potential issues when doing GPU
* texture allocations.
*
* They make use of the available OpenGL mechanisms to try to detect whether
* a volume would not fit in the GPU (due to MAX_TEXTURE_SIZE limitations,
* memory availability, etc.).
*/
bool AreDimensionsValid(
vtkTextureObject* texture, int const width, int const height, int const depth);
bool SafeLoadTexture(vtkTextureObject* texture, int const width, int const height,
int const depth, int numComps, int dataType, void* dataPtr);
///@}
void UpdateInterpolationType(int const interpolation);
void SetInterpolation(int const interpolation);
//----------------------------------------------------------------------------
vtkTimeStamp UpdateTime;
vtkSmartPointer<vtkTextureObject> Texture;
std::vector<vtkDataSet*> ImageDataBlocks;
std::map<vtkDataSet*, VolumeBlock*> ImageDataBlockMap;
std::vector<VolumeBlock*> SortedVolumeBlocks;
size_t CurrentBlockIdx;
bool StreamBlocks;
std::vector<Size3> TextureSizes;
Size6 FullExtent;
Size3 FullSize;
Size3 Partitions;
vtkDataArray* Scalars;
};
VTK_ABI_NAMESPACE_END
#endif // vtkVolumeTexture_h
|