Skip to content

Commit 8689c40

Browse files
committedAug 8, 2018
[opencl] Fix small OpenCL alg issues
From comparison tests with CPU results + some minor speed improvements
1 parent 573283f commit 8689c40

File tree

7 files changed

+75
-42
lines changed

7 files changed

+75
-42
lines changed
 

‎python/analysis/auto_generated/raster/qgsaspectfilter.sip.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ nodata value if not present or outside of the border. Must be implemented by sub
3030
%End
3131

3232

33+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
3334
<<<<<<< d6e747ca57807318c0b3f6be8266f0b43c0f7747
3435
=======
3536

3637

3738
>>>>>>> More updates for opencl
39+
=======
40+
>>>>>>> [opencl] Fix small OpenCL alg issues
3841
};
3942

4043
/************************************************************************

‎python/analysis/auto_generated/raster/qgsslopefilter.sip.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ Calculates output value from nine input values. The input values and the output
2929
nodata value if not present or outside of the border. Must be implemented by subclasses*
3030
%End
3131

32+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
3233
<<<<<<< d6e747ca57807318c0b3f6be8266f0b43c0f7747
3334

3435
=======
3536
>>>>>>> More updates for opencl
37+
=======
38+
39+
>>>>>>> [opencl] Fix small OpenCL alg issues
3640
};
3741

3842
/************************************************************************

‎src/analysis/raster/qgshillshadefilter.cpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121
QgsHillshadeFilter::QgsHillshadeFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat, double lightAzimuth,
2222
double lightAngle )
2323
: QgsDerivativeFilter( inputFile, outputFile, outputFormat )
24+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
2425
, mLightAzimuth( static_cast<float>( lightAzimuth ) )
2526
, mLightAngle( static_cast<float>( lightAngle ) )
2627
, mCosZenithRad( std::cos( static_cast<float>( lightAngle * M_PI ) / 180.0f ) )
2728
, mSinZenithRad( std::sin( static_cast<float>( lightAngle * M_PI ) / 180.0f ) )
2829
, mAzimuthRad( static_cast<float>( lightAzimuth * M_PI ) / 180.0f )
30+
=======
31+
, mLightAzimuth( lightAzimuth )
32+
, mLightAngle( lightAngle )
33+
, mCosZenithRad( std::cos( mLightAngle * M_PI / 180.0 ) )
34+
, mSinZenithRad( std::sin( mLightAngle * M_PI / 180.0 ) )
35+
, mAzimuthRad( mLightAzimuth * M_PI / 180.0 )
36+
>>>>>>> [opencl] Fix small OpenCL alg issues
2937
{
3038
}
3139

@@ -46,12 +54,17 @@ float QgsHillshadeFilter::processNineCellWindow( float *x11, float *x21, float *
4654
float aspect_rad = 0;
4755
if ( derX == 0 && derY == 0 ) //aspect undefined, take a neutral value. Better solutions?
4856
{
57+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
4958
aspect_rad = mAzimuthRad / 2.0f;
59+
=======
60+
aspect_rad = mAzimuthRad / 2.0;
61+
>>>>>>> [opencl] Fix small OpenCL alg issues
5062
}
5163
else
5264
{
5365
aspect_rad = M_PI + std::atan2( derX, derY );
5466
}
67+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
5568
return std::max( 0.0f, 255.0f * ( ( mCosZenithRad * std::cos( slope_rad ) ) +
5669
( mSinZenithRad * std::sin( slope_rad ) *
5770
std::cos( mAzimuthRad - aspect_rad ) ) ) );
@@ -84,23 +97,34 @@ void QgsHillshadeFilter::addExtraRasterParams( std::vector<float> &params )
8497

8598
#endif
8699
=======
100+
=======
101+
return std::max( 0.0, 255.0 * ( ( mCosZenithRad * std::cos( slope_rad ) ) +
102+
( mSinZenithRad * std::sin( slope_rad ) *
103+
std::cos( mAzimuthRad - aspect_rad ) ) ) );
104+
}
105+
106+
#ifdef HAVE_OPENCL
107+
108+
>>>>>>> [opencl] Fix small OpenCL alg issues
87109
void QgsHillshadeFilter::addExtraRasterParams( std::vector<float> &params )
88110
{
89-
float azimuthRad = -1 * mLightAzimuth * M_PI / 180.0;
90-
float zenithRad = std::max( 0.0f, 90.0f - mLightAngle ) * M_PI / 180.0;
91-
float cosZenithRad = std::cos( zenithRad );
92-
float cos_az_mul_cos_alt_mul_z = std::cos( azimuthRad ) * cosZenithRad * mZFactor;
93-
float sin_az_mul_cos_alt_mul_z = std::sin( azimuthRad ) * cosZenithRad * mZFactor;
94-
float cos_az_mul_cos_alt_mul_z_mul_254 = 254.0 * cos_az_mul_cos_alt_mul_z;
95-
float sin_az_mul_cos_alt_mul_z_mul_254 = 254.0 * sin_az_mul_cos_alt_mul_z;
96-
float square_z = mZFactor * mZFactor;
97-
float sin_altRadians_mul_254 = 254.0 * std::sin( zenithRad );
98-
99-
// For fast formula from GDAL DEM
100-
params.push_back( cos_az_mul_cos_alt_mul_z_mul_254 ); // 5
101-
params.push_back( sin_az_mul_cos_alt_mul_z_mul_254 ); // 6
102-
params.push_back( square_z ); // 7
103-
params.push_back( sin_altRadians_mul_254 ); // 8
111+
112+
// Original CPU formula
113+
float zenith_rad = mLightAngle * M_PI / 180.0;
114+
float azimuth_rad = mLightAzimuth * M_PI / 180.0;
115+
params.push_back( zenith_rad ); // 5
116+
params.push_back( azimuth_rad ); // 6
117+
118+
/*
119+
params.push_back( std::cos( mLightAngle * M_PI / 180.0 ) ); // cos_zenith_rad 5
120+
params.push_back( mLightAzimuth * M_PI / 180.0 ); // azimuth_rad 6
121+
params.push_back( std::sin( mLightAzimuth * M_PI / 180.0 ) ); // sin_zenith_rad 7
122+
*/
104123

