Skip to content

Commit 4847b1e

Browse files
chore: bind lights on ios
1 parent 497b5c1 commit 4847b1e

2 files changed

Lines changed: 71 additions & 12 deletions

File tree

ViroRenderer/VROMaterialSubstrateMetal.cpp

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,28 +460,64 @@ VROConcurrentBuffer &VROMaterialSubstrateMetal::bindMaterialUniforms(float opaci
460460
return *_materialUniformsBuffer;
461461
}
462462

463-
void VROMaterialSubstrateMetal::updateSortKey(VROSortKey &key) const {
463+
void VROMaterialSubstrateMetal::updateSortKey(VROSortKey &key, const std::vector<std::shared_ptr<VROLight>> &lights,
464+
const VRORenderContext &context,
465+
std::shared_ptr<VRODriver> driver) {
464466
key.shader = _program->getShaderId();
465467
key.textures = hashTextures(_textures);
466468
}
467469

470+
bool VROMaterialSubstrateMetal::bindShader(int lightsHash,
471+
const std::vector<std::shared_ptr<VROLight>> &lights,
472+
const VRORenderContext &context,
473+
std::shared_ptr<VRODriver> &driver) {
474+
// In Metal, pipeline state is bound by the geometry substrate, not the material substrate.
475+
// However, we need to bind the lighting uniforms here, similar to OpenGL.
476+
// This is the CRITICAL FIX: bindLights was defined but never called!
477+
NSLog(@"[METAL LIGHTING] bindShader() called with %zu lights, hash=%d", lights.size(), lightsHash);
478+
bindLights(lightsHash, lights, context, driver);
479+
NSLog(@"[METAL LIGHTING] bindLights() completed");
480+
return true;
481+
}
482+
483+
void VROMaterialSubstrateMetal::bindProperties(std::shared_ptr<VRODriver> &driver) {
484+
// In Metal, material properties are bound via bindMaterialUniforms in the geometry substrate
485+
// This is called from VROGeometrySubstrateMetal::renderMaterial
486+
}
487+
488+
void VROMaterialSubstrateMetal::bindGeometry(float opacity, const VROGeometry &geometry) {
489+
// In Metal, geometry-specific properties are handled in the geometry substrate
490+
}
491+
492+
void VROMaterialSubstrateMetal::bindView(VROMatrix4f modelMatrix, VROMatrix4f viewMatrix,
493+
VROMatrix4f projectionMatrix, VROMatrix4f normalMatrix,
494+
VROVector3f cameraPosition, VROEyeType eyeType,
495+
const VRORenderContext &context) {
496+
// In Metal, view uniforms are bound in VROGeometrySubstrateMetal::render
497+
}
498+
499+
void VROMaterialSubstrateMetal::updateTextures() {
500+
// Textures are managed through the _textures vector and updated when materials change
501+
}
502+
468503
void VROMaterialSubstrateMetal::bindShader() {
469-
// Do nothing in Metal, consider changing this to binding pipeline state?
470-
// The problem is that pipeline state in metal emcompasses both shader and
471-
// vertex layout
504+
// Legacy method kept for compatibility
505+
// The virtual bindShader(int lightsHash, ...) should be used instead
472506
}
473507

474508
void VROMaterialSubstrateMetal::bindLights(int lightsHash,
475509
const std::vector<std::shared_ptr<VROLight>> &lights,
476510
const VRORenderContext &context,
477511
std::shared_ptr<VRODriver> &driver) {
478-
512+
513+
NSLog(@"[METAL LIGHTING] bindLights() starting - received %zu lights", lights.size());
514+
479515
VRODriverMetal &metal = (VRODriverMetal &)(*driver.get());
480516
id <MTLRenderCommandEncoder> renderEncoder = metal.getRenderTarget()->getRenderEncoder();
481-
517+
482518
VROEyeType eyeType = context.getEyeType();
483519
int frame = context.getFrame();
484-
520+
485521
VROSceneLightingUniforms *uniforms = (VROSceneLightingUniforms *)_lightingUniformsBuffer->getWritableContents(eyeType,
486522
frame);
487523
uniforms->num_lights = 0;
@@ -508,13 +544,21 @@ void VROMaterialSubstrateMetal::bindLights(int lightsHash,
508544
}
509545

510546
uniforms->ambient_light_color = toVectorFloat3(ambientLight);
511-
547+
548+
NSLog(@"[METAL LIGHTING] Final values - num_lights=%d, ambient=(%f,%f,%f)",
549+
uniforms->num_lights,
550+
uniforms->ambient_light_color.x,
551+
uniforms->ambient_light_color.y,
552+
uniforms->ambient_light_color.z);
553+
512554
[renderEncoder setVertexBuffer:_lightingUniformsBuffer->getMTLBuffer(eyeType)
513555
offset:_lightingUniformsBuffer->getWriteOffset(frame)
514556
atIndex:4];
515557
[renderEncoder setFragmentBuffer:_lightingUniformsBuffer->getMTLBuffer(eyeType)
516558
offset:_lightingUniformsBuffer->getWriteOffset(frame)
517559
atIndex:4];
560+
561+
NSLog(@"[METAL LIGHTING] Lighting buffer bound at index 4 for vertex and fragment shaders");
518562
}
519563

520564
uint32_t VROMaterialSubstrateMetal::hashTextures(const std::vector<std::shared_ptr<VROTexture>> &textures) const {

ViroRenderer/VROMaterialSubstrateMetal.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,30 @@ class VROMaterialSubstrateMetal : public VROMaterialSubstrate {
5656
VROMaterialSubstrateMetal(const VROMaterial &material,
5757
VRODriverMetal &driver);
5858
virtual ~VROMaterialSubstrateMetal();
59-
59+
60+
// Override VROMaterialSubstrate virtual methods
61+
bool bindShader(int lightsHash,
62+
const std::vector<std::shared_ptr<VROLight>> &lights,
63+
const VRORenderContext &context,
64+
std::shared_ptr<VRODriver> &driver) override;
65+
void bindProperties(std::shared_ptr<VRODriver> &driver) override;
66+
void bindGeometry(float opacity, const VROGeometry &geometry) override;
67+
void bindView(VROMatrix4f modelMatrix, VROMatrix4f viewMatrix,
68+
VROMatrix4f projectionMatrix, VROMatrix4f normalMatrix,
69+
VROVector3f cameraPosition, VROEyeType eyeType,
70+
const VRORenderContext &context) override;
71+
void updateTextures() override;
72+
void updateSortKey(VROSortKey &key, const std::vector<std::shared_ptr<VROLight>> &lights,
73+
const VRORenderContext &context,
74+
std::shared_ptr<VRODriver> driver) override;
75+
76+
// Metal-specific methods
6077
void bindShader();
6178
void bindLights(int lightsHash,
6279
const std::vector<std::shared_ptr<VROLight>> &lights,
6380
const VRORenderContext &context,
6481
std::shared_ptr<VRODriver> &driver);
65-
82+
6683
/*
6784
Set the uniforms required to render this material, and return the buffer.
6885
*/
@@ -81,8 +98,6 @@ class VROMaterialSubstrateMetal : public VROMaterialSubstrate {
8198
VROConcurrentBuffer *getCustomUniformsBuffer() const {
8299
return _customUniformsBuffer;
83100
}
84-
85-
void updateSortKey(VROSortKey &key) const;
86101

87102
private:
88103
struct VROUniformLayout {

0 commit comments

Comments
 (0)