Generators.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "Generators.h"
  2. #include "GenericMandelbrot.h"
  3. #include "Fixed.h"
  4. #include <iostream>
  5. #include <iterator>
  6. using namespace cl;
  7. Platform getPlatform() {
  8. /* Returns the first platform found. */
  9. std::vector<Platform> all_platforms;
  10. Platform::get(&all_platforms);
  11. if (all_platforms.size()==0) {
  12. std::cout << "No platforms found. Check OpenCL installation!\n";
  13. exit(1);
  14. }
  15. return all_platforms[0];
  16. }
  17. Device getDevice(Platform platform, int i, bool display = false) {
  18. /* Returns the deviced specified by the index i on platform.
  19. * If display is true, then all of the platforms are listed.
  20. */
  21. std::vector<Device> all_devices;
  22. platform.getDevices(CL_DEVICE_TYPE_GPU, &all_devices);
  23. if (all_devices.size() == 0) {
  24. std::cout << "No devices found. Check OpenCL installation!\n";
  25. exit(1);
  26. }
  27. if (display) {
  28. for (::size_t j = 0; j < all_devices.size(); j++) {
  29. printf("Device %d: %s\n", int(j), all_devices[j].getInfo<CL_DEVICE_NAME>().c_str());
  30. printf("preferred float width: %d\n", all_devices[j].getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>());
  31. printf("vendor: %s\n", all_devices[j].getInfo<CL_DEVICE_VENDOR>().c_str());
  32. }
  33. }
  34. return all_devices[i];
  35. }
  36. ClGenerator::ClGenerator(void)
  37. {
  38. Platform p = getPlatform();
  39. device = getDevice(p, 0, true);
  40. context = Context{ device };
  41. Program::Sources sources;
  42. std::string kcode;
  43. if (device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>() == 4) {
  44. kcode =
  45. "__kernel void iterate(__global float* A, const int width, float xl, float yt, float pixelScaleX, float pixelScaleY, int max) {\n"
  46. " int index = get_global_id(0) * 4;\n"
  47. " int x = index % (width);\n"
  48. " int y = index / (width);\n"
  49. " float4 av = (float4)(x * pixelScaleX + xl, (x + 1) * pixelScaleX + xl, (x + 2) * pixelScaleX + xl, (x + 3) * pixelScaleX + xl);\n"
  50. // "(x + 4) * pixelScale + xl, (x + 5) * pixelScale + xl, (x + 6) * pixelScale + xl, (x + 7) * pixelScale + xl);\n"
  51. " float4 bv = (float4)(y * pixelScaleY + yt);\n"
  52. " float4 ca = av;\n"
  53. " float4 cb = bv;\n"
  54. ""
  55. " int4 counter = (int4) 1;"
  56. " float4 threshold = (float4) 16;"
  57. " int n = 0;\n"
  58. " while (n < max) {\n"
  59. " float4 aa = av * av;\n"
  60. " float4 bb = bv * bv;\n"
  61. " float4 ab = av * bv;\n"
  62. " av = aa - bb + ca;\n"
  63. " bv = 2 * ab + cb;\n"
  64. " counter += -(threshold > (aa + bb));\n"
  65. " if(all(threshold < (aa + bb))) break;\n"
  66. " //if (aa + bb > 16) break;\n"
  67. " n++;\n"
  68. " }\n\n"
  69. " A[index] = (float) counter[0];\n"
  70. " A[index + 1] = (float) counter[1];\n"
  71. " A[index + 2] = (float) counter[2];\n"
  72. " A[index + 3] = (float) counter[3];\n"
  73. /* " A[index + 4] = (float) counter[4];\n"
  74. " A[index + 5] = (float) counter[5];\n"
  75. " A[index + 6] = (float) counter[6];\n"
  76. " A[index + 7] = (float) counter[7];\n"*/
  77. // " A[get_global_id(0)] = 1;\n"
  78. "}\n";
  79. }
  80. else {
  81. kcode =
  82. // "#pragma OPENCL EXTENSION cl_khr_fp64 : enable"
  83. "__kernel void iterate(__global float* A, const int width, float xl, float yt, float pixelScaleX, float pixelScaleY, int max) {"
  84. " int index = get_global_id(0);\n"
  85. " int x = index % width;"
  86. " int y = index / width;"
  87. " float a = x * pixelScaleX + xl;"
  88. " float b = y * pixelScaleY + yt;"
  89. " float ca = a;"
  90. " float cb = b;"
  91. ""
  92. " int n = 0;"
  93. " while (n < max) {"
  94. " float aa = a * a;"
  95. " float bb = b * b;"
  96. " float ab = a * b;"
  97. " if (aa + bb > 16) break;"
  98. " a = aa - bb + ca;"
  99. " b = 2 * ab + cb;"
  100. " n++;"
  101. " }\n"
  102. " A[index] = ((float)n) + 1 - (a * a + b * b - 16) / (256 - 16);\n"
  103. // " A[get_global_id(0)] = 5;"
  104. "}";
  105. }
  106. sources.push_back({ kcode.c_str(), kcode.length() });
  107. program = Program{ context, sources };
  108. if (program.build({ device }) != CL_SUCCESS) {
  109. std::cout << "Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
  110. exit(1);
  111. }
  112. queue = CommandQueue(context, device);
  113. }
  114. /*Bitmap<RGBColor> ClGenerator::generate(const MandelInfo& info)
  115. {
  116. return enqueueMandelbrot(info.bWidth, info.bHeight, info.view.x, info.view.y, info.view.width).get();
  117. }*/
  118. Bitmap<float> ClGenerator::generateRaw(const MandelInfo& info)
  119. {
  120. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  121. Bitmap<float> bitmap{ info.bWidth, info.bHeight };
  122. Buffer buffer_A(context, CL_MEM_READ_WRITE, bufferSize);
  123. float pixelScaleX = info.view.width / info.bWidth;
  124. float pixelScaleY = info.view.height / info.bHeight;
  125. Kernel iterate = Kernel(program, "iterate");
  126. iterate.setArg(0, buffer_A);
  127. iterate.setArg(1, int(info.bWidth));
  128. iterate.setArg(2, float(info.view.x));
  129. iterate.setArg(3, float(info.view.y));
  130. iterate.setArg(4, float(pixelScaleX));
  131. iterate.setArg(5, float(pixelScaleY));
  132. iterate.setArg(6, int(info.maxIter));
  133. if (device.getInfo<CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT>() == 4) {
  134. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight / 4));
  135. } else {
  136. queue.enqueueNDRangeKernel(iterate, 0, NDRange(info.bWidth * info.bHeight));
  137. }
  138. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, bitmap.pixels.get());
  139. return bitmap;
  140. }
  141. std::future<Bitmap<RGBColor>> ClGenerator::enqueueMandelbrot(long width, long height, float x, float y, float fwidth)
  142. {
  143. x = x - fwidth / 2;
  144. y = y - fwidth * height / width / 2;
  145. auto mandelCreator = [width, height, x, y, fwidth, this] () -> Bitmap<RGBColor> {
  146. ::size_t bufferSize = width * height * sizeof(float);
  147. Bitmap<float> bitmap{ width, height };
  148. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  149. //CommandQueue queue(context, device);
  150. //queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, bufferSize, A);
  151. /*float x = -2.3;
  152. float y = -1.5;*/
  153. float pixelScale = fwidth / width;
  154. Kernel iterate = Kernel(program, "iterate");
  155. iterate.setArg(0, buffer_A);
  156. iterate.setArg(1, width);
  157. iterate.setArg(2, x);
  158. iterate.setArg(3, y);
  159. iterate.setArg(4, pixelScale);
  160. queue.enqueueNDRangeKernel(iterate, NullRange, NDRange(width * height), NDRange(32));
  161. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, bitmap.pixels.get());
  162. auto converted = bitmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  163. return converted;
  164. };
  165. //return std::future<Bitmap<RGBColor>(mandelCreator(), );
  166. return std::async(/*std::launch::deferred,*/ mandelCreator);
  167. }
  168. /*
  169. std::future<Bitmap<RGBColor>> createMandelbrot()
  170. {
  171. auto mandelCreator = [] () -> Bitmap<RGBColor> {
  172. Bitmap<int> bitmap{1024, 1024};
  173. calculateMandel(bitmap);
  174. return bitmap.map<RGBColor>([](int x) { return RGBColor{ unsigned char(x), unsigned char(x), unsigned char(x) }; });
  175. };
  176. return std::async(mandelCreator);
  177. }
  178. */
  179. std::future<Bitmap<RGBColor>> createHPM()
  180. {
  181. /*auto mandelCreator = [] () -> Bitmap<RGBColor> {
  182. Fixed128 smallFact { 10000ULL, 0 };
  183. Bitmap<float> bitmap{ 128, 128 };
  184. for (::size_t y = 0; y < bitmap.height; y++) {
  185. for (::size_t x = 0; x < bitmap.width; x++) {
  186. Fixed128 a = Fixed128(x) * smallFact;
  187. Fixed128 b = Fixed128(y) * smallFact;
  188. bitmap.get(x, y) = iterate<Fixed128>(a, b, 250);
  189. }
  190. }
  191. return bitmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  192. };*/
  193. double xx = -10.6;
  194. double yy = 4.7;
  195. Fixed128 x = xx;
  196. Fixed128 y = yy;
  197. std::cout << double(-x) << " * " << double(-y) << " = " << double(x * y) << " --> " << (xx * yy) << std::endl;
  198. //exit(0);
  199. auto mandelCreator = [] () -> Bitmap<RGBColor> {
  200. Bitmap<float> bitmap{ 512, 512 };
  201. for (::size_t y = 0; y < bitmap.height; y++) {
  202. for (::size_t x = 0; x < bitmap.width; x++) {
  203. Fixed128 a = x * 2.0 / bitmap.width - 1;
  204. Fixed128 b = y * 2.0 / bitmap.height - 1;
  205. bitmap.get(x, y) = iterate<Fixed128>(a, b, 250);
  206. }
  207. }
  208. return bitmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  209. };
  210. return std::async(mandelCreator);
  211. }