@@ -15,6 +15,9 @@ using namespace core;
15
15
constexpr const char * GLSL_COMPUTE_LIGHTING =
16
16
R"(
17
17
#define _IRR_COMPUTE_LIGHTING_DEFINED_
18
+
19
+ #include <irr/builtin/glsl/format/decode.glsl>
20
+
18
21
struct SLight
19
22
{
20
23
vec3 position;
@@ -25,20 +28,43 @@ layout (set = 2, binding = 0, std430) readonly restrict buffer Lights
25
28
{
26
29
SLight lights[];
27
30
};
31
+ layout(set = 2, binding = 1) uniform sampler2D envMap;
32
+
33
+ vec2 SampleSphericalMap(in vec3 v)
34
+ {
35
+ vec2 uv = vec2(atan(v.z, v.x), asin(v.y));
36
+ uv *= irr_glsl_RECIPROCAL_PI*0.5;
37
+ uv += 0.5;
38
+ return uv;
39
+ }
28
40
29
41
vec3 irr_computeLighting(inout irr_glsl_IsotropicViewSurfaceInteraction out_interaction, in mat2 dUV)
30
42
{
31
- vec3 emissive = decodeRGB19E7 (InstData.data[InstanceIndex].emissive);
43
+ vec3 emissive = irr_glsl_decodeRGB19E7 (InstData.data[InstanceIndex].emissive);
32
44
33
- irr_glsl_BSDFIsotropicParams params;
34
45
vec3 color = vec3(0.0);
46
+ /*
47
+ instr_stream_t gcs = getGenChoiceStream();
48
+ const int N = 100;
49
+ for (int i = 0; i < N; ++i)
50
+ {
51
+ vec3 rem;
52
+ vec2 rand;//TODO
53
+ vec3 L = runGeneratorChoiceStream(gcs, rand, rem);
54
+ vec2 uv = SampleSphericalMap(L);
55
+ color += rem*textureLod(envMap, uv, 0.0).xyz;
56
+ }
57
+ */
58
+ irr_glsl_BSDFIsotropicParams params;
35
59
for (int i = 0; i < 13; ++i)
36
60
{
37
61
SLight l = lights[i];
38
62
vec3 L = l.position-WorldPos;
39
63
params.L = L;
40
- color += irr_bsdf_cos_eval(params, out_interaction, dUV)*l.intensity*0.01 / dot(L,L);
64
+ #define INTENSITY 0.01 //ehh might want to render to hdr fbo and do tonemapping
65
+ color += irr_bsdf_cos_eval(params, out_interaction, dUV)*l.intensity*INTENSITY / dot(L,L);
41
66
}
67
+
42
68
return color+emissive;
43
69
}
44
70
)" ;
@@ -61,6 +87,33 @@ static core::smart_refctd_ptr<asset::ICPUSpecializedShader> createModifiedFragSh
61
87
return fsNew;
62
88
}
63
89
90
+ static auto createGPUImageView (const std::string& path, asset::IAssetManager* am, video::IVideoDriver* driver)
91
+ {
92
+ using namespace asset ;
93
+ using namespace video ;
94
+
95
+ IAssetLoader::SAssetLoadParams lp (0ull , nullptr , IAssetLoader::ECF_DONT_CACHE_REFERENCES);
96
+ auto cpuTexture = am->getAsset (path, lp);
97
+ auto cpuTextureContents = cpuTexture.getContents ();
98
+
99
+ auto asset = *cpuTextureContents.begin ();
100
+ auto cpuimg = core::smart_refctd_ptr_static_cast<asset::ICPUImage>(asset);
101
+
102
+ IGPUImageView::SCreationParams viewParams;
103
+ viewParams.flags = static_cast <IGPUImageView::E_CREATE_FLAGS>(0u );
104
+ viewParams.image = driver->getGPUObjectsFromAssets (&cpuimg.get (),&cpuimg.get ()+1u )->front ();
105
+ viewParams.format = viewParams.image ->getCreationParameters ().format ;
106
+ viewParams.viewType = IImageView<IGPUImage>::ET_2D;
107
+ viewParams.subresourceRange .baseArrayLayer = 0u ;
108
+ viewParams.subresourceRange .layerCount = 1u ;
109
+ viewParams.subresourceRange .baseMipLevel = 0u ;
110
+ viewParams.subresourceRange .levelCount = viewParams.image ->getCreationParameters ().mipLevels ;
111
+
112
+ auto gpuImageView = driver->createGPUImageView (std::move (viewParams));
113
+
114
+ return gpuImageView;
115
+ };
116
+
64
117
#include " irr/irrpack.h"
65
118
// std430-compatible
66
119
struct SLight
@@ -187,17 +240,27 @@ int main()
187
240
188
241
189
242
video::IVideoDriver* driver = device->getVideoDriver ();
243
+ asset::IAssetManager* am = device->getAssetManager ();
190
244
191
245
core::smart_refctd_ptr<asset::ICPUDescriptorSetLayout> ds2layout;
192
246
{
193
- asset::ICPUDescriptorSetLayout::SBinding bnd;
194
- bnd.binding = 0u ;
195
- bnd.count = 1u ;
196
- bnd.samplers = nullptr ;
197
- bnd.stageFlags = asset::ISpecializedShader::ESS_FRAGMENT;
198
- bnd.type = asset::EDT_STORAGE_BUFFER;
199
-
200
- ds2layout = core::make_smart_refctd_ptr<asset::ICPUDescriptorSetLayout>(&bnd, &bnd + 1 );
247
+ asset::ICPUDescriptorSetLayout::SBinding bnd[2 ];
248
+ bnd[0 ].binding = 0u ;
249
+ bnd[0 ].count = 1u ;
250
+ bnd[0 ].samplers = nullptr ;
251
+ bnd[0 ].stageFlags = asset::ISpecializedShader::ESS_FRAGMENT;
252
+ bnd[0 ].type = asset::EDT_STORAGE_BUFFER;
253
+
254
+ using namespace asset ;
255
+ ISampler::SParams samplerParams = { ISampler::ETC_CLAMP_TO_EDGE, ISampler::ETC_CLAMP_TO_EDGE, ISampler::ETC_CLAMP_TO_EDGE, ISampler::ETBC_FLOAT_OPAQUE_BLACK, ISampler::ETF_LINEAR, ISampler::ETF_LINEAR, ISampler::ESMM_LINEAR, 0u , false , ECO_ALWAYS };
256
+ auto smplr = core::make_smart_refctd_ptr<asset::ICPUSampler>(samplerParams);
257
+ bnd[1 ].binding = 1u ;
258
+ bnd[1 ].count = 1u ;
259
+ bnd[1 ].samplers = &smplr;
260
+ bnd[1 ].stageFlags = asset::ISpecializedShader::ESS_FRAGMENT;
261
+ bnd[1 ].type = asset::EDT_COMBINED_IMAGE_SAMPLER;
262
+
263
+ ds2layout = core::make_smart_refctd_ptr<asset::ICPUDescriptorSetLayout>(bnd, bnd + 2 );
201
264
}
202
265
203
266
// gather all meshes into core::vector and modify their pipelines
@@ -364,20 +427,31 @@ int main()
364
427
auto gpuds2layout = driver->getGPUObjectsFromAssets (&ds2layout.get (), &ds2layout.get ()+1 )->front ();
365
428
auto gpuds2 = driver->createGPUDescriptorSet (std::move (gpuds2layout));
366
429
{
367
- video::IGPUDescriptorSet::SDescriptorInfo info;
368
- video::IGPUDescriptorSet::SWriteDescriptorSet w;
369
- w.arrayElement = 0u ;
370
- w.binding = 0u ;
371
- w.count = 1u ;
372
- w.descriptorType = asset::EDT_STORAGE_BUFFER;
373
- w.dstSet = gpuds2.get ();
374
- w.info = & info;
430
+ video::IGPUDescriptorSet::SDescriptorInfo info[ 2 ] ;
431
+ video::IGPUDescriptorSet::SWriteDescriptorSet w[ 2 ] ;
432
+ w[ 0 ] .arrayElement = 0u ;
433
+ w[ 0 ] .binding = 0u ;
434
+ w[ 0 ] .count = 1u ;
435
+ w[ 0 ] .descriptorType = asset::EDT_STORAGE_BUFFER;
436
+ w[ 0 ] .dstSet = gpuds2.get ();
437
+ w[ 0 ] .info = info+ 0 ;
375
438
auto lightsBuf = driver->createFilledDeviceLocalGPUBufferOnDedMem (lights.size ()*sizeof (SLight), lights.data ());
376
- info.buffer .offset = 0u ;
377
- info.buffer .size = lightsBuf->getSize ();
378
- info.desc = std::move (lightsBuf);
379
-
380
- driver->updateDescriptorSets (1u , &w, 0u , nullptr );
439
+ info[0 ].buffer .offset = 0u ;
440
+ info[0 ].buffer .size = lightsBuf->getSize ();
441
+ info[0 ].desc = std::move (lightsBuf);
442
+
443
+ w[1 ].arrayElement = 0u ;
444
+ w[1 ].binding = 1u ;
445
+ w[1 ].count = 1u ;
446
+ w[1 ].descriptorType = asset::EDT_COMBINED_IMAGE_SAMPLER;
447
+ w[1 ].dstSet = gpuds2.get ();
448
+ w[1 ].info = info+1 ;
449
+ auto gpuEnvmapImageView = createGPUImageView (" ../../media/envmap/envmap_0.exr" , am, driver);
450
+ info[1 ].image .imageLayout = asset::EIL_UNDEFINED;
451
+ info[1 ].image .sampler = nullptr ;
452
+ info[1 ].desc = std::move (gpuEnvmapImageView);
453
+
454
+ driver->updateDescriptorSets (2u , w, 0u , nullptr );
381
455
}
382
456
383
457
// camera and viewport
@@ -399,7 +473,7 @@ int main()
399
473
viewport = core::recti (core::position2di (film.cropOffsetX ,film.cropOffsetY ), core::position2di (film.cropWidth ,film.cropHeight ));
400
474
401
475
auto extent = sceneBound.getExtent ();
402
- camera = smgr->addCameraSceneNodeFPS (nullptr ,100 .f ,core::min (extent.X ,extent.Y ,extent.Z )*0 .001f );
476
+ camera = smgr->addCameraSceneNodeFPS (nullptr ,100 .f ,core::min (extent.X ,extent.Y ,extent.Z )*0 .0001f );
403
477
// need to extract individual components
404
478
bool leftHandedCamera = false ;
405
479
{
0 commit comments