Commit 16b397c1 authored by Pierre NARVOR's avatar Pierre NARVOR
Browse files

Completed Texture2D with move and copy constructor and assignment

parent 146bf9d9
...@@ -59,8 +59,13 @@ class Texture2D ...@@ -59,8 +59,13 @@ class Texture2D
public: public:
Texture2D(); Texture2D();
Texture2D(const Texture2D<T>& other);
Texture2D(Texture2D<T>&& other);
~Texture2D(); ~Texture2D();
Texture2D<T>& operator=(const Texture2D<T>& other);
Texture2D<T>& operator=(Texture2D<T>&& other);
void allocate_data(size_t width, size_t height); void allocate_data(size_t width, size_t height);
void set_image(size_t width, size_t height, const T* data); void set_image(size_t width, size_t height, const T* data);
void set_image(size_t width, size_t height, const DeviceVector<T>& data); void set_image(size_t width, size_t height, const DeviceVector<T>& data);
...@@ -101,17 +106,7 @@ class Texture2D ...@@ -101,17 +106,7 @@ class Texture2D
void disable_mipmap(bool updateTexture = true); void disable_mipmap(bool updateTexture = true);
}; };
//Implementation // Implementation (static methods)
template <typename T>
Texture2D<T>::Texture2D() :
width_(0),
height_(0),
data_(nullptr),
description_(default_texture_description()),
textureHandle_(0)
{
}
template <typename T> template <typename T>
cudaChannelFormatDesc Texture2D<T>::channel_description() cudaChannelFormatDesc Texture2D<T>::channel_description()
{ {
...@@ -164,6 +159,62 @@ cudaTextureDesc Texture2D<T>::default_texture_description() ...@@ -164,6 +159,62 @@ cudaTextureDesc Texture2D<T>::default_texture_description()
return res; return res;
} }
// Implementation (non-static methods)
template <typename T>
Texture2D<T>::Texture2D() :
width_(0),
height_(0),
data_(nullptr),
description_(default_texture_description()),
textureHandle_(0)
{}
template <typename T>
Texture2D<T>::Texture2D(const Texture2D<T>& other) :
Texture2D<T>()
{
*this = other;
}
template <typename T>
Texture2D<T>::Texture2D(Texture2D<T>&& other) :
width_(std::exchange(other.width_, 0)),
height_(std::exchange(other.height_, 0)),
data_(std::exchange(other.data_, nullptr)),
description_(std::move(other.description_)),
textureHandle_(std::exchange(other.textureHandle_, 0))
{}
template <typename T>
Texture2D<T>& Texture2D<T>::operator=(const Texture2D<T>& other)
{
this->allocate_data(other.width_, other.height_);
description_ = other.description_;
CUDA_CHECK( cudaMemcpy2DArrayToArray(data_, 0, 0,
other.data_, 0, 0,
sizeof(T)*width_,
height_,
cudaMemcpyDeviceToDevice) );
this->update_texture_handle();
return *this;
}
template <typename T>
Texture2D<T>& Texture2D<T>::operator=(Texture2D<T>&& other)
{
this->free_data();
this->destroy_texture_handle();
width_ = std::exchange(other.width_, 0);
height_ = std::exchange(other.height_, 0);
data_ = std::exchange(other.data_, nullptr);
description_ = std::move(other.description_);
textureHandle_ = std::exchange(other.textureHandle_, 0);
return *this;
}
template <typename T> template <typename T>
Texture2D<T>::~Texture2D() Texture2D<T>::~Texture2D()
{ {
......
...@@ -44,6 +44,9 @@ int main() ...@@ -44,6 +44,9 @@ int main()
std::vector<float4> subdata1(W2*H2, float4({0.5f,0.5f,0.5f,1.0f})); std::vector<float4> subdata1(W2*H2, float4({0.5f,0.5f,0.5f,1.0f}));
tex1.set_subimage(W2,H2,W2,H2, subdata1.data()); tex1.set_subimage(W2,H2,W2,H2, subdata1.data());
res1 = render_texture(Wout,Hout,tex1); res1 = render_texture(Wout,Hout,tex1);
// testing texture copy
Texture2D<float4> tex2(tex1);
tex2.set_subimage(W2,H2,0,0, subdata1.data());
auto rgbData = std::vector<uint8_t>(3*res1.size()); auto rgbData = std::vector<uint8_t>(3*res1.size());
for(int i = 0, j = 0; i < res1.size(); i++) { for(int i = 0, j = 0; i < res1.size(); i++) {
...@@ -52,9 +55,31 @@ int main() ...@@ -52,9 +55,31 @@ int main()
rgbData[j + 2] = 255*res1[i].z; rgbData[j + 2] = 255*res1[i].z;
j += 3; j += 3;
} }
write_ppm("output.ppm", Wout, Hout, (const char*)rgbData.data()); write_ppm("output.ppm", Wout, Hout, (const char*)rgbData.data());
HostVector<float4> res2;
res2 = render_texture(Wout,Hout,tex2);
rgbData.resize(3*res2.size());
for(int i = 0, j = 0; i < res2.size(); i++) {
rgbData[j] = 255*res2[i].x;
rgbData[j + 1] = 255*res2[i].y;
rgbData[j + 2] = 255*res2[i].z;
j += 3;
}
write_ppm("output_copy.ppm", Wout, Hout, (const char*)rgbData.data());
Texture2D<float4> tex3(std::move(tex2));
res2 = render_texture(Wout,Hout,tex3);
rgbData.resize(3*res2.size());
for(int i = 0, j = 0; i < res2.size(); i++) {
rgbData[j] = 255*res2[i].x;
rgbData[j + 1] = 255*res2[i].y;
rgbData[j + 2] = 255*res2[i].z;
j += 3;
}
write_ppm("output_move.ppm", Wout, Hout, (const char*)rgbData.data());
return 0; return 0;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment