aboutsummaryrefslogtreecommitdiff
path: root/vrayPlug/plugin/shaveVrayPlugin.cpp
blob: 8f1de6548fc8b6dda58576ffb7599e52a610e8be (plain) (blame)
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
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962

/**********************************************************************
 *<
	FILE:			shaveVrayPlugin.cpp  

	DESCRIPTION:	VRay plugin

	CREATED BY:		Vladimir Dubovoy <[email protected]>

	HISTORY:		created  30-03-2010 

 *>
 **********************************************************************/

#include "shaveVrayPlugin.h"
#include "shaveVrayInstance.h"	//regular hair
#include "shaveVrayInstanceI.h" //instnanced hair
#include "shaveVraySharedFunctions.h"

#include <iostream>
#include <stdexcept>
#include <stdlib.h>

extern "C"
{
#include "shaveSDKFUNCS2.h"
}

#ifdef WIN32
FILE* alog = NULL;
#endif

VR::CharString   shaveVrayPlugin::draFile = VR::CharString();

const char* shaveVrayPlugin::version = "v1.0.18";

void shaveVrayPlugin::pause(int minSeconds, int maxSeconds)
{
	int range = maxSeconds - minSeconds + 1;
	int delayInSeconds = (int)((float)rand() / (((float)RAND_MAX) + 1.0f) * (float)(range+1)) + minSeconds;

#ifdef _WIN32
	Sleep(delayInSeconds * 1000);
#else
	sleep(delayInSeconds);
#endif
}


shaveVrayPlugin::shaveVrayPlugin(VR::VRayPluginDesc *desc)
	: VRayStaticGeomSource(desc) 
{
	LOGMSG("SHAVE","shaveVrayPlugin ctor");
	//LOGMSGS("SHAVE","version",version);
	fprintf(stdout,"SHAVE: Shave and A Haircut for V-Ray (%s)\n",version); 
	fprintf(stdout,"SHAVE: Shave core (%s)\n",SHAVE2query_version());
	fprintf(stdout,"SHAVE: (c) 2019 Epic Games\n"); 
	fflush(stdout);

	//LOGMSG("SHAVE","SHAVE2init");
	//SHAVE2init(); //crash on mac

	_instanced() = 0;
	_stackIndex()= 0;

	ctxs=NULL;

	paramList->setParamCache(VR30_CONST_STR_HACK("stackId"), &m_stackIndex);
	paramList->setParamCache(VR30_CONST_STR_HACK("instanced"), &m_instanced);
	paramList->setParamCache(VR30_CONST_STR_HACK("ownshader"), &m_ownshader);
	paramList->setParamCache(VR30_CONST_STR_HACK("squirrel"), &m_squirrel);
	paramList->setParamCache(VR30_CONST_STR_HACK("tipfade"), &m_tipfade);
	paramList->setParamCache(VR30_CONST_STR_HACK("spectint"), &m_spectint);
	paramList->setParamCache(VR30_CONST_STR_HACK("spectint2"), &m_spectint2);
	paramList->setParamCache(VR30_CONST_STR_HACK("draFile"), &draFile);
	paramList->setParamCache(VR30_CONST_STR_HACK("libPath"), &m_libPath);
	//instance uvs-related params
	paramList->setParamCache(VR30_CONST_STR_HACK("numFacesPerInst"), &m_numFacesPerInst);
	paramList->setParamCache(VR30_CONST_STR_HACK("numUVSets"), &m_numUVSets);
	//ray visibility params
	paramList->setParamCache(VR30_CONST_STR_HACK("cameraVisibility"), &m_cameraVisibility);
	paramList->setParamCache(VR30_CONST_STR_HACK("reflVisibility"), &m_reflVisibility);
	paramList->setParamCache(VR30_CONST_STR_HACK("refrVisibility"), &m_refrVisibility);
	paramList->setParamCache(VR30_CONST_STR_HACK("lightVisibility"), &m_lightVisibility);
	paramList->setParamCache(VR30_CONST_STR_HACK("GiVisibility"), &m_GiVisibility);
	paramList->setParamCache(VR30_CONST_STR_HACK("selfShadow"), &m_selfShadow);
	paramList->setParamCache(VR30_CONST_STR_HACK("recvShadow"), &m_recvShadow);
#if defined(VRAY30) || defined(VRAY40)
	paramList->setParamCache(VR30_CONST_STR_HACK("useGlobalHairTree"), &useGlobalHairTree);
	paramList->setParamCache(VR30_CONST_STR_HACK("dynamicHairTesselation"), &dynHairTessel);
	paramList->setParamCache(VR30_CONST_STR_HACK("hairTesselMaxEdgleLen"), &edgeLen);
#endif

	LOGMSG("SHAVE","shaveVrayPlugin ctor - OK");
}

