-
Notifications
You must be signed in to change notification settings - Fork 60
/
AssetDraw2.C
1431 lines (1207 loc) · 34.2 KB
/
AssetDraw2.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <maya/MTypes.h>
#if MAYA_API_VERSION >= 20180000
#include <maya/MString.h>
#include <maya/MFileObject.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MVector.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MColor.h>
#include <maya/M3dView.h>
#include <maya/MModelMessage.h>
#include <maya/MSelectionList.h>
#include <maya/MFnMessageAttribute.h>
#define MNoVersionString
#define MNoPluginEntry
#include <maya/MFnPlugin.h>
#include <maya/MDistance.h>
#include <maya/MFnUnitAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MGlobal.h>
#include <maya/MEvaluationNode.h>
// Viewport 2.0 includes
#include <maya/MDrawRegistry.h>
#include <maya/MUserData.h>
#include <maya/MDrawContext.h>
#include <maya/MShaderManager.h>
#include <maya/MHWGeometry.h>
#include <maya/MHWGeometryUtilities.h>
#include <maya/MPointArray.h>
#include <unordered_map>
#include <signal.h>
#include <limits>
#include "MayaTypeID.h"
#include "AssetNode.h"
#include "AssetDraw2.h"
#include "hapiutil.h"
static std::unordered_map<MShaderInstance*,AssetDrawGeometryOverride*>
theShaderToOverrideMap;
// Ctor of AssetDrawTraits
AssetDrawTraits::AssetDrawTraits()
{
}
namespace
{
// Viewport 2.0 specific data
//
const MString colorParameterName_ = "solidColor";
const MString wireframeItemName_ = "houdiniDrawWires";
const MString shadedItemName_ = "houdiniDrawTriangles";
// Maintain a mini cache for 3d solid shaders in order to reuse the shader
// instance whenever possible. This can allow Viewport 2.0 optimization e.g.
// the GPU instancing system and the consolidation system to be leveraged.
//
struct MColorHash
{
std::size_t
operator()(const MColor& color) const
{
std::size_t seed = 0;
CombineHashCode(seed, color.r);
CombineHashCode(seed, color.g);
CombineHashCode(seed, color.b);
CombineHashCode(seed, color.a);
return seed;
}
void
CombineHashCode(std::size_t& seed, float v) const
{
std::hash<float> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
};
std::unordered_map<MColor, MHWRender::MShaderInstance*, MColorHash> the3dSolidShaders;
std::unordered_map<MColor, MHWRender::MShaderInstance*, MColorHash> the3dMaterialShaders;
#if 1
void
printTechniques(const MStringArray& techniqueNames)
{
for( size_t i=0; i<techniqueNames.length(); ++i)
{
const MString &s = techniqueNames[i];
printf("technique[%d] = %s\n", (int)i, s.asChar());
}
}
void
printShaderParameters(MHWRender::MShaderInstance *test)
{
const char * parameterTypes[] {
"kInvalid",
"kBoolean",
"kInteger",
"kFloat",
"kFloat2",
"kFloat3",
"kFloat4",
"kFloat4x4Row",
"kFloat4x4Col",
"kTexture1",
"kTexture2",
"kTexture3",
"kTextureCube",
"kSampler"
};
MStringArray l;
test->parameterList(l);
for (unsigned int i=0; i<l.length(); ++i)
{
int pt = (int)test->parameterType(l[i]);
printf("%s %s\n",parameterTypes[pt],l[i].asChar());
}
}
#endif
MHWRender::MShaderInstance*
get3dSolidShader(const MColor& color, bool &newshader)
{
// Return the shader instance if exists.
auto it = the3dSolidShaders.find(color);
if (it != the3dSolidShaders.end())
return it->second;
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
if (!renderer)
return nullptr;
const MHWRender::MShaderManager* shaderMgr = renderer->getShaderManager();
if (!shaderMgr)
return nullptr;
MHWRender::MShaderInstance* shader = shaderMgr->getStockShader(
MHWRender::MShaderManager::k3dSolidShader);
if (!shader)
return nullptr;
float solidColor[] = { color.r, color.g, color.b, 1.0f };
shader->setParameter(colorParameterName_, solidColor);
the3dSolidShaders[color] = shader;
newshader = true;
return shader;
}
MHWRender::MShaderInstance*
get3dDefaultMaterialShader(const MColor& color, bool &newshader)
{
// Return the shader instance if exists.
auto it = the3dMaterialShaders.find(color);
if (it != the3dMaterialShaders.end())
return it->second;
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
if (!renderer)
return nullptr;
const MHWRender::MShaderManager* shaderMgr = renderer->getShaderManager();
if (!shaderMgr)
return nullptr;
MHWRender::MShaderInstance* shader = shaderMgr->getStockShader(
MHWRender::MShaderManager::k3dDefaultMaterialShader);
if (!shader)
return nullptr;
//printShaderParameters(shader);
//kFloat4 diffuseColor
float diffuseColor[] = { 0.7, 0.7, 0.7, 1.0f };
shader->setParameter("diffuseColor", diffuseColor);
the3dMaterialShaders[color] = shader;
newshader = true;
return shader;
}
MStatus
releaseShaders()
{
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
if (!renderer)
return MS::kFailure;
const MHWRender::MShaderManager* shaderMgr = renderer->getShaderManager();
if (!shaderMgr)
return MS::kFailure;
for (auto it = the3dSolidShaders.begin(); it != the3dSolidShaders.end(); it++)
shaderMgr->releaseShader(it->second);
for (auto it = the3dMaterialShaders.begin(); it != the3dMaterialShaders.end(); it++)
shaderMgr->releaseShader(it->second);
the3dSolidShaders.clear();
the3dMaterialShaders.clear();
return MS::kSuccess;
}
} // anonymous namespace
MTypeId AssetDraw::id( MayaTypeID_HoudiniAssetDraw );
MString AssetDraw::drawDbClassification("drawdb/geometry/houdiniDraw");
MString AssetDraw::drawRegistrantId("houdiniDrawPlugin");
AssetDraw::AssetDraw()
: myOutputDeform(/*topo=*/true,/*normal=*/true, /*skippoints=*/true, /*uvs=*/true)
{
}
AssetDraw::~AssetDraw()
{
}
MStatus
AssetDraw::compute( const MPlug& plug, MDataBlock& data )
{
AssetDrawTraits &traits = AssetDraw::theTraits;
MString plugName = plug.name();
if (plug.attribute() == traits.output)
{
// Pull on the output plug ourself to trigger cooking
// Store a pointer to the output for access during drawing
MPlug inPlug(thisMObject(), traits.inputNodeId);
int i=0;
inPlug.getValue(i);
data.setClean(plug);
MPlugArray plugs;
if (!inPlug.connectedTo(plugs,/*asDst=*/true,/*asSrc=*/false))
return MS::kFailure;
AssetNode *pAssetNode = dynamic_cast<AssetNode*>(
MFnDependencyNode(plugs[0].node()).userNode());
if (!pAssetNode)
return MS::kFailure;
Asset *a = pAssetNode->getAsset();
if (!a || !a->isValid())
return MS::kFailure;
OutputObject *obj = a->getOutputObject(0);
size_t n = 0;
if (!myOutputDeform.compute(obj,n))
return MS::kFailure;
return MS::kSuccess;
}
return MS::kUnknownParameter;
}
bool
AssetDraw::isBounded() const
{
return false;
}
MBoundingBox
AssetDraw::boundingBox() const
{
return MPxLocatorNode::boundingBox();
}
MSelectionMask
AssetDraw::getShapeSelectionMask() const
{
return MSelectionMask("houdiniDrawSelection");
}
// Called before this node is evaluated by Evaluation Manager
MStatus
AssetDraw::preEvaluation( const MDGContext& context,
const MEvaluationNode& evaluationNode)
{
AssetDrawTraits &traits = AssetDraw::theTraits;
if (context.isNormal())
{
MStatus status;
if( (evaluationNode.dirtyPlugExists(traits.inputNodeId, &status) && status) ||
(evaluationNode.dirtyPlugExists(traits.output, &status) && status) )
{
MHWRender::MRenderer::setGeometryDrawDirty(thisMObject());
}
}
return MStatus::kSuccess;
}
void*
AssetDraw::creator()
{
return new AssetDraw();
}
// Viewport 2.0 override implementation
AssetDrawGeometryOverride::AssetDrawGeometryOverride(const MObject& obj)
: MHWRender::MPxGeometryOverride(obj)
, mLocatorNode(obj)
, myOutput(nullptr)
, myShaderInstance(nullptr)
, myLightCount(0)
{
}
AssetDrawGeometryOverride::~AssetDrawGeometryOverride()
{
releaseShader();
}
void
AssetDrawGeometryOverride::releaseShader()
{
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
if (!renderer)
return;
const MHWRender::MShaderManager* shaderMgr = renderer->getShaderManager();
if (!shaderMgr)
return;
const MHWRender::MTextureManager* textureMgr = renderer->getTextureManager();
if (!textureMgr)
return;
// Release the existing shader
if (myShaderInstance)
{
shaderMgr->releaseShader(myShaderInstance);
theShaderToOverrideMap.erase(myShaderInstance);
}
myShaderInstance = nullptr;
myShaderNode = MObject();
myShaderFile = MString();
for (size_t i=0; i<myTextures.size(); ++i)
{
if (myTextures[i])
textureMgr->releaseTexture(myTextures[i]);
}
myTextures.clear();
myTextureParms.clear();
myLightCount = 0;
}
const static MString kLightColorF = "Light%dColor";
const static MString kLightTypeF = "Light%dType";
const static MString kLightIntensityF = "Light%dIntensity";
const static MString kLightPosF = "Light%dPos";
const static MString kLightDirF = "Light%dDir";
struct LightNames
{
MString kLightColor;
MString kLightType;
MString kLightIntensity;
MString kLightPos;
MString kLightDir;
};
enum ELightType
{
eInvalidLight,
eUndefinedLight,
eSpotLight,
ePointLight,
eDirectionalLight,
eAmbientLight,
eVolumeLight,
eAreaLight,
eDefaultLight,
eLightCount
};
// Convert Maya light type to glslShader light type
ELightType
getLightType(const MHWRender::MLightParameterInformation* lightParam)
{
ELightType type = eUndefinedLight;
MString lightType = lightParam->lightType();
// The 3rd letter of the light name is a perfect hash,
// so let's cut on the number of string comparisons.
if (lightType.length() <= 2)
return type;
char c2 = lightType.asChar()[2];
switch (c2)
{
case 'o':
//if (STRICMP(lightType.asChar(),"spotLight") == 0)
type = eSpotLight;
break;
case 'r':
//if (STRICMP(lightType.asChar(),"directionalLight") == 0)
{
// The headlamp used in the "Use default lighting" mode
// does not have the same set of attributes as a regular
// directional light, so we must disambiguate them
// otherwise we might not know how to fetch shadow data
// from the regular kind.
if (lightParam->lightPath().isValid())
type = eDirectionalLight;
else
type = eDefaultLight;
}
break;
case 'i':
//if (STRICMP(lightType.asChar(),"pointLight") == 0)
type = ePointLight;
break;
case 'b':
//if (STRICMP(lightType.asChar(),"ambientLight") == 0)
type = eAmbientLight;
break;
case 'l':
//if (STRICMP(lightType.asChar(),"volumeLight") == 0)
type = eVolumeLight;
break;
case 'e':
//if (STRICMP(lightType.asChar(),"areaLight") == 0)
type = eAreaLight;
break;
}
return type;
}
void
AssetDraw2PreCallback(MDrawContext& ctx,
const MRenderItemList& renderItemList, MShaderInstance *sh)
{
AssetDrawGeometryOverride *drawOverride = theShaderToOverrideMap[sh];
drawOverride->preDrawCallback(ctx,renderItemList,sh);
}
void
AssetDrawGeometryOverride::preDrawCallback(MDrawContext& ctx,
const MRenderItemList& renderItemList, MShaderInstance *sh)
{
sh->bind(ctx);
MStatus status;
//Prebuild a list of shader parameter names for each light index
static LightNames names[32];
static bool initNames = true;
if (initNames)
{
char buf[128];
for( int i=0; i<32; ++i)
{
LightNames &namei = names[i];
#define INIT_NAME(X) \
sprintf(buf, X ## F.asChar(), i); \
namei.X.set(buf)
INIT_NAME(kLightColor);
INIT_NAME(kLightType);
INIT_NAME(kLightIntensity);
INIT_NAME(kLightPos);
INIT_NAME(kLightDir);
}
initNames=false;
}
// the set parameter calls somehow hold the pointers
// so we'll use a buffer to hold the values until
// updateParameters is called.
float zero4[4] = {0.f,0.f,0.f,0.f};
// Run over the active lights
size_t n = ctx.numberOfActiveLights();
if (n>myLightCount)
n = myLightCount;
for (size_t i=0; i<n; ++i)
{
MLightParameterInformation* li = ctx.getLightParameterInformation(i);
if (!li)
continue;
MStringArray parms;
li->parameterList(parms);
for (size_t j=0; j<parms.length(); ++j)
{
const MString &p = parms[j];
printf("Light%d%s\n",(int)i,p.asChar());
}
MIntArray ivals;
MFloatArray fvals;
MFloatVector vec;
LightNames &namei = names[i];
bool enabled = true;
{
status = li->getParameter(
MLightParameterInformation::kLightEnabled,ivals);
if (status==MS::kSuccess)
enabled = ivals[0];
}
if (!enabled)
{
sh->setParameter(namei.kLightColor, zero4);
sh->setParameter(namei.kLightIntensity, 0.f);
sh->setParameter(namei.kLightType, 0);//not sure of this value
continue;
}
ELightType lightType = getLightType(li);
printf("%s->%d\n",li->lightType().asChar(), (int)lightType);
sh->setParameter(namei.kLightType, (int)lightType);
status = li->getParameter(
MLightParameterInformation::kWorldPosition,fvals);
if (status==MS::kSuccess)
{
sh->setParameter(namei.kLightPos,&fvals[0]);
}
status = li->getParameter(
MLightParameterInformation::kWorldDirection,fvals);
if (status==MS::kSuccess)
{
sh->setParameter(namei.kLightDir,&fvals[0]);
}
status = li->getParameter(
MLightParameterInformation::kIntensity,fvals);
if (status==MS::kSuccess)
sh->setParameter(namei.kLightIntensity,fvals[0]);
status = li->getParameter(
MLightParameterInformation::kColor,fvals);
if (status==MS::kSuccess)
{
//printf("setParm%d %s %f %f %f\n", (int)i, namei.kLightColor.asChar(), fvals[0], fvals[1], fvals[2]);
sh->setParameter(namei.kLightColor,&fvals[0]);
}
}
// Disable all the remaining lights
for (size_t i=n; i<myLightCount; ++i)
{
LightNames &namei = names[i];
sh->setParameter(namei.kLightColor, zero4);
sh->setParameter(namei.kLightIntensity, 0.f);
sh->setParameter(namei.kLightType, 0);//not sure of this value
}
// Force texture parms, to avoid flickering
/*for (size_t i=0; i<myTextures.size(); ++i)
{
MHWRender::MTexture *texture = myTextures[i];
const MString &pname = myTextureParms[i];
// Set the shader parameter
MHWRender::MTextureAssignment assign;
assign.texture = texture;
sh->setParameter(pname,assign);
}*/
sh->updateParameters(ctx);
sh->unbind(ctx);
}
MHWRender::MShaderInstance*
AssetDrawGeometryOverride::getShader(const MDagPath& path, bool &newshader)
{
MStatus status;
AssetDrawTraits &traits = AssetDraw::theTraits;
MPlug shaderPlug(path.node(),traits.shader);
MPlug shaderFilePlug(path.node(),traits.shaderfile);
MObject shaderNode = shaderPlug.source().node();
MString shaderFile = shaderFilePlug.asString();
if (myShaderNode==shaderNode && myShaderFile==shaderFile)
return myShaderInstance;
releaseShader();
if (shaderNode.isNull())
return nullptr;
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
if (!renderer)
return nullptr;
const MHWRender::MShaderManager* shaderMgr = renderer->getShaderManager();
if (!shaderMgr)
return nullptr;
MHWRender::MTextureManager* textureMgr = renderer->getTextureManager();
if (!textureMgr)
return nullptr;
MFnDependencyNode shaderDep(shaderNode);
CHECK_MSTATUS(status);
MHWRender::MShaderInstance* shader = nullptr;
size_t lightCount = 0;
// Try building a shader from the file attribute
if (shaderFile.length()>0)
{
MFileObject shaderFileObject;
shaderFileObject.setRawFullName(shaderFile);
MString folder = shaderFileObject.resolvedPath();
printf("folder=%s\n", folder.asChar());
MHWRender::MShaderCompileMacro *macros = nullptr;
const unsigned int numMacros = 0;
bool useEffectCache = false;
MShaderInstance::DrawCallback preCb = AssetDraw2PreCallback;
MStringArray techniques;
shaderMgr->getEffectsTechniques(shaderFile, techniques,
macros, numMacros, useEffectCache);
printTechniques(techniques);
std::unordered_map<std::string,size_t> prefixes;
for (size_t i=0; i<32; ++i)
{
std::string key;
key = "Light";
key += std::to_string(i);
prefixes[key] = i;
key = "light";
key += std::to_string(i);
prefixes[key] = i;
}
if (techniques.length()>0)
{
const MString &technique = techniques[0];
shader = shaderMgr->getEffectsFileShader(shaderFile,technique,
macros, numMacros, useEffectCache, preCb);
// set textures
if (shader)
{
MStringArray l;
shader->parameterList(l);
// count the number of lights by looking at the parameter
// names and light index
for (unsigned int i=0; i<l.length(); ++i)
{
size_t lightIndex = 0;
const MString & pname = l[i];
std::string s = pname.asChar();
std::string key;
std::unordered_map<std::string,size_t>::const_iterator it;
key = s.substr(0,7);
it = prefixes.find(key);
if (it==prefixes.end())
{
key = s.substr(0,6);
it = prefixes.find(key);
}
if (it==prefixes.end())
continue;
lightIndex = it->second;
if (lightIndex+1>lightCount)
lightCount = lightIndex+1;
}
printf("lightCount -> %d\n", (int)lightCount);
// Set parameters
for (unsigned int i=0; i<l.length(); ++i)
{
// Support only 2D textures
const MString & pname = l[i];
MShaderInstance::ParameterType pt = shader->parameterType(pname);
if (pt!=MShaderInstance::kTexture2)
continue;
// Heuristic to get a full filename from the resource name
// We might want to expose a texture folder on AssetDraw
// as a string parameter.
// I could only get the name of the file without the folder
// from the shader file.
MString resourceName = shader->resourceName(pname,status);
printf("texture %s -> %s\n",
pname.asChar(), resourceName.asChar());
if (resourceName.length()==0)
continue;
// resolve the texture path
MString texturePath;
{
MFileObject textureFileObject;
textureFileObject.setRawPath(folder);
textureFileObject.setName(resourceName);
texturePath = textureFileObject.resolvedFullName();
}
// Get a texture
// TODO add mipmap control
MHWRender::MTextureArguments args(texturePath);
MHWRender::MTexture *texture =
textureMgr->acquireTexture(args);
if (!texture)
continue;
// Set the shader parameter
MHWRender::MTextureAssignment assign;
assign.texture = texture;
shader->setParameter(pname,assign);
// TODO use shared textures if the paths are the same
myTextures.push_back(texture);
myTextureParms.push_back(pname);
}
}
}
}
// Fallback to the connected shader
if (!shader)
shader = shaderMgr->getShaderFromNode(shaderNode, path);
// No shader at this point is a failure.
if (!shader)
return nullptr;
// Debug shader parameters
printShaderParameters(shader);
newshader = true;
myShaderInstance = shader;
theShaderToOverrideMap[myShaderInstance] = this;
myShaderNode = shaderNode;
myShaderFile = shaderFile;
myLightCount = lightCount;
return shader;
}
MHWRender::DrawAPI
AssetDrawGeometryOverride::supportedDrawAPIs() const
{
// this plugin supports both GL and DX
return (MHWRender::kOpenGL | MHWRender::kDirectX11 | MHWRender::kOpenGLCoreProfile);
}
void
AssetDrawGeometryOverride::updateDG()
{
myOutput = nullptr;
AssetDrawTraits &traits = AssetDraw::theTraits;
// Pull on the output plug ourself to trigger cooking
// Store a pointer to the output for access during drawing
MPlug plug(mLocatorNode, traits.output);
int i=0;
plug.getValue(i);
MString plugName = plug.name();
AssetDraw *pAssetDraw2 = dynamic_cast<AssetDraw*>(
MFnDependencyNode(mLocatorNode).userNode());
if (!pAssetDraw2)
return;
myOutput = &pAssetDraw2->myOutputDeform;
}
bool
AssetDrawGeometryOverride::isIndexingDirty(const MHWRender::MRenderItem &item)
{
return myOutput ? myOutput->myTopoDirty : false;
}
bool
AssetDrawGeometryOverride::isStreamDirty(const MHWRender::MVertexBufferDescriptor &desc)
{
return myOutput ? myOutput->myPos.myDirty||myOutput->myTopoDirty : false;
}
void
AssetDrawGeometryOverride::updateRenderItems( const MDagPath& path,
MHWRender::MRenderItemList& list )
{
if (!myOutput)
return;
bool newshader=false;
MColor color = MHWRender::MGeometryUtilities::wireframeColor(path);
MHWRender::MShaderInstance* shader0 = get3dSolidShader(color,newshader);
if (!shader0)
return;
MHWRender::MShaderInstance* shader1 = get3dDefaultMaterialShader(color,newshader);
if (!shader1)
return;
MHWRender::MShaderInstance* shaderSG = getShader(path,newshader);
if (shaderSG)
shader1 = shaderSG;
if (newshader)
myOutput->myTopoDirty = true;
unsigned int depthPriority;
switch (MHWRender::MGeometryUtilities::displayStatus(path))
{
case MHWRender::kLead:
case MHWRender::kActive:
case MHWRender::kHilite:
case MHWRender::kActiveComponent:
depthPriority = MHWRender::MRenderItem::sActiveWireDepthPriority;
break;
default:
depthPriority = MHWRender::MRenderItem::sDormantFilledDepthPriority;
break;
}
MHWRender::MRenderItem* wireframeItem = NULL;
int index = 0;
index = list.indexOf(wireframeItemName_);
if (index < 0)
{
wireframeItem = MHWRender::MRenderItem::Create(
wireframeItemName_,
MHWRender::MRenderItem::DecorationItem,
MHWRender::MGeometry::kLines);
wireframeItem->setDrawMode(MHWRender::MGeometry::kWireframe);
list.append(wireframeItem);
}
else
{
wireframeItem = list.itemAt(index);
}
if(wireframeItem)
{
wireframeItem->setShader(shader0);
wireframeItem->depthPriority(depthPriority);
wireframeItem->enable(true);
}
MHWRender::MRenderItem* shadedItem = NULL;
index = list.indexOf(shadedItemName_);
if (index < 0)
{
shadedItem = MHWRender::MRenderItem::Create(
shadedItemName_,
MHWRender::MRenderItem::MaterialSceneItem,
MHWRender::MGeometry::kTriangles);
shadedItem->setDrawMode((MHWRender::MGeometry::DrawMode)
(MHWRender::MGeometry::kShaded | MHWRender::MGeometry::kTextured));
list.append(shadedItem);
}
else
{
shadedItem = list.itemAt(index);
}
if(shadedItem)
{
shadedItem->setShader(shader1);
shadedItem->depthPriority(depthPriority);
shadedItem->enable(true);
}
}
void
AssetDrawGeometryOverride::populateGeometry(
const MHWRender::MGeometryRequirements& requirements,
const MHWRender::MRenderItemList& renderItems,
MHWRender::MGeometry& data)
{
if (!myOutput)
return;
MHWRender::MVertexBuffer* verticesBuffer = NULL;
MHWRender::MVertexBuffer* normalsBuffer = NULL;
MHWRender::MVertexBuffer* texturesBuffer = NULL;
MHWRender::MVertexBuffer* tangentsBuffer = NULL;
MHWRender::MVertexBuffer* bitangentsBuffer = NULL;
float* vertices = NULL;
float* normals = NULL;
float* textures = NULL;
float* tangents = NULL;
float* bitangents = NULL;
const MHWRender::MVertexBufferDescriptorList& vertexBufferDescriptorList =
requirements.vertexRequirements();
const int numberOfVertexRequirments = vertexBufferDescriptorList.length();
const HAPI_PartInfo &info = myOutput->myPartInfo;
int pointCount = myOutput->myPos.myIsValid ? info.pointCount : 0;
MHWRender::MVertexBufferDescriptor vertexBufferDescriptor;
for (int requirementNumber = 0;
requirementNumber < numberOfVertexRequirments; ++requirementNumber)
{
if (!vertexBufferDescriptorList.getDescriptor(
requirementNumber, vertexBufferDescriptor))
continue;
switch (vertexBufferDescriptor.semantic())
{
case MHWRender::MGeometry::kPosition:
{
if (!verticesBuffer)
{
verticesBuffer = data.createVertexBuffer(vertexBufferDescriptor);
if (verticesBuffer)
vertices = (float*)verticesBuffer->acquire(pointCount,/*writeOnly=*/true);
}
break;
}
case MHWRender::MGeometry::kNormal:
{
if (!normalsBuffer)
{
normalsBuffer = data.createVertexBuffer(vertexBufferDescriptor);
if (normalsBuffer)
normals = (float*)normalsBuffer->acquire(pointCount,/*writeOnly=*/true);
}
break;
}
case MHWRender::MGeometry::kTexture:
{
if (!texturesBuffer && myOutput->myTopoDirty)
{
texturesBuffer = data.createVertexBuffer(vertexBufferDescriptor);
if (texturesBuffer)
textures = (float*)texturesBuffer->acquire(pointCount,/*writeOnly=*/true);
}
break;
}
case MHWRender::MGeometry::kTangent:
{
if (!tangentsBuffer && myOutput->myTopoDirty)
{
tangentsBuffer = data.createVertexBuffer(vertexBufferDescriptor);
if (tangentsBuffer)
tangents = (float*)tangentsBuffer->acquire(pointCount,/*writeOnly=*/true);
}
break;
}
case MHWRender::MGeometry::kBitangent:
{
if (!bitangentsBuffer && myOutput->myTopoDirty)
{
bitangentsBuffer = data.createVertexBuffer(vertexBufferDescriptor);
if (bitangentsBuffer)