@@ -66,6 +66,75 @@ float4 PS() : SV_Target
6666 ASSERT_TEXTURE (tex.get (), " triangle" );
6767 }
6868
69+ TEST (Core.HAL , RenderInstancing)
70+ {
71+ auto & device = HAL::Device::get ();
72+ constexpr uint WIDTH = 256 , HEIGHT = 256 ;
73+
74+ auto tex = std::make_shared<HAL ::TextureResource>(device,
75+ HAL::ResourceDesc::Tex2D (HAL ::Format::R8G8B8A8_UNORM , {WIDTH , HEIGHT }, 1 , 1 ,
76+ HAL ::ResFlags::RenderTarget),
77+ HAL ::HeapType::DEFAULT );
78+
79+ // 9 instances of a small triangle arranged in a 3×3 grid.
80+ // SV_InstanceID drives position (column/row offset) and color.
81+ static constexpr const char * kInstHLSL = R"hlsl(
82+ static const float2 kLocalPos[3] = {
83+ float2(-0.12, -0.10),
84+ float2( 0.12, -0.10),
85+ float2( 0.00, 0.12),
86+ };
87+ static const float4 kColor[9] = {
88+ float4(1,0,0,1), float4(0,1,0,1), float4(0,0,1,1),
89+ float4(1,1,0,1), float4(1,0,1,1), float4(0,1,1,1),
90+ float4(1,0.5,0,1), float4(0,0.5,1,1), float4(0.5,1,0,1),
91+ };
92+ struct VSOut { float4 pos : SV_Position; float4 col : COLOR0; };
93+ VSOut VS(uint vid : SV_VertexID, uint iid : SV_InstanceID)
94+ {
95+ uint col = iid % 3;
96+ uint row = iid / 3;
97+ float2 offset = float2((float(col) - 1.0) * 0.62,
98+ (1.0 - float(row)) * 0.62);
99+ VSOut o;
100+ o.pos = float4(kLocalPos[vid] + offset, 0.0, 1.0);
101+ o.col = kColor[iid];
102+ return o;
103+ }
104+ float4 PS(VSOut i) : SV_Target { return i.col; }
105+ )hlsl" ;
106+
107+ SimpleGraphicsPSO mpso (" TestInstancing" );
108+ mpso.root_signature = Layouts::NoneLayout;
109+ mpso.vertex = { kInstHLSL , " VS" , HAL ::ShaderOptions::None, {}, true };
110+ mpso.pixel = { kInstHLSL , " PS" , HAL ::ShaderOptions::None, {}, true };
111+ mpso.rtv_formats = { HAL ::Format::R8G8B8A8_UNORM };
112+ mpso.enable_depth = false ;
113+ mpso.cull = HAL ::CullMode::None;
114+ mpso.topology = HAL ::PrimitiveTopologyType::TRIANGLE ;
115+
116+ auto pso = mpso.create (device);
117+
118+ auto & queue = device.get_queue (HAL ::CommandListType::DIRECT );
119+ auto list = queue->get_free_list ();
120+ list->begin (L" RenderInstancing" );
121+
122+ HAL ::Texture2DView view (tex, *list);
123+ HAL ::CompiledRT compiled;
124+ compiled.table_rtv = view.renderTarget ;
125+
126+ auto & gfx = list->get_graphics ();
127+ gfx.set_rtv (compiled, HAL ::RTOptions::Default | HAL ::RTOptions::ClearColor, 0 , 0 , vec4 (0 , 0 , 0 , 1 ));
128+
129+ gfx.set_pipeline (pso);
130+ gfx.set_topology (HAL ::PrimitiveTopologyType::TRIANGLE );
131+ gfx.draw (3 , 0 , 9 ); // 3 vertices per instance, 9 instances
132+
133+ list->execute_and_wait ();
134+
135+ ASSERT_TEXTURE (tex.get (), " instancing" );
136+ }
137+
69138 TEST (Core.HAL , RenderCube)
70139 {
71140 auto & device = HAL::Device::get ();
0 commit comments