glamor_rects.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright © 2014 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include "glamor_priv.h"
  23. #include "glamor_program.h"
  24. #include "glamor_transform.h"
  25. static const glamor_facet glamor_facet_polyfillrect_130 = {
  26. .name = "poly_fill_rect",
  27. .version = 130,
  28. .vs_vars = "attribute vec4 primitive;\n",
  29. .vs_exec = (" vec2 pos = primitive.zw * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
  30. GLAMOR_POS(gl_Position, (primitive.xy + pos))),
  31. };
  32. static const glamor_facet glamor_facet_polyfillrect_120 = {
  33. .name = "poly_fill_rect",
  34. .vs_vars = "attribute vec2 primitive;\n",
  35. .vs_exec = (" vec2 pos = vec2(0,0);\n"
  36. GLAMOR_POS(gl_Position, primitive.xy)),
  37. };
  38. static Bool
  39. glamor_poly_fill_rect_gl(DrawablePtr drawable,
  40. GCPtr gc, int nrect, xRectangle *prect)
  41. {
  42. ScreenPtr screen = drawable->pScreen;
  43. glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
  44. PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
  45. glamor_pixmap_private *pixmap_priv;
  46. glamor_program *prog;
  47. int off_x, off_y;
  48. GLshort *v;
  49. char *vbo_offset;
  50. int box_x, box_y;
  51. pixmap_priv = glamor_get_pixmap_private(pixmap);
  52. if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
  53. goto bail;
  54. glamor_make_current(glamor_priv);
  55. if (glamor_priv->glsl_version >= 130) {
  56. prog = glamor_use_program_fill(pixmap, gc,
  57. &glamor_priv->poly_fill_rect_program,
  58. &glamor_facet_polyfillrect_130);
  59. if (!prog)
  60. goto bail_ctx;
  61. /* Set up the vertex buffers for the points */
  62. v = glamor_get_vbo_space(drawable->pScreen, nrect * sizeof (xRectangle), &vbo_offset);
  63. glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
  64. glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1);
  65. glVertexAttribPointer(GLAMOR_VERTEX_POS, 4, GL_SHORT, GL_FALSE,
  66. 4 * sizeof (short), vbo_offset);
  67. memcpy(v, prect, nrect * sizeof (xRectangle));
  68. glamor_put_vbo_space(screen);
  69. } else {
  70. int n;
  71. prog = glamor_use_program_fill(pixmap, gc,
  72. &glamor_priv->poly_fill_rect_program,
  73. &glamor_facet_polyfillrect_120);
  74. if (!prog)
  75. goto bail_ctx;
  76. /* Set up the vertex buffers for the points */
  77. v = glamor_get_vbo_space(drawable->pScreen, nrect * 8 * sizeof (short), &vbo_offset);
  78. glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
  79. glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
  80. 2 * sizeof (short), vbo_offset);
  81. for (n = 0; n < nrect; n++) {
  82. v[0] = prect->x; v[1] = prect->y;
  83. v[2] = prect->x; v[3] = prect->y + prect->height;
  84. v[4] = prect->x + prect->width; v[5] = prect->y + prect->height;
  85. v[6] = prect->x + prect->width; v[7] = prect->y;
  86. prect++;
  87. v += 8;
  88. }
  89. glamor_put_vbo_space(screen);
  90. }
  91. glEnable(GL_SCISSOR_TEST);
  92. glamor_pixmap_loop(pixmap_priv, box_x, box_y) {
  93. int nbox = RegionNumRects(gc->pCompositeClip);
  94. BoxPtr box = RegionRects(gc->pCompositeClip);
  95. glamor_set_destination_drawable(drawable, box_x, box_y, TRUE, FALSE, prog->matrix_uniform, &off_x, &off_y);
  96. while (nbox--) {
  97. glScissor(box->x1 + off_x,
  98. box->y1 + off_y,
  99. box->x2 - box->x1,
  100. box->y2 - box->y1);
  101. box++;
  102. if (glamor_priv->glsl_version >= 130)
  103. glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, nrect);
  104. else {
  105. if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
  106. glDrawArrays(GL_QUADS, 0, nrect * 4);
  107. } else {
  108. int i;
  109. for (i = 0; i < nrect; i++) {
  110. glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. glDisable(GL_SCISSOR_TEST);
  117. glDisable(GL_COLOR_LOGIC_OP);
  118. if (glamor_priv->glsl_version >= 130)
  119. glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
  120. glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
  121. return TRUE;
  122. bail_ctx:
  123. glDisable(GL_COLOR_LOGIC_OP);
  124. bail:
  125. return FALSE;
  126. }
  127. static void
  128. glamor_poly_fill_rect_bail(DrawablePtr drawable,
  129. GCPtr gc, int nrect, xRectangle *prect)
  130. {
  131. glamor_fallback("to %p (%c)\n", drawable,
  132. glamor_get_drawable_location(drawable));
  133. if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
  134. glamor_prepare_access_gc(gc)) {
  135. fbPolyFillRect(drawable, gc, nrect, prect);
  136. }
  137. glamor_finish_access_gc(gc);
  138. glamor_finish_access(drawable);
  139. }
  140. void
  141. glamor_poly_fill_rect(DrawablePtr drawable,
  142. GCPtr gc, int nrect, xRectangle *prect)
  143. {
  144. if (glamor_poly_fill_rect_gl(drawable, gc, nrect, prect))
  145. return;
  146. glamor_poly_fill_rect_bail(drawable, gc, nrect, prect);
  147. }
  148. Bool
  149. glamor_poly_fill_rect_nf(DrawablePtr drawable,
  150. GCPtr gc, int nrect, xRectangle *prect)
  151. {
  152. if (glamor_poly_fill_rect_gl(drawable, gc, nrect, prect))
  153. return TRUE;
  154. if (glamor_ddx_fallback_check_pixmap(drawable) && glamor_ddx_fallback_check_gc(gc))
  155. return FALSE;
  156. glamor_poly_fill_rect_bail(drawable, gc, nrect, prect);
  157. return TRUE;
  158. }