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
public:
Texture2D();
Texture2D(const Texture2D<T>& other);
Texture2D(Texture2D<T>&& other);
~Texture2D();
Texture2D<T>& operator=(const Texture2D<T>& other);
Texture2D<T>& operator=(Texture2D<T>&& other);
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 DeviceVector<T>& data);
......@@ -101,17 +106,7 @@ class Texture2D
void disable_mipmap(bool updateTexture = true);
};
//Implementation
template <typename T>
Texture2D<T>::Texture2D() :
width_(0),
height_(0),
data_(nullptr),
description_(default_texture_description()),
textureHandle_(0)
{
}
// Implementation (static methods)
template <typename T>
cudaChannelFormatDesc Texture2D<T>::channel_description()
{
......@@ -164,6 +159,62 @@ cudaTextureDesc Texture2D<T>::default_texture_description()
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>
Texture2D<T>::~Texture2D()
{
......
......@@ -44,6 +44,9 @@ int main()
std::vector<float4> subdata1(W2*H2, float4({0.5f,0.5f,0.5f,1.0f}));
tex1.set_subimage(W2,H2,W2,H2, subdata1.data());
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());
for(int i = 0, j = 0; i < res1.size(); i++) {
......@@ -52,9 +55,31 @@ int main()
rgbData[j + 2] = 255*res1[i].z;
j += 3;
}
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;
}
......
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