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.

st_texture.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /**************************************************************************
  2. *
  3. * Copyright 2007 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. #include "st_context.h"
  28. #include "st_format.h"
  29. #include "st_public.h"
  30. #include "st_texture.h"
  31. #include "st_cb_fbo.h"
  32. #include "main/enums.h"
  33. #include "main/teximage.h"
  34. #include "main/texstore.h"
  35. #undef Elements /* fix re-defined macro warning */
  36. #include "pipe/p_state.h"
  37. #include "pipe/p_context.h"
  38. #include "pipe/p_defines.h"
  39. #include "pipe/p_inlines.h"
  40. #include "pipe/p_inlines.h"
  41. #include "util/u_rect.h"
  42. #define DBG if(0) printf
  43. #if 0
  44. static GLenum
  45. target_to_target(GLenum target)
  46. {
  47. switch (target) {
  48. case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
  49. case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
  50. case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
  51. case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
  52. case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
  53. case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
  54. return GL_TEXTURE_CUBE_MAP_ARB;
  55. default:
  56. return target;
  57. }
  58. }
  59. #endif
  60. /**
  61. * Allocate a new pipe_texture object
  62. * width0, height0, depth0 are the dimensions of the level 0 image
  63. * (the highest resolution). last_level indicates how many mipmap levels
  64. * to allocate storage for. For non-mipmapped textures, this will be zero.
  65. */
  66. struct pipe_texture *
  67. st_texture_create(struct st_context *st,
  68. enum pipe_texture_target target,
  69. enum pipe_format format,
  70. GLuint last_level,
  71. GLuint width0,
  72. GLuint height0,
  73. GLuint depth0,
  74. GLuint compress_byte,
  75. GLuint usage )
  76. {
  77. struct pipe_texture pt, *newtex;
  78. struct pipe_screen *screen = st->pipe->screen;
  79. assert(target <= PIPE_TEXTURE_CUBE);
  80. DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
  81. _mesa_lookup_enum_by_nr(target),
  82. _mesa_lookup_enum_by_nr(format), last_level);
  83. assert(format);
  84. assert(screen->is_format_supported(screen, format, target,
  85. PIPE_TEXTURE_USAGE_SAMPLER, 0));
  86. memset(&pt, 0, sizeof(pt));
  87. pt.target = target;
  88. pt.format = format;
  89. pt.last_level = last_level;
  90. pt.width[0] = width0;
  91. pt.height[0] = height0;
  92. pt.depth[0] = depth0;
  93. pt.compressed = compress_byte ? 1 : 0;
  94. pf_get_block(format, &pt.block);
  95. pt.tex_usage = usage;
  96. newtex = screen->texture_create(screen, &pt);
  97. assert(!newtex || newtex->refcount == 1);
  98. return newtex;
  99. }
  100. /**
  101. * Check if a texture image be pulled into a unified mipmap texture.
  102. * This mirrors the completeness test in a lot of ways.
  103. *
  104. * Not sure whether I want to pass gl_texture_image here.
  105. */
  106. GLboolean
  107. st_texture_match_image(const struct pipe_texture *pt,
  108. const struct gl_texture_image *image,
  109. GLuint face, GLuint level)
  110. {
  111. /* Images with borders are never pulled into mipmap textures.
  112. */
  113. if (image->Border)
  114. return GL_FALSE;
  115. if (st_mesa_format_to_pipe_format(image->TexFormat->MesaFormat) != pt->format ||
  116. image->IsCompressed != pt->compressed)
  117. return GL_FALSE;
  118. /* Test image dimensions against the base level image adjusted for
  119. * minification. This will also catch images not present in the
  120. * texture, changed targets, etc.
  121. */
  122. if (image->Width != pt->width[level] ||
  123. image->Height != pt->height[level] ||
  124. image->Depth != pt->depth[level])
  125. return GL_FALSE;
  126. return GL_TRUE;
  127. }
  128. #if 000
  129. /* Although we use the image_offset[] array to store relative offsets
  130. * to cube faces, Mesa doesn't know anything about this and expects
  131. * each cube face to be treated as a separate image.
  132. *
  133. * These functions present that view to mesa:
  134. */
  135. const GLuint *
  136. st_texture_depth_offsets(struct pipe_texture *pt, GLuint level)
  137. {
  138. static const GLuint zero = 0;
  139. if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1)
  140. return &zero;
  141. else
  142. return pt->level[level].image_offset;
  143. }
  144. /**
  145. * Return the offset to the given mipmap texture image within the
  146. * texture memory buffer, in bytes.
  147. */
  148. GLuint
  149. st_texture_image_offset(const struct pipe_texture * pt,
  150. GLuint face, GLuint level)
  151. {
  152. if (pt->target == PIPE_TEXTURE_CUBE)
  153. return (pt->level[level].level_offset +
  154. pt->level[level].image_offset[face] * pt->cpp);
  155. else
  156. return pt->level[level].level_offset;
  157. }
  158. #endif
  159. /**
  160. * Map a teximage in a mipmap texture.
  161. * \param row_stride returns row stride in bytes
  162. * \param image_stride returns image stride in bytes (for 3D textures).
  163. * \return address of mapping
  164. */
  165. GLubyte *
  166. st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
  167. GLuint zoffset, enum pipe_transfer_usage usage,
  168. GLuint x, GLuint y, GLuint w, GLuint h)
  169. {
  170. struct pipe_screen *screen = st->pipe->screen;
  171. struct pipe_texture *pt = stImage->pt;
  172. DBG("%s \n", __FUNCTION__);
  173. stImage->transfer = screen->get_tex_transfer(screen, pt, stImage->face,
  174. stImage->level, zoffset,
  175. usage, x, y, w, h);
  176. if (stImage->transfer)
  177. return screen->transfer_map(screen, stImage->transfer);
  178. else
  179. return NULL;
  180. }
  181. void
  182. st_texture_image_unmap(struct st_context *st,
  183. struct st_texture_image *stImage)
  184. {
  185. struct pipe_screen *screen = st->pipe->screen;
  186. DBG("%s\n", __FUNCTION__);
  187. screen->transfer_unmap(screen, stImage->transfer);
  188. screen->tex_transfer_release(screen, &stImage->transfer);
  189. }
  190. /**
  191. * Upload data to a rectangular sub-region. Lots of choices how to do this:
  192. *
  193. * - memcpy by span to current destination
  194. * - upload data as new buffer and blit
  195. *
  196. * Currently always memcpy.
  197. */
  198. static void
  199. st_surface_data(struct pipe_context *pipe,
  200. struct pipe_transfer *dst,
  201. unsigned dstx, unsigned dsty,
  202. const void *src, unsigned src_stride,
  203. unsigned srcx, unsigned srcy, unsigned width, unsigned height)
  204. {
  205. struct pipe_screen *screen = pipe->screen;
  206. void *map = screen->transfer_map(screen, dst);
  207. pipe_copy_rect(map,
  208. &dst->block,
  209. dst->stride,
  210. dstx, dsty,
  211. width, height,
  212. src, src_stride,
  213. srcx, srcy);
  214. screen->transfer_unmap(screen, dst);
  215. }
  216. /* Upload data for a particular image.
  217. */
  218. void
  219. st_texture_image_data(struct pipe_context *pipe,
  220. struct pipe_texture *dst,
  221. GLuint face,
  222. GLuint level,
  223. void *src,
  224. GLuint src_row_stride, GLuint src_image_stride)
  225. {
  226. struct pipe_screen *screen = pipe->screen;
  227. GLuint depth = dst->depth[level];
  228. GLuint i;
  229. const GLubyte *srcUB = src;
  230. struct pipe_transfer *dst_transfer;
  231. DBG("%s\n", __FUNCTION__);
  232. for (i = 0; i < depth; i++) {
  233. dst_transfer = screen->get_tex_transfer(screen, dst, face, level, i,
  234. PIPE_TRANSFER_WRITE, 0, 0,
  235. dst->width[level],
  236. dst->height[level]);
  237. st_surface_data(pipe, dst_transfer,
  238. 0, 0, /* dstx, dsty */
  239. srcUB,
  240. src_row_stride,
  241. 0, 0, /* source x, y */
  242. dst->width[level], dst->height[level]); /* width, height */
  243. screen->tex_transfer_release(screen, &dst_transfer);
  244. srcUB += src_image_stride;
  245. }
  246. }
  247. /* Copy mipmap image between textures
  248. */
  249. void
  250. st_texture_image_copy(struct pipe_context *pipe,
  251. struct pipe_texture *dst, GLuint dstLevel,
  252. struct pipe_texture *src,
  253. GLuint face)
  254. {
  255. struct pipe_screen *screen = pipe->screen;
  256. GLuint width = dst->width[dstLevel];
  257. GLuint height = dst->height[dstLevel];
  258. GLuint depth = dst->depth[dstLevel];
  259. struct pipe_surface *src_surface;
  260. struct pipe_surface *dst_surface;
  261. GLuint i;
  262. for (i = 0; i < depth; i++) {
  263. GLuint srcLevel;
  264. /* find src texture level of needed size */
  265. for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
  266. if (src->width[srcLevel] == width &&
  267. src->height[srcLevel] == height) {
  268. break;
  269. }
  270. }
  271. assert(src->width[srcLevel] == width);
  272. assert(src->height[srcLevel] == height);
  273. #if 0
  274. {
  275. src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
  276. PIPE_BUFFER_USAGE_CPU_READ);
  277. ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ);
  278. map += src_surface->width * src_surface->height * 4 / 2;
  279. printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
  280. __FUNCTION__,
  281. map[0], map[1], map[2], map[3],
  282. src, srcLevel, dst, dstLevel);
  283. screen->surface_unmap(screen, src_surface);
  284. pipe_surface_reference(&src_surface, NULL);
  285. }
  286. #endif
  287. dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
  288. PIPE_BUFFER_USAGE_GPU_WRITE);
  289. src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
  290. PIPE_BUFFER_USAGE_GPU_READ);
  291. pipe->surface_copy(pipe,
  292. FALSE,
  293. dst_surface,
  294. 0, 0, /* destX, Y */
  295. src_surface,
  296. 0, 0, /* srcX, Y */
  297. width, height);
  298. screen->tex_surface_release(screen, &src_surface);
  299. screen->tex_surface_release(screen, &dst_surface);
  300. }
  301. }
  302. /** Bind a pipe surface for use as a texture image */
  303. int
  304. st_set_teximage(struct pipe_texture *pt, int target)
  305. {
  306. GET_CURRENT_CONTEXT(ctx);
  307. const GLuint unit = ctx->Texture.CurrentUnit;
  308. struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
  309. struct gl_texture_object *texObj;
  310. struct gl_texture_image *texImage;
  311. struct st_texture_image *stImage;
  312. int internalFormat;
  313. switch (pt->format) {
  314. case PIPE_FORMAT_A8R8G8B8_UNORM:
  315. internalFormat = GL_RGBA8;
  316. break;
  317. default:
  318. return 0;
  319. };
  320. switch (target) {
  321. case ST_TEXTURE_2D:
  322. target = GL_TEXTURE_2D;
  323. break;
  324. case ST_TEXTURE_RECT:
  325. target = GL_TEXTURE_RECTANGLE_ARB;
  326. break;
  327. default:
  328. return 0;
  329. }
  330. texObj = _mesa_select_tex_object(ctx, texUnit, target);
  331. texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
  332. stImage = st_texture_image(texImage);
  333. _mesa_init_teximage_fields(ctx, GL_TEXTURE_2D, texImage, pt->width[0],
  334. pt->height[0], 1, 0, internalFormat);
  335. texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, GL_RGBA,
  336. GL_UNSIGNED_BYTE);
  337. _mesa_set_fetch_functions(texImage, 2);
  338. pipe_texture_reference(&stImage->pt, pt);
  339. return 1;
  340. }
  341. /** Redirect rendering into stfb's surface to a texture image */
  342. int
  343. st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex,
  344. int target, int format, int level)
  345. {
  346. GET_CURRENT_CONTEXT(ctx);
  347. struct st_context *st = ctx->st;
  348. struct pipe_context *pipe = st->pipe;
  349. struct pipe_screen *screen = pipe->screen;
  350. const GLuint unit = ctx->Texture.CurrentUnit;
  351. struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
  352. struct gl_texture_object *texObj;
  353. struct gl_texture_image *texImage;
  354. struct st_texture_image *stImage;
  355. struct st_renderbuffer *strb;
  356. GLint face = 0, slice = 0;
  357. assert(surfIndex <= ST_SURFACE_DEPTH);
  358. strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
  359. if (strb->texture_save || strb->surface_save) {
  360. /* Error! */
  361. return 0;
  362. }
  363. if (target == ST_TEXTURE_2D) {
  364. texObj = texUnit->Current2D;
  365. texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, level);
  366. stImage = st_texture_image(texImage);
  367. }
  368. else {
  369. /* unsupported target */
  370. return 0;
  371. }
  372. st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
  373. /* save the renderbuffer's surface/texture info */
  374. pipe_texture_reference(&strb->texture_save, strb->texture);
  375. pipe_surface_reference(&strb->surface_save, strb->surface);
  376. /* plug in new surface/texture info */
  377. pipe_texture_reference(&strb->texture, stImage->pt);
  378. strb->surface = screen->get_tex_surface(screen, strb->texture,
  379. face, level, slice,
  380. (PIPE_BUFFER_USAGE_GPU_READ |
  381. PIPE_BUFFER_USAGE_GPU_WRITE));
  382. st->dirty.st |= ST_NEW_FRAMEBUFFER;
  383. return 1;
  384. }
  385. /** Undo surface-to-texture binding */
  386. int
  387. st_release_teximage(struct st_framebuffer *stfb, uint surfIndex,
  388. int target, int format, int level)
  389. {
  390. GET_CURRENT_CONTEXT(ctx);
  391. struct st_context *st = ctx->st;
  392. struct st_renderbuffer *strb;
  393. assert(surfIndex <= ST_SURFACE_DEPTH);
  394. strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
  395. if (!strb->texture_save || !strb->surface_save) {
  396. /* Error! */
  397. return 0;
  398. }
  399. st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
  400. /* free tex surface, restore original */
  401. pipe_surface_reference(&strb->surface, strb->surface_save);
  402. pipe_texture_reference(&strb->texture, strb->texture_save);
  403. pipe_surface_reference(&strb->surface_save, NULL);
  404. pipe_texture_reference(&strb->texture_save, NULL);
  405. st->dirty.st |= ST_NEW_FRAMEBUFFER;
  406. return 1;
  407. }