diff options
author | Denis Steckelmacher <steckdenis@yahoo.fr> | 2011-08-20 17:05:16 +0200 |
---|---|---|
committer | Denis Steckelmacher <steckdenis@yahoo.fr> | 2011-08-20 17:05:16 +0200 |
commit | 977e53659fd99944f22538660683f2445e176876 (patch) | |
tree | bcb97bd96d5f5c53720e85ae52e4682525754e08 | |
parent | beb531f333d3838e7a4690706a43293c4cee45ef (diff) |
Use C++ templates to reduce code size and improve readability.
-rw-r--r-- | src/core/cpu/kernel.h | 14 | ||||
-rw-r--r-- | src/core/cpu/sampler.cpp | 215 |
2 files changed, 84 insertions, 145 deletions
diff --git a/src/core/cpu/kernel.h b/src/core/cpu/kernel.h index cb3a296..e9d7124 100644 --- a/src/core/cpu/kernel.h +++ b/src/core/cpu/kernel.h @@ -105,20 +105,28 @@ class CPUKernelWorkGroup void barrier(unsigned int flags); void *getImageData(Image2D *image, int x, int y, int z) const; + void writeImage(Image2D *image, int x, int y, int z, float *color) const; void writeImage(Image2D *image, int x, int y, int z, int32_t *color) const; void writeImage(Image2D *image, int x, int y, int z, uint32_t *color) const; void readImage(float *result, Image2D *image, int x, int y, int z, - int32_t sampler) const; + uint32_t sampler) const; void readImage(int32_t *result, Image2D *image, int x, int y, int z, - int32_t sampler) const; + uint32_t sampler) const; void readImage(uint32_t *result, Image2D *image, int x, int y, int z, - int32_t sampler) const; + uint32_t sampler) const; void builtinNotFound(const std::string &name) const; private: + template<typename T> + void writeImageImpl(Image2D *image, int x, int y, int z, T *color) const; + template<typename T> + void readImageImplI(T *result, Image2D *image, int x, int y, int z, + uint32_t sampler) const; + + private: CPUKernel *p_kernel; CPUKernelEvent *p_cpu_event; KernelEvent *p_event; diff --git a/src/core/cpu/sampler.cpp b/src/core/cpu/sampler.cpp index 58b3bd3..c215cf8 100644 --- a/src/core/cpu/sampler.cpp +++ b/src/core/cpu/sampler.cpp @@ -94,8 +94,8 @@ static void slow_shuffle4(uint32_t *rs, uint32_t *a, uint32_t *b, rs[3] = (w < 4 ? a[w] : b[w - 4]); } -static void slow_convert_to_format4f(void *dest, float *data, - cl_channel_type type, unsigned int channels) +static void slow_convert_to_format(void *dest, float *data, + cl_channel_type type, unsigned int channels) { // Convert always the four components of source to target if (type == CL_FLOAT) @@ -121,8 +121,8 @@ static void slow_convert_to_format4f(void *dest, float *data, } } -static void slow_convert_from_format4f(float *data, void *source, - cl_channel_type type, unsigned int channels) +static void slow_convert_from_format(float *data, void *source, + cl_channel_type type, unsigned int channels) { // Convert always the four components of source to target if (type == CL_FLOAT) @@ -148,8 +148,8 @@ static void slow_convert_from_format4f(float *data, void *source, } } -static void slow_convert_to_format4i(void *dest, int *data, - cl_channel_type type, unsigned int channels) +static void slow_convert_to_format(void *dest, int *data, + cl_channel_type type, unsigned int channels) { // Convert always the four components of source to target if (type == CL_SIGNED_INT32) @@ -169,8 +169,8 @@ static void slow_convert_to_format4i(void *dest, int *data, } } -static void slow_convert_from_format4i(int32_t *data, void *source, - cl_channel_type type, unsigned int channels) +static void slow_convert_from_format(int32_t *data, void *source, + cl_channel_type type, unsigned int channels) { // Convert always the four components of source to target if (type == CL_SIGNED_INT32) @@ -190,8 +190,8 @@ static void slow_convert_from_format4i(int32_t *data, void *source, } } -static void slow_convert_to_format4ui(void *dest, uint32_t *data, - cl_channel_type type, unsigned int channels) +static void slow_convert_to_format(void *dest, uint32_t *data, + cl_channel_type type, unsigned int channels) { // Convert always the four components of source to target if (type == CL_UNSIGNED_INT32) @@ -211,8 +211,8 @@ static void slow_convert_to_format4ui(void *dest, uint32_t *data, } } -static void slow_convert_from_format4ui(uint32_t *data, void *source, - cl_channel_type type, unsigned int channels) +static void slow_convert_from_format(uint32_t *data, void *source, + cl_channel_type type, unsigned int channels) { // Convert always the four components of source to target if (type == CL_UNSIGNED_INT32) @@ -241,23 +241,11 @@ static void slow_convert_from_format4ui(uint32_t *data, void *source, slow_shuffle4(rs, a, b, x, y, z, w) #endif - #define convert_to_format4f(dest, data, type, channels) \ - slow_convert_to_format4f(dest, data, type, channels) - - #define convert_to_format4i(dest, data, type, channels) \ - slow_convert_to_format4i(dest, data, type, channels) - - #define convert_to_format4ui(data, source, type, channels) \ - slow_convert_to_format4ui(data, source, type, channels) + #define convert_to_format(dest, data, type, channels) \ + slow_convert_to_format(dest, data, type, channels) - #define convert_from_format4f(data, source, type, channels) \ - slow_convert_from_format4f(data, source, type, channels) - - #define convert_from_format4i(data, source, type, channels) \ - slow_convert_from_format4i(data, source, type, channels) - - #define convert_from_format4ui(data, source, type, channels) \ - slow_convert_from_format4ui(data, source, type, channels) + #define convert_from_format(data, source, type, channels) \ + slow_convert_from_format(data, source, type, channels) static void swizzle(uint32_t *target, uint32_t *source, cl_channel_order order, bool reading, uint32_t t_max) @@ -352,10 +340,11 @@ void *CPUKernelWorkGroup::getImageData(Image2D *image, int x, int y, int z) cons image->pixel_size()); } -void CPUKernelWorkGroup::writeImage(Image2D *image, int x, int y, int z, - float *color) const +template<typename T> +void CPUKernelWorkGroup::writeImageImpl(Image2D *image, int x, int y, int z, + T *color) const { - float converted[4]; + T converted[4]; // Swizzle to the correct order (float, int and uint are 32-bit, so the // type has no importance @@ -366,53 +355,57 @@ void CPUKernelWorkGroup::writeImage(Image2D *image, int x, int y, int z, void *target = getImageData(image, x, y, z); // Convert color to the correct format - convert_to_format4f(target, - converted, - image->format().image_channel_data_type, - image->channels()); + convert_to_format(target, + converted, + image->format().image_channel_data_type, + image->channels()); } void CPUKernelWorkGroup::writeImage(Image2D *image, int x, int y, int z, - int32_t *color) const + float *color) const { - int32_t converted[4]; - - // Swizzle to the correct order (float, int and uint are 32-bit, so the - // type has no importance - swizzle((uint32_t *)converted, (uint32_t *)color, - image->format().image_channel_order, false, 0); - - // Get a pointer in the image where to write the data - void *target = getImageData(image, x, y, z); + writeImageImpl<float>(image, x, y, z, color); +} - // Convert color to the correct format - convert_to_format4i(target, - converted, - image->format().image_channel_data_type, - image->channels()); +void CPUKernelWorkGroup::writeImage(Image2D *image, int x, int y, int z, + int32_t *color) const +{ + writeImageImpl<int32_t>(image, x, y, z, color); } void CPUKernelWorkGroup::writeImage(Image2D *image, int x, int y, int z, uint32_t *color) const { - uint32_t converted[4]; + writeImageImpl<uint32_t>(image, x, y, z, color); +} - // Swizzle to the correct order (float, int and uint are 32-bit, so the - // type has no importance - swizzle(converted, color, image->format().image_channel_order, false, 0); +template<typename T> +uint32_t type_max_value() +{ + return 0; +} - // Get a pointer in the image where to write the data - void *target = getImageData(image, x, y, z); +template<> +uint32_t type_max_value<float>() +{ + return 1065353216; // 1.0f in decimal form +} - // Convert color to the correct format - convert_to_format4ui(target, - converted, - image->format().image_channel_data_type, - image->channels()); +template<> +uint32_t type_max_value<int32_t>() +{ + return 0x7fffffff; } -void CPUKernelWorkGroup::readImage(float *result, Image2D *image, int x, int y, - int z, int32_t sampler) const +template<> +uint32_t type_max_value<uint32_t>() +{ + return 0xffffffff; +} + +template<typename T> +void CPUKernelWorkGroup::readImageImplI(T *result, Image2D *image, int x, int y, + int z, uint32_t sampler) const { // Handle the addressing mode of the sampler if (handle_address_mode(image, x, y, z, sampler)) @@ -439,94 +432,32 @@ void CPUKernelWorkGroup::readImage(float *result, Image2D *image, int x, int y, // Load the data from the image, converting it void *source = getImageData(image, x, y, z); - float converted[4]; + T converted[4]; - convert_from_format4f(converted, - source, - image->format().image_channel_data_type, - image->channels()); + convert_from_format(converted, + source, + image->format().image_channel_data_type, + image->channels()); // Swizzle the pixel just read and place it in result swizzle((uint32_t *)result, (uint32_t *)converted, - image->format().image_channel_order, true, 1065353216 /* 1.0f */); + image->format().image_channel_order, true, type_max_value<T>()); } -void CPUKernelWorkGroup::readImage(int32_t *result, Image2D *image, int x, int y, - int z, int32_t sampler) const +void CPUKernelWorkGroup::readImage(float *result, Image2D *image, int x, int y, + int z, uint32_t sampler) const { - // Handle the addressing mode of the sampler - if (handle_address_mode(image, x, y, z, sampler)) - { - // Border color - result[0] = 0; - result[1] = 0; - result[2] = 0; - - switch (image->format().image_channel_order) - { - case CL_R: - case CL_RG: - case CL_RGB: - case CL_LUMINANCE: - result[3] = 0x7fffffff; - break; - default: - result[3] = 0; - } - - return; - } - - // Load the data from the image, converting it - void *source = getImageData(image, x, y, z); - int32_t converted[4]; - - convert_from_format4i(converted, - source, - image->format().image_channel_data_type, - image->channels()); + readImageImplI<float>(result, image, x, y, z, sampler); +} - // Swizzle the pixel just read and place it in result - swizzle((uint32_t *)result, (uint32_t *)converted, - image->format().image_channel_order, true, 0x7fffffff); +void CPUKernelWorkGroup::readImage(int32_t *result, Image2D *image, int x, int y, + int z, uint32_t sampler) const +{ + readImageImplI<int32_t>(result, image, x, y, z, sampler); } void CPUKernelWorkGroup::readImage(uint32_t *result, Image2D *image, int x, int y, - int z, int32_t sampler) const + int z, uint32_t sampler) const { - // Handle the addressing mode of the sampler - if (handle_address_mode(image, x, y, z, sampler)) - { - // Border color - result[0] = 0; - result[1] = 0; - result[2] = 0; - - switch (image->format().image_channel_order) - { - case CL_R: - case CL_RG: - case CL_RGB: - case CL_LUMINANCE: - result[3] = 0xffffffff; - break; - default: - result[3] = 0; - } - - return; - } - - // Load the data from the image, converting it - void *source = getImageData(image, x, y, z); - uint32_t converted[4]; - - convert_from_format4ui(converted, - source, - image->format().image_channel_data_type, - image->channels()); - - // Swizzle the pixel just read and place it in result - swizzle(result, converted, image->format().image_channel_order, true, - 0x7fffffff); -}
\ No newline at end of file + readImageImplI<uint32_t>(result, image, x, y, z, sampler); +} |