105124
}
125+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
106126
>>>>>>> [opencl] Use fast formula for hillshade
127+
=======
128+
129+
#endif
130+
>>>>>>> [opencl] Fix small OpenCL alg issues

‎src/analysis/raster/qgshillshadefilter.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,56 @@ class ANALYSIS_EXPORT QgsHillshadeFilter: public QgsDerivativeFilter
4444
void setLightAngle( float angle );
4545

4646
private:
47+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
4748
<<<<<<< a73bbbad21629d81b9b1d4217a096a930473eb5c
4849

4950
#ifdef HAVE_OPENCL
5051

5152
=======
5253
>>>>>>> [opencl] Use fast formula for hillshade
54+
=======
55+
56+
#ifdef HAVE_OPENCL
57+
58+
>>>>>>> [opencl] Fix small OpenCL alg issues
5359
const QString openClProgramBaseName() const override
5460
{
5561
return QStringLiteral( "hillshade" );
5662
}
63+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
5764
<<<<<<< a73bbbad21629d81b9b1d4217a096a930473eb5c
5865
#endif
5966

6067
=======
6168
>>>>>>> [opencl] Use fast formula for hillshade
69+
=======
70+
#endif
71+
72+
>>>>>>> [opencl] Fix small OpenCL alg issues
6273
float mLightAzimuth;
6374
float mLightAngle;
6475
// Precalculate for speed:
6576
float mCosZenithRad;
6677
float mSinZenithRad;
6778
float mAzimuthRad;
79+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
6880

6981

7082
#ifdef HAVE_OPENCL
7183
private:
7284

7385
void addExtraRasterParams( std::vector<float> &params ) override;
7486
#endif
87+
=======
88+
>>>>>>> [opencl] Fix small OpenCL alg issues
7589

7690

77-
// QgsNineCellFilter interface
91+
#ifdef HAVE_OPENCL
7892
private:
7993

8094
void addExtraRasterParams( std::vector<float> &params ) override;
95+
#endif
96+
8197
};
8298

8399
#endif // QGSHILLSHADEFILTER_H

