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.

afsmultiarb.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * GL_ATI_fragment_shader test
  3. * Roland Scheidegger
  4. *
  5. * Command line options:
  6. * -info print GL implementation information
  7. */
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define GL_GLEXT_PROTOTYPES
  13. #include <GL/glut.h>
  14. #include "readtex.h"
  15. #define TEXTURE_1_FILE "../images/girl.rgb"
  16. #define TEXTURE_2_FILE "../images/reflect.rgb"
  17. #define TEX0 1
  18. #define TEX7 8
  19. #define ANIMATE 10
  20. #define SHADER 20
  21. #define QUIT 100
  22. static GLboolean Animate = GL_TRUE;
  23. static GLint NumUnits = 6;
  24. static GLboolean TexEnabled[8];
  25. static GLuint boringshaderID = 0;
  26. static GLuint boring2passID = 0;
  27. static GLboolean Shader = GL_FALSE;
  28. static GLfloat Drift = 0.0;
  29. static GLfloat drift_increment = 0.005;
  30. static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
  31. static GLfloat shaderconstant[4] = {0.5, 0.0, 0.0, 0.0};
  32. static void Idle( void )
  33. {
  34. if (Animate) {
  35. GLint i;
  36. Drift += drift_increment;
  37. if (Drift >= 1.0)
  38. Drift = 0.0;
  39. for (i = 0; i < NumUnits; i++) {
  40. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  41. glMatrixMode(GL_TEXTURE);
  42. glLoadIdentity();
  43. if (i == 0) {
  44. glTranslatef(Drift, 0.0, 0.0);
  45. glScalef(2, 2, 1);
  46. }
  47. else if (i == 1) {
  48. glTranslatef(0.0, Drift, 0.0);
  49. }
  50. else {
  51. glTranslatef(0.5, 0.5, 0.0);
  52. glRotatef(180.0 * Drift, 0, 0, 1);
  53. glScalef(1.0/i, 1.0/i, 1.0/i);
  54. glTranslatef(-0.5, -0.5, 0.0);
  55. }
  56. }
  57. glMatrixMode(GL_MODELVIEW);
  58. glutPostRedisplay();
  59. }
  60. }
  61. static void DrawObject(void)
  62. {
  63. GLint i;
  64. GLint j;
  65. static const GLfloat tex_coords[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
  66. static const GLfloat vtx_coords[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
  67. if (!TexEnabled[0] && !TexEnabled[1])
  68. glColor3f(0.1, 0.1, 0.1); /* add onto this */
  69. else
  70. glColor3f(1, 1, 1); /* modulate this */
  71. glBegin(GL_QUADS);
  72. /* Toggle between the vector and scalar entry points. This is done purely
  73. * to hit multiple paths in the driver.
  74. */
  75. if ( Drift > 0.49 ) {
  76. for (j = 0; j < 4; j++ ) {
  77. for (i = 0; i < NumUnits; i++)
  78. glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i,
  79. tex_coords[j], tex_coords[j+1]);
  80. glVertex2f( vtx_coords[j], vtx_coords[j+1] );
  81. }
  82. }
  83. else {
  84. for (j = 0; j < 4; j++ ) {
  85. for (i = 0; i < NumUnits; i++)
  86. glMultiTexCoord2fvARB(GL_TEXTURE0_ARB + i, & tex_coords[j]);
  87. glVertex2fv( & vtx_coords[j] );
  88. }
  89. }
  90. glEnd();
  91. }
  92. static void Display( void )
  93. {
  94. static GLint T0 = 0;
  95. static GLint Frames = 0;
  96. GLint t;
  97. glClear( GL_COLOR_BUFFER_BIT );
  98. glPushMatrix();
  99. glRotatef(Xrot, 1.0, 0.0, 0.0);
  100. glRotatef(Yrot, 0.0, 1.0, 0.0);
  101. glRotatef(Zrot, 0.0, 0.0, 1.0);
  102. glScalef(5.0, 5.0, 5.0);
  103. DrawObject();
  104. glPopMatrix();
  105. glutSwapBuffers();
  106. Frames++;
  107. t = glutGet(GLUT_ELAPSED_TIME);
  108. if (t - T0 >= 2500) {
  109. GLfloat seconds = (t - T0) / 1000.0;
  110. GLfloat fps = Frames / seconds;
  111. drift_increment = 2.2 * seconds / Frames;
  112. printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
  113. T0 = t;
  114. Frames = 0;
  115. }
  116. }
  117. static void Reshape( int width, int height )
  118. {
  119. glViewport( 0, 0, width, height );
  120. glMatrixMode( GL_PROJECTION );
  121. glLoadIdentity();
  122. glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  123. /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
  124. glMatrixMode( GL_MODELVIEW );
  125. glLoadIdentity();
  126. glTranslatef( 0.0, 0.0, -70.0 );
  127. }
  128. static void ModeMenu(int entry)
  129. {
  130. if (entry >= TEX0 && entry <= TEX7) {
  131. /* toggle */
  132. GLint i = entry - TEX0;
  133. TexEnabled[i] = !TexEnabled[i];
  134. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  135. if (TexEnabled[i])
  136. glEnable(GL_TEXTURE_2D);
  137. else
  138. glDisable(GL_TEXTURE_2D);
  139. printf("Enabled: ");
  140. for (i = 0; i < NumUnits; i++)
  141. printf("%d ", (int) TexEnabled[i]);
  142. printf("\n");
  143. }
  144. else if (entry==ANIMATE) {
  145. Animate = !Animate;
  146. }
  147. else if (entry==SHADER) {
  148. Shader = !Shader;
  149. if (Shader) {
  150. fprintf(stderr, "using 2-pass shader\n");
  151. glBindFragmentShaderATI(boring2passID);
  152. }
  153. else {
  154. fprintf(stderr, "using 1-pass shader\n");
  155. glBindFragmentShaderATI(boringshaderID);
  156. }
  157. }
  158. else if (entry==QUIT) {
  159. exit(0);
  160. }
  161. glutPostRedisplay();
  162. }
  163. static void Key( unsigned char key, int x, int y )
  164. {
  165. (void) x;
  166. (void) y;
  167. switch (key) {
  168. case 27:
  169. exit(0);
  170. break;
  171. }
  172. glutPostRedisplay();
  173. }
  174. static void SpecialKey( int key, int x, int y )
  175. {
  176. float step = 3.0;
  177. (void) x;
  178. (void) y;
  179. switch (key) {
  180. case GLUT_KEY_UP:
  181. Xrot += step;
  182. break;
  183. case GLUT_KEY_DOWN:
  184. Xrot -= step;
  185. break;
  186. case GLUT_KEY_LEFT:
  187. Yrot += step;
  188. break;
  189. case GLUT_KEY_RIGHT:
  190. Yrot -= step;
  191. break;
  192. }
  193. glutPostRedisplay();
  194. }
  195. static void Init( int argc, char *argv[] )
  196. {
  197. GLuint texObj[8];
  198. GLint size, i;
  199. const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  200. if (!strstr(exten, "GL_ATI_fragment_shader")) {
  201. printf("Sorry, GL_ATI_fragment_shader not supported by this renderer.\n");
  202. exit(1);
  203. }
  204. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
  205. printf("%d x %d max texture size\n", size, size);
  206. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  207. for (i = 0; i < NumUnits; i++) {
  208. if (i < 6)
  209. TexEnabled[i] = GL_TRUE;
  210. else
  211. TexEnabled[i] = GL_FALSE;
  212. }
  213. /* allocate two texture objects */
  214. glGenTextures(NumUnits, texObj);
  215. /* setup the texture objects */
  216. for (i = 0; i < NumUnits; i++) {
  217. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  218. glBindTexture(GL_TEXTURE_2D, texObj[i]);
  219. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  220. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  221. if (i == 0) {
  222. if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
  223. printf("Error: couldn't load texture image\n");
  224. exit(1);
  225. }
  226. }
  227. else if (i == 1) {
  228. if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
  229. printf("Error: couldn't load texture image\n");
  230. exit(1);
  231. }
  232. }
  233. else {
  234. /* checker */
  235. GLubyte image[8][8][3];
  236. GLint i, j;
  237. for (i = 0; i < 8; i++) {
  238. for (j = 0; j < 8; j++) {
  239. if ((i + j) & 1) {
  240. image[i][j][0] = 50;
  241. image[i][j][1] = 50;
  242. image[i][j][2] = 50;
  243. }
  244. else {
  245. image[i][j][0] = 25;
  246. image[i][j][1] = 25;
  247. image[i][j][2] = 25;
  248. }
  249. }
  250. }
  251. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
  252. GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
  253. }
  254. /* Bind texObj[i] to ith texture unit */
  255. /* if (i < 2)
  256. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  257. else
  258. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);*/
  259. if (TexEnabled[i])
  260. glEnable(GL_TEXTURE_2D);
  261. }
  262. boringshaderID = glGenFragmentShadersATI(1);
  263. boring2passID = glGenFragmentShadersATI(1);
  264. if (boring2passID == 0)
  265. {
  266. fprintf(stderr, "couldn't get frag shader id\n");
  267. exit(1);
  268. }
  269. glBindFragmentShaderATI(boringshaderID);
  270. /* maybe not the most creative shader but at least I know how it should look like! */
  271. glBeginFragmentShaderATI();
  272. glSampleMapATI(GL_REG_0_ATI, GL_TEXTURE0_ARB, GL_SWIZZLE_STR_ATI);
  273. glSampleMapATI(GL_REG_1_ATI, GL_TEXTURE1_ARB, GL_SWIZZLE_STR_ATI);
  274. glSampleMapATI(GL_REG_2_ATI, GL_TEXTURE2_ARB, GL_SWIZZLE_STR_ATI);
  275. glSampleMapATI(GL_REG_3_ATI, GL_TEXTURE3_ARB, GL_SWIZZLE_STR_ATI);
  276. glSampleMapATI(GL_REG_4_ATI, GL_TEXTURE4_ARB, GL_SWIZZLE_STR_ATI);
  277. glSampleMapATI(GL_REG_5_ATI, GL_TEXTURE5_ARB, GL_SWIZZLE_STR_ATI);
  278. glColorFragmentOp2ATI(GL_MUL_ATI,
  279. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  280. GL_REG_0_ATI, GL_NONE, GL_NONE,
  281. GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
  282. glAlphaFragmentOp1ATI(GL_MOV_ATI,
  283. GL_REG_0_ATI, GL_NONE,
  284. GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
  285. glColorFragmentOp3ATI(GL_MAD_ATI,
  286. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  287. GL_REG_0_ATI, GL_NONE, GL_NONE,
  288. GL_REG_1_ATI, GL_NONE, GL_NONE,
  289. GL_REG_2_ATI, GL_NONE, GL_NONE);
  290. glColorFragmentOp2ATI(GL_ADD_ATI,
  291. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  292. GL_REG_0_ATI, GL_NONE, GL_NONE,
  293. GL_REG_3_ATI, GL_NONE, GL_NONE);
  294. glColorFragmentOp2ATI(GL_ADD_ATI,
  295. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  296. GL_REG_0_ATI, GL_NONE, GL_NONE,
  297. GL_REG_4_ATI, GL_NONE, GL_NONE);
  298. glColorFragmentOp2ATI(GL_ADD_ATI,
  299. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  300. GL_REG_0_ATI, GL_NONE, GL_NONE,
  301. GL_REG_5_ATI, GL_NONE, GL_NONE);
  302. glEndFragmentShaderATI();
  303. /* mathematically equivalent to first shader but using 2 passes together with
  304. some tex coord rerouting */
  305. glBindFragmentShaderATI(boring2passID);
  306. glBeginFragmentShaderATI();
  307. glPassTexCoordATI(GL_REG_1_ATI, GL_TEXTURE0_ARB, GL_SWIZZLE_STR_ATI);
  308. glSampleMapATI(GL_REG_2_ATI, GL_TEXTURE2_ARB, GL_SWIZZLE_STR_ATI);
  309. glSampleMapATI(GL_REG_3_ATI, GL_TEXTURE3_ARB, GL_SWIZZLE_STR_ATI);
  310. glSampleMapATI(GL_REG_4_ATI, GL_TEXTURE4_ARB, GL_SWIZZLE_STR_ATI);
  311. glSampleMapATI(GL_REG_5_ATI, GL_TEXTURE5_ARB, GL_SWIZZLE_STR_ATI);
  312. glColorFragmentOp2ATI(GL_ADD_ATI,
  313. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  314. GL_REG_2_ATI, GL_NONE, GL_NONE,
  315. GL_REG_3_ATI, GL_NONE, GL_NONE);
  316. glColorFragmentOp2ATI(GL_ADD_ATI,
  317. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  318. GL_REG_0_ATI, GL_NONE, GL_NONE,
  319. GL_REG_4_ATI, GL_NONE, GL_NONE);
  320. glColorFragmentOp2ATI(GL_ADD_ATI,
  321. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  322. GL_REG_0_ATI, GL_NONE, GL_NONE,
  323. GL_REG_5_ATI, GL_NONE, GL_NONE);
  324. /* not really a dependant read */
  325. glSampleMapATI(GL_REG_0_ATI, GL_REG_1_ATI, GL_SWIZZLE_STR_ATI);
  326. glSampleMapATI(GL_REG_1_ATI, GL_TEXTURE1_ARB, GL_SWIZZLE_STR_ATI);
  327. glPassTexCoordATI(GL_REG_5_ATI, GL_REG_0_ATI, GL_SWIZZLE_STR_ATI);
  328. glColorFragmentOp2ATI(GL_MUL_ATI,
  329. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  330. GL_REG_0_ATI, GL_NONE, GL_NONE,
  331. GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
  332. glAlphaFragmentOp1ATI(GL_MOV_ATI,
  333. GL_REG_0_ATI, GL_NONE,
  334. GL_PRIMARY_COLOR, GL_NONE, GL_NONE);
  335. glColorFragmentOp3ATI(GL_MAD_ATI,
  336. GL_REG_0_ATI, GL_NONE, GL_SATURATE_BIT_ATI,
  337. GL_REG_0_ATI, GL_NONE, GL_NONE,
  338. GL_REG_1_ATI, GL_NONE, GL_NONE,
  339. GL_REG_5_ATI, GL_NONE, GL_NONE);
  340. /* in principle we're finished here, but to test a bit more
  341. we do some fun with dot ops, replication et al. */
  342. glSetFragmentShaderConstantATI(GL_CON_3_ATI, shaderconstant);
  343. glColorFragmentOp2ATI(GL_DOT4_ATI,
  344. GL_REG_3_ATI, GL_GREEN_BIT_ATI, GL_EIGHTH_BIT_ATI,
  345. GL_ZERO, GL_NONE, GL_COMP_BIT_ATI | GL_NEGATE_BIT_ATI,
  346. GL_CON_3_ATI, GL_RED, GL_2X_BIT_ATI);
  347. /* those args must get ignored, except dstReg */
  348. glAlphaFragmentOp2ATI(GL_DOT4_ATI,
  349. GL_REG_4_ATI, GL_NONE,
  350. GL_ZERO, GL_NONE, GL_NONE,
  351. GL_ZERO, GL_NONE, GL_NONE);
  352. /* -> reg3 g = reg4 alpha = -0.5 */
  353. glAlphaFragmentOp2ATI(GL_ADD_ATI,
  354. GL_REG_5_ATI, GL_NONE,
  355. GL_REG_3_ATI, GL_GREEN, GL_NONE,
  356. GL_REG_4_ATI, GL_NONE, GL_NONE);
  357. /* -> reg5 a = -1 */
  358. glColorFragmentOp3ATI(GL_DOT2_ADD_ATI,
  359. GL_REG_4_ATI, GL_BLUE_BIT_ATI, GL_HALF_BIT_ATI,
  360. GL_REG_5_ATI, GL_ALPHA, GL_NEGATE_BIT_ATI,
  361. GL_ONE, GL_NONE, GL_BIAS_BIT_ATI,
  362. GL_ONE, GL_ALPHA, GL_2X_BIT_ATI | GL_NEGATE_BIT_ATI);
  363. /* -> reg 4 b = -0.5 */
  364. glColorFragmentOp2ATI(GL_MUL_ATI,
  365. GL_REG_0_ATI, GL_NONE, GL_NONE,
  366. GL_REG_4_ATI, GL_BLUE, GL_NEGATE_BIT_ATI | GL_2X_BIT_ATI,
  367. GL_REG_0_ATI, GL_NONE, GL_NONE);
  368. glEndFragmentShaderATI();
  369. glBindFragmentShaderATI(boringshaderID);
  370. glEnable(GL_FRAGMENT_SHADER_ATI);
  371. glShadeModel(GL_FLAT);
  372. glClearColor(0.3, 0.3, 0.4, 1.0);
  373. if (argc > 1 && strcmp(argv[1], "-info")==0) {
  374. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  375. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  376. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  377. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  378. }
  379. printf("output should be identical with both shaders to multiarb demo when 6 textures are enabled\n");
  380. }
  381. int main( int argc, char *argv[] )
  382. {
  383. /* GLint i;*/
  384. glutInit( &argc, argv );
  385. glutInitWindowSize( 300, 300 );
  386. glutInitWindowPosition( 0, 0 );
  387. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  388. glutCreateWindow(argv[0] );
  389. Init( argc, argv );
  390. glutReshapeFunc( Reshape );
  391. glutKeyboardFunc( Key );
  392. glutSpecialFunc( SpecialKey );
  393. glutDisplayFunc( Display );
  394. glutIdleFunc( Idle );
  395. glutCreateMenu(ModeMenu);
  396. /* for (i = 0; i < NumUnits; i++) {
  397. char s[100];
  398. sprintf(s, "Toggle Texture %d", i);
  399. glutAddMenuEntry(s, TEX0 + i);
  400. }*/
  401. glutAddMenuEntry("Toggle 1/2 Pass Shader", SHADER);
  402. glutAddMenuEntry("Toggle Animation", ANIMATE);
  403. glutAddMenuEntry("Quit", QUIT);
  404. glutAttachMenu(GLUT_RIGHT_BUTTON);
  405. glutMainLoop();
  406. return 0;
  407. }