Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ jobs:
templates/**/*.apk
tests/**/*.apk
ios:
runs-on: macos-latest
runs-on: macos-14
strategy:
matrix:
target_os:
Expand Down
2 changes: 1 addition & 1 deletion 1k/build.profiles
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# --- region platfom:common

# The axmol shader compiler, legacy name is 'glslcc' before axmol-2.3.0
axslcc=1.12.0+
axslcc=1.13.1

# The cmake, @gradle @axmol-cmdline
# as latest as possible
Expand Down
58 changes: 22 additions & 36 deletions axmol/3d/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ using namespace ax;

namespace ax
{

namespace
{
// It's used for creating a default texture when lightMap is nullpter
static unsigned char ax_2x2_white_image[] = {
// RGBA8888
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
} // namespace

Terrain* Terrain::create(TerrainData& parameter, CrackFixedType fixedType)
{
Terrain* terrain = new Terrain();
Expand Down Expand Up @@ -135,37 +126,36 @@ void Terrain::draw(ax::Renderer* renderer, const ax::Mat4& transform, uint32_t f
_programState->setUniform(_lightDirLocation, &_lightDir, sizeof(_lightDir));
if (!_alphaMap)
{
_programState->setTexture(_detailMapLocation[0], 0, _detailMapTextures[0]->getRHITexture());
_programState->setTexture(_detailMapLocation, _detailMapBindings[0].slot, _detailMapBindings[0].tex);
int hasAlphaMap = 0;
_programState->setUniform(_alphaIsHasAlphaMapLocation, &hasAlphaMap, sizeof(hasAlphaMap));
_programState->setTexture(_lightMapLocation, BINDING_SLOT_ALPHA_MAP, _dummyTexture->getRHITexture());
}
else
{
float detailMapSize[4] = {0.0f, 0.0f, 0.0f, 0.0f};
for (int i = 0; i < _maxDetailMapValue; ++i)
{
_programState->setTexture(_detailMapLocation[i], i, _detailMapTextures[i]->getRHITexture());
detailMapSize[i] = _terrainData._detailMaps[i]._detailMapSize;
}
_programState->setTextureArray(_detailMapLocation, _detailMapBindings);
_programState->setUniform(_detailMapSizeLocation, detailMapSize, sizeof(detailMapSize));

int hasAlphaMap = 1;
_programState->setUniform(_alphaIsHasAlphaMapLocation, &hasAlphaMap, sizeof(hasAlphaMap));
_programState->setTexture(_alphaMapLocation, 4, _alphaMap->getRHITexture());
_programState->setTexture(_alphaMapLocation, BINDING_SLOT_ALPHA_MAP, _alphaMap->getRHITexture());
}
if (_lightMap)
{
int hasLightMap = 1;
_programState->setUniform(_lightMapCheckLocation, &hasLightMap, sizeof(hasLightMap));
_programState->setTexture(_lightMapLocation, 5, _lightMap->getRHITexture());
_programState->setTexture(_lightMapLocation, BINDING_SLOT_LIGHT_MAP, _lightMap->getRHITexture());
}
else
{
int hasLightMap = 0;
_programState->setUniform(_lightMapCheckLocation, &hasLightMap, sizeof(hasLightMap));
#if AX_RENDER_API == AX_RENDER_API_MTL || AX_RENDER_API == AX_RENDER_API_D3D
_programState->setTexture(_lightMapLocation, 5, _detailMapTextures[0]->getRHITexture());
#endif
_programState->setTexture(_lightMapLocation, BINDING_SLOT_LIGHT_MAP, _dummyTexture->getRHITexture());
}
auto camera = Camera::getVisitingCamera();

Expand Down Expand Up @@ -265,14 +255,8 @@ Terrain::Terrain()
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { reload(); });
_director->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
#endif
#if AX_RENDER_API == AX_RENDER_API_MTL || AX_RENDER_API == AX_RENDER_API_D3D
auto image = new Image();
bool AX_UNUSED isOK = image->initWithRawData(ax_2x2_white_image, sizeof(ax_2x2_white_image), 2, 2, 8);
AXASSERT(isOK, "The 2x2 empty texture was created unsuccessfully.");
_dummyTexture = new Texture2D();
_dummyTexture->initWithImage(image);
AX_SAFE_RELEASE(image);
#endif
_dummyTexture = _director->getTextureCache()->getWhiteTexture();
AX_SAFE_RETAIN(_dummyTexture);
}

void Terrain::setChunksLOD(const Vec3& cameraPos)
Expand Down Expand Up @@ -477,6 +461,7 @@ Terrain::~Terrain()
{
_detailMapTextures[i]->release();
}
_detailMapBindings[i].reset();
}
for (int i = 0; i < MAX_CHUNKES; ++i)
{
Expand Down Expand Up @@ -689,14 +674,16 @@ void Terrain::setDetailMap(unsigned int index, DetailMap detailMap)
_terrainData._detailMaps[index] = detailMap;
if (_detailMapTextures[index])
{

_detailMapTextures[index]->release();
}
_detailMapTextures[index] = new Texture2D();
auto textImage = new Image();
textImage->initWithImageFile(detailMap._detailMapSrc);
_detailMapTextures[index]->initWithImage(textImage);
delete textImage;

_detailMapBindings[index].slot = BINDING_SLOT_DETAIL_BASE + index;
_detailMapBindings[index].tex = _detailMapTextures[index]->getRHITexture();
}

Terrain::ChunkIndices Terrain::lookForIndicesLOD(int neighborLod[4], int selfLod, bool* result)
Expand Down Expand Up @@ -799,26 +786,18 @@ void Terrain::onEnter()
void Terrain::cacheUniformAttribLocation()
{
_alphaMapLocation.reset();
for (int i = 0; i < 4; ++i)
{
_detailMapLocation[i].reset();
}
_detailMapLocation.reset();
_detailMapSizeLocation.reset();

_alphaIsHasAlphaMapLocation = _programState->getUniformLocation("u_has_alpha");
_lightMapCheckLocation = _programState->getUniformLocation("u_has_light_map");
if (!_alphaMap)
{
_detailMapLocation[0] = _programState->getUniformLocation("u_tex0");
_detailMapLocation = _programState->getUniformLocation("u_details");
}
else
{
char str[20];
for (int i = 0; i < _maxDetailMapValue; ++i)
{
auto key = fmt::format_to_z(str, "u_tex{}", i);
_detailMapLocation[i] = _programState->getUniformLocation(key);
}
_detailMapLocation = _programState->getUniformLocation("u_details");

_detailMapSizeLocation = _programState->getUniformLocation("u_detailSize"); // float[4]

Expand All @@ -834,6 +813,7 @@ bool Terrain::initTextures()
for (int i = 0; i < 4; ++i)
{
_detailMapTextures[i] = nullptr;
_detailMapBindings[i].reset();
}

rhi::TextureDesc texDesc;
Expand All @@ -855,6 +835,9 @@ bool Terrain::initTextures()
texture->initWithSpec(texDesc, subDatas);
_detailMapTextures[0] = texture;
image->release();

_detailMapBindings[0].slot = BINDING_SLOT_DETAIL_BASE;
_detailMapBindings[0].tex = texture->getRHITexture();
}
else
{
Expand Down Expand Up @@ -887,6 +870,9 @@ bool Terrain::initTextures()
texture->initWithSpec(texDesc, subDatas);
_detailMapTextures[i] = texture;

_detailMapBindings[i].slot = BINDING_SLOT_DETAIL_BASE + i;
_detailMapBindings[i].tex = texture->getRHITexture();

image->release();
}
}
Expand Down
14 changes: 13 additions & 1 deletion axmol/3d/Terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ class AX_DLL Terrain : public Node
INCREASE_LOWER,
};

