Generators.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_ALL, &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. }
  31. return all_devices[i];
  32. }
  33. ClGenerator::ClGenerator(void)
  34. {
  35. Platform p = getPlatform();
  36. device = getDevice(p, 0, false);
  37. context = Context{ device };
  38. Program::Sources sources;
  39. std::string kcode_alt =
  40. "void kernel iterate(global float* A, const int width, float xl, float yt, float pixelScale, int max) {"
  41. " int x = get_global_id(0) % width;"
  42. " int y = get_global_id(0) / width;"
  43. " float a = x * pixelScale + xl;"
  44. " float b = y * pixelScale + yt;"
  45. " float ca = a;"
  46. " float cb = b;"
  47. ""
  48. " int n = 0;"
  49. " while (n < max) {"
  50. " float aa = a * a;"
  51. " float bb = b * b;"
  52. " float ab = a * b;"
  53. " a = aa - bb + ca;"
  54. " b = 2 * ab + cb;"
  55. " if (aa + bb > 16) break;"
  56. " n++;"
  57. " }\n"
  58. " A[get_global_id(0)] = n;//((float)n) + (a + b - 16) / (256 - 16);\n"
  59. // " A[get_global_id(0)] = 5;"
  60. "}";
  61. std::string kcode =
  62. "void kernel iterate(global float* A, const int width, float xl, float yt, float pixelScale, int max) {"
  63. " int x = get_global_id(0) % (width);"
  64. " int y = get_global_id(0) / (width);"
  65. " float a = x * pixelScale + xl;"
  66. " float b = y * pixelScale + yt;"
  67. " float ca = a;"
  68. " float cb = b;"
  69. ""
  70. " int n = 0;"
  71. " while (n < max) {"
  72. " float aa = a * a;"
  73. " float bb = b * b;"
  74. " float ab = a * b;"
  75. " a = aa - bb + ca;"
  76. " b = 2 * ab + cb;"
  77. " if (aa + bb > 16) break;"
  78. " n++;"
  79. " }\n"
  80. " A[get_global_id(0)] = n;//((float)n) + (a + b - 16) / (256 - 16);\n"
  81. // " A[get_global_id(0)] = 5;"
  82. "}";
  83. sources.push_back({ kcode.c_str(), kcode.length() });
  84. program = Program{ context, sources };
  85. if (program.build({ device }) != CL_SUCCESS) {
  86. std::cout << "Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
  87. exit(1);
  88. }
  89. queue = CommandQueue(context, device);
  90. }
  91. /*Bitmap<RGBColor> ClGenerator::generate(const MandelInfo& info)
  92. {
  93. return enqueueMandelbrot(info.bWidth, info.bHeight, info.view.x, info.view.y, info.view.width).get();
  94. }*/
  95. Bitmap<float> ClGenerator::generateRaw(const MandelInfo& info)
  96. {
  97. ::size_t bufferSize = info.bWidth * info.bHeight * sizeof(float);
  98. Bitmap<float> bitmap{ info.bWidth, info.bHeight };
  99. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  100. float pixelScale = info.view.width / info.bWidth;
  101. Kernel iterate = Kernel(program, "iterate");
  102. iterate.setArg(0, buffer_A);
  103. iterate.setArg(1, info.bWidth);
  104. iterate.setArg(2, float(info.view.x));
  105. iterate.setArg(3, float(info.view.y));
  106. iterate.setArg(4, pixelScale);
  107. iterate.setArg(5, info.maxIter);
  108. queue.enqueueNDRangeKernel(iterate, NullRange, NDRange(info.bWidth * info.bHeight), NDRange(32));
  109. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, bitmap.pixels.get());
  110. return bitmap;
  111. }
  112. std::future<Bitmap<RGBColor>> ClGenerator::enqueueMandelbrot(long width, long height, float x, float y, float fwidth)
  113. {
  114. x = x - fwidth / 2;
  115. y = y - fwidth * height / width / 2;
  116. auto mandelCreator = [width, height, x, y, fwidth, this] () -> Bitmap<RGBColor> {
  117. ::size_t bufferSize = width * height * sizeof(float);
  118. Bitmap<float> bitmap{ width, height };
  119. Buffer buffer_A(context, CL_MEM_WRITE_ONLY, bufferSize);
  120. //CommandQueue queue(context, device);
  121. //queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, bufferSize, A);
  122. /*float x = -2.3;
  123. float y = -1.5;*/
  124. float pixelScale = fwidth / width;
  125. Kernel iterate = Kernel(program, "iterate");
  126. iterate.setArg(0, buffer_A);
  127. iterate.setArg(1, width);
  128. iterate.setArg(2, x);
  129. iterate.setArg(3, y);
  130. iterate.setArg(4, pixelScale);
  131. queue.enqueueNDRangeKernel(iterate, NullRange, NDRange(width * height), NDRange(32));
  132. queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, bufferSize, bitmap.pixels.get());
  133. 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) }; });
  134. return converted;
  135. };
  136. //return std::future<Bitmap<RGBColor>(mandelCreator(), );
  137. return std::async(/*std::launch::deferred,*/ mandelCreator);
  138. }
  139. /*
  140. std::future<Bitmap<RGBColor>> createMandelbrot()
  141. {
  142. auto mandelCreator = [] () -> Bitmap<RGBColor> {
  143. Bitmap<int> bitmap{1024, 1024};
  144. calculateMandel(bitmap);
  145. return bitmap.map<RGBColor>([](int x) { return RGBColor{ unsigned char(x), unsigned char(x), unsigned char(x) }; });
  146. };
  147. return std::async(mandelCreator);
  148. }
  149. */
  150. std::future<Bitmap<RGBColor>> createHPM()
  151. {
  152. /*auto mandelCreator = [] () -> Bitmap<RGBColor> {
  153. Fixed128 smallFact { 10000ULL, 0 };
  154. Bitmap<float> bitmap{ 128, 128 };
  155. for (::size_t y = 0; y < bitmap.height; y++) {
  156. for (::size_t x = 0; x < bitmap.width; x++) {
  157. Fixed128 a = Fixed128(x) * smallFact;
  158. Fixed128 b = Fixed128(y) * smallFact;
  159. bitmap.get(x, y) = iterate<Fixed128>(a, b, 250);
  160. }
  161. }
  162. 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) }; });
  163. };*/
  164. double xx = -10.6;
  165. double yy = 4.7;
  166. Fixed128 x = xx;
  167. Fixed128 y = yy;
  168. std::cout << double(-x) << " * " << double(-y) << " = " << double(x * y) << " --> " << (xx * yy) << std::endl;
  169. //exit(0);
  170. auto mandelCreator = [] () -> Bitmap<RGBColor> {
  171. Bitmap<float> bitmap{ 512, 512 };
  172. for (::size_t y = 0; y < bitmap.height; y++) {
  173. for (::size_t x = 0; x < bitmap.width; x++) {
  174. Fixed128 a = x * 2.0 / bitmap.width - 1;
  175. Fixed128 b = y * 2.0 / bitmap.height - 1;
  176. bitmap.get(x, y) = iterate<Fixed128>(a, b, 250);
  177. }
  178. }
  179. 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) }; });
  180. };
  181. return std::async(mandelCreator);
  182. }