File indexing completed on 2026-04-09 07:49:29
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <vector>
0017 #include <iomanip>
0018 #include <cassert>
0019
0020 #include <vector_types.h>
0021 #include "NP.hh"
0022 #include "scuda.h"
0023
0024 struct MockTexture
0025 {
0026 NP* a ;
0027 NP::INT width ;
0028 NP::INT height ;
0029 float4 dom ;
0030
0031 MockTexture(const NP* a);
0032
0033 std::string desc() const ;
0034 template<typename T> T lookup(float x, float y ) const ;
0035 template<typename T> std::string dump() const ;
0036 };
0037
0038 inline MockTexture::MockTexture(const NP* a_ )
0039 :
0040 a(NP::MakeNarrowIfWide(a_)),
0041 width(0),
0042 height(0)
0043 {
0044 a->size_2D<4>(width, height);
0045
0046 dom.x = a->get_meta<float>("domain_low", 0.f );
0047 dom.y = a->get_meta<float>("domain_high", 0.f );
0048 dom.z = a->get_meta<float>("domain_step", 0.f );
0049 dom.w = a->get_meta<float>("domain_range", 0.f );
0050 }
0051
0052 inline std::string MockTexture::desc() const
0053 {
0054 std::stringstream ss ;
0055 ss << "MockTexture::desc"
0056 << " a " << ( a ? a->sstr() : "-" )
0057 << " width " << width
0058 << " height " << height
0059 << " dom " << dom
0060 ;
0061
0062 std::string str = ss.str();
0063 return str ;
0064 }
0065
0066 template<typename T>
0067 inline T MockTexture::lookup(float x, float y ) const
0068 {
0069 const T* vv = a->cvalues<T>() ;
0070 int nx = width ;
0071 int ny = height ;
0072
0073 int ix = int(x*float(nx)) ;
0074 int iy = int(y*float(ny)) ;
0075 int idx = iy*nx + ix ;
0076
0077 T v0 = vv[idx] ;
0078
0079 return v0 ;
0080 }
0081
0082 template<typename T>
0083 inline std::string MockTexture::dump() const
0084 {
0085 std::stringstream ss ;
0086 ss << "MockTexture::dump<" << (sizeof(T) == 16 ? "float4" : "float" ) << ">" << std::endl ;
0087 const T* vv = a->cvalues<T>() ;
0088 for(NP::INT i=0 ; i < std::min(NP::INT(10), width*height) ; i++)
0089 {
0090 ss << " *(vv+" << std::setw(3) << i << ") : " << *(vv+i) << std::endl;
0091 }
0092 std::string str = ss.str();
0093 return str ;
0094 }
0095
0096
0097
0098 struct MockTextureManager
0099 {
0100 static MockTextureManager* INSTANCE ;
0101 static MockTextureManager* Get();
0102 static MockTexture Get(cudaTextureObject_t tex);
0103 static cudaTextureObject_t Add(const NP* a );
0104
0105 std::vector<MockTexture> tt ;
0106
0107 MockTextureManager() ;
0108
0109 cudaTextureObject_t add( const NP* a );
0110
0111 static std::string Desc();
0112 std::string desc() const ;
0113 MockTexture get(cudaTextureObject_t tex ) const ;
0114
0115 template<typename T> T tex2D( cudaTextureObject_t t, float x, float y ) const ;
0116
0117 float4 boundary_lookup(cudaTextureObject_t tex, float nm, int line, int k ) const ;
0118
0119 template<typename T> std::string dump(cudaTextureObject_t tex) const ;
0120 };
0121
0122
0123 inline MockTextureManager* MockTextureManager::Get()
0124 {
0125 return INSTANCE ;
0126 }
0127
0128 inline MockTextureManager::MockTextureManager()
0129 {
0130 INSTANCE = this ;
0131 }
0132
0133 inline MockTexture MockTextureManager::Get(cudaTextureObject_t obj)
0134 {
0135 assert(INSTANCE);
0136 return INSTANCE->get(obj) ;
0137 }
0138 inline cudaTextureObject_t MockTextureManager::Add(const NP* a )
0139 {
0140 if(INSTANCE == nullptr) new MockTextureManager ;
0141 assert(INSTANCE);
0142 return INSTANCE->add(a);
0143 }
0144
0145 inline cudaTextureObject_t MockTextureManager::add(const NP* a )
0146 {
0147 cudaTextureObject_t idx = tt.size() ;
0148 MockTexture tex(a) ;
0149 tt.push_back(tex);
0150 return idx ;
0151 }
0152
0153 inline std::string MockTextureManager::Desc()
0154 {
0155 return INSTANCE ? INSTANCE->desc() : "-" ;
0156 }
0157 inline std::string MockTextureManager::desc() const
0158 {
0159 int num_tex = tt.size();
0160 std::stringstream ss ;
0161 ss << "MockTextureManager::desc num_tex " << num_tex << std::endl ;
0162 for(int i=0 ; i < num_tex ; i++) ss << std::setw(4) << i << " : " << tt[i].desc() << std::endl ;
0163 std::string str = ss.str();
0164 return str ;
0165 }
0166
0167 inline MockTexture MockTextureManager::get(cudaTextureObject_t t ) const
0168 {
0169 assert( t < tt.size() );
0170 return tt[t] ;
0171 }
0172
0173 template<typename T>
0174 inline std::string MockTextureManager::dump( cudaTextureObject_t t ) const
0175 {
0176 MockTexture tex = get(t) ;
0177 return tex.dump<T>();
0178 }
0179
0180 template<typename T>
0181 inline T MockTextureManager::tex2D( cudaTextureObject_t t, float x, float y ) const
0182 {
0183 MockTexture tex = get(t) ;
0184 return tex.lookup<T>(x,y) ;
0185 }
0186
0187 template<typename T> T tex2D(cudaTextureObject_t t, float x, float y )
0188 {
0189 MockTextureManager* mgr = MockTextureManager::Get() ;
0190 if( mgr == nullptr )
0191 {
0192 std::cerr
0193 << "s_mock_texture.h/tex2D : FATAL : null MockTextureManager "
0194 << std::endl
0195 << " manager is instanciated when adding MOCK texture arrays with MockTextureManager::Add "
0196 << std::endl
0197 ;
0198 assert(0);
0199 }
0200 return mgr->tex2D<T>( t, x, y );
0201 }
0202
0203