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.

pb_validate.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**************************************************************************
  2. *
  3. * Copyright 2008 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. * @file
  29. * Buffer validation.
  30. *
  31. * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
  32. */
  33. #include "pipe/p_compiler.h"
  34. #include "pipe/p_defines.h"
  35. #include "util/u_memory.h"
  36. #include "util/u_debug.h"
  37. #include "pb_buffer.h"
  38. #include "pb_buffer_fenced.h"
  39. #include "pb_validate.h"
  40. #define PB_VALIDATE_INITIAL_SIZE 1 /* 512 */
  41. struct pb_validate_entry
  42. {
  43. struct pb_buffer *buf;
  44. unsigned flags;
  45. };
  46. struct pb_validate
  47. {
  48. struct pb_validate_entry *entries;
  49. unsigned used;
  50. unsigned size;
  51. };
  52. enum pipe_error
  53. pb_validate_add_buffer(struct pb_validate *vl,
  54. struct pb_buffer *buf,
  55. unsigned flags)
  56. {
  57. assert(buf);
  58. if(!buf)
  59. return PIPE_ERROR;
  60. assert(flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE);
  61. assert(!(flags & ~PIPE_BUFFER_USAGE_GPU_READ_WRITE));
  62. flags &= PIPE_BUFFER_USAGE_GPU_READ_WRITE;
  63. /* We only need to store one reference for each buffer, so avoid storing
  64. * consecutive references for the same buffer. It might not be the most
  65. * common pattern, but it is easy to implement.
  66. */
  67. if(vl->used && vl->entries[vl->used - 1].buf == buf) {
  68. vl->entries[vl->used - 1].flags |= flags;
  69. return PIPE_OK;
  70. }
  71. /* Grow the table */
  72. if(vl->used == vl->size) {
  73. unsigned new_size;
  74. struct pb_validate_entry *new_entries;
  75. new_size = vl->size * 2;
  76. if(!new_size)
  77. return PIPE_ERROR_OUT_OF_MEMORY;
  78. new_entries = (struct pb_validate_entry *)REALLOC(vl->entries,
  79. vl->size*sizeof(struct pb_validate_entry),
  80. new_size*sizeof(struct pb_validate_entry));
  81. if(!new_entries)
  82. return PIPE_ERROR_OUT_OF_MEMORY;
  83. memset(new_entries + vl->size, 0, (new_size - vl->size)*sizeof(struct pb_validate_entry));
  84. vl->size = new_size;
  85. vl->entries = new_entries;
  86. }
  87. assert(!vl->entries[vl->used].buf);
  88. pb_reference(&vl->entries[vl->used].buf, buf);
  89. vl->entries[vl->used].flags = flags;
  90. ++vl->used;
  91. return PIPE_OK;
  92. }
  93. enum pipe_error
  94. pb_validate_foreach(struct pb_validate *vl,
  95. enum pipe_error (*callback)(struct pb_buffer *buf, void *data),
  96. void *data)
  97. {
  98. unsigned i;
  99. for(i = 0; i < vl->used; ++i) {
  100. enum pipe_error ret;
  101. ret = callback(vl->entries[i].buf, data);
  102. if(ret != PIPE_OK)
  103. return ret;
  104. }
  105. return PIPE_OK;
  106. }
  107. enum pipe_error
  108. pb_validate_validate(struct pb_validate *vl)
  109. {
  110. unsigned i;
  111. for(i = 0; i < vl->used; ++i) {
  112. enum pipe_error ret;
  113. ret = pb_validate(vl->entries[i].buf, vl, vl->entries[i].flags);
  114. if(ret != PIPE_OK) {
  115. while(i--)
  116. pb_validate(vl->entries[i].buf, NULL, 0);
  117. return ret;
  118. }
  119. }
  120. return PIPE_OK;
  121. }
  122. void
  123. pb_validate_fence(struct pb_validate *vl,
  124. struct pipe_fence_handle *fence)
  125. {
  126. unsigned i;
  127. for(i = 0; i < vl->used; ++i) {
  128. pb_fence(vl->entries[i].buf, fence);
  129. pb_reference(&vl->entries[i].buf, NULL);
  130. }
  131. vl->used = 0;
  132. }
  133. void
  134. pb_validate_destroy(struct pb_validate *vl)
  135. {
  136. unsigned i;
  137. for(i = 0; i < vl->used; ++i)
  138. pb_reference(&vl->entries[i].buf, NULL);
  139. FREE(vl->entries);
  140. FREE(vl);
  141. }
  142. struct pb_validate *
  143. pb_validate_create()
  144. {
  145. struct pb_validate *vl;
  146. vl = CALLOC_STRUCT(pb_validate);
  147. if(!vl)
  148. return NULL;
  149. vl->size = PB_VALIDATE_INITIAL_SIZE;
  150. vl->entries = (struct pb_validate_entry *)CALLOC(vl->size, sizeof(struct pb_validate_entry));
  151. if(!vl->entries) {
  152. FREE(vl);
  153. return NULL;
  154. }
  155. return vl;
  156. }