PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cl4d/buffer.d

http://github.com/Trass3r/cl4d
D | 182 lines | 95 code | 19 blank | 68 comment | 0 complexity | f5beed4ca7ed0eeb64d637b47e5cf3ae MD5 | raw file
  1. /**
  2. * cl4d - object-oriented wrapper for the OpenCL C API
  3. * written in the D programming language
  4. *
  5. * Copyright:
  6. * (C) 2009-2014 Andreas Hollandt
  7. *
  8. * License:
  9. * see LICENSE.txt
  10. */
  11. module cl4d.buffer;
  12. import cl4d.c.cl;
  13. import cl4d.c.cl_gl;
  14. import cl4d.context;
  15. import cl4d.error;
  16. import cl4d.memory;
  17. import cl4d.wrapper;
  18. /**
  19. * buffer objects are generic memory objects for containing any type of data
  20. */
  21. // TODO: make CLBuffer know its type?
  22. struct CLBuffer
  23. {
  24. CLMemory sup;
  25. alias sup this;
  26. this(cl_mem obj)
  27. {
  28. sup = CLMemory(obj);
  29. }
  30. /**
  31. * create a buffer object from hostbuf
  32. *
  33. * Params:
  34. * context = is a valid OpenCL context used to create the buffer object
  35. * flags = is a bit-field that is used to specify allocation and usage information such as the memory
  36. * arena that should be used to allocate the buffer object and how it will be used
  37. * datasize= size in bytes of the buffer object to be allocated
  38. * hostptr = is a pointer to the buffer data that may already be allocated by the application
  39. */
  40. this(CLContext context, cl_mem_flags flags, size_t datasize, void* hostptr = null)
  41. {
  42. // call "base constructor"
  43. cl_errcode res;
  44. auto obj = clCreateBuffer(context.cptr, flags, datasize, hostptr, &res);
  45. mixin(exceptionHandling(
  46. ["CL_INVALID_CONTEXT", ""],
  47. ["CL_INVALID_BUFFER_SIZE", "hostbuf is empty"],
  48. ["CL_INVALID_HOST_PTR", "hostbuf is null and CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR are set in flags or hostbuf !is null but CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR are not set in flags"],
  49. ["CL_MEM_OBJECT_ALLOCATION_FAILURE",""],
  50. ["CL_OUT_OF_RESOURCES", ""],
  51. ["CL_OUT_OF_HOST_MEMORY", ""]
  52. ));
  53. this(obj);
  54. }
  55. version(CL_VERSION_1_1)
  56. /**
  57. * create a new buffer object representing a specific region in this buffer
  58. *
  59. * Params:
  60. * flags = a bit-field that is used to specify allocation and usage information about the image
  61. * memory object being created and is described in table 5.3
  62. * origin = defines the region's offset in this buffer
  63. * size = defines the size in bytes
  64. */
  65. CLBuffer createRegionSubBuffer(cl_mem_flags flags, size_t origin, size_t size)
  66. {
  67. cl_buffer_region reg = {origin, size};
  68. cl_errcode res;
  69. cl_mem ret = clCreateSubBuffer(this.cptr, flags, CL_BUFFER_CREATE_TYPE_REGION, &reg, &res);
  70. // TODO: handle flags separately? see CL_INVALID_VALUE message
  71. mixin(exceptionHandling(
  72. ["CL_INVALID_VALUE", "the region specified by (origin, size) is out of bounds in buffer OR buffer was created with CL_MEM_WRITE_ONLY and flags specifies CL_MEM_READ_WRITE or CL_MEM_READ_ONLY, OR if buffer was created with CL_MEM_READ_ONLY and flags specifies CL_MEM_READ_WRITE or CL_MEM_WRITE_ONLY, OR if flags specifies CL_MEM_USE_HOST_PTR or CL_MEM_ALLOC_HOST_PTR or CL_MEM_COPY_HOST_PTR"],
  73. ["CL_INVALID_BUFFER_SIZE", "size is 0"],
  74. ["CL_INVALID_MEM_OBJECT", "buffer is not a valid buffer object or is a sub-buffer object"],
  75. ["CL_MISALIGNED_SUB_BUFFER_OFFSET", "there are no devices in context associated with buffer for which the origin value is aligned to the CL_DEVICE_MEM_BASE_ADDR_ALIGN value"],
  76. ["CL_MEM_OBJECT_ALLOCATION_FAILURE",""],
  77. ["CL_OUT_OF_RESOURCES", ""],
  78. ["CL_OUT_OF_HOST_MEMORY", ""]
  79. ));
  80. return CLBuffer(ret);
  81. }
  82. @property
  83. {
  84. //! offset of a sub-buffer object, 0 otherwise
  85. size_t offset()
  86. {
  87. return this.getInfo!size_t(CL_MEM_OFFSET);
  88. }
  89. //! the the memory object specified as buffer argument to createSubBuffer, null otherwise
  90. CLBuffer superBuffer()
  91. {
  92. cl_mem sub = this.getInfo!cl_mem(CL_MEM_ASSOCIATED_MEMOBJECT);
  93. return CLBuffer(sub);
  94. }
  95. }
  96. }
  97. //! Memory buffer interface for GL interop.
  98. struct CLBufferGL
  99. {
  100. CLBuffer sup;
  101. alias sup this;
  102. /**
  103. * creates an OpenCL buffer object from an OpenGL buffer object
  104. *
  105. * Params:
  106. * context = a valid OpenCL context created from an OpenGL context
  107. * flags = only CL_MEM_READ_ONLY, CL_MEM_WRITE_ONLY and CL_MEM_READ_WRITE can be used
  108. * bufobj = a GL buffer object. The data store of the GL buffer object must have have
  109. * been previously created by calling glBufferData, although its contents need not be initialized.
  110. * The size of the data store will be used to determine the size of the CL buffer object
  111. */
  112. this(CLContext context, cl_mem_flags flags, cl_GLuint bufobj)
  113. {
  114. cl_errcode res;
  115. sup = CLBuffer(clCreateFromGLBuffer(context.cptr, flags, bufobj, &res));
  116. mixin(exceptionHandling(
  117. ["CL_INVALID_CONTEXT", "context is not a valid context or was not created from a GL context"],
  118. ["CL_INVALID_VALUE", "invalid flags"],
  119. ["CL_INVALID_GL_OBJECT", "bufobj is not a GL buffer object or is a GL buffer object but does not have an existing data store"],
  120. ["CL_OUT_OF_RESOURCES", ""],
  121. ["CL_OUT_OF_HOST_MEMORY", ""]
  122. ));
  123. }
  124. }
  125. /**
  126. * memory buffer interface for GL interop with renderbuffer
  127. *
  128. * NB: If the state of a GL renderbuffer object is modified through the GL API (i.e. changes to the
  129. * dimensions or format used to represent pixels of the GL renderbuffer using appropriate GL API
  130. * calls such as glRenderbufferStorage) while there exists a corresponding CL image object,
  131. * subsequent use of the CL image object will result in undefined behavior
  132. */
  133. struct CLBufferRenderGL
  134. {
  135. CLBuffer sup;
  136. alias sup this;
  137. /**
  138. * creates an OpenCL 2D image object from an OpenGL renderbuffer object
  139. *
  140. * Params:
  141. * context = a valid OpenCL context created from an OpenGL context
  142. * flags = only CL_MEM_READ_ONLY, CL_MEM_WRITE_ONLY and CL_MEM_READ_WRITE can be used
  143. * renderbuffer = renderbuffer is the name of a GL renderbuffer object.
  144. * The renderbuffer storage must be specified before the image object can be created. The renderbuffer format and dimensions
  145. * defined by OpenGL will be used to create the 2D image object. Only GL renderbuffers with
  146. * internal formats that maps to appropriate image channel order and data type specified in tables
  147. * 5.5 and 5.6 of the spec can be used to create the 2D image object
  148. */
  149. this(CLContext context, cl_mem_flags flags, cl_GLuint renderbuffer)
  150. {
  151. cl_errcode res;
  152. sup = CLBuffer(clCreateFromGLRenderbuffer(context.cptr, flags, renderbuffer, &res));
  153. mixin(exceptionHandling(
  154. ["CL_INVALID_CONTEXT", "context is not a valid context or was not created from a GL context"],
  155. ["CL_INVALID_VALUE", "invalid flags"],
  156. ["CL_INVALID_GL_OBJECT", "renderbuffer is not a GL renderbuffer object or if the width or height of renderbuffer is zero"],
  157. ["CL_INVALID_IMAGE_FORMAT_DESCRIPTOR", "the OpenGL renderbuffer internal format does not map to a supported OpenCL image format"],
  158. ["CL_INVALID_OPERATION", "renderbuffer is a multi-sample GL renderbuffer object"],
  159. ["CL_OUT_OF_RESOURCES", ""],
  160. ["CL_OUT_OF_HOST_MEMORY", ""]
  161. ));
  162. }
  163. }