Skip to content

Commit 9ea5964

Browse files
committed
HLSL51: standardize vertex semantics, add instancing macros, pin axslcc 3.6.0
1 parent dccacf0 commit 9ea5964

7 files changed

Lines changed: 64 additions & 37 deletions

File tree

axmol/renderer/shaders/base.hlsli

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//------------------------------------------------------------------------------
2+
// Basic definitions for HLSL shaders
3+
//------------------------------------------------------------------------------
4+
15
// Vertex input semantic alias
26
#ifndef DIRLIGHT
37
#define DIRLIGHT TEXCOORD1
@@ -36,7 +40,9 @@
3640
// GLSL/ESSL → no flip: ((v).y)
3741
#define AX_Y_UP(v) (AXSLC_UV_TOP ? (1.0 - (v).y) : ((v).y))
3842

39-
// ==== Builtin Samplers ====
43+
//------------------------------------------------------------------------------
44+
// Builtin Samplers
45+
//------------------------------------------------------------------------------
4046

4147
// --- Linear sampling ---
4248
SamplerState LinearClamp : register(s0, space1);
@@ -71,3 +77,24 @@ SamplerComparisonState ShadowCmpBorder : register(s19, space1);
7177
// --- Special cases ---
7278
SamplerState LinearNoMipClamp : register(s20, space1);
7379
SamplerState PointNoMipClamp : register(s21, space1);
80+
81+
//------------------------------------------------------------------------------
82+
// GPU Instancing
83+
//------------------------------------------------------------------------------
84+
85+
// Declares the per-instance transform input.
86+
//
87+
// Example:
88+
// AX_INSTANCE_INPUT(TEXCOORD1)
89+
#define AX_INSTANCE_INPUT(semantic) \
90+
float4x4 __ax_instance : semantic
91+
92+
// Transforms an object-space position to clip space using the current
93+
// per-instance transform.
94+
//
95+
// NOTE:
96+
// Axmol stores instance transforms as four column vectors. HLSL assembles
97+
// vertex input matrices from semantic rows, so the matrix is transposed
98+
// to recover the logical object-to-world transform.
99+
#define AX_INSTANCE_TRANSFORM(input, position, mvp) \
100+
mul(mul((mvp), transpose((input).__ax_instance)), (position))

axmol/renderer/shaders/unlit_instance_vs.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
struct VS_IN {
44
float4 a_position : POSITION;
55
float2 a_texCoord : TEXCOORD0;
6-
float4x4 a_instance : TEXCOORD1;
6+
AX_INSTANCE_INPUT(TEXCOORD1);
77
};
88

99
struct VS_OUT {
@@ -18,7 +18,7 @@ cbuffer vs_ub : register(b0, space0) {
1818
VS_OUT main(VS_IN input)
1919
{
2020
VS_OUT output;
21-
output.position = mul(mul(u_MVPMatrix, input.a_instance), input.a_position);
21+
output.position = AX_INSTANCE_TRANSFORM(input, input.a_position, u_MVPMatrix);
2222
output.v_texCoord = input.a_texCoord;
2323
output.v_texCoord.y = 1.0 - output.v_texCoord.y;
2424
return output;

tests/cpp-tests/Source/Box2DTestBed/samples/draw.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ bool SampleDrawNode::initWithWorld(b2WorldId worldId)
310310
auto pipelinePS = new rhi::ProgramState(program);
311311
auto vfmt = axvlm->allocateVertexLayoutDesc();
312312
vfmt.startLayout(3);
313-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"LOCAL_POSITION", 0}), rhi::VertexElementType::FLOAT2, 0,
313+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::POSITION), rhi::VertexElementType::FLOAT2, 0,
314314
false);
315315
cmd.createVertexBuffer(sizeof(Vec2), 6, CustomCommand::BufferUsage::STATIC);
316316
float a = 1.1f;
@@ -319,9 +319,9 @@ bool SampleDrawNode::initWithWorld(b2WorldId worldId)
319319
cmd.setVertexDrawInfo(0, 6);
320320