shaveVrayPlugin::~shaveVrayPlugin() 
{
	LOGMSG("SHAVE","shaveVrayPlugin dtor");

	freeMem();
#ifdef WIN32
	assert(_CrtCheckMemory());
#endif
	LOGMSG("SHAVE","shaveVrayPlugin dtor - OK");
}

void shaveVrayPlugin::freeMem() 
{
	if (ctxs) 
	{
		for (int threadIdx=0; threadIdx < numThreads; threadIdx++)
			if (ctxs[threadIdx])
				ctxs[threadIdx]->releaseContext();
		delete[] ctxs;
		ctxs=NULL;
	}
}

void shaveVrayPlugin::createRayContextes(VR::VRayRenderer *vray) 
{
	numThreads = vray->getSequenceData().maxRenderThreads;

	ctxs = new VR::VRayContext*[numThreads];

	for (int threadIdx=0; threadIdx<numThreads; threadIdx++) 
	{
		ctxs[threadIdx] = ((vray->getThreadData(threadIdx))->newVRayContext());
		VR::VRayContext &rc = *(ctxs[threadIdx]);

		rc.clear();
		VR::TraceRay tray;
#if defined(VRAY30)
		tray.p=VR::TracePoint(0.0f, 0.0f, 0.0f);
		tray.dir=VR::Vector(0.0f, 0.0f, 1.0f);
		rc.setRay(0.0f, 0.0f, tray, VR::RT_CAMERA , 0.0f, 0.0f, LARGE_FLOAT);
#elif defined(VRAY40)
		tray.p=VR::simd::Vector3f(0.0f, 0.0f, 0.0f);
		tray.dir=VR::simd::Vector3f(0.0f, 0.0f, 1.0f);
		VR::ShadeCol multResult;
		multResult.makeWhite();
		rc.setRay(0.0f, 0.0f, tray, VR::RayFlags(VR::RT_CAMERA), 0.0f, 0.0f, LARGE_FLOAT);
#endif
		// rc.rayparams.computeGBuffer=false;

		VR::IntersectionData isData;
		isData.primitive=NULL;
		isData.skipTag=NULL;
		isData.faceIndex=0;
		isData.sb=NULL;
		isData.si=NULL;
		isData.sd=NULL;
		isData.surfaceProps=NULL;
		isData.volume=NULL;
		isData.wpointCoeff=1.0f;

#if defined(VRAY30) || defined(VRAY40)
		isData.bary=VR::ShadeVec(0.0f, 0.0f, 0.0f);
		isData.wpoint=VR::ShadeVec(0.0f, 0.0f, 0.0f);
		isData.faceBase=isData.faceEdge0=isData.faceEdge1=VR::ShadeVec(0.0f, 0.0f, 0.0f);
		isData.gnormal=isData.normal=VR::ShadeVec(0.0f, 0.0f, 1.0f);
#endif

		rc.setRayResult(true, &isData, 0);
#if defined(VRAY30) || defined(VRAY40)
		rc.rayresult.dPdx=VR::ShadeVec(0.0f, 0.0f, 0.0f);
		rc.rayresult.dPdy=VR::ShadeVec(0.0f, 0.0f, 0.0f);
#endif
	}
}

// true if SHAVE2init() has been called so that we know to call SHAVE2cleanup() when the plugin is unloaded.
int shaveInitCalled=false;

// Critical section for accessing shaveInitCalled in case we have multiple renders going on at the same time
VUtils::CriticalSection shaveInit_csect;

