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.

fill.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
  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
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /**
  22. * Measure fill rates.
  23. *
  24. * Brian Paul
  25. * 21 Sep 2009
  26. */
  27. #include "glmain.h"
  28. #include "common.h"
  29. int WinWidth = 1000, WinHeight = 1000;
  30. static GLuint VBO, TexObj;
  31. struct vertex
  32. {
  33. GLfloat x, y, s, t, r, g, b, a;
  34. };
  35. #define VOFFSET(F) ((void *) offsetof(struct vertex, F))
  36. static const struct vertex vertices[4] = {
  37. /* x y s t r g b a */
  38. { -1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5 },
  39. { 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5 },
  40. { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.5 },
  41. { -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.5 }
  42. };
  43. static const char *VertexShader =
  44. "void main() \n"
  45. "{ \n"
  46. " gl_Position = ftransform(); \n"
  47. " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
  48. " gl_FrontColor = gl_Color; \n"
  49. "} \n";
  50. /* simple fragment shader */
  51. static const char *FragmentShader1 =
  52. "uniform sampler2D Tex; \n"
  53. "void main() \n"
  54. "{ \n"
  55. " vec4 t = texture2D(Tex, gl_TexCoord[0].xy); \n"
  56. " gl_FragColor = vec4(1.0) - t * gl_Color; \n"
  57. "} \n";
  58. /**
  59. * A more complex fragment shader (but equivalent to first shader).
  60. * A good optimizer should catch some of these no-op operations, but
  61. * probably not all of them.
  62. */
  63. static const char *FragmentShader2 =
  64. "uniform sampler2D Tex; \n"
  65. "void main() \n"
  66. "{ \n"
  67. " // as above \n"
  68. " vec4 t = texture2D(Tex, gl_TexCoord[0].xy); \n"
  69. " t = vec4(1.0) - t * gl_Color; \n"
  70. " vec4 u; \n"
  71. " // no-op negate/swizzle \n"
  72. " u = -t.wzyx; \n"
  73. " t = -u.wzyx; \n"
  74. " // no-op inverts \n"
  75. " t = vec4(1.0) - t; \n"
  76. " t = vec4(1.0) - t; \n"
  77. " // no-op min/max \n"
  78. " t = min(t, t); \n"
  79. " t = max(t, t); \n"
  80. " // no-op moves \n"
  81. " u = t; \n"
  82. " t = u; \n"
  83. " u = t; \n"
  84. " t = u; \n"
  85. " // no-op add/mul \n"
  86. " t = (t + t + t + t) * 0.25; \n"
  87. " // no-op mul/sub \n"
  88. " t = 3.0 * t - 2.0 * t; \n"
  89. " // no-op negate/min/max \n"
  90. " t = -min(-t, -t); \n"
  91. " t = -max(-t, -t); \n"
  92. " gl_FragColor = t; \n"
  93. "} \n";
  94. static GLuint ShaderProg1, ShaderProg2;
  95. /** Called from test harness/main */
  96. void
  97. PerfInit(void)
  98. {
  99. GLint u;
  100. /* setup VBO w/ vertex data */
  101. glGenBuffersARB(1, &VBO);
  102. glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
  103. glBufferDataARB(GL_ARRAY_BUFFER_ARB,
  104. sizeof(vertices), vertices, GL_STATIC_DRAW_ARB);
  105. glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), VOFFSET(x));
  106. glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), VOFFSET(s));
  107. glColorPointer(4, GL_FLOAT, sizeof(struct vertex), VOFFSET(r));
  108. glEnableClientState(GL_VERTEX_ARRAY);
  109. glEnableClientState(GL_COLOR_ARRAY);
  110. /* setup texture */
  111. TexObj = PerfCheckerTexture(128, 128);
  112. /* setup shaders */
  113. ShaderProg1 = PerfShaderProgram(VertexShader, FragmentShader1);
  114. glUseProgram(ShaderProg1);
  115. u = glGetUniformLocation(ShaderProg1, "Tex");
  116. glUniform1i(u, 0); /* texture unit 0 */
  117. ShaderProg2 = PerfShaderProgram(VertexShader, FragmentShader2);
  118. glUseProgram(ShaderProg2);
  119. u = glGetUniformLocation(ShaderProg2, "Tex");
  120. glUniform1i(u, 0); /* texture unit 0 */
  121. glUseProgram(0);
  122. }
  123. static void
  124. Ortho(void)
  125. {
  126. glMatrixMode(GL_PROJECTION);
  127. glLoadIdentity();
  128. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  129. glMatrixMode(GL_MODELVIEW);
  130. glLoadIdentity();
  131. }
  132. static void
  133. DrawQuad(unsigned count)
  134. {
  135. unsigned i;
  136. glClear(GL_COLOR_BUFFER_BIT);
  137. for (i = 0; i < count; i++) {
  138. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  139. /* Avoid sending command buffers with huge numbers of fullscreen
  140. * quads. Graphics schedulers don't always cope well with
  141. * this...
  142. */
  143. if (i % 128 == 0) {
  144. PerfSwapBuffers();
  145. glClear(GL_COLOR_BUFFER_BIT);
  146. }
  147. }
  148. glFinish();
  149. if (1)
  150. PerfSwapBuffers();
  151. }
  152. void
  153. PerfNextRound(void)
  154. {
  155. }
  156. /** Called from test harness/main */
  157. void
  158. PerfDraw(void)
  159. {
  160. double rate;
  161. double pixelsPerDraw = WinWidth * WinHeight;
  162. Ortho();
  163. /* simple fill */
  164. rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
  165. perf_printf(" Simple fill: %s pixels/second\n",
  166. PerfHumanFloat(rate));
  167. /* blended fill */
  168. glEnable(GL_BLEND);
  169. rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
  170. glDisable(GL_BLEND);
  171. perf_printf(" Blended fill: %s pixels/second\n",
  172. PerfHumanFloat(rate));
  173. /* textured fill */
  174. glEnable(GL_TEXTURE_2D);
  175. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  176. rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
  177. glDisable(GL_TEXTURE_2D);
  178. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  179. perf_printf(" Textured fill: %s pixels/second\n",
  180. PerfHumanFloat(rate));
  181. /* shader1 fill */
  182. glUseProgram(ShaderProg1);
  183. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  184. rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
  185. glUseProgram(0);
  186. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  187. perf_printf(" Shader1 fill: %s pixels/second\n",
  188. PerfHumanFloat(rate));
  189. /* shader2 fill */
  190. glUseProgram(ShaderProg2);
  191. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  192. rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
  193. glUseProgram(0);
  194. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  195. perf_printf(" Shader2 fill: %s pixels/second\n",
  196. PerfHumanFloat(rate));
  197. exit(0);
  198. }