/* must match in shader terrain.frag
layout(binding = 0) uniform sampler2D u_details[4]; // will take slot 0~3
layout(binding = 4) uniform sampler2D u_alphaMap;
layout(binding = 5) uniform sampler2D u_lightMap;
*/
enum TextureBindingSlot {
BINDING_SLOT_DETAIL_BASE = 0,
BINDING_SLOT_ALPHA_MAP = 4,
BINDING_SLOT_LIGHT_MAP = 5
};

/**
*DetailMap
*this struct maintain a detail map data ,including source file ,detail size.
Expand Down Expand Up @@ -524,6 +535,7 @@ class AX_DLL Terrain : public Node
unsigned char* _data;
float _lodDistance[3];
Texture2D* _detailMapTextures[4];
rhi::TextureBinding _detailMapBindings[4]; // weak ref
Texture2D* _alphaMap;
Texture2D* _lightMap;
Texture2D* _dummyTexture = nullptr;
Expand Down Expand Up @@ -561,7 +573,7 @@ class AX_DLL Terrain : public Node

private:
// uniform locations
rhi::UniformLocation _detailMapLocation[4];
rhi::UniformLocation _detailMapLocation;
rhi::UniformLocation _alphaMapLocation;
rhi::UniformLocation _alphaIsHasAlphaMapLocation;
rhi::UniformLocation _lightMapCheckLocation;
Expand Down
6 changes: 4 additions & 2 deletions axmol/3d/VertexInputBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace ax
{

/**
* axmol-3.0: we use meshIndexData+instancing as key to ensure cache hit
* axmol-3.0: we use meshIndexData+program+instancing as key to ensure cache hit
* older version: cache miss always due to programState always changes when switch
* render objects
*/
Expand Down Expand Up @@ -67,12 +67,14 @@ VertexInputBinding* VertexInputBinding::spawn(MeshIndexData* meshIndexData,
struct HashMe
{
MeshIndexData* meshData;
Program* shaderProg;
bool instancing;
};

HashMe hashMe;
memset(&hashMe, 0, sizeof(hashMe));
hashMe.meshData = meshIndexData;
hashMe.shaderProg = pass->getProgramState()->getProgram();
hashMe.instancing = instancing;

auto hash = XXH32(&hashMe, sizeof(hashMe), 0);
Expand Down Expand Up @@ -166,7 +168,7 @@ void VertexInputBinding::setVertexInputPointer(VertexLayoutDesc& desc,
}
else
{
AXLOGD("VertexInputBinding: warning: attribute: '{}' not present in shader", name);
AXLOGI("VertexInputBinding: attribute: '{}' not present in shader", name);
}
}

