Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i915_resource_buffer.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**************************************************************************
  2. *
  3. * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors:
  29. * Keith Whitwell <keith@tungstengraphics.com>
  30. * Michel Dänzer <michel@tungstengraphics.com>
  31. */
  32. #include "pipe/p_context.h"
  33. #include "pipe/p_defines.h"
  34. #include "util/u_inlines.h"
  35. #include "util/u_math.h"
  36. #include "util/u_memory.h"
  37. #include "i915_context.h"
  38. #include "i915_resource.h"
  39. static boolean
  40. i915_buffer_get_handle(struct pipe_screen *screen,
  41. struct pipe_resource *resource,
  42. struct winsys_handle *handle)
  43. {
  44. return FALSE;
  45. }
  46. static void
  47. i915_buffer_destroy(struct pipe_screen *screen,
  48. struct pipe_resource *resource)
  49. {
  50. struct i915_buffer *buffer = i915_buffer(resource);
  51. if (buffer->free_on_destroy)
  52. align_free(buffer->data);
  53. FREE(buffer);
  54. }
  55. static struct pipe_transfer *
  56. i915_get_transfer(struct pipe_context *pipe,
  57. struct pipe_resource *resource,
  58. unsigned level,
  59. unsigned usage,
  60. const struct pipe_box *box)
  61. {
  62. struct i915_context *i915 = i915_context(pipe);
  63. struct pipe_transfer *transfer;
  64. if (usage & PIPE_TRANSFER_MAP_PERMANENTLY) {
  65. return NULL;
  66. }
  67. transfer = util_slab_alloc(&i915->transfer_pool);
  68. if (transfer == NULL)
  69. return NULL;
  70. transfer->resource = resource;
  71. transfer->level = level;
  72. transfer->usage = usage;
  73. transfer->box = *box;
  74. /* Note strides are zero, this is ok for buffers, but not for
  75. * textures 2d & higher at least.
  76. */
  77. return transfer;
  78. }
  79. static void
  80. i915_transfer_destroy(struct pipe_context *pipe,
  81. struct pipe_transfer *transfer)
  82. {
  83. struct i915_context *i915 = i915_context(pipe);
  84. util_slab_free(&i915->transfer_pool, transfer);
  85. }
  86. static void *
  87. i915_buffer_transfer_map( struct pipe_context *pipe,
  88. struct pipe_transfer *transfer )
  89. {
  90. struct i915_buffer *buffer = i915_buffer(transfer->resource);
  91. return buffer->data + transfer->box.x;
  92. }
  93. static void
  94. i915_buffer_transfer_inline_write( struct pipe_context *rm_ctx,
  95. struct pipe_resource *resource,
  96. unsigned level,
  97. unsigned usage,
  98. const struct pipe_box *box,
  99. const void *data,
  100. unsigned stride,
  101. unsigned layer_stride)
  102. {
  103. struct i915_buffer *buffer = i915_buffer(resource);
  104. memcpy(buffer->data + box->x,
  105. data,
  106. box->width);
  107. }
  108. struct u_resource_vtbl i915_buffer_vtbl =
  109. {
  110. i915_buffer_get_handle, /* get_handle */
  111. i915_buffer_destroy, /* resource_destroy */
  112. i915_get_transfer, /* get_transfer */
  113. i915_transfer_destroy, /* transfer_destroy */
  114. i915_buffer_transfer_map, /* transfer_map */
  115. u_default_transfer_flush_region, /* transfer_flush_region */
  116. u_default_transfer_unmap, /* transfer_unmap */
  117. i915_buffer_transfer_inline_write /* transfer_inline_write */
  118. };
  119. struct pipe_resource *
  120. i915_buffer_create(struct pipe_screen *screen,
  121. const struct pipe_resource *template)
  122. {
  123. struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
  124. if (!buf)
  125. return NULL;
  126. buf->b.b = *template;
  127. buf->b.vtbl = &i915_buffer_vtbl;
  128. pipe_reference_init(&buf->b.b.reference, 1);
  129. buf->b.b.screen = screen;
  130. buf->data = align_malloc(template->width0, 16);
  131. buf->free_on_destroy = TRUE;
  132. if (!buf->data)
  133. goto err;
  134. return &buf->b.b;
  135. err:
  136. FREE(buf);
  137. return NULL;
  138. }
  139. struct pipe_resource *
  140. i915_user_buffer_create(struct pipe_screen *screen,
  141. void *ptr,
  142. unsigned bytes,
  143. unsigned bind)
  144. {
  145. struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
  146. if (!buf)
  147. return NULL;
  148. pipe_reference_init(&buf->b.b.reference, 1);
  149. buf->b.vtbl = &i915_buffer_vtbl;
  150. buf->b.b.screen = screen;
  151. buf->b.b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
  152. buf->b.b.usage = PIPE_USAGE_IMMUTABLE;
  153. buf->b.b.bind = bind;
  154. buf->b.b.flags = 0;
  155. buf->b.b.width0 = bytes;
  156. buf->b.b.height0 = 1;
  157. buf->b.b.depth0 = 1;
  158. buf->b.b.array_size = 1;
  159. buf->data = ptr;
  160. buf->free_on_destroy = FALSE;
  161. return &buf->b.b;
  162. }