static bool isRenderStarted = false;
void shaveVrayPlugin::renderBegin(VR::VRayRenderer *vray)
{
	LOGMSG("SHAVE","shaveVrayPlugin::renderBegin");

	VR::ProgressCallback* progress = NULL;
	if (vray != NULL)
	{
		const VR::VRaySequenceData &sdata=vray->getSequenceData();
		progress = sdata.progress;
	}

	// Initialize Shave (just once, the first time we render)
	if (!shaveInitCalled) {
		VUtils::CriticalSectionRAII lock(shaveInit_csect);
		if (!shaveInitCalled) {
			SHAVE2init();
			shaveInitCalled=true;
		}
	}

	VRayStaticGeomSource::renderBegin(vray);

	LOGMSG("SHAVE","shaveVrayPlugin::renderBegin - OK");
}

void shaveVrayPlugin::renderEnd(VR::VRayRenderer *vray)
{
	LOGMSG("SHAVE","shaveVrayPlugin::renderEnd");

	UnLoadShaveBSDFPoolLib();

	VR::ProgressCallback* progress = NULL;
	if (vray != NULL)
	{
		const VR::VRaySequenceData &sdata=vray->getSequenceData();
		progress = sdata.progress;
	}

	VRayStaticGeomSource::renderEnd(vray);

	LOGMSG("SHAVE","shaveVrayPlugin::renderEnd - OK");
}

void shaveVrayPlugin::frameBegin(VR::VRayRenderer *vray) 
{
	LOGMSG("SHAVE","shaveVrayPlugin::frameBegin");

	VRayStaticGeomSource::frameBegin(vray);

	//this->vray = vray;
	//double time=vray->getFrameData().t;

	//meshInfoInterface = (MeshInfoInterface*)GET_INTERFACE(meshPlugin, EXT_MESH_INFO);

	////getIntListData(faceList, paramList->getParam("faceList"), time);
	////getFloatListData(areaList, paramList->getParam("areaList"), time);

	createRayContextes(vray);

	////we need to export DRA or use dra exported in shaves ::compute ?

	LOGMSG("SHAVE","shaveVrayPlugin::frameBegin - OK");
}

void shaveVrayPlugin::frameEnd(VR::VRayRenderer *vray) 
{
	LOGMSG("SHAVE","shaveVrayPlugin::frameEnd");

	freeMem();

	LOGMSG("SHAVE","shaveVrayPlugin::frameEnd - OK");
}

VR::VRayStaticGeometry* shaveVrayPlugin::newInstance(
								VR::MaterialInterface *mtl, 
								VR::BSDFInterface *bsdf, int renderID, 
								VR::VolumetricInterface *volume, VR::LightList *lightList,
#ifdef VRAY40
								const VR::Transform &baseTM, 
#else
								const VR::TraceTransform &baseTM, 
#endif
								int objectID, const tchar *userAttributes, int primaryVisibility) {

	LOGMSG("SHAVE","shaveVrayPlugin::newInstance");

	LoadShaveBSDFPoolLib(m_libPath.ptr());

	if(instanced() == 1)
	{
		return new shaveVrayInstanceI(this, mtl, bsdf, renderID, volume, lightList, baseTM, objectID, 
			userAttributes, m_cameraVisibility);
	}
	else
	{
		//we gonna use own bsdf
		return new shaveVrayInstance(this, mtl, bsdf, renderID, volume, lightList, baseTM, objectID, 
			userAttributes, m_cameraVisibility);
	}
}

void shaveVrayPlugin::deleteInstance(VR::VRayStaticGeometry *instance) 
{
	LOGMSG("SHAVE","shaveVrayPlugin::deleteInstance");

	delete (shaveVrayInstance*)instance;
}

VR::VRayContext* shaveVrayPlugin::getVRayContext() 
{
	VR::ThreadManager *threadMan = (const_cast<VR::VRaySequenceData&>(vray->getSequenceData())).threadManager;
	if (threadMan) {
		int tIdx = threadMan->getThreadIndex();	// This didn't seem to work in my tests, returned -1
		if (tIdx==-1)
			return ctxs[0];
		else 
			return ctxs[tIdx];
	}
	return ctxs[0];
}