321321
// instanced attributes
322-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"INSTANCE_COLOR", 0}), rhi::VertexElementType::FLOAT4,
322+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::TEXCOORD0), rhi::VertexElementType::FLOAT4,
323323
offsetof(CircleData, rgba), false, 1);
324-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"INSTANCE_POS_AND_RADIUS", 0}),
324+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::TEXCOORD1),
325325
rhi::VertexElementType::FLOAT4, offsetof(CircleData, position), false, 1);
326326
vfmt.endLayout();
327327

@@ -342,7 +342,7 @@ bool SampleDrawNode::initWithWorld(b2WorldId worldId)
342342
auto pipelinePS = new rhi::ProgramState(program);
343343
auto vfmt = axvlm->allocateVertexLayoutDesc();
344344
vfmt.startLayout(4);
345-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"LOCAL_POSITION", 0}), rhi::VertexElementType::FLOAT2, 0,
345+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::POSITION), rhi::VertexElementType::FLOAT2, 0,
346346
false);
347347

348348
cmd.createVertexBuffer(sizeof(Vec2), 6, CustomCommand::BufferUsage::STATIC);
@@ -352,11 +352,11 @@ bool SampleDrawNode::initWithWorld(b2WorldId worldId)
352352
cmd.setVertexDrawInfo(0, 6);
353353

354354
// instanced attributes
355-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"INSTANCE_TRANSFORM", 0}),
355+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::TEXCOORD2),
356356
rhi::VertexElementType::FLOAT4, offsetof(SolidCircleData, transform), false, 1);
357-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"INSTANCE_COLOR", 0}), rhi::VertexElementType::FLOAT4,
357+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::TEXCOORD0), rhi::VertexElementType::FLOAT4,
358358
offsetof(SolidCircleData, rgba), false, 1);
359-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"INSTANCE_RADIUS", 0}), rhi::VertexElementType::FLOAT4,
359+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::TEXCOORD1), rhi::VertexElementType::FLOAT4,
360360
offsetof(SolidCircleData, radius), false, 1);
361361
vfmt.endLayout();
362362

@@ -376,7 +376,7 @@ bool SampleDrawNode::initWithWorld(b2WorldId worldId)
376376
auto pipelinePS = new rhi::ProgramState(program);
377377
auto vfmt = axvlm->allocateVertexLayoutDesc();
378378
vfmt.startLayout(4);
379-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"LOCAL_POSITION", 0}), rhi::VertexElementType::FLOAT2, 0,
379+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::POSITION), rhi::VertexElementType::FLOAT2, 0,
380380
false);
381381

382382
cmd.createVertexBuffer(sizeof(Vec2), 6, CustomCommand::BufferUsage::STATIC);
@@ -386,12 +386,12 @@ bool SampleDrawNode::initWithWorld(b2WorldId worldId)
386386
cmd.setVertexDrawInfo(0, 6);
387387

388388
// instanced attributes
389-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"INSTANCE_TRANSFORM", 0}),
389+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::TEXCOORD2),
390390
rhi::VertexElementType::FLOAT4, offsetof(CapsuleData, transform), false, 1);
391-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"INSTANCE_COLOR", 0}), rhi::VertexElementType::FLOAT4,
391+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::TEXCOORD0), rhi::VertexElementType::FLOAT4,
392392
offsetof(CapsuleData, rgba), false, 1);
393-
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic{"INSTANCE_RADIUS_AND_LENGTH", 0}),
394-
rhi::VertexElementType::FLOAT4, offsetof(CapsuleData, radius), false, 1);
393+
vfmt.addAttrib(program->getVertexInputDesc(rhi::VertexSemantic::TEXCOORD1), rhi::VertexElementType::FLOAT4,
394+
offsetof(CapsuleData, radius), false, 1);
395395
vfmt.endLayout();
396396

397397
auto pipelineVL = axvlm->getVertexLayout(std::move(vfmt));

tests/cpp-tests/Source/shaders/circle_vs.hlsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#include "base.hlsli"
22

