1
1
#include " core/slang-basic.h"
2
2
#include " examples/example-base/example-base.h"
3
- #include " slang-rhi/shader-cursor.h"
4
3
#include " platform/vector-math.h"
5
4
#include " platform/window.h"
6
5
#include " slang-com-ptr.h"
7
6
#include " slang-rhi.h"
7
+ #include " slang-rhi/shader-cursor.h"
8
8
#include " slang.h"
9
9
10
10
using namespace rhi ;
@@ -96,10 +96,7 @@ struct AutoDiffTexture : public WindowedAppBase
96
96
return SLANG_OK;
97
97
}
98
98
99
- Result loadComputeProgram (
100
- IDevice* device,
101
- const char * fileName,
102
- IShaderProgram** outProgram)
99
+ Result loadComputeProgram (IDevice* device, const char * fileName, IShaderProgram** outProgram)
103
100
{
104
101
ComPtr<slang::ISession> slangSession;
105
102
slangSession = device->getSlangSession ();
@@ -178,19 +175,16 @@ struct AutoDiffTexture : public WindowedAppBase
178
175
179
176
bool resetLearntTexture = false ;
180
177
181
- ComPtr<ITexture> createRenderTargetTexture (
182
- Format format,
183
- int w,
184
- int h,
185
- int levels)
178
+ ComPtr<ITexture> createRenderTargetTexture (Format format, int w, int h, int levels)
186
179
{
187
180
TextureDesc textureDesc = {};
188
181
textureDesc.format = format;
189
182
textureDesc.size .width = w;
190
183
textureDesc.size .height = h;
191
184
textureDesc.size .depth = 1 ;
192
185
textureDesc.mipCount = levels;
193
- textureDesc.usage = TextureUsage::ShaderResource | TextureUsage::UnorderedAccess | TextureUsage::RenderTarget;
186
+ textureDesc.usage = TextureUsage::ShaderResource | TextureUsage::UnorderedAccess |
187
+ TextureUsage::RenderTarget;
194
188
textureDesc.defaultState = ResourceState::RenderTarget;
195
189
return gDevice ->createTexture (textureDesc);
196
190
}
@@ -225,9 +219,7 @@ struct AutoDiffTexture : public WindowedAppBase
225
219
TextureViewDesc srvDesc = {};
226
220
return gDevice ->createTextureView (tex, srvDesc);
227
221
}
228
- ComPtr<IRenderPipeline> createRenderPipeline (
229
- IInputLayout* inputLayout,
230
- IShaderProgram* program)
222
+ ComPtr<IRenderPipeline> createRenderPipeline (IInputLayout* inputLayout, IShaderProgram* program)
231
223
{
232
224
ColorTargetDesc colorTarget;
233
225
colorTarget.format = Format::RGBA8Unorm;
@@ -254,8 +246,8 @@ struct AutoDiffTexture : public WindowedAppBase
254
246
TextureViewDesc desc = {};
255
247
SubresourceRange textureViewRange = {};
256
248
textureViewRange.mipCount = 1 ;
257
- textureViewRange.mip = level; // Fixed: should be level, not 0
258
- textureViewRange.layerCount = 1 ; // Fixed: should be 1, not level
249
+ textureViewRange.mip = level; // Fixed: should be level, not 0
250
+ textureViewRange.layerCount = 1 ; // Fixed: should be 1, not level
259
251
textureViewRange.layer = 0 ;
260
252
desc.subresourceRange = textureViewRange;
261
253
return gDevice ->createTextureView (texture, desc);
@@ -357,21 +349,21 @@ struct AutoDiffTexture : public WindowedAppBase
357
349
// Load texture from file - this would need to be adapted to use slang-rhi texture loading
358
350
Slang::String imagePath = resourceBase.resolveResource (" checkerboard.jpg" );
359
351
gTexView = createTextureFromFile (imagePath.getBuffer (), textureWidth, textureHeight);
360
- textureWidth = 512 ; // Placeholder values
352
+ textureWidth = 512 ; // Placeholder values
361
353
textureHeight = 512 ;
362
354
initMipOffsets (textureWidth, textureHeight);
363
355
364
356
BufferDesc bufferDesc = {};
365
357
bufferDesc.size = mipMapOffset.getLast () * sizeof (uint32_t );
366
358
bufferDesc.usage = BufferUsage::ShaderResource | BufferUsage::UnorderedAccess;
367
-
359
+
368
360
gAccumulateBuffer = gDevice ->createBuffer (bufferDesc);
369
361
if (!gAccumulateBuffer )
370
362
{
371
363
printf (" ERROR: Failed to create accumulate buffer!\n " );
372
364
return SLANG_FAIL;
373
365
}
374
-
366
+
375
367
gReconstructBuffer = gDevice ->createBuffer (bufferDesc);
376
368
if (!gReconstructBuffer )
377
369
{
@@ -381,21 +373,14 @@ struct AutoDiffTexture : public WindowedAppBase
381
373
382
374
int mipCount = 1 + Math::Log2Ceil (Math::Max (textureWidth, textureHeight));
383
375
SubresourceData initialData = {};
384
- initialData.data =
385
- gLearningTexture = createRenderTargetTexture (
386
- Format::RGBA32Float,
387
- textureWidth,
388
- textureHeight,
389
- mipCount);
376
+ initialData.data = gLearningTexture =
377
+ createRenderTargetTexture (Format::RGBA32Float, textureWidth, textureHeight, mipCount);
390
378
gLearningTextureSRV = createSRV (gLearningTexture );
391
379
for (int i = 0 ; i < mipCount; i++)
392
380
gLearningTextureUAVs .add (createUAV (gLearningTexture , i));
393
381
394
- gDiffTexture = createRenderTargetTexture (
395
- Format::RGBA32Float,
396
- textureWidth,
397
- textureHeight,
398
- mipCount);
382
+ gDiffTexture =
383
+ createRenderTargetTexture (Format::RGBA32Float, textureWidth, textureHeight, mipCount);
399
384
gDiffTextureSRV = createSRV (gDiffTexture );
400
385
for (int i = 0 ; i < mipCount; i++)
401
386
gDiffTextureUAVs .add (createUAV (gDiffTexture , i));
@@ -409,8 +394,7 @@ struct AutoDiffTexture : public WindowedAppBase
409
394
gRefImage = createRenderTargetTexture (Format::RGBA8Unorm, windowWidth, windowHeight, 1 );
410
395
gRefImageSRV = createSRV (gRefImage );
411
396
412
- gIterImage =
413
- createRenderTargetTexture (Format::RGBA8Unorm, windowWidth, windowHeight, 1 );
397
+ gIterImage = createRenderTargetTexture (Format::RGBA8Unorm, windowWidth, windowHeight, 1 );
414
398
gIterImageSRV = createSRV (gIterImage );
415
399
416
400
// Initialize textures
@@ -419,7 +403,7 @@ struct AutoDiffTexture : public WindowedAppBase
419
403
// Clear learning and diff textures
420
404
commandEncoder->clearTextureFloat (gLearningTexture , kEntireTexture , clearValue);
421
405
commandEncoder->clearTextureFloat (gDiffTexture , kEntireTexture , clearValue);
422
-
406
+
423
407
gQueue ->submit (commandEncoder->finish ());
424
408
}
425
409
@@ -464,9 +448,7 @@ struct AutoDiffTexture : public WindowedAppBase
464
448
}
465
449
466
450
template <typename SetupPipelineFunc>
467
- void renderImage (
468
- ITexture* renderTarget,
469
- const SetupPipelineFunc& setupPipeline)
451
+ void renderImage (ITexture* renderTarget, const SetupPipelineFunc& setupPipeline)
470
452
{
471
453
auto commandEncoder = gQueue ->createCommandEncoder ();
472
454
@@ -517,7 +499,8 @@ struct AutoDiffTexture : public WindowedAppBase
517
499
gRefImage ,
518
500
[&](IRenderPassEncoder* encoder)
519
501
{
520
- auto rootObject = encoder->bindPipeline (static_cast <IRenderPipeline*>(gRefPipeline .get ()));
502
+ auto rootObject =
503
+ encoder->bindPipeline (static_cast <IRenderPipeline*>(gRefPipeline .get ()));
521
504
ShaderCursor rootCursor (rootObject);
522
505
rootCursor[" Uniforms" ][" modelViewProjection" ].setData (
523
506
&transformMatrix,
@@ -528,7 +511,8 @@ struct AutoDiffTexture : public WindowedAppBase
528
511
mipMapOffset.getBuffer (),
529
512
sizeof (uint32_t ) * mipMapOffset.getCount ());
530
513
rootCursor[" Uniforms" ][" texRef" ].setBinding (gTexView );
531
- rootCursor[" Uniforms" ][" bwdTexture" ][" accumulateBuffer" ].setBinding (gAccumulateBuffer );
514
+ rootCursor[" Uniforms" ][" bwdTexture" ][" accumulateBuffer" ].setBinding (
515
+ gAccumulateBuffer );
532
516
});
533
517
}
534
518
@@ -544,7 +528,7 @@ struct AutoDiffTexture : public WindowedAppBase
544
528
auto commandEncoder = gQueue ->createCommandEncoder ();
545
529
commandEncoder->clearBuffer (gAccumulateBuffer , 0 , gAccumulateBuffer ->getDesc ().size );
546
530
commandEncoder->clearBuffer (gReconstructBuffer , 0 , gReconstructBuffer ->getDesc ().size );
547
-
531
+
548
532
if (resetLearntTexture)
549
533
{
550
534
commandEncoder->clearTextureFloat (gLearningTexture , kEntireTexture , clearValue);
@@ -570,7 +554,8 @@ struct AutoDiffTexture : public WindowedAppBase
570
554
mipMapOffset.getBuffer (),
571
555
sizeof (uint32_t ) * mipMapOffset.getCount ());
572
556
rootCursor[" Uniforms" ][" texRef" ].setBinding (gRefImageSRV );
573
- rootCursor[" Uniforms" ][" bwdTexture" ][" accumulateBuffer" ].setBinding (gAccumulateBuffer );
557
+ rootCursor[" Uniforms" ][" bwdTexture" ][" accumulateBuffer" ].setBinding (
558
+ gAccumulateBuffer );
574
559
rootCursor[" Uniforms" ][" bwdTexture" ][" minLOD" ].setData (5.0 );
575
560
});
576
561
@@ -671,7 +656,7 @@ struct AutoDiffTexture : public WindowedAppBase
671
656
renderPass.colorAttachmentCount = 1 ;
672
657
673
658
auto renderEncoder = commandEncoder->beginRenderPass (renderPass);
674
-
659
+
675
660
drawTexturedQuad (renderEncoder, 0 , 0 , textureWidth, textureHeight, gLearningTextureSRV );
676
661
677
662
int refImageWidth = windowWidth - textureWidth - 10 ;
@@ -718,7 +703,8 @@ struct AutoDiffTexture : public WindowedAppBase
718
703
renderState.vertexBufferCount = 1 ;
719
704
renderEncoder->setRenderState (renderState);
720
705
721
- auto root = renderEncoder->bindPipeline (static_cast <IRenderPipeline*>(gDrawQuadPipeline .get ()));
706
+ auto root =
707
+ renderEncoder->bindPipeline (static_cast <IRenderPipeline*>(gDrawQuadPipeline .get ()));
722
708
ShaderCursor rootCursor (root);
723
709
rootCursor[" Uniforms" ][" x" ].setData (x);
724
710
rootCursor[" Uniforms" ][" y" ].setData (y);
@@ -728,7 +714,7 @@ struct AutoDiffTexture : public WindowedAppBase
728
714
rootCursor[" Uniforms" ][" viewHeight" ].setData (windowHeight);
729
715
rootCursor[" Uniforms" ][" texture" ].setBinding (srv);
730
716
rootCursor[" Uniforms" ][" sampler" ].setBinding (gSampler );
731
-
717
+
732
718
DrawArguments drawArgs = {};
733
719
drawArgs.vertexCount = 4 ;
734
720
renderEncoder->draw (drawArgs);
0 commit comments