@@ -47,15 +47,6 @@ using namespace ax;
4747
4848namespace ax
4949{
50-
51- namespace
52- {
53- // It's used for creating a default texture when lightMap is nullpter
54- static unsigned char ax_2x2_white_image[] = {
55- // RGBA8888
56- 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
57- } // namespace
58-
5950Terrain* Terrain::create (TerrainData& parameter, CrackFixedType fixedType)
6051{
6152 Terrain* terrain = new Terrain ();
@@ -135,37 +126,36 @@ void Terrain::draw(ax::Renderer* renderer, const ax::Mat4& transform, uint32_t f
135126 _programState->setUniform (_lightDirLocation, &_lightDir, sizeof (_lightDir));
136127 if (!_alphaMap)
137128 {
138- _programState->setTexture (_detailMapLocation[0 ], 0 , _detailMapTextures [0 ]-> getRHITexture () );
129+ _programState->setTexture (_detailMapLocation, _detailMapBindings [0 ]. slot , _detailMapBindings [0 ]. tex );
139130 int hasAlphaMap = 0 ;
140131 _programState->setUniform (_alphaIsHasAlphaMapLocation, &hasAlphaMap, sizeof (hasAlphaMap));
132+ _programState->setTexture (_lightMapLocation, BINDING_SLOT_ALPHA_MAP, _dummyTexture->getRHITexture ());
141133 }
142134 else
143135 {
144136 float detailMapSize[4 ] = {0 .0f , 0 .0f , 0 .0f , 0 .0f };
145137 for (int i = 0 ; i < _maxDetailMapValue; ++i)
146138 {
147- _programState->setTexture (_detailMapLocation[i], i, _detailMapTextures[i]->getRHITexture ());
148139 detailMapSize[i] = _terrainData._detailMaps [i]._detailMapSize ;
149140 }
141+ _programState->setTextureArray (_detailMapLocation, _detailMapBindings);
150142 _programState->setUniform (_detailMapSizeLocation, detailMapSize, sizeof (detailMapSize));
151143
152144 int hasAlphaMap = 1 ;
153145 _programState->setUniform (_alphaIsHasAlphaMapLocation, &hasAlphaMap, sizeof (hasAlphaMap));
154- _programState->setTexture (_alphaMapLocation, 4 , _alphaMap->getRHITexture ());
146+ _programState->setTexture (_alphaMapLocation, BINDING_SLOT_ALPHA_MAP , _alphaMap->getRHITexture ());
155147 }
156148 if (_lightMap)
157149 {
158150 int hasLightMap = 1 ;
159151 _programState->setUniform (_lightMapCheckLocation, &hasLightMap, sizeof (hasLightMap));
160- _programState->setTexture (_lightMapLocation, 5 , _lightMap->getRHITexture ());
152+ _programState->setTexture (_lightMapLocation, BINDING_SLOT_LIGHT_MAP , _lightMap->getRHITexture ());
161153 }
162154 else
163155 {
164156 int hasLightMap = 0 ;
165157 _programState->setUniform (_lightMapCheckLocation, &hasLightMap, sizeof (hasLightMap));
166- #if AX_RENDER_API == AX_RENDER_API_MTL || AX_RENDER_API == AX_RENDER_API_D3D
167- _programState->setTexture (_lightMapLocation, 5 , _detailMapTextures[0 ]->getRHITexture ());
168- #endif
158+ _programState->setTexture (_lightMapLocation, BINDING_SLOT_LIGHT_MAP, _dummyTexture->getRHITexture ());
169159 }
170160 auto camera = Camera::getVisitingCamera ();
171161
@@ -265,14 +255,8 @@ Terrain::Terrain()
265255 EventListenerCustom::create (EVENT_RENDERER_RECREATED, [this ](EventCustom*) { reload (); });
266256 _director->getEventDispatcher ()->addEventListenerWithFixedPriority (_backToForegroundListener, 1 );
267257#endif
268- #if AX_RENDER_API == AX_RENDER_API_MTL || AX_RENDER_API == AX_RENDER_API_D3D
269- auto image = new Image ();
270- bool AX_UNUSED isOK = image->initWithRawData (ax_2x2_white_image, sizeof (ax_2x2_white_image), 2 , 2 , 8 );
271- AXASSERT (isOK, " The 2x2 empty texture was created unsuccessfully." );
272- _dummyTexture = new Texture2D ();
273- _dummyTexture->initWithImage (image);
274- AX_SAFE_RELEASE (image);
275- #endif
258+ _dummyTexture = _director->getTextureCache ()->getWhiteTexture ();
259+ AX_SAFE_RETAIN (_dummyTexture);
276260}
277261
278262void Terrain::setChunksLOD (const Vec3& cameraPos)
@@ -477,6 +461,7 @@ Terrain::~Terrain()
477461 {
478462 _detailMapTextures[i]->release ();
479463 }
464+ _detailMapBindings[i].reset ();
480465 }
481466 for (int i = 0 ; i < MAX_CHUNKES; ++i)
482467 {
@@ -689,14 +674,16 @@ void Terrain::setDetailMap(unsigned int index, DetailMap detailMap)
689674 _terrainData._detailMaps [index] = detailMap;
690675 if (_detailMapTextures[index])
691676 {
692-
693677 _detailMapTextures[index]->release ();
694678 }
695679 _detailMapTextures[index] = new Texture2D ();
696680 auto textImage = new Image ();
697681 textImage->initWithImageFile (detailMap._detailMapSrc );
698682 _detailMapTextures[index]->initWithImage (textImage);
699683 delete textImage;
684+
685+ _detailMapBindings[index].slot = BINDING_SLOT_DETAIL_BASE + index;
686+ _detailMapBindings[index].tex = _detailMapTextures[index]->getRHITexture ();
700687}
701688
702689Terrain::ChunkIndices Terrain::lookForIndicesLOD (int neighborLod[4 ], int selfLod, bool * result)
@@ -799,26 +786,18 @@ void Terrain::onEnter()
799786void Terrain::cacheUniformAttribLocation ()
800787{
801788 _alphaMapLocation.reset ();
802- for (int i = 0 ; i < 4 ; ++i)
803- {
804- _detailMapLocation[i].reset ();
805- }
789+ _detailMapLocation.reset ();
806790 _detailMapSizeLocation.reset ();
807791
808792 _alphaIsHasAlphaMapLocation = _programState->getUniformLocation (" u_has_alpha" );
809793 _lightMapCheckLocation = _programState->getUniformLocation (" u_has_light_map" );
810794 if (!_alphaMap)
811795 {
812- _detailMapLocation[ 0 ] = _programState->getUniformLocation (" u_tex0 " );
796+ _detailMapLocation = _programState->getUniformLocation (" u_details " );
813797 }
814798 else
815799 {
816- char str[20 ];
817- for (int i = 0 ; i < _maxDetailMapValue; ++i)
818- {
819- auto key = fmt::format_to_z (str, " u_tex{}" , i);
820- _detailMapLocation[i] = _programState->getUniformLocation (key);
821- }
800+ _detailMapLocation = _programState->getUniformLocation (" u_details" );
822801
823802 _detailMapSizeLocation = _programState->getUniformLocation (" u_detailSize" ); // float[4]
824803
@@ -834,6 +813,7 @@ bool Terrain::initTextures()
834813 for (int i = 0 ; i < 4 ; ++i)
835814 {
836815 _detailMapTextures[i] = nullptr ;
816+ _detailMapBindings[i].reset ();
837817 }
838818
839819 rhi::TextureDesc texDesc;
@@ -855,6 +835,9 @@ bool Terrain::initTextures()
855835 texture->initWithSpec (texDesc, subDatas);
856836 _detailMapTextures[0 ] = texture;
857837 image->release ();
838+
839+ _detailMapBindings[0 ].slot = BINDING_SLOT_DETAIL_BASE;
840+ _detailMapBindings[0 ].tex = texture->getRHITexture ();
858841 }
859842 else
860843 {
@@ -887,6 +870,9 @@ bool Terrain::initTextures()
887870 texture->initWithSpec (texDesc, subDatas);
888871 _detailMapTextures[i] = texture;
889872
873+ _detailMapBindings[i].slot = BINDING_SLOT_DETAIL_BASE + i;
874+ _detailMapBindings[i].tex = texture->getRHITexture ();
875+
890876 image->release ();
891877 }
892878 }
0 commit comments