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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2008 Ben Skeggs
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  19. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "pipe/p_state.h"
  23. #include "pipe/p_defines.h"
  24. #include "pipe/p_inlines.h"
  25. #include "nv50_context.h"
  26. static struct pipe_texture *
  27. nv50_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *tmp)
  28. {
  29. struct pipe_winsys *ws = pscreen->winsys;
  30. struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
  31. struct pipe_texture *pt = &mt->base;
  32. unsigned usage, width = tmp->width[0], height = tmp->height[0];
  33. unsigned depth = tmp->depth[0];
  34. int i, l;
  35. mt->base = *tmp;
  36. mt->base.refcount = 1;
  37. mt->base.screen = pscreen;
  38. usage = PIPE_BUFFER_USAGE_PIXEL;
  39. switch (pt->format) {
  40. case PIPE_FORMAT_Z24S8_UNORM:
  41. case PIPE_FORMAT_Z16_UNORM:
  42. usage |= NOUVEAU_BUFFER_USAGE_ZETA;
  43. break;
  44. default:
  45. break;
  46. }
  47. switch (pt->target) {
  48. case PIPE_TEXTURE_3D:
  49. mt->image_nr = pt->depth[0];
  50. break;
  51. case PIPE_TEXTURE_CUBE:
  52. mt->image_nr = 6;
  53. break;
  54. default:
  55. mt->image_nr = 1;
  56. break;
  57. }
  58. for (l = 0; l <= pt->last_level; l++) {
  59. struct nv50_miptree_level *lvl = &mt->level[l];
  60. pt->width[l] = width;
  61. pt->height[l] = height;
  62. pt->depth[l] = depth;
  63. pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
  64. pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
  65. lvl->image_offset = CALLOC(mt->image_nr, sizeof(int));
  66. lvl->pitch = align(pt->width[l] * pt->block.size, 64);
  67. width = MAX2(1, width >> 1);
  68. height = MAX2(1, height >> 1);
  69. depth = MAX2(1, depth >> 1);
  70. }
  71. for (i = 0; i < mt->image_nr; i++) {
  72. for (l = 0; l <= pt->last_level; l++) {
  73. struct nv50_miptree_level *lvl = &mt->level[l];
  74. int size;
  75. size = align(pt->width[l], 8) * pt->block.size;
  76. size = align(size, 64);
  77. size *= align(pt->height[l], 8) * pt->block.size;
  78. lvl->image_offset[i] = mt->total_size;
  79. mt->total_size += size;
  80. }
  81. }
  82. mt->buffer = ws->buffer_create(ws, 256, usage, mt->total_size);
  83. if (!mt->buffer) {
  84. FREE(mt);
  85. return NULL;
  86. }
  87. return &mt->base;
  88. }
  89. static struct pipe_texture *
  90. nv50_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
  91. const unsigned *stride, struct pipe_buffer *pb)
  92. {
  93. struct nv50_miptree *mt;
  94. /* Only supports 2D, non-mipmapped textures for the moment */
  95. if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
  96. pt->depth[0] != 1)
  97. return NULL;
  98. mt = CALLOC_STRUCT(nv50_miptree);
  99. if (!mt)
  100. return NULL;
  101. mt->base = *pt;
  102. mt->base.refcount = 1;
  103. mt->base.screen = pscreen;
  104. mt->image_nr = 1;
  105. mt->level[0].pitch = *stride;
  106. mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
  107. pipe_buffer_reference(pscreen, &mt->buffer, pb);
  108. return &mt->base;
  109. }
  110. static void
  111. nv50_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
  112. {
  113. struct pipe_texture *pt = *ppt;
  114. *ppt = NULL;
  115. if (--pt->refcount <= 0) {
  116. struct nv50_miptree *mt = nv50_miptree(pt);
  117. pipe_buffer_reference(pscreen, &mt->buffer, NULL);
  118. FREE(mt);
  119. }
  120. }
  121. static struct pipe_surface *
  122. nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
  123. unsigned face, unsigned level, unsigned zslice,
  124. unsigned flags)
  125. {
  126. struct nv50_miptree *mt = nv50_miptree(pt);
  127. struct nv50_miptree_level *lvl = &mt->level[level];
  128. struct pipe_surface *ps;
  129. int img;
  130. if (pt->target == PIPE_TEXTURE_CUBE)
  131. img = face;
  132. else
  133. if (pt->target == PIPE_TEXTURE_3D)
  134. img = zslice;
  135. else
  136. img = 0;
  137. ps = CALLOC_STRUCT(pipe_surface);
  138. if (!ps)
  139. return NULL;
  140. pipe_texture_reference(&ps->texture, pt);
  141. ps->format = pt->format;
  142. ps->width = pt->width[level];
  143. ps->height = pt->height[level];
  144. ps->usage = flags;
  145. ps->status = PIPE_SURFACE_STATUS_DEFINED;
  146. ps->refcount = 1;
  147. ps->face = face;
  148. ps->level = level;
  149. ps->zslice = zslice;
  150. ps->offset = lvl->image_offset[img];
  151. return ps;
  152. }
  153. static void
  154. nv50_miptree_surface_del(struct pipe_screen *pscreen,
  155. struct pipe_surface **psurface)
  156. {
  157. struct pipe_surface *ps = *psurface;
  158. struct nv50_surface *s = nv50_surface(ps);
  159. *psurface = NULL;
  160. if (--ps->refcount <= 0) {
  161. pipe_texture_reference(&ps->texture, NULL);
  162. FREE(s);
  163. }
  164. }
  165. void
  166. nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
  167. {
  168. pscreen->texture_create = nv50_miptree_create;
  169. pscreen->texture_blanket = nv50_miptree_blanket;
  170. pscreen->texture_release = nv50_miptree_release;
  171. pscreen->get_tex_surface = nv50_miptree_surface_new;
  172. pscreen->tex_surface_release = nv50_miptree_surface_del;
  173. }