‎src/analysis/raster/qgsninecellfilter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,13 @@ int QgsNineCellFilter::processRasterGPU( const QString &source, QgsFeedback *fee
422422
queue.enqueueWriteBuffer( *scanLineBuffer[rowIndex[2]], CL_TRUE, 0, bufferSize, scanLine.get() ); // row 0
423423
>>>>>>> [opencl] Use fast formula for hillshade
424424
}
425+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
425426
else // Overwrite from input, skip first and last
426427
>>>>>>> [opencl] Reduce memory footprint and optimize
428+
=======
429+
else // Read line i + 1 and put it into scanline 3
430+
// Overwrite from input, skip first and last
431+
>>>>>>> [opencl] Fix small OpenCL alg issues
427432
{
428433
if ( GDALRasterIO( rasterBand, GF_Read, 0, i + 1, xSize, 1, &scanLine[1], xSize, 1, GDT_Float32, 0, 0 ) != CE_None )
429434
{
@@ -497,7 +502,10 @@ int QgsNineCellFilter::processRasterGPU( const QString &source, QgsFeedback *fee
497502
return 0;
498503
}
499504
#endif
505+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
500506

507+
=======
508+
>>>>>>> [opencl] Fix small OpenCL alg issues
501509

502510
// TODO: return an anum instead of an int
503511
int QgsNineCellFilter::processRasterCPU( QgsFeedback *feedback )

‎src/analysis/raster/qgsruggednessfilter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ class ANALYSIS_EXPORT QgsRuggednessFilter: public QgsNineCellFilter
4747
{
4848
return QStringLiteral( "ruggedness" );
4949
}
50+
<<<<<<< 573283f0dcf022e84bd615e84fd2656043a9722b
5051
<<<<<<< 8b81f1bb0993c3755019921eaa064d95f430c9db
5152
#endif
5253
=======
5354
>>>>>>> [opencl] Ruggedness index OpenCL program
55+
=======
56+
#endif
57+
>>>>>>> [opencl] Fix small OpenCL alg issues
5458

5559
};
5660

‎src/core/raster/qgshillshaderenderer.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -159,32 +159,6 @@ QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &ext
159159
QRgb defaultNodataColor = NODATA_COLOR;
160160

161161

162-
// Common pre-calculated values
163-
float cellXSize = static_cast<float>( extent.width() ) / width;
164-
float cellYSize = static_cast<float>( extent.height() ) / height;
165-
float zenithRad = static_cast<float>( std::max( 0.0, 90 - mLightAngle ) * M_PI / 180.0 );
166-
float azimuthRad = static_cast<float>( -1 * mLightAzimuth * M_PI / 180.0 );
167-
float cosZenithRad = std::cos( zenithRad );
168-
float sinZenithRad = std::sin( zenithRad );
169-
170-
// For fast formula from GDAL DEM
171-
float cos_alt_mul_z = cosZenithRad * static_cast<float>( mZFactor );
172-
float cos_az_mul_cos_alt_mul_z = std::cos( azimuthRad ) * cos_alt_mul_z;
173-
float sin_az_mul_cos_alt_mul_z = std::sin( azimuthRad ) * cos_alt_mul_z;
174-
float cos_az_mul_cos_alt_mul_z_mul_254 = 254.0f * cos_az_mul_cos_alt_mul_z;
175-
float sin_az_mul_cos_alt_mul_z_mul_254 = 254.0f * sin_az_mul_cos_alt_mul_z;
176-
float square_z = static_cast<float>( mZFactor * mZFactor );
177-
float sin_altRadians_mul_254 = 254.0f * sinZenithRad;
178-
179-
// For multi directional
180-
float sin_altRadians_mul_127 = 127.0f * sinZenithRad;
181-
// 127.0 * std::cos(225.0 * M_PI / 180.0) = -32.87001872802012
182-
float cos225_az_mul_cos_alt_mul_z_mul_127 = -32.87001872802012f * cos_alt_mul_z;
183-
float cos_alt_mul_z_mul_127 = 127.0f * cos_alt_mul_z;
184-
185-
QRgb defaultNodataColor = NODATA_COLOR;
186-
187-
188162
#ifdef HAVE_OPENCL
189163

190164
// Use OpenCL? For now OpenCL is enabled in the default configuration only

0 commit comments

Comments
 (0)
Please sign in to comment.