InfoQueryObject.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright LWJGL. All rights reserved.
  3. * License terms: http://lwjgl.org/license.php
  4. */
  5. package com.jme3.opencl.lwjgl.info;
  6. import com.jme3.lwjgl3.utils.APIBuffer;
  7. import static com.jme3.lwjgl3.utils.APIUtil.apiBuffer;
  8. import static com.jme3.opencl.lwjgl.info.CLUtil.checkCLError;
  9. import org.lwjgl.PointerBuffer;
  10. import static org.lwjgl.system.Checks.*;
  11. import static org.lwjgl.system.MemoryUtil.*;
  12. import static org.lwjgl.system.Pointer.*;
  13. /**
  14. * Base class for OpenCL object information queries.
  15. * <p>
  16. * All methods require the object being queried (a pointer value), a second
  17. * object argument (another pointer value) and the integer parameter name.
  18. *
  19. * @see org.lwjgl.opencl.Info
  20. */
  21. abstract class InfoQueryObject {
  22. protected abstract int get(long pointer, long arg, int parameterName,
  23. long parameterValueSize, long parameterValue, long parameterValueSizeRet);
  24. InfoQueryObject() {
  25. }
  26. /**
  27. * Returns the integer value for the specified {@code parameterName}, converted
  28. * to a boolean.
  29. *
  30. * @param object the object to query
  31. * @param arg an object argument
  32. * @param parameterName the parameter to query
  33. *
  34. * @return the parameter's boolean value
  35. */
  36. boolean getBoolean(long object, long arg, int parameterName) {
  37. return getInt(object, arg, parameterName) != 0;
  38. }
  39. /**
  40. * Returns the integer value for the specified {@code parameterName}.
  41. * <p>
  42. * For integer parameters that may be 32 or 64 bits (e.g. {@code size_t}),
  43. * {@link #getPointer} should be used instead.
  44. *
  45. * @param object the object to query
  46. * @param arg an object argument
  47. * @param parameterName the parameter to query
  48. *
  49. * @return the parameter's int value
  50. */
  51. int getInt(long object, long arg, int parameterName) {
  52. APIBuffer __buffer = apiBuffer();
  53. int errcode = get(object, arg, parameterName, 4L, __buffer.address(), NULL);
  54. if (DEBUG) {
  55. checkCLError(errcode);
  56. }
  57. return __buffer.intValue(0);
  58. }
  59. /**
  60. * Returns the long value for the specified {@code parameterName}.
  61. * <p>
  62. * For integer parameters that may be 32 or 64 bits (e.g. {@code size_t}),
  63. * {@link #getPointer} should be used instead.
  64. *
  65. * @param object the object to query
  66. * @param arg an object argument
  67. * @param parameterName the parameter to query
  68. *
  69. * @return the parameter's long value
  70. */
  71. long getLong(long object, long arg, int parameterName) {
  72. APIBuffer __buffer = apiBuffer();
  73. int errcode = get(object, arg, parameterName, 8L, __buffer.address(), NULL);
  74. if (DEBUG) {
  75. checkCLError(errcode);
  76. }
  77. return __buffer.longValue(0);
  78. }
  79. /**
  80. * Returns the pointer value for the specified {@code parameterName}.
  81. * <p>
  82. * This method should also be used for integer parameters that may be 32 or
  83. * 64 bits (e.g. {@code size_t}).
  84. *
  85. * @param object the object to query
  86. * @param arg an object argument
  87. * @param parameterName the parameter to query
  88. *
  89. * @return the parameter's pointer value
  90. */
  91. long getPointer(long object, long arg, int parameterName) {
  92. APIBuffer __buffer = apiBuffer();
  93. int errcode = get(object, arg, parameterName, POINTER_SIZE, __buffer.address(), NULL);
  94. if (DEBUG) {
  95. checkCLError(errcode);
  96. }
  97. return __buffer.pointerValue(0);
  98. }
  99. /**
  100. * Writes the pointer list for the specified {@code parameterName} into
  101. * {@code target}.
  102. * <p>
  103. * This method should also be used for integer parameters that may be 32 or
  104. * 64 bits (e.g. {@code size_t}).
  105. *
  106. * @param object the object to query
  107. * @param arg an object argument
  108. * @param parameterName the parameter to query
  109. * @param target the buffer in which to put the returned pointer list
  110. *
  111. * @return how many pointers were actually returned
  112. */
  113. int getPointers(long object, long arg, int parameterName, PointerBuffer target) {
  114. APIBuffer __buffer = apiBuffer();
  115. int errcode = get(object, arg, parameterName, target.remaining() * POINTER_SIZE, memAddress(target), __buffer.address());
  116. if (DEBUG) {
  117. checkCLError(errcode);
  118. }
  119. return (int) (__buffer.pointerValue(0) >> POINTER_SHIFT);
  120. }
  121. /**
  122. * Returns the string value for the specified {@code parameterName}. The raw
  123. * bytes returned are assumed to be ASCII encoded.
  124. *
  125. * @param object the object to query
  126. * @param arg an object argument
  127. * @param parameterName the parameter to query
  128. *
  129. * @return the parameter's string value
  130. */
  131. String getStringASCII(long object, long arg, int parameterName) {
  132. APIBuffer __buffer = apiBuffer();
  133. int bytes = getString(object, arg, parameterName, __buffer);
  134. return __buffer.stringValueASCII(0, bytes);
  135. }
  136. /**
  137. * Returns the string value for the specified {@code parameterName}. The raw
  138. * bytes returned are assumed to be ASCII encoded and have length equal to {@code
  139. * parameterValueSize}.
  140. *
  141. * @param object the object to query
  142. * @param arg an object argument
  143. * @param parameterName the parameter to query
  144. * @param parameterValueSize the explicit string length
  145. *
  146. * @return the parameter's string value
  147. */
  148. String getStringASCII(long object, long arg, int parameterName, int parameterValueSize) {
  149. APIBuffer __buffer = apiBuffer();
  150. int errcode = get(object, arg, parameterName, parameterValueSize, __buffer.address(), NULL);
  151. if (DEBUG) {
  152. checkCLError(errcode);
  153. }
  154. return __buffer.stringValueASCII(0, parameterValueSize);
  155. }
  156. /**
  157. * Returns the string value for the specified {@code parameterName}. The raw
  158. * bytes returned are assumed to be UTF-8 encoded.
  159. *
  160. * @param object the object to query
  161. * @param arg an object argument
  162. * @param parameterName the parameter to query
  163. *
  164. * @return the parameter's string value
  165. */
  166. String getStringUTF8(long object, long arg, int parameterName) {
  167. APIBuffer __buffer = apiBuffer();
  168. int bytes = getString(object, arg, parameterName, __buffer);
  169. return __buffer.stringValueUTF8(0, bytes);
  170. }
  171. /**
  172. * Returns the string value for the specified {@code parameterName}. The raw
  173. * bytes returned are assumed to be UTF-8 encoded and have length equal to {@code
  174. * parameterValueSize}.
  175. *
  176. * @param object the object to query
  177. * @param arg an object argument
  178. * @param parameterName the parameter to query
  179. * @param parameterValueSize the explicit string length
  180. *
  181. * @return the parameter's string value
  182. */
  183. String getStringUTF8(long object, long arg, int parameterName, int parameterValueSize) {
  184. APIBuffer __buffer = apiBuffer();
  185. int errcode = get(object, arg, parameterName, parameterValueSize, __buffer.address(), NULL);
  186. if (DEBUG) {
  187. checkCLError(errcode);
  188. }
  189. return __buffer.stringValueUTF8(0, parameterValueSize);
  190. }
  191. private int getString(long object, long arg, int parameterName, APIBuffer buffer) {
  192. // Get string length
  193. int errcode = get(object, arg, parameterName, 0, NULL, buffer.address());
  194. if (DEBUG) {
  195. checkCLError(errcode);
  196. }
  197. int bytes = (int) buffer.pointerValue(0);
  198. buffer.bufferParam(bytes + POINTER_SIZE);
  199. // Get string
  200. errcode = get(object, arg, parameterName, bytes, buffer.address(), NULL);
  201. if (DEBUG) {
  202. checkCLError(errcode);
  203. }
  204. return bytes - 1; // all OpenCL char[] parameters are null-terminated
  205. }
  206. }