Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddRef() - Fix for compailation failed case. #1989

Merged
merged 6 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
306 changes: 204 additions & 102 deletions examples/pxScene2d/src/pxShaderResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ pxShaderResource::~pxShaderResource()

void pxShaderResource::onInit()
{
postlink();
if(mProgram != -1)
{
postlink();
}

if( mInitialized)
return;
Expand Down Expand Up @@ -94,18 +97,31 @@ void pxShaderResource::setupResource()

const char* vtxCode = mVertexSrc.length() > 0 ? (const char*) mVertexSrc.data() : vShaderText; // or use "default" Vertex shader

// COMPILE SHADER PROGRAM
// COMPILE SHADER PROGRAM
// COMPILE SHADER PROGRAM

if(shaderProgram::initShader( vtxCode, (const char*) mFragmentSrc.data() ) != RT_OK)
{
rtLogError("FATAL: Shader error: %s \n", mCompilation.cString() );

setLoadStatus("glError", mCompilation.cString() );
setLoadStatus("statusCode",PX_RESOURCE_STATUS_DECODE_FAILURE);

gUIThreadQueue->addTask(onDownloadCanceledUI, this, (void*)"reject");
if (gUIThreadQueue)
{
AddRef(); // async
gUIThreadQueue->addTask(onDownloadCanceledUI, this, (void*)"reject");
}

rtValue nullValue;
mReady.send("reject", nullValue );
mReady.send("reject", this );
}

// COMPILE SHADER PROGRAM
// COMPILE SHADER PROGRAM
// COMPILE SHADER PROGRAM


double stopDecodeTime = pxMilliseconds();
setLoadStatus("decodeTimeMs", static_cast<int>(stopDecodeTime-startDecodeTime));
}
Expand Down Expand Up @@ -324,111 +340,109 @@ rtError pxShaderResource::setUniforms(rtObjectRef o)
return RT_OK;
}

void pxShaderResource::loadResourceFromFile()
rtError pxShaderResource::loadShaderSource(rtString url, rtData &source)
{
init();

rtError loadFrgShader = RT_FAIL;

if(mVertexUrl.beginsWith("data:text/plain,"))
if (url.length() == 0)
{
mVertexSrc.init( (const uint8_t* ) mVertexUrl.substring(16).cString(),
mVertexUrl.byteLength() - 16);
// Bad URL...
rtLogError("Bad URL for SHADER: zero length. ");
return RT_FAIL;
}
if(mFragmentUrl.beginsWith("data:text/plain,"))

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Remove (optional) FILE:// protocol prefix...
//
bool isFILE = ( url.beginsWith("file://") || url.beginsWith("FILE://") );
if (isFILE == false && url.beginsWith("/") == false)
{
mFragmentSrc.init( (const uint8_t* ) mFragmentUrl.substring(16).cString(),
mFragmentUrl.byteLength() - 16);
rtLogError("SHADER url is NOT a local file.");
return RT_FAIL;
}


rtString path = url;

if(isFILE)
{
path.init( (const char* ) url.substring(7).cString(),
url.byteLength() - 7);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Load the Shader source code...
//
if (rtLoadFile( path, source) == RT_OK)
return RT_OK;

if (rtIsPathAbsolute(path))
return RT_OK;;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
do
//
// Resolve path & Load the Shader source code...
//
rtModuleDirs *dirs = rtModuleDirs::instance();

for (rtModuleDirs::iter it = dirs->iterator(); it.first != it.second; it.first++)
{
if (mFragmentSrc.length() != 0)
{
// We have FRAGMENT SHADER source already...
loadFrgShader = RT_OK;
break;
}

bool isFrgFILE = ( mFragmentUrl.beginsWith("file://") || mFragmentUrl.beginsWith("FILE://") );
rtString pathFrg = mFragmentUrl;

if(isFrgFILE)
if (rtLoadFile(rtConcatenatePath(*it.first, path.cString()).c_str(), source) == RT_OK)
{
pathFrg.init( (const char* ) mFragmentUrl.substring(7).cString(),
mFragmentUrl.byteLength() - 7);
return RT_OK;;
}

loadFrgShader = rtLoadFile( pathFrg, mFragmentSrc);
if (loadFrgShader == RT_OK)
break;

if (rtIsPathAbsolute(pathFrg))
break;

rtModuleDirs *dirs = rtModuleDirs::instance();

for (rtModuleDirs::iter it = dirs->iterator(); it.first != it.second; it.first++)
{
if (rtLoadFile(rtConcatenatePath(*it.first, pathFrg.cString()).c_str(), mFragmentSrc) == RT_OK)
{
loadFrgShader = RT_OK;
break;
}
}
} while(0);

}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

rtLogError("Failed to load SHADER: %s ", path.cString() );
return RT_FAIL;
}

