PageRenderTime 94ms CodeModel.GetById 17ms RepoModel.GetById 4ms app.codeStats 1ms

/GT-I5700/gles20/src/glShaderApi.cpp

https://github.com/DE-NISkA/cm_android_vendor_spica
C++ | 2452 lines | 1700 code | 528 blank | 224 comment | 555 complexity | 1265ebb273c29b64e9be47aa6cd2bb30 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. *******************************************************************************
  3. *
  4. * SAMSUNG INDIA SOFTWARE OPERATIONS
  5. * Copyright(C) 2006
  6. * ALL RIGHTS RESERVED
  7. *
  8. * This program is proprietary to Samsung India Software Operations Pvt. Ltd.,
  9. * and is protected under International Copyright Act as an unpublished work.Its
  10. * use and disclosure is limited by the terms and conditions of a license agree-
  11. * -ment. It may not be copied or otherwise reproduced or disclosed to persons
  12. * outside the licensee's organization except in accordance with the terms and
  13. * conditions of such an agreement. All copies and reproductions shall be the
  14. * property of Samsung India Software Operations Pvt. Ltd. and must bear this
  15. * notice in its entirety.
  16. *
  17. *******************************************************************************
  18. */
  19. /*
  20. ***************************************************************************//*!
  21. *
  22. * \file glShaderApi.cpp
  23. * \author Sandeep Kakarlapudi (s.kakarla@samsung.com)
  24. * Anurag Ved (anuragv@samsung.com)
  25. * \brief Implementation of GL shader and program entry points
  26. *
  27. *//*---------------------------------------------------------------------------
  28. * NOTES:
  29. *
  30. *//*---------------------------------------------------------------------------
  31. * HISTORY:
  32. *
  33. * 07.08.2006 Sandeep Kakarlapudi Initial version from ogl2_fimg.c
  34. *
  35. *******************************************************************************
  36. */
  37. #include "glState.h"
  38. #include "platform.h"
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. #include <stdio.h>
  43. #include <cutils/log.h>
  44. #include <cutils/properties.h>
  45. static const char VSMagicNum[] = {'V', 'S', ' ', ' '};
  46. static const char FSMagicNum[] = {'P', 'S', ' ', ' '};
  47. GL_API void GL_APIENTRY glAttachShader (GLuint program, GLuint shader)
  48. {
  49. GET_GL_STATE(ctx);
  50. if(program == 0)
  51. {
  52. set_err(GL_INVALID_VALUE);
  53. return; //TODO: Spec is not clear on this
  54. // silently ignore or not
  55. }
  56. if(shader == 0)
  57. {
  58. set_err(GL_INVALID_VALUE);
  59. return;
  60. }
  61. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  62. ShaderProgNameList::const_iterator shaderIt = ctx->shaderProgNames.find(shader);
  63. if((progIt == ctx->shaderProgNames.end()) || (shaderIt == ctx->shaderProgNames.end()))
  64. {
  65. set_err(GL_INVALID_VALUE);
  66. return;
  67. }
  68. if((progIt->type != PROGRAM) || (shaderIt->type != SHADER))
  69. {
  70. set_err(GL_INVALID_OPERATION);
  71. return;
  72. }
  73. gAssert((progIt->programPtr != 0) && "program ptr in name list is 0!!!");
  74. gAssert((shaderIt->shaderPtr != 0) && "shader ptr in name list is 0!!!");
  75. if((progIt->programPtr->isAttached(shaderIt->shaderPtr)))
  76. {
  77. set_err(GL_INVALID_OPERATION);
  78. return;
  79. }
  80. progIt->programPtr->attachShader(shaderIt->shaderPtr);
  81. }
  82. GL_API void GL_APIENTRY glBindAttribLocation (GLuint program, GLuint index, const char *name)
  83. {
  84. GET_GL_STATE(ctx);
  85. //-------- Validate attrib binding values --------
  86. if((index >= MAX_VERTEX_ATTRIBS)||(program == 0))
  87. {
  88. set_err(GL_INVALID_VALUE);
  89. return;
  90. }
  91. if( (name == 0) || ( (name[0] == 'g') && (name[1] == 'l') && (name[2] == '_') ) )
  92. {
  93. //TODO: Spec ambiguous on error on name == 0!
  94. set_err(GL_INVALID_OPERATION);
  95. return;
  96. }
  97. //-------- Check if the program name a valid program name --------
  98. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  99. if( (progIt == ctx->shaderProgNames.end()) )
  100. {
  101. set_err(GL_INVALID_VALUE);
  102. return;
  103. }
  104. if( progIt->type != PROGRAM )
  105. {
  106. set_err(GL_INVALID_OPERATION);
  107. return;
  108. }
  109. gAssert( (progIt->programPtr != 0) && "Prog ptr is 0!\n" );
  110. if( ! progIt->programPtr->attribBindings.setBinding(name, index) )
  111. {
  112. set_err(GL_OUT_OF_MEMORY);
  113. return;
  114. }
  115. }
  116. GL_API GLuint GL_APIENTRY glCreateProgram (void)
  117. {
  118. GET_GL_STATE(ctx);
  119. GLuint i = ctx->shaderProgNames.getUnusedName();
  120. ctx->shaderProgNames.freeUnused();
  121. //create a program and add it to the list.
  122. ShaderProgramNameMap temp(new Program(i),i);
  123. ctx->shaderProgNames.push_back(temp);
  124. return temp.id;
  125. }
  126. GL_API GLuint GL_APIENTRY glCreateShader (GLenum type)
  127. {
  128. GET_GL_STATE(ctx);
  129. ShaderType sType;
  130. switch(type)
  131. {
  132. case GL_VERTEX_SHADER:
  133. sType = VERTEX_SHADER;
  134. break;
  135. case GL_FRAGMENT_SHADER:
  136. sType = FRAGMENT_SHADER;
  137. break;
  138. default:
  139. set_err(GL_INVALID_ENUM);
  140. return 0;
  141. }
  142. GLuint i = ctx->shaderProgNames.getUnusedName();
  143. ctx->shaderProgNames.freeUnused();
  144. //Create a shader and add it to the list
  145. ShaderProgramNameMap temp(new Shader(sType,i), i);
  146. ctx->shaderProgNames.push_back(temp);
  147. return temp.id;
  148. }
  149. GL_API void GL_APIENTRY glDeleteProgram (GLuint program)
  150. {
  151. GET_GL_STATE(ctx);
  152. if(program == 0)
  153. {
  154. return;
  155. }
  156. ShaderProgNameList::const_iterator it = ctx->shaderProgNames.find(program);
  157. if(it == ctx->shaderProgNames.end())
  158. {
  159. set_err(GL_INVALID_VALUE);
  160. return;
  161. }
  162. else if(it->type != PROGRAM) //TODO: Spec is ambiguous, clarify
  163. {
  164. set_err(GL_INVALID_OPERATION);
  165. return;
  166. }
  167. else if(it->id == ctx->current_program)
  168. {
  169. gAssert(it->programPtr != 0);
  170. it->programPtr->deleteStatus = true;
  171. }
  172. else
  173. {
  174. ctx->shaderProgNames.remove(it->id);
  175. }
  176. }
  177. GL_API void GL_APIENTRY glDeleteShader (GLuint shader)
  178. {
  179. GET_GL_STATE(ctx);
  180. if(shader == 0)
  181. {
  182. return;
  183. }
  184. ShaderProgNameList::const_iterator it = ctx->shaderProgNames.find(shader);
  185. if(it == ctx->shaderProgNames.end())
  186. {
  187. set_err(GL_INVALID_VALUE);
  188. return;
  189. }
  190. else if( it->type != SHADER) //TODO: Spec is ambiguous, clarify
  191. {
  192. set_err(GL_INVALID_OPERATION);
  193. return;
  194. }
  195. else if( it->shaderPtr->refCount != 0)
  196. {
  197. it->shaderPtr->deleteStatus = true;
  198. }
  199. else
  200. {
  201. ctx->shaderProgNames.remove(it->id);
  202. }
  203. }
  204. GL_API void GL_APIENTRY glDetachShader (GLuint program, GLuint shader)
  205. {
  206. GET_GL_STATE(ctx);
  207. if((program == 0))
  208. {
  209. set_err(GL_INVALID_VALUE); //added by chanchal
  210. return; //TODO: Spec is not clear on this:
  211. // silently ignore or not?
  212. }
  213. if(shader == 0)
  214. {
  215. set_err(GL_INVALID_VALUE);
  216. return;
  217. }
  218. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  219. ShaderProgNameList::const_iterator shaderIt = ctx->shaderProgNames.find(shader);
  220. if((progIt == ctx->shaderProgNames.end()) || (shaderIt == ctx->shaderProgNames.end()))
  221. {
  222. set_err(GL_INVALID_VALUE);
  223. return;
  224. }
  225. if((progIt->type != PROGRAM) || (shaderIt->type != SHADER))
  226. {
  227. set_err(GL_INVALID_OPERATION);
  228. return;
  229. }
  230. gAssert((progIt->programPtr != 0) && "program ptr in name list is 0!!!");
  231. gAssert((shaderIt->shaderPtr != 0) && "shader ptr in name list is 0!!!");
  232. if( !(progIt->programPtr->isAttached(shaderIt->shaderPtr)) )
  233. {
  234. set_err(GL_INVALID_OPERATION);
  235. return;
  236. }
  237. progIt->programPtr->detachShader(shaderIt->shaderPtr);
  238. }
  239. GL_API void GL_APIENTRY glGetActiveAttrib (GLuint program, GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, char *name)
  240. {
  241. GET_GL_STATE(ctx);
  242. if( (!size) || (!type) || (!name))
  243. {
  244. return;
  245. }
  246. // if(program == 0) //error checking added by chanchal
  247. // {
  248. // set_err(GL_INVALID_VALUE);
  249. // return;
  250. // }
  251. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  252. if( (progIt == ctx->shaderProgNames.end()) )
  253. {
  254. set_err(GL_INVALID_VALUE);
  255. return;
  256. }
  257. if( progIt->type != PROGRAM )
  258. {
  259. set_err(GL_INVALID_OPERATION);
  260. return;
  261. }
  262. gAssert((progIt->programPtr!=0) && "prog ptr is 0!!");
  263. Program* prog = progIt->programPtr;
  264. if(index >= prog->vars.getNumActiveAttributes())
  265. {
  266. set_err(GL_INVALID_VALUE);
  267. return;
  268. }
  269. if(bufsize < 0)
  270. {
  271. set_err(GL_INVALID_VALUE);
  272. return;
  273. }
  274. if(bufsize < 1)
  275. {
  276. return;
  277. }
  278. int len = Plat::min(bufsize-1, GLsizei(prog->vars.attributeArray[index].name.length()));
  279. *type = prog->vars.attributeArray[index].type;
  280. *size = 1;
  281. strncpy(name, prog->vars.attributeArray[index].name.c_str(), len);
  282. name[len] = '\0';
  283. if(length)
  284. {
  285. *length = len;
  286. }
  287. }
  288. GL_API void GL_APIENTRY glGetActiveUniform (GLuint program, GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, char *name)
  289. {
  290. GET_GL_STATE(ctx);
  291. if( (!size) || (!type) || (!name))
  292. {
  293. return;
  294. }
  295. // if(program == 0) //error checking added by chanchal
  296. // {
  297. // set_err(GL_INVALID_VALUE);
  298. // return;
  299. // }
  300. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  301. if( (progIt == ctx->shaderProgNames.end()) )
  302. {
  303. set_err(GL_INVALID_VALUE);
  304. return;
  305. }
  306. if( progIt->type != PROGRAM )
  307. {
  308. set_err(GL_INVALID_OPERATION);
  309. return;
  310. }
  311. gAssert((progIt->programPtr!=0) && "prog ptr is 0!!");
  312. Program* prog = progIt->programPtr;
  313. if(index >= prog->vars.getNumActiveUniforms())
  314. {
  315. set_err(GL_INVALID_VALUE);
  316. return;
  317. }
  318. if(bufsize < 0)
  319. {
  320. set_err(GL_INVALID_VALUE);
  321. return;
  322. }
  323. if(bufsize < 1)
  324. {
  325. return;
  326. }
  327. NameMap::const_iterator it = prog->vars.uniformTbl.uniformNameMap.begin();
  328. for(unsigned int i=0; i<index; i++)
  329. {
  330. ++it;
  331. }
  332. int len = Plat::min(bufsize-1, GLsizei(it->first.length()));
  333. *type = prog->vars.uniformTbl.uniforms[it->second.index].type;
  334. *size = it->second.size;
  335. strncpy(name, prog->vars.uniformTbl.uniforms[it->second.index].name.c_str(), len);
  336. name[len] = '\0';
  337. if(length)
  338. {
  339. *length = len;
  340. }
  341. }
  342. GL_API void GL_APIENTRY glGetAttachedShaders (GLuint program, GLsizei maxcount, GLsizei *count, GLuint *shaders)
  343. {
  344. GET_GL_STATE(ctx);
  345. if(program == 0) // error checking added by chanchal
  346. {
  347. set_err(GL_INVALID_VALUE);
  348. return;
  349. }
  350. ShaderProgNameList::const_iterator it = ctx->shaderProgNames.find(program);
  351. if(it == ctx->shaderProgNames.end())
  352. {
  353. set_err(GL_INVALID_VALUE);
  354. return;
  355. }
  356. else if(it->type != PROGRAM)
  357. {
  358. set_err(GL_INVALID_OPERATION);
  359. return;
  360. }
  361. gAssert( (it->programPtr != 0) && "Prog ptr is 0!\n" );
  362. if(maxcount < 0)
  363. {
  364. set_err(GL_INVALID_VALUE);
  365. return;
  366. }
  367. Program* prog = it->programPtr;
  368. int numShaders = 0;
  369. if(numShaders > maxcount)
  370. return;
  371. if( prog->vertexShader!=0)
  372. {
  373. if(shaders) //TODO: SPEC AMBIGUITY : what error on null? nv driver crashes!
  374. {
  375. shaders[numShaders] = prog->vertexShader->id;
  376. }
  377. numShaders++;
  378. }
  379. if(numShaders > maxcount)
  380. return;
  381. if( prog->fragmentShader!=0)
  382. {
  383. if(shaders) //TODO: SPEC AMBIGUITY : what error on null? nv driver crashes!
  384. {
  385. shaders[numShaders] = prog->fragmentShader->id;
  386. }
  387. numShaders++;
  388. }
  389. if(count)
  390. {
  391. *count = numShaders;
  392. }
  393. }
  394. GL_API int GL_APIENTRY glGetAttribLocation (GLuint program, const char *name)
  395. {
  396. GET_GL_STATE(ctx);
  397. // if(program == 0) //error checking added by chanchal
  398. // {
  399. // set_err(GL_INVALID_VALUE);
  400. // return -1;
  401. // }
  402. //-------- Check if the program name a valid program name --------
  403. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  404. if( (progIt == ctx->shaderProgNames.end()) )
  405. {
  406. set_err(GL_INVALID_VALUE);
  407. return -1;
  408. }
  409. if( progIt->type != PROGRAM )
  410. {
  411. set_err(GL_INVALID_OPERATION);
  412. return -1;
  413. }
  414. gAssert((progIt->programPtr!=0) && "prog ptr is 0!!");
  415. if( progIt->programPtr->linkStatus != true )
  416. {
  417. set_err(GL_INVALID_OPERATION);
  418. return -1;
  419. }
  420. const ustl::string nameStr(name);
  421. for( unsigned int i=0; i< progIt->programPtr->vars.numAttributeVars; i++)
  422. {
  423. if(nameStr == progIt->programPtr->vars.attributeArray[i].name)
  424. {
  425. int vsRegIndex = progIt->programPtr->vars.attributeArray[i].vsRegIndex;
  426. return static_cast<int>(progIt->programPtr->progExecutable.attribMap[vsRegIndex]);
  427. }
  428. }
  429. return -1;
  430. }
  431. GL_API void GL_APIENTRY glGetProgramiv (GLuint program, GLenum pname, GLint *params)
  432. {
  433. GET_GL_STATE(ctx);
  434. if(!params) //TODO: SPEC AMBIGUITY: behaviour when params is null?
  435. {
  436. return;
  437. }
  438. ShaderProgNameList::const_iterator it = ctx->shaderProgNames.find(program);
  439. if(it == ctx->shaderProgNames.end())
  440. {
  441. set_err(GL_INVALID_VALUE);
  442. return;
  443. }
  444. else if(it->type != PROGRAM)
  445. {
  446. set_err(GL_INVALID_OPERATION);
  447. return;
  448. }
  449. gAssert( (it->programPtr != 0) && "Prog ptr is 0!\n" );
  450. Program *prog = it->programPtr;
  451. switch(pname)
  452. {
  453. case GL_DELETE_STATUS:
  454. *params = (prog->deleteStatus)? GL_TRUE : GL_FALSE;
  455. break;
  456. case GL_LINK_STATUS:
  457. *params = (prog->linkStatus)? GL_TRUE: GL_FALSE;
  458. break;
  459. case GL_VALIDATE_STATUS:
  460. *params = (prog->validationStatus)? GL_TRUE: GL_FALSE;
  461. break;
  462. case GL_INFO_LOG_LENGTH:
  463. *params = prog->getInfoLogLength();
  464. break;
  465. case GL_ATTACHED_SHADERS:
  466. *params = prog->getNumAttachedShaders();
  467. break;
  468. case GL_ACTIVE_ATTRIBUTES:
  469. *params = prog->vars.getNumActiveAttributes();
  470. break;
  471. case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
  472. *params = prog->vars.getActiveAttributeMaxLength();
  473. break;
  474. case GL_ACTIVE_UNIFORMS:
  475. *params = prog->vars.getNumActiveUniforms();
  476. break;
  477. case GL_ACTIVE_UNIFORM_MAX_LENGTH:
  478. *params = prog->vars.getActiveUniformMaxLength();
  479. break;
  480. default:
  481. set_err(GL_INVALID_ENUM);
  482. }
  483. }
  484. GL_API void GL_APIENTRY glGetProgramInfoLog (GLuint program, GLsizei bufsize, GLsizei *length, char *infolog)
  485. {
  486. GET_GL_STATE(ctx);
  487. if(!infolog) //TODO: SPEC AMBIGUITY: behaviour when infolog is null?
  488. {
  489. return;
  490. }
  491. //-------- Check if the program name a valid program name --------
  492. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  493. if( (progIt == ctx->shaderProgNames.end()) )
  494. {
  495. set_err(GL_INVALID_VALUE);
  496. return;
  497. }
  498. if( progIt->type != PROGRAM )
  499. {
  500. set_err(GL_INVALID_OPERATION);
  501. return;
  502. }
  503. gAssert( (progIt->programPtr != 0) && "Prog ptr is 0!\n" );
  504. if(bufsize < 0)
  505. {
  506. set_err(GL_INVALID_VALUE);
  507. return;
  508. }
  509. #if 0 //chanchal
  510. if(bufsize <1)
  511. {
  512. return;
  513. }
  514. #endif
  515. int len = Plat::min(size_t(bufsize-1), strlen(progIt->programPtr->infoLog.c_str()));
  516. Plat::memcpy(infolog, progIt->programPtr->infoLog.c_str(), len);
  517. infolog[len] = '\0';
  518. if(length)
  519. {
  520. *length = len;
  521. }
  522. }
  523. GL_API void GL_APIENTRY glGetShaderiv (GLuint shader, GLenum pname, GLint *params)
  524. {
  525. GET_GL_STATE(ctx);
  526. //fprintf(stderr, "\n\n\n\n#######shader = %d\n############pname = %d, \n###########params = %p\n\n\n\n", shader, pname, params);
  527. if(shader == 0)
  528. {
  529. set_err(GL_INVALID_VALUE);
  530. return;
  531. }
  532. if(!params) //TODO: SPEC AMBIGUITY: behaviour when params is null?
  533. {
  534. return;
  535. }
  536. ShaderProgNameList::const_iterator shaderIt = ctx->shaderProgNames.find(shader);
  537. if(shaderIt == ctx->shaderProgNames.end())
  538. {
  539. set_err(GL_INVALID_VALUE);
  540. return;
  541. }
  542. if(shaderIt->type != SHADER)
  543. {
  544. set_err(GL_INVALID_OPERATION);
  545. return;
  546. }
  547. gAssert((shaderIt->shaderPtr != 0) && "shader ptr in name list is 0!!!");
  548. Shader* sh = shaderIt->shaderPtr;
  549. switch(pname)
  550. {
  551. case GL_SHADER_TYPE:
  552. if(sh->type == VERTEX_SHADER)
  553. {
  554. *params = GL_VERTEX_SHADER;
  555. }
  556. else if(sh->type == FRAGMENT_SHADER)
  557. {
  558. *params = GL_FRAGMENT_SHADER;
  559. }
  560. else
  561. {
  562. gAssert(false && "Shader type is not vertex or fragment!");
  563. }
  564. break;
  565. case GL_DELETE_STATUS:
  566. *params = (sh->deleteStatus) ? GL_TRUE : GL_FALSE;
  567. break;
  568. case GL_COMPILE_STATUS:
  569. *params = (sh->compileStatus) ? GL_TRUE : GL_FALSE;
  570. break;
  571. case GL_INFO_LOG_LENGTH:
  572. *params = sh->getInfoLogLength();
  573. break;
  574. case GL_SHADER_SOURCE_LENGTH:
  575. *params = 0; //No online compiling for now
  576. break;
  577. default:
  578. set_err(GL_INVALID_ENUM);
  579. }
  580. }
  581. int getUniform(GLuint program, GLint location, GLfloat *params)
  582. {
  583. GET_GL_STATE(ctx);
  584. if(!params)
  585. {
  586. return 0;
  587. }
  588. // if(program == 0) //error checking added by chanchal
  589. // {
  590. // set_err(GL_INVALID_VALUE);
  591. // return 0;
  592. // }
  593. if(location == -1)
  594. {
  595. set_err(GL_INVALID_OPERATION); //added by chanchal
  596. return 0;
  597. }
  598. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  599. if( (progIt == ctx->shaderProgNames.end()) )
  600. {
  601. set_err(GL_INVALID_VALUE);
  602. return 0;
  603. }
  604. if( progIt->type != PROGRAM )
  605. {
  606. set_err(GL_INVALID_OPERATION);
  607. return 0;
  608. }
  609. gAssert( (progIt->programPtr != 0) && "Prog ptr is 0!\n" );
  610. Program* prog = progIt->programPtr;
  611. if(!prog->linkStatus)
  612. {
  613. set_err(GL_INVALID_OPERATION);
  614. return 0;
  615. }
  616. if(location >= static_cast<int>(prog->vars.uniformTbl.uniforms.size()))
  617. {
  618. set_err(GL_INVALID_OPERATION);
  619. return 0;
  620. }
  621. //All uniforms are currently stored as floats
  622. GLenum type = prog->vars.uniformTbl.uniforms[location].type;
  623. int numComponents = getNumComponents(type);
  624. int fsLoc = prog->vars.uniformTbl.uniforms[location].fsLocation;
  625. int vsLoc = prog->vars.uniformTbl.uniforms[location].vsLocation;
  626. unsigned int regSet = 0;
  627. float* fMemBlock = 0;
  628. char* iMemBlock = 0;
  629. char* bMemBlock = 0;
  630. int loc = -1;
  631. SamplerMapEntry *samplerMappings =0;
  632. //Need to get either the one in vs constants or fs constants, whichever exists.
  633. //If both exist they will have the same value.
  634. if(vsLoc != -1)
  635. {
  636. loc = vsLoc;
  637. fMemBlock = prog->vars.vsFloatMemBlock;
  638. iMemBlock = prog->vars.vsIntMemBlock;
  639. bMemBlock = prog->vars.vsBoolMemBlock;
  640. regSet = prog->vars.uniformTbl.uniforms[location].vsRegSet;
  641. samplerMappings = prog->progExecutable.vsSamplerMappings;
  642. }
  643. else if(fsLoc != -1)
  644. {
  645. loc = fsLoc;
  646. fMemBlock = prog->vars.fsFloatMemBlock;
  647. iMemBlock = prog->vars.fsIntMemBlock;
  648. bMemBlock = prog->vars.fsBoolMemBlock;
  649. regSet = prog->vars.uniformTbl.uniforms[location].fsRegSet;
  650. samplerMappings = prog->progExecutable.fsSamplerMappings;
  651. }
  652. else
  653. {
  654. gAssert(loc != -1);
  655. }
  656. const float* memLoc = fMemBlock+loc;
  657. if(isVector(type))
  658. {
  659. if(regSet == DCL_REGSET_CONST_FLOAT)
  660. Plat::memcpy(params, memLoc, numComponents*4);
  661. else if(regSet == DCL_REGSET_CONST_INT)
  662. for(int i=0; i< numComponents; i++)
  663. {
  664. params[i] = float(*(iMemBlock+4*loc+i));
  665. }
  666. else if(regSet == DCL_REGSET_CONST_BOOL)
  667. //LOGMSG("//TBD: handle getting of bool\n");
  668. return numComponents;
  669. }
  670. else if(isMatrix(type))
  671. {
  672. if(type == GL_FLOAT_MAT2)
  673. {
  674. Plat::memcpy(params, memLoc, 4*4);
  675. return 4;
  676. }
  677. else if(type == GL_FLOAT_MAT3)
  678. {
  679. Plat::memcpy(params, memLoc, 3*4);
  680. Plat::memcpy(params+3, memLoc+4, 3*4);
  681. Plat::memcpy(params+6, memLoc+8, 3*4);
  682. return 9;
  683. }
  684. else if(type == GL_FLOAT_MAT4)
  685. {
  686. Plat::memcpy(params, memLoc, 16*4);
  687. return 16;
  688. }
  689. else
  690. {
  691. gAssert(false && "isMatrix has a bug!");
  692. }
  693. }
  694. else if(isSampler(type))
  695. {
  696. params[0] = float(samplerMappings[loc].glTexUnit);
  697. return 1;
  698. }
  699. else
  700. {
  701. gAssert(false && " Uniform type is not Vector or Matrix or Sampler!");
  702. }
  703. return 0;
  704. }
  705. GL_API void GL_APIENTRY glGetUniformfv (GLuint program, GLint location, GLfloat *params)
  706. {
  707. //GET_GL_STATE(ctx);
  708. getUniform(program, location, params);
  709. }
  710. GL_API void GL_APIENTRY glGetUniformiv (GLuint program, GLint location, GLint *params)
  711. {
  712. //GET_GL_STATE(ctx);
  713. if(!params)
  714. {
  715. return;
  716. }
  717. GLfloat vals[16];
  718. int count = getUniform(program, location, vals);
  719. for(int i=0; i< count; i++)
  720. {
  721. params[i] = GLint(vals[i]);
  722. }
  723. }
  724. GL_API int GL_APIENTRY glGetUniformLocation (GLuint program, const char *name)
  725. {
  726. GET_GL_STATE(ctx);
  727. // if(program == 0) //error checking added by chanchal
  728. // {
  729. // set_err(GL_INVALID_VALUE);
  730. // return -1;
  731. // }
  732. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  733. if( (progIt == ctx->shaderProgNames.end()) )
  734. {
  735. set_err(GL_INVALID_VALUE);
  736. return -1;
  737. }
  738. if( progIt->type != PROGRAM )
  739. {
  740. set_err(GL_INVALID_OPERATION);
  741. return -1;
  742. }
  743. gAssert( (progIt->programPtr != 0) && "Prog ptr is 0!\n" );
  744. if( progIt->programPtr->linkStatus != true )
  745. {
  746. set_err(GL_INVALID_OPERATION);
  747. return -1;
  748. }
  749. return progIt->programPtr->vars.uniformTbl.getLocation(name);
  750. }
  751. GL_API GLboolean GL_APIENTRY glIsProgram (GLuint program)
  752. {
  753. GET_GL_STATE(ctx);
  754. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  755. if((progIt != ctx->shaderProgNames.end())&&(progIt->type == PROGRAM))
  756. {
  757. return GL_TRUE;
  758. }
  759. return GL_FALSE;
  760. }
  761. GL_API GLboolean GL_APIENTRY glIsShader (GLuint shader)
  762. {
  763. GET_GL_STATE(ctx);
  764. ShaderProgNameList::const_iterator shaderIt = ctx->shaderProgNames.find(shader);
  765. if((shaderIt != ctx->shaderProgNames.end())&&(shaderIt->type == SHADER))
  766. {
  767. return GL_TRUE;
  768. }
  769. return GL_FALSE;
  770. }
  771. // glLinkProgram Tries to link the program
  772. // Notes: link can fail for the following reasons:
  773. // 'CL' means this error should be caught by the compiler and
  774. // low level linker and not by linkProgram as it will not
  775. // the required information
  776. // '+' indicated the following step is implemented
  777. //
  778. // * The number of active attribute variables supported by the implementation has been exceeded.
  779. // * The storage limit for uniform variables has been exceeded.
  780. // * The number of active uniform variables supported by the implementation has been exceeded.
  781. // CL->* The main function is missing for the vertex shader or the fragment shader.
  782. // * A varying variable actually used in the fragment shader is not declared in the same way (or is not declared at all) in the vertex shader.
  783. // CL->* A reference to a function or variable name is unresolved.
  784. // +* One or more of the attached shader objects has not been successfully compiled.
  785. // * Binding a generic attribute matrix caused some rows of the matrix to fall outside the allowed maximum of GL_MAX_VERTEX_ATTRIBS.
  786. // * Not enough contiguous vertex attribute slots could be found to bind attribute matrices.
  787. GL_API void GL_APIENTRY glLinkProgram (GLuint program)
  788. {
  789. GET_GL_STATE(ctx);
  790. /*the error invalid_value is generated if program is not a valid shader object created with glCreateProgram()*/
  791. // if(program == 0) //error checking added by chanchal
  792. // {
  793. // set_err(GL_INVALID_VALUE);
  794. // return;
  795. // }
  796. //-------- Check if the program name is a valid program name --------
  797. ShaderProgNameList::const_iterator progIt = ctx->shaderProgNames.find(program);
  798. if( (progIt == ctx->shaderProgNames.end()) )
  799. {
  800. set_err(GL_INVALID_VALUE);
  801. return;
  802. }
  803. if( progIt->type != PROGRAM )
  804. {
  805. set_err(GL_INVALID_OPERATION);
  806. return;
  807. }
  808. gAssert( (progIt->programPtr != 0) && "Prog ptr is 0!\n" );
  809. Program* prog = progIt->programPtr;
  810. // if the current program is been linked then we make a deep copy of it.
  811. if(prog->id == ctx->current_program)
  812. {
  813. //ctx->current_executable = (Executable*)Plat::malloc(sizeof(struct Executable));
  814. ctx->current_executable = new Executable();
  815. ctx->current_executable->copyFrom(prog->progExecutable);
  816. ctx->cur_exe_own_memory = GL_TRUE;
  817. }
  818. //ALERT! link status is being set to false here!
  819. prog->linkStatus = false;
  820. prog->validationStatus = false;
  821. //-------- Clear the generated parts of program --------
  822. prog->vars.clear();
  823. prog->infoLog.clear();
  824. prog->progExecutable.clear();
  825. //-------- Check that both vs and fs exist and they are compiled --------
  826. if( (prog->vertexShader == 0) || (prog->fragmentShader == 0) )
  827. {
  828. // prog->infoLog.append("LINK FAILED: shader missing\n");
  829. return;
  830. }
  831. if( prog->vertexShader->compileStatus != true )
  832. {
  833. // prog->infoLog.append("LINK FAILED: vertex shader is not compiled\n");
  834. return;
  835. }
  836. if( prog->fragmentShader->compileStatus != true)
  837. {
  838. // prog->infoLog.append("LINK FAILED: fragment shader is not compiled\n");
  839. return;
  840. }
  841. //-------- Back patching linking ---------
  842. //TODO: call the low level linker if needed
  843. //-------- INSERT SYMBOL TABLE ENTRIES --------
  844. bool retVal = false;
  845. ustl::string errString;
  846. //-------- Insert uniform entries while checking link errors (match + limits)---------
  847. //-------- Insert sampler entries while checking link errors (match + limits)-------
  848. //-------- Insert attribute entries while checking link errors (match + limits)---------
  849. //-------- Insert varying entries while checking link errors (match + limits)-------
  850. retVal = prog->vars.insertVars(prog->progExecutable, prog->vertexShader->shaderBinary,
  851. prog->vertexShader->sizeShaderBinary, VERTEX_SHADER, errString);
  852. // if( !retVal) { prog->infoLog.append(errString); prog->vars.clear(); return; }
  853. retVal = prog->vars.insertVars(prog->progExecutable, prog->fragmentShader->shaderBinary,
  854. prog->fragmentShader->sizeShaderBinary, FRAGMENT_SHADER, errString);
  855. // if( !retVal) { prog->infoLog.append(errString); prog->vars.clear(); return; }
  856. //Update varying register map in exe
  857. retVal = prog->vars.updateVaryingMap(prog->progExecutable, errString);
  858. // if( !retVal) { prog->infoLog.append(errString); prog->vars.clear(); prog->progExecutable.clear(); return; }
  859. //Update the attribute map
  860. //retVal = prog->vars.updateAttribMap(prog->progExecutable, errString);
  861. retVal = prog->updateAttribMap(errString);
  862. // if( !retVal) { prog->infoLog.append(errString); prog->vars.clear(); prog->progExecutable.clear(); return; }
  863. //DEBUG:
  864. //prog->vars.printVaryings();
  865. //-------- Generate attrib bindings while checking link errors (match + limits)--------
  866. /*
  867. retVal = prog->vars.insertAttribs(prog->vertexShader->shaderBinary,
  868. prog->vertexShader->sizeShaderBinary, VERTEX_SHADER, errString);
  869. if( !retVal) { prog->infoLog.append(errString); return; }
  870. */
  871. //-------- Make executable --------
  872. //-------- Copy VS instructions --------
  873. prog->progExecutable.vs.binaryCodeSize = ((const ShaderHeader*)(prog->vertexShader->shaderBinary))->instruction_sz*16;
  874. prog->progExecutable.vs.pcStart = prog->vertexShader->pcStart;
  875. prog->progExecutable.vs.binaryCode = (unsigned char*)Plat::malloc(prog->progExecutable.vs.binaryCodeSize);
  876. int shaderInstrOffset = ((const ShaderHeader*)(prog->vertexShader->shaderBinary))->header_sz;
  877. Plat::memcpy(prog->progExecutable.vs.binaryCode, ((unsigned char*)(prog->vertexShader->shaderBinary))+shaderInstrOffset,
  878. prog->progExecutable.vs.binaryCodeSize);
  879. //-------- Copy FS instructions --------
  880. prog->progExecutable.fs.binaryCodeSize = ((const ShaderHeader*)(prog->fragmentShader->shaderBinary))->instruction_sz*16;
  881. prog->progExecutable.fs.pcStart = prog->fragmentShader->pcStart;
  882. prog->progExecutable.fs.binaryCode = (unsigned char*)Plat::malloc(prog->progExecutable.fs.binaryCodeSize);
  883. shaderInstrOffset = ((const ShaderHeader*)(prog->fragmentShader->shaderBinary))->header_sz;
  884. Plat::memcpy(prog->progExecutable.fs.binaryCode, ((unsigned char*)(prog->fragmentShader->shaderBinary))+shaderInstrOffset,
  885. prog->progExecutable.fs.binaryCodeSize);
  886. #ifdef FSO_JITO
  887. retVal = prog->progExecutable.vs.extractJitoData(VERTEX_SHADER);
  888. //LOGMSG("JIT optimization is %s for this VS\n",retVal ? "enabled" : "disabled");
  889. retVal = prog->progExecutable.fs.extractJitoData(FRAGMENT_SHADER);
  890. //LOGMSG("JIT optimization is %s for this FS\n",retVal ? "enabled" : "disabled");
  891. #endif
  892. // Linking successful!
  893. // prog->infoLog.append("LINK SUCCESSFUL\n");
  894. prog->linkStatus = true;
  895. //-------- Update current executable if this is the current program --------
  896. #if 0
  897. if(prog->id == ctx->current_program)
  898. {
  899. ctx->current_executable->copyFrom(prog->progExecutable);
  900. }
  901. #else
  902. if(prog->id == ctx->current_program)
  903. {
  904. ctx->current_executable->clear();
  905. delete ctx->current_executable;
  906. //Plat::safe_free(ctx->current_executable);
  907. ctx->current_executable = &(prog->progExecutable);
  908. ctx->cur_exe_own_memory = GL_FALSE;
  909. }
  910. #endif
  911. }
  912. //TODO: update the design a bit?
  913. bool updateCurrentExecutable()
  914. {
  915. GET_GL_STATE(ctx);
  916. if(ctx->current_program == 0)
  917. {
  918. return false;
  919. }
  920. Program* prog = ctx->current_program_ptr;
  921. gAssert((prog != 0) && "Prog ptr is 0!\n");
  922. ctx->current_executable = &(prog->progExecutable);
  923. //TODO: move to linker?
  924. //FIMG specific stuff
  925. ////LOGMSG("Varying (VS: %d FS: %d)\n", ctx->current_executable.numVSVarying,ctx->current_executable.numFSVarying);
  926. #if 0
  927. if((ctx->current_executable->numVSVarying == 1)&&(ctx->current_executable->numFSVarying==0))
  928. {
  929. //Add a dummy varying
  930. ctx->current_executable->numVSVarying++;
  931. ctx->current_executable->numFSVarying++;
  932. ctx->current_executable->varyingMap[1]=0;
  933. }
  934. #else
  935. if((ctx->current_executable->numVSVarying >= 1)&&(ctx->current_executable->numFSVarying==0)){
  936. //Add a dummy varying
  937. //ctx->current_executable->numVSVarying++;
  938. ctx->current_executable->numFSVarying++;
  939. ctx->current_executable->varyingMap[1]=0;
  940. }
  941. #endif
  942. #ifdef FSO_JITO
  943. ctx->current_executable->vs.jitoptimize(ctx->cgd);
  944. ctx->current_executable->fs.jitoptimize(ctx->cgd);
  945. #endif
  946. return true;
  947. }
  948. template <typename T>
  949. void setValues(T* vsMemBlock, T* fsMemBlock, UniformVar* first, const void* data,int count, int numComponents)
  950. {
  951. if(getNumComponents(first->type) != numComponents)
  952. {
  953. set_err(GL_INVALID_OPERATION);
  954. return;
  955. }
  956. T* vsDst = vsMemBlock;
  957. T* fsDst = fsMemBlock;
  958. const unsigned char* src = static_cast<const unsigned char*>(data);
  959. UniformVar* cur = first;
  960. int size = numComponents*sizeof(T);
  961. for( int i=0; i< count; i++)
  962. {
  963. if(!cur)
  964. {
  965. return;
  966. }
  967. if(cur->vsLocation >=0)
  968. Plat::memcpy(vsDst + cur->vsLocation, src, size);
  969. if(cur->fsLocation >=0)
  970. Plat::memcpy(fsDst + cur->fsLocation, src, size);
  971. src += size;
  972. cur = cur->nextElement;
  973. }
  974. }
  975. template <typename D, typename S, ShaderType sType>
  976. void setValues(D* dst, UniformVar* first, const void* data, int count, int numComponents)
  977. {
  978. const S* src = static_cast<const S*>(data);
  979. UniformVar* cur = first;
  980. for (int i=0; i <count; i++)
  981. {
  982. if(!cur)
  983. {
  984. return;
  985. }
  986. if(sType == VERTEX_SHADER)
  987. {
  988. if(cur->vsLocation >=0)
  989. {
  990. for(int k=0; k<numComponents; k++)
  991. {
  992. *(dst+cur->vsLocation+k) = static_cast<D>( *(src + k));
  993. }
  994. }
  995. }
  996. else if(sType == FRAGMENT_SHADER)
  997. {
  998. if(cur->fsLocation >=0)
  999. {
  1000. for(int k=0; k<numComponents; k++)
  1001. {
  1002. *(dst+cur->fsLocation+k) = static_cast<D>( *(src + k));
  1003. }
  1004. }
  1005. }
  1006. src += numComponents;
  1007. cur = cur->nextElement;
  1008. }
  1009. }
  1010. template <typename D, typename S, ShaderType sType>
  1011. void setValuesBool(D* dst, UniformVar* first, const void* data, int count, int numComponents)
  1012. {
  1013. const S* src = static_cast<const S*>(data);
  1014. UniformVar* cur = first;
  1015. for (int i=0; i <count; i++)
  1016. {
  1017. if(!cur)
  1018. {
  1019. return;
  1020. }
  1021. if(sType == VERTEX_SHADER)
  1022. {
  1023. if(cur->vsLocation >=0)
  1024. {
  1025. for(int k=0; k<numComponents; k++)
  1026. {
  1027. *(dst+cur->vsLocation+k) = static_cast<D>( (*(src + k)) ? 1 : 0 );
  1028. }
  1029. }
  1030. }
  1031. else if(sType == FRAGMENT_SHADER)
  1032. {
  1033. if(cur->fsLocation >=0)
  1034. {
  1035. for(int k=0; k<numComponents; k++)
  1036. {
  1037. *(dst+cur->fsLocation+k) = static_cast<D>( (*(src + k)) ? 1 : 0 );
  1038. }
  1039. }
  1040. }
  1041. src += numComponents;
  1042. cur = cur->nextElement;
  1043. }
  1044. }
  1045. template <typename S>
  1046. void setBoolValues(unsigned int* vsMemBlock, unsigned int* fsMemBlock, UniformVar* first, const S* data, int count, int numComponents)
  1047. {
  1048. if(getNumComponents(first->type) != numComponents)
  1049. {
  1050. set_err(GL_INVALID_OPERATION);
  1051. return;
  1052. }
  1053. const S* src = static_cast<const S*>(data);
  1054. int size = numComponents*sizeof(S);
  1055. UniformVar* cur = first;
  1056. for( int i=0; i< count; i++)
  1057. {
  1058. if(!cur)
  1059. {
  1060. return;
  1061. }
  1062. if(cur->vsLocation >=0)
  1063. {
  1064. for(int k=0; k<numComponents; k++)
  1065. {
  1066. unsigned int temp = *vsMemBlock;
  1067. unsigned int mask = 1<<(cur->vsLocation+k);
  1068. *vsMemBlock = temp & (~mask) | ( (src[0] == 0)? 0 : mask);
  1069. }
  1070. }
  1071. if(cur->fsLocation >=0)
  1072. {
  1073. for(int k=0; k<numComponents; k++)
  1074. {
  1075. unsigned int temp = *fsMemBlock;
  1076. unsigned int mask = 1<<(cur->fsLocation+k);
  1077. *fsMemBlock = temp & (~mask) | ( (src[0] == 0)? 0 : mask);
  1078. }
  1079. }
  1080. src += size;
  1081. cur = cur->nextElement;
  1082. }
  1083. }
  1084. //uniform mats can only be floats
  1085. void setUniformMat(GLint location, GLsizei count, const void* data, GLenum type)
  1086. {
  1087. GET_GL_STATE(ctx);
  1088. if(ctx->current_program == 0)
  1089. {
  1090. set_err(GL_INVALID_OPERATION);
  1091. return;
  1092. }
  1093. if(location == -1)
  1094. {
  1095. return; //Silently ignore
  1096. }
  1097. if(count < 0)
  1098. {
  1099. set_err(GL_INVALID_VALUE);
  1100. return;
  1101. }
  1102. Program* prog = ctx->current_program_ptr;
  1103. gAssert( (prog != 0) && "ProgramPtr is 0!!");
  1104. if( location >= static_cast<int>(prog->vars.uniformTbl.uniforms.size()))
  1105. {
  1106. set_err(GL_INVALID_OPERATION);
  1107. return;
  1108. }
  1109. if((count > 1) && (!(prog->vars.uniformTbl.uniforms[location].isArray)))
  1110. {
  1111. set_err(GL_INVALID_OPERATION);
  1112. return;
  1113. }
  1114. const GLenum varType = prog->vars.uniformTbl.uniforms[location].type;
  1115. if( (varType != GL_FLOAT_MAT2) && (varType != GL_FLOAT_MAT3) && (varType != GL_FLOAT_MAT4))
  1116. {
  1117. set_err(GL_INVALID_OPERATION);
  1118. return;
  1119. }
  1120. if( varType != type)
  1121. {
  1122. set_err(GL_INVALID_OPERATION);
  1123. return;
  1124. }
  1125. const float* src = static_cast<const float*>(data);
  1126. UniformVar* cur = &(prog->vars.uniformTbl.uniforms[location]);
  1127. const int numFloats = getNumComponents(type);
  1128. const int compSize = sizeof(GLfloat);
  1129. float * vsMemBlock = prog->vars.vsFloatMemBlock;
  1130. float * fsMemBlock = prog->vars.fsFloatMemBlock;
  1131. for(int i=0; i< count ; i++)
  1132. {
  1133. if(!cur)
  1134. return;
  1135. if(cur->vsLocation >= 0)
  1136. {
  1137. switch(varType)
  1138. {
  1139. case GL_FLOAT_MAT2:
  1140. Plat::memcpy(vsMemBlock + cur->vsLocation, src, compSize*numFloats);
  1141. break;
  1142. case GL_FLOAT_MAT3:
  1143. Plat::memcpy(vsMemBlock + cur->vsLocation, src, compSize*3);
  1144. Plat::memcpy(vsMemBlock + cur->vsLocation+4, src+3, compSize*3);
  1145. Plat::memcpy(vsMemBlock + cur->vsLocation+8, src+6, compSize*3);
  1146. break;
  1147. case GL_FLOAT_MAT4:
  1148. //TODO: can be done in a single memcpy
  1149. Plat::memcpy(vsMemBlock + cur->vsLocation, src, compSize*4);
  1150. Plat::memcpy(vsMemBlock + cur->vsLocation+4, src+4, compSize*4);
  1151. Plat::memcpy(vsMemBlock + cur->vsLocation+8, src+8, compSize*4);
  1152. Plat::memcpy(vsMemBlock + cur->vsLocation+12, src+12, compSize*4);
  1153. break;
  1154. }
  1155. }
  1156. if(cur->fsLocation >= 0)
  1157. {
  1158. switch(varType)
  1159. {
  1160. case GL_FLOAT_MAT2:
  1161. Plat::memcpy(fsMemBlock + cur->fsLocation, src, compSize*numFloats);
  1162. break;
  1163. case GL_FLOAT_MAT3:
  1164. Plat::memcpy(fsMemBlock + cur->fsLocation, src, compSize*3);
  1165. Plat::memcpy(fsMemBlock + cur->fsLocation+4, src+3, compSize*3);
  1166. Plat::memcpy(fsMemBlock + cur->fsLocation+8, src+6, compSize*3);
  1167. break;
  1168. case GL_FLOAT_MAT4:
  1169. //TODO: can be done in a single memcpy
  1170. Plat::memcpy(fsMemBlock + cur->fsLocation, src, compSize*4);
  1171. Plat::memcpy(fsMemBlock + cur->fsLocation+4, src+4, compSize*4);
  1172. Plat::memcpy(fsMemBlock + cur->fsLocation+8, src+8, compSize*4);
  1173. Plat::memcpy(fsMemBlock + cur->fsLocation+12, src+12, compSize*4);
  1174. break;
  1175. }
  1176. }
  1177. cur = cur->nextElement;
  1178. src += numFloats;
  1179. }
  1180. }
  1181. //NOTE: does not support setting of bools yet.
  1182. void setUniformVec(GLint location, GLsizei count, const void* data, GLenum type)
  1183. {
  1184. GET_GL_STATE(ctx);
  1185. if(ctx->current_program == 0)
  1186. {
  1187. set_err(GL_INVALID_OPERATION);
  1188. return;
  1189. }
  1190. if(location == -1)
  1191. {
  1192. return; //Silently ignore
  1193. }
  1194. if(count < 0)
  1195. {
  1196. set_err(GL_INVALID_VALUE);
  1197. return;
  1198. }
  1199. Program* prog = ctx->current_program_ptr;
  1200. gAssert( (prog != 0) && "ProgramPtr is 0!!");
  1201. if( location >= static_cast<int>(prog->vars.uniformTbl.uniforms.size()))
  1202. {
  1203. set_err(GL_INVALID_OPERATION);
  1204. return;
  1205. }
  1206. if((count > 1) && (!(prog->vars.uniformTbl.uniforms[location].isArray)))
  1207. {
  1208. set_err(GL_INVALID_OPERATION);
  1209. return;
  1210. }
  1211. if(data == 0)
  1212. {
  1213. return;
  1214. }
  1215. UniformVar& uniform = prog->vars.uniformTbl.uniforms[location];
  1216. const GLenum varType = uniform.type;
  1217. const GLenum baseType = getBaseType(type);
  1218. const GLenum baseVarType = getBaseType(varType);
  1219. //NEW:
  1220. if(!isSampler(varType))
  1221. {
  1222. if(getNumComponents(varType) != getNumComponents(type))
  1223. {
  1224. set_err(GL_INVALID_OPERATION);
  1225. return;
  1226. }
  1227. }
  1228. //type checking:
  1229. if( baseType == GL_FLOAT)
  1230. {
  1231. if( (varType == GL_FLOAT) || (varType == GL_FLOAT_VEC2) || (varType == GL_FLOAT_VEC3) || (varType == GL_FLOAT_VEC4))
  1232. {
  1233. //Check the storage regset and set the appropriate one
  1234. //setValues<float>(prog->vars.vsFloatMemBlock, prog->vars.fsFloatMemBlock,
  1235. // &(uniform), data,count, getNumComponents(type));
  1236. if(uniform.vsLocation>=0) { gAssert(uniform.vsRegSet == DCL_REGSET_CONST_FLOAT); }
  1237. if(uniform.fsLocation>=0) { gAssert(uniform.fsRegSet == DCL_REGSET_CONST_FLOAT); }
  1238. setValues<float,float,VERTEX_SHADER>(prog->vars.vsFloatMemBlock,
  1239. &(uniform),data,count, getNumComponents(type));
  1240. setValues<float,float,FRAGMENT_SHADER>(prog->vars.fsFloatMemBlock,
  1241. &(uniform),data,count, getNumComponents(type));
  1242. return;
  1243. }
  1244. else if( baseVarType == GL_BOOL)
  1245. {
  1246. //Check the storage regset and set the appropriate one
  1247. if(uniform.vsLocation>=0) { gAssert(uniform.vsRegSet == DCL_REGSET_CONST_FLOAT); }
  1248. if(uniform.fsLocation>=0) { gAssert(uniform.fsRegSet == DCL_REGSET_CONST_FLOAT); }
  1249. setValuesBool<float,float,VERTEX_SHADER>(prog->vars.vsFloatMemBlock,
  1250. &(uniform),data,count, getNumComponents(type));
  1251. setValuesBool<float,float,FRAGMENT_SHADER>(prog->vars.fsFloatMemBlock,
  1252. &(uniform),data,count, getNumComponents(type));
  1253. }
  1254. else
  1255. {
  1256. set_err(GL_INVALID_OPERATION);
  1257. return;
  1258. }
  1259. }
  1260. else if( baseType == GL_INT)
  1261. {
  1262. if( baseVarType == GL_INT)
  1263. {
  1264. //Check the storage regset and set the appropriate one
  1265. if(uniform.vsLocation>=0) { gAssert(uniform.vsRegSet == DCL_REGSET_CONST_FLOAT); }
  1266. if(uniform.fsLocation>=0) { gAssert(uniform.fsRegSet == DCL_REGSET_CONST_FLOAT); }
  1267. setValues<float,int,VERTEX_SHADER>(prog->vars.vsFloatMemBlock,
  1268. &(uniform),data,count, getNumComponents(type));
  1269. setValues<float,int,FRAGMENT_SHADER>(prog->vars.fsFloatMemBlock,
  1270. &(uniform),data,count, getNumComponents(type));
  1271. }
  1272. else if( baseVarType == GL_BOOL)
  1273. {
  1274. //Check the storage regset and set the appropriate one
  1275. if(uniform.vsLocation>=0) { gAssert(uniform.vsRegSet == DCL_REGSET_CONST_FLOAT); }
  1276. if(uniform.fsLocation>=0) { gAssert(uniform.fsRegSet == DCL_REGSET_CONST_FLOAT); }
  1277. setValuesBool<float,int,VERTEX_SHADER>(prog->vars.vsFloatMemBlock,
  1278. &(uniform),data,count, getNumComponents(type));
  1279. setValuesBool<float,int,FRAGMENT_SHADER>(prog->vars.fsFloatMemBlock,
  1280. &(uniform),data,count, getNumComponents(type));
  1281. }
  1282. else if( isSampler(varType))
  1283. {
  1284. if(getNumComponents(type) != 1)
  1285. {
  1286. set_err(GL_INVALID_OPERATION);
  1287. return;
  1288. }
  1289. const int* src = static_cast<const int*>(data);
  1290. UniformVar* cur = &(uniform);
  1291. for(int i=0; i< count ; i++)
  1292. {
  1293. if(!cur)
  1294. return;
  1295. if(cur->vsLocation >= 0)
  1296. {
  1297. if(prog->progExecutable.vsSamplerMappings[cur->vsLocation].isUsed != true)
  1298. {
  1299. gAssert(false&&"sampler entry does not exist in exe but is used from setUniformVec");
  1300. return;
  1301. }
  1302. prog->progExecutable.vsSamplerMappings[cur->vsLocation].glTexUnit = *src;
  1303. }
  1304. if(cur->fsLocation >= 0)
  1305. {
  1306. if(prog->progExecutable.fsSamplerMappings[cur->fsLocation].isUsed != true)
  1307. {
  1308. gAssert(false&&"sampler entry does not exist in exe but is used from setUniformVec");
  1309. return;
  1310. }
  1311. prog->progExecutable.fsSamplerMappings[cur->fsLocation].glTexUnit = *src;
  1312. }
  1313. cur = cur->nextElement;
  1314. src += 1;
  1315. }
  1316. }
  1317. else
  1318. {
  1319. set_err(GL_INVALID_OPERATION);
  1320. return;
  1321. }
  1322. }
  1323. }
  1324. GL_API void GL_APIENTRY glUniform1i (GLint location, GLint x)
  1325. {
  1326. if(location == -1) { return; }
  1327. GET_GL_STATE(ctx);
  1328. ctx->tempInt4[0] = x;
  1329. setUniformVec(location, 1, ctx->tempInt4, GL_INT);
  1330. }
  1331. GL_API void GL_APIENTRY glUniform2i (GLint location, GLint x, GLint y)
  1332. {
  1333. if(location == -1) { return; }
  1334. GET_GL_STATE(ctx);
  1335. ctx->tempInt4[0] = x;
  1336. ctx->tempInt4[1] = y;
  1337. setUniformVec(location, 1, ctx->tempInt4, GL_INT_VEC2);
  1338. }
  1339. GL_API void GL_APIENTRY glUniform3i (GLint location, GLint x, GLint y, GLint z)
  1340. {
  1341. if(location == -1) { return; }
  1342. GET_GL_STATE(ctx);
  1343. ctx->tempInt4[0] = x;
  1344. ctx->tempInt4[1] = y;
  1345. ctx->tempInt4[2] = z;
  1346. setUniformVec(location, 1, ctx->tempInt4, GL_INT_VEC3);
  1347. }
  1348. GL_API void GL_APIENTRY glUniform4i (GLint location, GLint x, GLint y, GLint z, GLint w)
  1349. {
  1350. if(location == -1) { return; }
  1351. GET_GL_STATE(ctx);
  1352. ctx->tempInt4[0] = x;
  1353. ctx->tempInt4[1] = y;
  1354. ctx->tempInt4[2] = z;
  1355. ctx->tempInt4[3] = w;
  1356. setUniformVec(location, 1, ctx->tempInt4, GL_INT_VEC4);
  1357. }
  1358. GL_API void GL_APIENTRY glUniform1f (GLint location, GLfloat x)
  1359. {
  1360. if(location == -1) { return; }
  1361. GET_GL_STATE(ctx);
  1362. ctx->temp

Large files files are truncated, but you can click here to view the full file