Expand Down
15 changes: 6 additions & 9 deletions axmol/renderer/shaders/terrain.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ precision highp int;

layout(location = TEXCOORD0) in vec2 v_texCoord;
layout(location = NORMAL) in vec3 v_normal;
layout(binding = 0) uniform sampler2D u_alphaMap;
layout(binding = 1) uniform sampler2D u_tex0;
layout(binding = 2) uniform sampler2D u_tex1;
layout(binding = 3) uniform sampler2D u_tex2;
layout(binding = 4) uniform sampler2D u_tex3;
layout(binding = 0) uniform sampler2D u_details[4]; // will take slot 0~3
layout(binding = 4) uniform sampler2D u_alphaMap;
layout(binding = 5) uniform sampler2D u_lightMap;
layout(std140) uniform fs_ub {
int u_has_alpha;
Expand All @@ -35,15 +32,15 @@ void main()
float lightFactor = dot(-u_lightDir,v_normal);
if(u_has_alpha<=0)
{
FragColor = texture(u_tex0, v_texCoord)*lightColor*lightFactor;
FragColor = texture(u_details[0], v_texCoord)*lightColor*lightFactor;
}
else
{
vec4 blendFactor =texture(u_alphaMap,v_texCoord);
vec4 color = vec4(0.0,0.0,0.0,0.0);
color = texture(u_tex0, v_texCoord*vfloat_at(u_detailSize, 0))*blendFactor.r +
texture(u_tex1, v_texCoord*vfloat_at(u_detailSize, 1))*blendFactor.g + texture(u_tex2, v_texCoord*vfloat_at(u_detailSize, 2))*blendFactor.b
+ texture(u_tex3, v_texCoord*vfloat_at(u_detailSize, 3))*(1.0 - blendFactor.a);
color = texture(u_details[0], v_texCoord*vfloat_at(u_detailSize, 0))*blendFactor.r +
texture(u_details[1], v_texCoord*vfloat_at(u_detailSize, 1))*blendFactor.g + texture(u_details[2], v_texCoord*vfloat_at(u_detailSize, 2))*blendFactor.b
+ texture(u_details[3], v_texCoord*vfloat_at(u_detailSize, 3))*(1.0 - blendFactor.a);
FragColor = vec4(color.rgb*lightColor.rgb*lightFactor, 1.0);
}
}
Loading