rtError loadVtxShader = RT_FAIL;
void pxShaderResource::loadResourceFromFile()
{
init();

do
rtError loadFrgShader = RT_FAIL;
rtError loadVtxShader = RT_FAIL;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// FRAGMENT SHADER
//
if(mFragmentUrl.beginsWith("data:text/plain,"))
{
if (mVertexSrc.length() != 0)
{
// We have VERTEX SHADER source already...
loadVtxShader = RT_OK;
break;
}

mFragmentSrc.init( (const uint8_t* ) mFragmentUrl.substring(16).cString(),
mFragmentUrl.byteLength() - 16);

loadFrgShader = RT_OK; // it's a DATA URL !!!
}
else
{
loadFrgShader = loadShaderSource(mFragmentUrl, mFragmentSrc);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// VERTEX SHADER
//
if(mVertexUrl.beginsWith("data:text/plain,"))
{
mVertexSrc.init( (const uint8_t* ) mVertexUrl.substring(16).cString(),
mVertexUrl.byteLength() - 16);

loadVtxShader = RT_OK; // it's a DATA URL !!!
}
else
{
loadVtxShader = loadShaderSource(mVertexUrl, mVertexSrc);

if (mVertexSrc.length() == 0)
{
//Use the Default VERTEX SHADER source...
loadVtxShader = RT_OK;
break;
}

bool isVtxFILE = ( mVertexUrl.beginsWith("file://") || mVertexUrl.beginsWith("FILE://") );

rtString pathVtx = mVertexUrl;

if(isVtxFILE)
{
pathVtx.init( (const char* ) mVertexUrl.substring(7).cString(),
mVertexUrl.byteLength() - 7);
loadVtxShader = RT_OK; // use Default VERTEX SHADER source...
}

loadVtxShader = rtLoadFile(pathVtx, mVertexSrc);
if (loadVtxShader == RT_OK)
break;

if (rtIsPathAbsolute(pathVtx))
break;

rtModuleDirs *dirs = rtModuleDirs::instance();

for (rtModuleDirs::iter it = dirs->iterator(); it.first != it.second; it.first++)
{
if (rtLoadFile(rtConcatenatePath(*it.first, pathVtx.cString()).c_str(), mVertexSrc) == RT_OK)
{
loadVtxShader = RT_OK;
break;
}
}
} while(0);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

setLoadStatus("statusCode",0);

// CREATE SHADER PROGRAMS

Expand All @@ -439,7 +453,7 @@ void pxShaderResource::loadResourceFromFile()
else
{
loadFrgShader = RT_RESOURCE_NOT_FOUND;
rtLogError("Could not load FRAGMENT shader file %s.", mFragmentUrl.cString());
//rtLogError("Could not load FRAGMENT shader file %s.", mFragmentUrl.cString());
}
if ( loadFrgShader != RT_OK)
{
Expand All @@ -464,8 +478,6 @@ void pxShaderResource::loadResourceFromFile()
mFragmentSrc.term(); // Dump the source data...
mVertexSrc.term(); // Dump the source data...

setLoadStatus("statusCode",0);

if (gUIThreadQueue)
{
AddRef(); // async
Expand Down Expand Up @@ -575,6 +587,104 @@ void pxShaderResource::loadResourceFromArchive(rtObjectRef /*archiveRef*/)
#endif // 0
}

void pxShaderResource::processDownloadedResource(rtFileDownloadRequest* fileDownloadRequest)
{
FitzerIRL marked this conversation as resolved.
Show resolved Hide resolved
rtString val = "reject";

if (fileDownloadRequest != NULL)
{
bool wasCanceled = fileDownloadRequest->isCanceled();
if (wasCanceled)
{
//rtLogDebug("download was canceled, no need to notify: %s", fileDownloadRequest->fileUrl().cString());
setLoadStatus("statusCode", 0);
setLoadStatus("httpStatusCode",(uint32_t)fileDownloadRequest->httpStatusCode());
if (gUIThreadQueue)
{
gUIThreadQueue->addTask(pxResource::onDownloadCanceledUI, this, (void*)"reject");
}
}
else if (fileDownloadRequest->downloadStatusCode() == 0 &&
fileDownloadRequest->httpStatusCode() == 200 &&
fileDownloadRequest->downloadedData() != NULL)
{
double startResourceSetupTime = pxMilliseconds();
int32_t result = loadResourceData(fileDownloadRequest);
double stopResourceSetupTime = pxMilliseconds();

setLoadStatus("setupTimeMs", static_cast<int>(stopResourceSetupTime-startResourceSetupTime));
if (fileDownloadRequest->isDataCached())
{
setLoadStatus("loadedFromCache", true);
}
else
{
rtObjectRef metrics = fileDownloadRequest->downloadMetrics();
rtValue connectTimeMs;
rtValue sslConnectTimeMs;
rtValue totalDownloadTimeMs;
rtValue downloadSpeedBytesPerSecond;
metrics.get("connectTimeMs", connectTimeMs);
metrics.get("sslConnectTimeMs", sslConnectTimeMs);
metrics.get("totalDownloadTimeMs", totalDownloadTimeMs);
metrics.get("downloadSpeedBytesPerSecond", downloadSpeedBytesPerSecond);
setLoadStatus("connectTimeMs", connectTimeMs);
setLoadStatus("sslConnectTimeMs", sslConnectTimeMs);
setLoadStatus("totalDownloadTimeMs", totalDownloadTimeMs);
setLoadStatus("downloadSpeedBytesPerSecond", downloadSpeedBytesPerSecond);
setLoadStatus("loadedFromCache", false);
}

if(result == PX_RESOURCE_LOAD_FAIL)
{
rtLogError("Resource Decode Failed: %s with proxy: %s", fileDownloadRequest->fileUrl().cString(), fileDownloadRequest->proxy().cString());
setLoadStatus("statusCode", PX_RESOURCE_STATUS_DECODE_FAILURE);
setLoadStatus("httpStatusCode", (uint32_t)fileDownloadRequest->httpStatusCode());
// Since this object can be released before we get a async completion
// We need to maintain this object's lifetime
// TODO review overall flow and organization
if (gUIThreadQueue)
{
gUIThreadQueue->addTask(pxResource::onDownloadCompleteUI, this, (void*)"reject");
}
}
else if (result == PX_RESOURCE_LOAD_SUCCESS)
{
//rtLogInfo("File download Successful: %s", fileDownloadRequest->fileUrl().cString());
// ToDo: Could context.createTexture ever fail and return null here?
// mTexture = context.createTexture(imageOffscreen);
setLoadStatus("statusCode", 0);
val = "resolve";
// Since this object can be released before we get a async completion
// We need to maintain this object's lifetime
// TODO review overall flow and organization
if (gUIThreadQueue)
{
// This calls "setupResource()" to finish up.
//
gUIThreadQueue->addTask(pxResource::onDownloadCompleteUI, this, (void*)"resolve");
}
}
}
else
{
rtLogWarn("Resource Download Failed: %s Error: %s HTTP Status Code: %ld",
fileDownloadRequest->fileUrl().cString(),
fileDownloadRequest->errorString().cString(),
fileDownloadRequest->httpStatusCode());
setLoadStatus("statusCode", PX_RESOURCE_STATUS_HTTP_ERROR);
setLoadStatus("httpStatusCode",(uint32_t)fileDownloadRequest->httpStatusCode());
// Since this object can be released before we get a async completion
// We need to maintain this object's lifetime
// TODO review overall flow and organization
if (gUIThreadQueue)
{
gUIThreadQueue->addTask(pxResource::onDownloadCompleteUI, this, (void*)"reject");
}
}
}
}

void pxShaderResource::loadResource(rtObjectRef archive, bool reloading)
{
if(!reloading && ((rtPromise*)mReady.getPtr())->status())
Expand Down Expand Up @@ -614,8 +724,6 @@ void pxShaderResource::loadResource(rtObjectRef archive, bool reloading)
mDownloadInProgressMutex.unlock();
AddRef(); //ensure this object is not deleted while downloading
rtFileDownloader::instance()->addToDownloadQueue(fragRequest);

return;
}

// VERTEX SHADER
Expand All @@ -637,8 +745,6 @@ void pxShaderResource::loadResource(rtObjectRef archive, bool reloading)
mDownloadInProgressMutex.unlock();
AddRef(); //ensure this object is not deleted while downloading
rtFileDownloader::instance()->addToDownloadQueue(vtxRequest);

return;
}
else if (isFrgDataURL)
FitzerIRL marked this conversation as resolved.
Show resolved Hide resolved
{
Expand Down Expand Up @@ -691,11 +797,7 @@ uint32_t pxShaderResource::loadResourceData(rtFileDownloadRequest* fileDownloadR
{
setLoadStatus("decodeTimeMs", static_cast<int>(stopDecodeTime-startDecodeTime));

#ifdef ENABLE_BACKGROUND_TEXTURE_CREATION
return PX_RESOURCE_LOAD_WAIT;
#else
return PX_RESOURCE_LOAD_SUCCESS;
#endif //ENABLE_BACKGROUND_TEXTURE_CREATION
}

return PX_RESOURCE_LOAD_FAIL;
Expand Down
Loading