renderer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #pragma once
  2. #include "rendering/shader.h"
  3. #include <vector>
  4. #include "rendering/camera.h"
  5. #include "rendering/skyBoxRenderer.h"
  6. #include <gl2d/gl2d.h>
  7. #include "blocks.h"
  8. #include <unordered_map>
  9. #include <rendering/model.h>
  10. struct BlocksLoader;
  11. struct ChunkSystem;
  12. struct ProgramData;
  13. struct SunShadow;
  14. struct ClientEntityManager;
  15. struct ModelsManager;
  16. struct BoneTransform;
  17. struct PlayerConnectionData;
  18. using uniform = GLint;
  19. struct QueryObject
  20. {
  21. GLuint id = 0;
  22. void create();
  23. void begin();
  24. void end();
  25. bool hasResult();
  26. int getQueryResult();
  27. void clear();
  28. };
  29. struct AdaptiveExposure
  30. {
  31. float currentExposure = 1.6;
  32. float minExposure = 1.0;
  33. float maxExposure = 2.0;
  34. float moveSpeed = 0;
  35. float bonusAmbient = 0;
  36. float currentLuminosity = 0.5; //0 - 1
  37. void update(float deltaTime, float newLuminosity);
  38. float getLuminosityOrDefaultValueIfDisabeled();
  39. };
  40. struct Renderer
  41. {
  42. //todo reset these
  43. int sunFlareQueryPos = 0;
  44. QueryObject sunFlareQueries[3] = {};
  45. float averageLuminosity = 0.5;
  46. struct BlockGeometryIndex
  47. {
  48. int startIndex = 0;
  49. int componentCount = 0;
  50. };
  51. BlockGeometryIndex blockGeometry[ModelsManager::BLOCK_MODELS_COUNT];
  52. BlockGeometryIndex blockGeometryForEntities[ModelsManager::BLOCK_MODELS_COUNT];
  53. void recreateBlockGeometryData(ModelsManager &modelsManager);
  54. struct FBO
  55. {
  56. void create(GLint addColor, bool addDepth, GLint addSecondaryRenderTarget = 0,
  57. GLint addThirdRenderTarget = 0, GLint addFourthRenderTarget = 0,
  58. GLint addFifthRenderTarget = 0);
  59. void updateSize(int x, int y);
  60. glm::ivec2 size = {};
  61. GLuint fbo = 0;
  62. GLuint fboOnlyFirstTarget = 0;
  63. GLuint fboOnlySecondTarget = 0;
  64. GLuint fboOnlyThirdTarget = 0;
  65. GLuint fboOnlyFourthTarget = 0;
  66. GLuint fboOnlyFifthTarget = 0;
  67. GLuint color = 0;
  68. GLuint secondaryColor = 0;
  69. GLuint thirdColor = 0;
  70. GLuint fourthColor = 0;
  71. GLuint fifthColor = 0;
  72. GLuint depth = 0;
  73. GLint colorFormat = 0;
  74. GLint secondaryColorFormat = 0;
  75. GLint thirdColorFormat = 0;
  76. GLint fourthColorFormat = 0;
  77. GLint fifthColorFormat = 0;
  78. void copyDepthFromMainFBO(int w, int h);
  79. void copyColorFromMainFBO(int w, int h);
  80. void copyDepthToMainFbo(int w, int h);
  81. void copyDepthAndColorToMainFbo(int w, int h);
  82. void copyDepthFromOtherFBO(GLuint other, int w, int h);
  83. void copyColorFromOtherFBO(GLuint other, int w, int h);
  84. void copyDepthAndColorFromOtherFBO(GLuint other, int w, int h);
  85. void writeAllToOtherFbo(GLuint other, int w, int h);
  86. void clearFBO();
  87. };
  88. struct DefaultShader
  89. {
  90. Shader shader;
  91. uniform u_viewProjection = -1;
  92. uniform u_typesCount = -1;
  93. uniform u_positionInt = -1;
  94. uniform u_positionFloat = -1;
  95. uniform u_texture = -1;
  96. uniform u_time = -1;
  97. uniform u_showLightLevels = -1;
  98. uniform u_skyLightIntensity = -1;
  99. uniform u_lightsCount = -1;
  100. uniform u_pointPosF = -1;
  101. uniform u_pointPosI = -1;
  102. uniform u_sunDirection = -1;
  103. GLuint u_vertexData = GL_INVALID_INDEX;
  104. GLuint u_vertexUV = GL_INVALID_INDEX;
  105. GLuint u_textureSamplerers = GL_INVALID_INDEX;
  106. GLuint u_normalsData = GL_INVALID_INDEX;
  107. GLuint u_lights = GL_INVALID_INDEX;
  108. uniform u_timeGrass = -1;
  109. uniform u_writeScreenSpacePositions = -1;
  110. uniform u_lastFrameColor = -1;
  111. uniform u_lastFramePositionViewSpace = -1;
  112. uniform u_cameraProjection = -1;
  113. uniform u_inverseView = -1;
  114. uniform u_view = -1;
  115. uniform u_metallic = -1;
  116. uniform u_roughness = -1;
  117. uniform u_underWater = -1;
  118. uniform u_sunLightColor = -1;
  119. uniform u_ambientColor = -1;
  120. uniform u_waterColor = -1;
  121. uniform u_depthPeelwaterPass = -1;
  122. uniform u_hasPeelInformation = -1;
  123. uniform u_depthTexture = -1;
  124. uniform u_PeelTexture = -1;
  125. uniform u_dudv = -1;
  126. uniform u_dudvNormal = -1;
  127. uniform u_waterMove = -1;
  128. uniform u_near = -1;
  129. uniform u_far = -1;
  130. uniform u_caustics = -1;
  131. uniform u_inverseProjMat = -1;
  132. uniform u_lightSpaceMatrix = -1;
  133. uniform u_lightPos = -1;
  134. uniform u_sunShadowTexture = -1;
  135. uniform u_brdf = -1;
  136. uniform u_skyTexture = -1;
  137. uniform u_ao = -1;
  138. uniform u_inverseViewProjMat = -1;
  139. GLuint u_shadingSettings = 0;
  140. GLuint shadingSettingsBuffer = 0;
  141. uniform u_lastViewProj = -1;
  142. uniform u_baseAmbientExtra = -1;
  143. uniform u_cascadedShadowsMaps;
  144. uniform u_cascadedShadowMatrix;
  145. uniform u_cascadedShadowPosition;
  146. struct ShadingSettings
  147. {
  148. glm::vec3 waterColor = (glm::vec3(6, 42, 52) / 255.f); // (glm::vec3(6, 27, 43) / 255.f);
  149. int tonemapper = 0;
  150. glm::vec3 underWaterColor = (glm::vec3(0, 17, 25) / 255.f);
  151. float fogDistance = 10 * 16 / 2; //this is controlled by chunk size
  152. float exposure = 1.6;
  153. float underwaterDarkenStrength = 0.94;
  154. float underwaterDarkenDistance = 29;
  155. float fogGradientUnderWater = 1.9;
  156. int shaders = true;
  157. float fogCloseGradient = 0;
  158. }shadingSettings;
  159. }defaultShader;
  160. struct FilterBloomDataShader
  161. {
  162. Shader shader;
  163. uniform u_exposure = -1;
  164. uniform u_tresshold = -1;
  165. uniform u_multiplier = -1;
  166. }filterBloomDataShader;
  167. struct SSRShader
  168. {
  169. Shader shader;
  170. uniform u_color = -1;
  171. uniform u_positionViewSpace = -1;
  172. uniform u_normals = -1;
  173. uniform u_cameraProjection = -1;
  174. uniform u_inverseView = -1;
  175. uniform u_view = -1;
  176. uniform u_inverseCameraViewProjection = -1;
  177. uniform u_materials = -1;
  178. }ssrShader;
  179. struct FilterDownShader
  180. {
  181. Shader shader;
  182. uniform u_texture = -1;
  183. uniform u_mip = -1;
  184. }filterDownShader;
  185. //http://blog.simonrodriguez.fr/articles/2016/07/implementing_fxaa.html
  186. struct FXAAData
  187. {
  188. float edgeMinTreshold = 0.028;
  189. float edgeDarkTreshold = 0.125;
  190. int ITERATIONS = 12;
  191. float quaityMultiplier = 0.8;
  192. float SUBPIXEL_QUALITY = 0.95;
  193. }fxaaData;
  194. struct FXAAShader
  195. {
  196. Shader shader;
  197. uniform u_texture = -1;
  198. }fxaaShader;
  199. struct AddMipsShader
  200. {
  201. Shader shader;
  202. uniform u_texture;
  203. uniform u_mip;
  204. }addMipsShader;
  205. struct GausianBLurShader
  206. {
  207. Shader shader;
  208. uniform u_horizontal;
  209. uniform u_mip;
  210. uniform u_texel;
  211. }gausianBLurShader;
  212. struct ApplyBloomDataShader
  213. {
  214. Shader shader;
  215. uniform u_waterDropsPower;
  216. uniform u_hitIntensity;
  217. uniform u_SSGR;
  218. }applyBloomDataShader;
  219. struct HBAOShader
  220. {
  221. Shader shader;
  222. uniform u_gPosition;
  223. uniform u_gNormal;
  224. uniform u_view;
  225. uniform u_projection;
  226. }hbaoShader;
  227. struct ApplyHBAOShader
  228. {
  229. Shader shader;
  230. uniform u_hbao;
  231. uniform u_currentViewSpace;
  232. uniform u_viewDistance;
  233. }applyHBAOShader;
  234. struct MaskDepth
  235. {
  236. Shader shader;
  237. uniform u_depthTexture;
  238. }maskDepthShader;
  239. struct RadialBlur
  240. {
  241. Shader shader;
  242. uniform u_texture;
  243. uniform u_center;
  244. }radialBlurShader;
  245. struct WarpShader
  246. {
  247. Shader shader;
  248. uniform u_color = 0;
  249. uniform u_time = 0;
  250. uniform u_currentViewSpace = 0;
  251. uniform u_underwaterColor = 0;
  252. }warpShader;
  253. struct ApplyToneMapper
  254. {
  255. Shader shader;
  256. uniform u_color = 0;
  257. uniform u_tonemapper = 0;
  258. uniform u_exposure = 0;
  259. uniform u_saturation = 0;
  260. uniform u_vibrance = 0;
  261. uniform u_gamma = 0;
  262. uniform u_shadowBoost = 0;
  263. uniform u_highlightBoost = 0;
  264. uniform u_vignette = 0;
  265. uniform u_lift = 0;
  266. uniform u_gain = 0;
  267. }applyToneMapper;
  268. //It is the same as the z pre pass shader
  269. struct ZpassShader
  270. {
  271. Shader shader;
  272. GLuint u_viewProjection;
  273. GLuint u_positionInt;
  274. GLuint u_positionFloat;
  275. GLuint u_vertexUV;
  276. GLuint u_vertexData = GL_INVALID_INDEX;
  277. GLuint u_textureSamplerers;
  278. GLuint u_renderOnlyWater;
  279. GLuint u_timeGrass;
  280. }zpassShader;
  281. struct EntityRenderer
  282. {
  283. struct
  284. {
  285. Shader shader;
  286. uniform u_entityPositionInt;
  287. uniform u_entityPositionFloat;
  288. uniform u_viewProjection;
  289. uniform u_cameraPositionInt;
  290. uniform u_cameraPositionFloat;
  291. uniform u_modelMatrix;
  292. uniform u_texture;
  293. uniform u_view;
  294. uniform u_lightValue;
  295. GLuint vaoCube = 0;
  296. GLuint vertexBufferCube = 0;
  297. GLuint indexBufferCube = 0;
  298. }blockEntityshader;
  299. struct
  300. {
  301. Shader shader;
  302. uniform u_entityPositionInt;
  303. uniform u_entityPositionFloat;
  304. uniform u_viewProjection;
  305. uniform u_cameraPositionInt;
  306. uniform u_cameraPositionFloat;
  307. uniform u_modelMatrix;
  308. uniform u_texture;
  309. uniform u_view;
  310. uniform u_lightValue;
  311. }itemEntityShader;
  312. struct ItemEntityRenderData
  313. {
  314. glm::dvec3 position;
  315. };
  316. //todo remove
  317. std::vector<ItemEntityRenderData> itemEntitiesToRender;
  318. struct BasicEntityShader
  319. {
  320. Shader shader;
  321. uniform u_viewProjection;
  322. uniform u_modelMatrix;
  323. uniform u_cameraPositionInt;
  324. uniform u_cameraPositionFloat;
  325. uniform u_view;
  326. uniform u_bonesPerModel;
  327. uniform u_exposure;
  328. GLuint u_skinningMatrix = GL_INVALID_INDEX;
  329. GLuint u_entityTextureSamplerers = GL_INVALID_INDEX;
  330. GLuint u_perEntityData = GL_INVALID_INDEX;
  331. }basicEntityShader;
  332. }entityRenderer;
  333. //for block cracks, it is the same as the z pre pass shader
  334. struct DecalShader
  335. {
  336. Shader shader;
  337. GLuint u_viewProjection;
  338. GLuint u_positionInt;
  339. GLuint u_positionFloat;
  340. GLuint u_vertexUV;
  341. GLuint u_vertexData = GL_INVALID_INDEX;
  342. GLuint u_textureSamplerers;
  343. GLuint u_renderOnlyWater;
  344. GLuint u_zBias;
  345. GLuint u_timeGrass;
  346. GLuint u_crackPosition;
  347. GLuint vao = 0;
  348. GLuint geometry = 0;
  349. GLuint index = 0;
  350. }decalShader;
  351. struct RenderUIBlocksShader
  352. {
  353. Shader shader;
  354. GLuint u_texture = GL_INVALID_INDEX;
  355. GLuint u_viewProjection = GL_INVALID_INDEX;
  356. GLuint u_useOneTexture = GL_INVALID_INDEX;
  357. }renderUIBlocksShader;
  358. float metallic = 0;
  359. float roughness = 0.5;
  360. bool unifiedGeometry = 0; //big gpu buffer
  361. bool sortChunks = 1;
  362. bool zprepass = 1;
  363. bool renderTransparent = 1;
  364. bool frustumCulling = 1;
  365. bool ssao = 1;
  366. FBO fboHBAO;
  367. FBO fboMain;
  368. FBO fboSunForGodRays;
  369. FBO fboSunForGodRaysSecond;
  370. FBO fboSkyBox;
  371. FBO fboCoppy;
  372. FBO fboLastFrame;
  373. FBO fboLastFramePositions;
  374. //FBO filteredBloomColor;
  375. GLuint filterFbo;
  376. GLuint blurFbo[2];
  377. GLuint bluredColorBuffer[2];
  378. glm::ivec2 filterBloomSize = {};
  379. int currentMips = 0;
  380. GLuint automatixExposureReadBUffer = 0;
  381. GLuint drawCommandsOpaqueBuffer;
  382. void recreateBlocksTexturesBuffer(BlocksLoader &blocksLoader);
  383. void renderAllBlocksUiTextures(BlocksLoader &blocksLoader, ModelsManager &modelsManager);
  384. void create(ModelsManager &modelsManager);
  385. void reloadShaders();
  386. //void render(std::vector<int> &data, Camera &c, gl2d::Texture &texture);
  387. void renderFromBakedData(SunShadow &sunShadow, ChunkSystem &chunkSystem, Camera &c,
  388. ProgramData &programData, BlocksLoader &blocksLoader,
  389. ClientEntityManager &entityManager, ModelsManager &modelsManager,
  390. AdaptiveExposure &adaptiveExposure,
  391. bool showLightLevels, glm::dvec3 pointPos,
  392. bool underWater, int screenX, int screenY, float deltaTime, float dayTime,
  393. GLuint64 currentSkinBindlessTexture, bool &playerClicked, float playerRunning,
  394. BoneTransform &playerHand, int currentHeldItemIndex, float waterDropsStrength,
  395. bool showHand, std::unordered_map<std::uint64_t, PlayerConnectionData> &playersConnectionData);
  396. void renderDecal(glm::ivec3 position, Camera &c, Block b, ProgramData &programData,
  397. float crack);
  398. void renderEntities(float deltaTime,
  399. Camera &c,
  400. ModelsManager &modelsManager,
  401. BlocksLoader &blocksLoader, ClientEntityManager &entityManager,
  402. glm::mat4 &vp, glm::mat4 &projection, glm::mat4 &viewMatrix, glm::vec3 posFloat, glm::ivec3 posInt,
  403. float exposure, ChunkSystem &chunkSystem, int skyLightIntensity,
  404. GLuint64 currentSkinBindlessTexture,
  405. bool &playerClicked, float playerRunning, BoneTransform &playerHand,
  406. int currentHeldItemIndex, bool showHand, std::unordered_map<std::uint64_t, PlayerConnectionData> &playersConnectionData
  407. );
  408. //todo implement this to optimize rendering
  409. void renderPlayersHand(float deltaTime, ModelsManager &modelsManager,
  410. BlocksLoader &blocksLoader, ClientEntityManager &entityManager,
  411. glm::mat4 &vp, glm::mat4 &viewMatrix, glm::vec3 posFloat, glm::ivec3 posInt);
  412. void renderShadow(SunShadow &sunShadow,
  413. ChunkSystem &chunkSystem, Camera &c, ProgramData &programData, glm::vec3 sunPos);
  414. float waterTimer = 0;
  415. GLuint lightBuffer = 0;
  416. size_t lightsBufferCount = 0;
  417. glm::vec3 sunPos = glm::normalize(glm::vec3(-1, 0.84, -1));//todo change
  418. GLuint vao = 0;
  419. GLuint vertexBuffer = 0;
  420. GLuint vertexDataBuffer = 0;
  421. GLuint vertexUVBuffer = 0;
  422. GLuint textureSamplerersBuffer = 0;
  423. GLuint vaoQuad = 0;
  424. GLuint vertexBufferQuad = 0;
  425. GLuint skinningMatrixSSBO = 0;
  426. GLuint perEntityDataSSBO = 0;
  427. };
  428. struct GyzmosRenderer
  429. {
  430. struct CubeData
  431. {
  432. int x = 0, y = 0, z = 0, unused = 0;
  433. float posFloatX = 0, posFloatY = 0, posFloatZ = 0, posFloat2 = 0;
  434. float sizeX = 0, sizeY = 0, sizeZ = 0, unused3 = 0;
  435. };
  436. struct LinesData
  437. {
  438. glm::vec3 a = {};
  439. glm::vec3 b = {};
  440. };
  441. Shader gyzmosLineShader;
  442. GLint u_gyzmosLineShaderViewProjection = -1;
  443. void create();
  444. Shader gyzmosCubeShader;
  445. GLint u_viewProjection = -1;
  446. GLint u_positionInt = -1;
  447. GLint u_positionFloat = -1;
  448. GLuint vao = 0;
  449. GLuint vertexDataBuffer = 0;
  450. GLuint blockPositionBuffer = 0;
  451. GLuint cubeIndices = 0;
  452. GLuint vaoLines = 0;
  453. GLuint vertexDataBufferLines = 0;
  454. std::vector<CubeData> cubes;
  455. std::vector<LinesData> lines;
  456. void drawCube(int x, int y, int z) { cubes.push_back({x, y, z}); };
  457. void drawCube(glm::ivec3 pos) { drawCube(pos.x, pos.y, pos.z); };
  458. void drawCube(glm::ivec3 pos, glm::vec3 posFloat, glm::vec3 size)
  459. { cubes.push_back({pos.x, pos.y, pos.z, 0,
  460. posFloat.x,posFloat.y,posFloat.z,0,
  461. size.x, size.y, size.z, 0}); };
  462. //todo not working at far distances rn
  463. void drawLine(glm::dvec3 a, glm::dvec3 b) { lines.push_back({a, b}); }
  464. void render(Camera &c, glm::ivec3 posInt, glm::vec3 posFloat);
  465. };
  466. struct PointDebugRenderer
  467. {
  468. void create();
  469. Shader pointDebugShader;
  470. GLint u_viewProjection = -1;
  471. GLint u_positionInt = -1;
  472. GLint u_positionFloat = -1;
  473. GLint u_blockPositionInt = -1;
  474. GLint u_blockPositionFloat = -1;
  475. void renderPoint(Camera &c, glm::dvec3 point);
  476. void renderCubePoint(Camera &c, glm::dvec3 point);
  477. };
  478. constexpr int mergeShortsUnsigned(unsigned short a, unsigned short b)
  479. {
  480. int rez = 0;
  481. ((unsigned short *)&rez)[0] = a;
  482. ((unsigned short *)&rez)[1] = b;
  483. return rez;
  484. }
  485. constexpr int mergeShorts(short a, short b)
  486. {
  487. int rez = 0;
  488. ((short*)&rez)[0] = a;
  489. ((short*)&rez)[1] = b;
  490. return rez;
  491. }
  492. constexpr unsigned char merge4bits(unsigned char a, unsigned char b)
  493. {
  494. unsigned char rez = b & 0b1111;
  495. a = a & 0b1111;
  496. a <<= 4;
  497. rez |= a;
  498. return rez;
  499. }
  500. constexpr unsigned short mergeChars(unsigned char a, unsigned char b)
  501. {
  502. unsigned short rez = b;
  503. unsigned short secondA = a;
  504. secondA <<= 8;
  505. rez |= secondA;
  506. return rez;
  507. }