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.

u_simple_shaders.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**************************************************************************
  2. *
  3. * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
  4. * All Rights Reserved.
  5. * Copyright 2009 Marek Olšák <maraeo@gmail.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  22. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  23. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /**
  29. * @file
  30. * Simple vertex/fragment shader generators.
  31. *
  32. * @author Brian Paul
  33. Marek Olšák
  34. */
  35. #include "pipe/p_context.h"
  36. #include "pipe/p_shader_tokens.h"
  37. #include "util/u_simple_shaders.h"
  38. #include "tgsi/tgsi_ureg.h"
  39. /**
  40. * Make simple vertex pass-through shader.
  41. * \param num_attribs number of attributes to pass through
  42. * \param semantic_names array of semantic names for each attribute
  43. * \param semantic_indexes array of semantic indexes for each attribute
  44. */
  45. void *
  46. util_make_vertex_passthrough_shader(struct pipe_context *pipe,
  47. uint num_attribs,
  48. const uint *semantic_names,
  49. const uint *semantic_indexes)
  50. {
  51. struct ureg_program *ureg;
  52. uint i;
  53. ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
  54. if (ureg == NULL)
  55. return NULL;
  56. for (i = 0; i < num_attribs; i++) {
  57. struct ureg_src src;
  58. struct ureg_dst dst;
  59. src = ureg_DECL_vs_input( ureg, i );
  60. dst = ureg_DECL_output( ureg,
  61. semantic_names[i],
  62. semantic_indexes[i]);
  63. ureg_MOV( ureg, dst, src );
  64. }
  65. ureg_END( ureg );
  66. return ureg_create_shader_and_destroy( ureg, pipe );
  67. }
  68. /**
  69. * Make simple fragment texture shader:
  70. * IMM {0,0,0,1} // (if writemask != 0xf)
  71. * MOV OUT[0], IMM[0] // (if writemask != 0xf)
  72. * TEX OUT[0].writemask, IN[0], SAMP[0], 2D;
  73. * END;
  74. */
  75. void *
  76. util_make_fragment_tex_shader_writemask(struct pipe_context *pipe,
  77. unsigned tex_target,
  78. unsigned writemask )
  79. {
  80. struct ureg_program *ureg;
  81. struct ureg_src sampler;
  82. struct ureg_src tex;
  83. struct ureg_dst out;
  84. ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
  85. if (ureg == NULL)
  86. return NULL;
  87. sampler = ureg_DECL_sampler( ureg, 0 );
  88. tex = ureg_DECL_fs_input( ureg,
  89. TGSI_SEMANTIC_GENERIC, 0,
  90. TGSI_INTERPOLATE_PERSPECTIVE );
  91. out = ureg_DECL_output( ureg,
  92. TGSI_SEMANTIC_COLOR,
  93. 0 );
  94. if (writemask != TGSI_WRITEMASK_XYZW) {
  95. struct ureg_src imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
  96. ureg_MOV( ureg, out, imm );
  97. }
  98. ureg_TEX( ureg,
  99. ureg_writemask(out, writemask),
  100. tex_target, tex, sampler );
  101. ureg_END( ureg );
  102. return ureg_create_shader_and_destroy( ureg, pipe );
  103. }
  104. /**
  105. * Make a simple fragment shader that sets the output color to a color
  106. * taken from a texture.
  107. * \param tex_target one of PIPE_TEXTURE_x
  108. */
  109. void *
  110. util_make_fragment_tex_shader(struct pipe_context *pipe, unsigned tex_target )
  111. {
  112. return util_make_fragment_tex_shader_writemask( pipe,
  113. tex_target,
  114. TGSI_WRITEMASK_XYZW );
  115. }
  116. /**
  117. * Make a simple fragment texture shader which reads an X component from
  118. * a texture and writes it as depth.
  119. */
  120. void *
  121. util_make_fragment_tex_shader_writedepth(struct pipe_context *pipe,
  122. unsigned tex_target)
  123. {
  124. struct ureg_program *ureg;
  125. struct ureg_src sampler;
  126. struct ureg_src tex;
  127. struct ureg_dst out, depth;
  128. struct ureg_src imm;
  129. ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
  130. if (ureg == NULL)
  131. return NULL;
  132. sampler = ureg_DECL_sampler( ureg, 0 );
  133. tex = ureg_DECL_fs_input( ureg,
  134. TGSI_SEMANTIC_GENERIC, 0,
  135. TGSI_INTERPOLATE_PERSPECTIVE );
  136. out = ureg_DECL_output( ureg,
  137. TGSI_SEMANTIC_COLOR,
  138. 0 );
  139. depth = ureg_DECL_output( ureg,
  140. TGSI_SEMANTIC_POSITION,
  141. 0 );
  142. imm = ureg_imm4f( ureg, 0, 0, 0, 1 );
  143. ureg_MOV( ureg, out, imm );
  144. ureg_TEX( ureg,
  145. ureg_writemask(depth, TGSI_WRITEMASK_Z),
  146. tex_target, tex, sampler );
  147. ureg_END( ureg );
  148. return ureg_create_shader_and_destroy( ureg, pipe );
  149. }
  150. /**
  151. * Make simple fragment color pass-through shader.
  152. */
  153. void *
  154. util_make_fragment_passthrough_shader(struct pipe_context *pipe)
  155. {
  156. return util_make_fragment_clonecolor_shader(pipe, 1);
  157. }
  158. /**
  159. * Make a fragment shader that copies the input color to N output colors.
  160. */
  161. void *
  162. util_make_fragment_clonecolor_shader(struct pipe_context *pipe, int num_cbufs)
  163. {
  164. struct ureg_program *ureg;
  165. struct ureg_src src;
  166. struct ureg_dst dst[8];
  167. int i;
  168. assert(num_cbufs <= 8);
  169. ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
  170. if (ureg == NULL)
  171. return NULL;
  172. src = ureg_DECL_fs_input( ureg, TGSI_SEMANTIC_COLOR, 0,
  173. TGSI_INTERPOLATE_PERSPECTIVE );
  174. for (i = 0; i < num_cbufs; i++)
  175. dst[i] = ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, i );
  176. for (i = 0; i < num_cbufs; i++)
  177. ureg_MOV( ureg, dst[i], src );
  178. ureg_END( ureg );
  179. return ureg_create_shader_and_destroy( ureg, pipe );
  180. }