Shader* D3D10VertexShader::CreateVertexShaderFromBlob(ShaderBlob const &blob, CTSTR lpShader, CTSTR lpFileName) { ShaderProcessor shaderProcessor; if (!shaderProcessor.ProcessShader(lpShader, lpFileName)) AppWarning(TEXT("Unable to process vertex shader '%s'"), lpFileName); //don't exit, leave it to the actual shader compiler to tell the errors //----------------------------------------------- if (!blob.size()) return nullptr; ComPtr<ID3D11VertexShader> vShader; ID3D11InputLayout *vShaderLayout; HRESULT err = GetD3D()->CreateVertexShader(&blob.front(), blob.size(), NULL, vShader.Assign()); if (FAILED(err)) { CrashError(TEXT("Unable to create vertex shader '%s', result = %08lX"), lpFileName, err); return NULL; } err = GetD3D()->CreateInputLayout(shaderProcessor.generatedLayout.Array(), shaderProcessor.generatedLayout.Num(), &blob.front(), blob.size(), &vShaderLayout); if (FAILED(err)) { CrashError(TEXT("Unable to create vertex layout for vertex shader '%s', result = %08lX"), lpFileName, err); return NULL; } //----------------------------------------------- D3D10VertexShader *shader = new D3D10VertexShader; shader->vertexShader = vShader.Detach(); shader->inputLayout = vShaderLayout; if (!shader->ProcessData(shaderProcessor, lpFileName)) { delete shader; return NULL; } shader->bHasNormals = shaderProcessor.bHasNormals; shader->bHasColors = shaderProcessor.bHasColors; shader->bHasTangents = shaderProcessor.bHasTangents; shader->nTextureCoords = shaderProcessor.numTextureCoords; shader->hViewProj = shader->GetParameterByName(TEXT("ViewProj")); return shader; }
Shader* D3D10VertexShader::CreateVertexShader(CTSTR lpShader, CTSTR lpFileName) { String errorString; ShaderProcessor shaderProcessor; if(!shaderProcessor.ProcessShader(lpShader, lpFileName)) AppWarning(TEXT("Unable to process vertex shader '%s'"), lpFileName); //don't exit, leave it to the actual shader compiler to tell the errors //----------------------------------------------- LPSTR lpAnsiShader = tstr_createUTF8(lpShader); LPSTR lpAnsiFileName = tstr_createUTF8(lpFileName); D3D10System *d3d10Sys = static_cast<D3D10System*>(GS); LPCSTR lpVSType = d3d10Sys->bDisableCompatibilityMode ? "vs_4_0" : "vs_4_0_level_9_3"; ID3D10Blob *errorMessages = NULL, *shaderBlob = NULL; HRESULT err = D3DX10CompileFromMemory(lpAnsiShader, strlen(lpAnsiShader), lpAnsiFileName, NULL, NULL, "main", lpVSType, D3D10_SHADER_OPTIMIZATION_LEVEL3, 0, NULL, &shaderBlob, &errorMessages, NULL); Free(lpAnsiFileName); Free(lpAnsiShader); if(FAILED(err)) { if(errorMessages) { if(errorMessages->GetBufferSize()) { LPSTR lpErrors = (LPSTR)errorMessages->GetBufferPointer(); Log(TEXT("Error compiling vertex shader '%s':\r\n\r\n%S\r\n"), lpFileName, lpErrors); } errorMessages->Release(); } CrashError(TEXT("Compilation of vertex shader '%s' failed, result = %08lX"), lpFileName, err); return NULL; } //----------------------------------------------- ID3D10VertexShader *vShader; ID3D10InputLayout *vShaderLayout; err = GetD3D()->CreateVertexShader(shaderBlob->GetBufferPointer(), shaderBlob->GetBufferSize(), &vShader); if(FAILED(err)) { CrashError(TEXT("Unable to create vertex shader '%s', result = %08lX"), lpFileName, err); SafeRelease(shaderBlob); return NULL; } err = GetD3D()->CreateInputLayout(shaderProcessor.generatedLayout.Array(), shaderProcessor.generatedLayout.Num(), shaderBlob->GetBufferPointer(), shaderBlob->GetBufferSize(), &vShaderLayout); if(FAILED(err)) { CrashError(TEXT("Unable to create vertex layout for vertex shader '%s', result = %08lX"), lpFileName, err); SafeRelease(shaderBlob); SafeRelease(vShader); return NULL; } shaderBlob->Release(); //----------------------------------------------- D3D10VertexShader *shader = new D3D10VertexShader; shader->vertexShader = vShader; shader->inputLayout = vShaderLayout; if(!shader->ProcessData(shaderProcessor, lpFileName)) { delete shader; return NULL; } shader->bHasNormals = shaderProcessor.bHasNormals; shader->bHasColors = shaderProcessor.bHasColors; shader->bHasTangents = shaderProcessor.bHasTangents; shader->nTextureCoords = shaderProcessor.numTextureCoords; shader->hViewProj = shader->GetParameterByName(TEXT("ViewProj")); return shader; }
Shader* D3D10VertexShader::CreateVertexShader(CTSTR lpShader, CTSTR lpFileName) { String errorString; ShaderProcessor shaderProcessor; if(!shaderProcessor.ProcessShader(lpShader, lpFileName)) AppWarning(TEXT("Unable to process vertex shader '%s'"), lpFileName); //don't exit, leave it to the actual shader compiler to tell the errors //----------------------------------------------- D3D10System *d3d10Sys = static_cast<D3D10System*>(GS); LPCSTR lpVSType = d3d10Sys->bDisableCompatibilityMode ? "vs_4_0" : "vs_4_0_level_9_3"; String cacheFilename = FormattedString(TEXT("%s/shaderCache/%s.blob"), OBSGetAppDataPath(), lpFileName).FindReplace(TEXT("\\"), TEXT("/")); List<BYTE> shaderBuffer; LPVOID shaderData; SIZE_T shaderDataSize; ID3D10Blob *errorMessages = NULL, *shaderBlob = NULL; HRESULT err; if(!OSFileExists(cacheFilename) || OSGetFileModificationTime(lpFileName) > OSGetFileModificationTime(cacheFilename)) { LPSTR lpAnsiShader = tstr_createUTF8(lpShader); LPSTR lpAnsiFileName = tstr_createUTF8(lpFileName); err = D3DX10CompileFromMemory(lpAnsiShader, strlen(lpAnsiShader), lpAnsiFileName, NULL, NULL, "main", lpVSType, D3D10_SHADER_OPTIMIZATION_LEVEL3, 0, NULL, &shaderBlob, &errorMessages, NULL); Free(lpAnsiFileName); Free(lpAnsiShader); if(FAILED(err)) { if(errorMessages) { if(errorMessages->GetBufferSize()) { LPSTR lpErrors = (LPSTR)errorMessages->GetBufferPointer(); Log(TEXT("Error compiling vertex shader '%s':\r\n\r\n%S\r\n"), lpFileName, lpErrors); } errorMessages->Release(); } CrashError(TEXT("Compilation of vertex shader '%s' failed, result = %08lX"), lpFileName, err); return NULL; } shaderData = shaderBlob->GetBufferPointer(); shaderDataSize = shaderBlob->GetBufferSize(); CreatePath(GetPathDirectory(cacheFilename)); XFile cacheFile(cacheFilename, XFILE_WRITE, XFILE_CREATEALWAYS); cacheFile.Write(shaderData, (DWORD)shaderDataSize); } else { XFile cacheFile(cacheFilename, XFILE_READ | XFILE_SHARED, XFILE_OPENEXISTING); shaderBuffer.SetSize((unsigned)cacheFile.GetFileSize()); cacheFile.Read(shaderBuffer.Array(), shaderBuffer.Num()); shaderData = shaderBuffer.Array(); shaderDataSize = shaderBuffer.Num(); } //----------------------------------------------- ID3D10VertexShader *vShader; ID3D10InputLayout *vShaderLayout; err = GetD3D()->CreateVertexShader(shaderData, shaderDataSize, &vShader); if(FAILED(err)) { CrashError(TEXT("Unable to create vertex shader '%s', result = %08lX"), lpFileName, err); SafeRelease(shaderBlob); return NULL; } err = GetD3D()->CreateInputLayout(shaderProcessor.generatedLayout.Array(), shaderProcessor.generatedLayout.Num(), shaderData, shaderDataSize, &vShaderLayout); if(FAILED(err)) { CrashError(TEXT("Unable to create vertex layout for vertex shader '%s', result = %08lX"), lpFileName, err); SafeRelease(shaderBlob); SafeRelease(vShader); return NULL; } SafeRelease(shaderBlob); //----------------------------------------------- D3D10VertexShader *shader = new D3D10VertexShader; shader->vertexShader = vShader; shader->inputLayout = vShaderLayout; if(!shader->ProcessData(shaderProcessor, lpFileName)) { delete shader; return NULL; } shader->bHasNormals = shaderProcessor.bHasNormals; shader->bHasColors = shaderProcessor.bHasColors; shader->bHasTangents = shaderProcessor.bHasTangents; shader->nTextureCoords = shaderProcessor.numTextureCoords; shader->hViewProj = shader->GetParameterByName(TEXT("ViewProj")); return shader; }