33
struct VS_IN {
4-
float2 a_localPosition : LOCAL_POSITION;
5-
float4 a_instanceColor : INSTANCE_COLOR0;
6-
float4 a_instancePosAndRadius : INSTANCE_POS_AND_RADIUS0;
4+
float2 a_localPosition : POSITION;
5+
float4 a_instanceColor : TEXCOORD0;
6+
float4 a_instancePosAndRadius : TEXCOORD1;
77
};
88

99
struct VS_OUT {
10-
float4 position : SV_Position;
1110
float2 f_position : TEXCOORD0;
1211
float4 f_color : TEXCOORD1;
1312
float f_thickness : TEXCOORD2;
13+
float4 position : SV_Position;
1414
};
1515

1616
cbuffer vs_ub : register(b0, space0) {

tests/cpp-tests/Source/shaders/solid_capsule_vs.hlsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "base.hlsli"
22

33
struct VS_IN {
4-
float2 a_localPosition : LOCAL_POSITION;
5-
float4 a_instanceTransform : INSTANCE_TRANSFORM0;
6-
float4 a_instanceColor : INSTANCE_COLOR0;
7-
float4 a_instanceRadiusAndLength : INSTANCE_RADIUS_AND_LENGTH0;
4+
float2 a_localPosition : POSITION;
5+
float4 a_instanceColor : TEXCOORD0;
6+
float4 a_instanceRadiusAndLength : TEXCOORD1;
7+
float4 a_instanceTransform : TEXCOORD2;
88
};
99

1010
struct VS_OUT {

tests/cpp-tests/Source/shaders/solid_circle_vs.hlsl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include "base.hlsli"
22

33
struct VS_IN {
4-
float2 a_localPosition : LOCAL_POSITION;
5-
float4 a_instanceTransform : INSTANCE_TRANSFORM0;
6-
float4 a_instanceColor : INSTANCE_COLOR0;
7-
float4 a_instanceRadius : INSTANCE_RADIUS0;
4+
float2 a_localPosition : POSITION;
5+
float4 a_instanceColor : TEXCOORD0;
6+
float4 a_instanceRadius : TEXCOORD1;
7+
float4 a_instanceTransform : TEXCOORD2;
88
};
99

1010
struct VS_OUT {
11-
float4 position : SV_Position;
1211
float2 f_position : TEXCOORD0;
1312
float4 f_color : TEXCOORD1;
1413
float f_thickness : TEXCOORD2;
14+
float4 position : SV_Position;
1515
};
1616

1717
cbuffer vs_ub : register(b0, space0) {

tests/cpp-tests/Source/shaders/solid_polygon_vs.hlsl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#include "base.hlsli"
22

33
struct VS_IN {
4-
float2 a_localPosition : LOCAL_POSITION;
5-
float4 a_instanceTransform : INSTANCE_TRANSFORM0;
6-
float4 a_instancePoints12 : INSTANCE_POINTS12_0;
7-
float4 a_instancePoints34 : INSTANCE_POINTS34_0;
8-
float4 a_instancePoints56 : INSTANCE_POINTS56_0;
9-
float4 a_instancePoints78 : INSTANCE_POINTS78_0;
10-
int a_instanceCount : INSTANCE_COUNT0;
11-
float a_instanceRadius : INSTANCE_RADIUS0;
12-
float4 a_instanceColor : INSTANCE_COLOR0;
4+
float2 a_localPosition : POSITION;
5+
float4 a_instanceTransform : TEXCOORD0;
6+
float4 a_instancePoints12 : TEXCOORD1;
7+
float4 a_instancePoints34 : TEXCOORD2;
8+
float4 a_instancePoints56 : TEXCOORD3;
9+
float4 a_instancePoints78 : TEXCOORD4;
10+
int a_instanceCount : TEXCOORD5;
11+
float a_instanceRadius : TEXCOORD6;
12+
float4 a_instanceColor : TEXCOORD7;
1313
};
1414

1515
struct VS_OUT {

0 commit comments

Comments
 (0)