win32: fist step glsl shaders

This commit is contained in:
OV2 2018-05-20 19:48:38 +02:00
parent f883561d11
commit ad2c72d26c
6 changed files with 88 additions and 163 deletions

View File

@ -11,10 +11,14 @@
#ifdef _WIN32
#include <windows.h>
#include <stdlib.h>
#include "gl_core_3_1.h"
#include <direct.h>
#define chdir(dir) _chdir(dir)
#define realpath(src, resolved) _fullpath(resolved, src, PATH_MAX)
}
#endif
#endif /* __SHADER_PLATFORM_H */

View File

@ -226,6 +226,7 @@ COpenGL::COpenGL(void)
cgAvailable = false;
frameCount = 0;
cgShader = NULL;
glslShader = NULL;
}
COpenGL::~COpenGL(void)
@ -305,6 +306,10 @@ bool COpenGL::Initialize(HWND hWnd)
cgShader = new CGLCG(cgContext);
}
if (ShaderAailable() && NPOTAvailable()) {
glslShader = new GLSLShader();
}
ApplyDisplayChanges();
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
@ -337,6 +342,10 @@ void COpenGL::DeInitialize()
afterRenderHeight = 0;
shaderFunctionsLoaded = false;
shader_type = OGL_SHADER_NONE;
if (glslShader) {
delete glslShader;
glslShader = NULL;
}
if(cgShader) {
delete cgShader;
cgShader = NULL;
@ -478,23 +487,10 @@ void COpenGL::Render(SSurface Src)
if (shader_type != OGL_SHADER_NONE) {
if(shader_type == OGL_SHADER_GLSL) {
GLint location;
float inputSize[2] = { (float)afterRenderWidth, (float)afterRenderHeight };
RECT windowSize;
GetClientRect(hWnd,&windowSize);
float outputSize[2] = {(float)(GUI.Stretch?windowSize.right:afterRenderWidth),
(float)(GUI.Stretch?windowSize.bottom:afterRenderHeight) };
float textureSize[2] = { (float)quadTextureSize, (float)quadTextureSize };
float frameCnt = (float)++frameCount;
location = glGetUniformLocation (shaderProgram, "rubyInputSize");
glUniform2fv (location, 1, inputSize);
location = glGetUniformLocation (shaderProgram, "rubyOutputSize");
glUniform2fv (location, 1, outputSize);
location = glGetUniformLocation (shaderProgram, "rubyTextureSize");
glUniform2fv (location, 1, textureSize);
RECT windowSize, displayRect;
GetClientRect(hWnd, &windowSize);
displayRect = CalculateDisplayRect(windowSize.right, windowSize.bottom, windowSize.right, windowSize.bottom);
glslShader->render(drawTexture, afterRenderWidth, afterRenderHeight, displayRect.right - displayRect.left, displayRect.bottom - displayRect.top, displayRect.left, displayRect.top);
} else if(shader_type == OGL_SHADER_CG) {
xySize inputSize = { (float)afterRenderWidth, (float)afterRenderHeight };
RECT windowSize, displayRect;
@ -732,153 +728,51 @@ bool COpenGL::SetShadersCG(const TCHAR *file)
bool COpenGL::SetShadersGLSL(const TCHAR *glslFileName)
{
char *fragment=NULL, *vertex=NULL;
IXMLDOMDocument * pXMLDoc = NULL;
IXMLDOMElement * pXDE = NULL;
IXMLDOMNode * pXDN = NULL;
HRESULT hr;
BSTR queryString, nodeContent;
TCHAR errorMsg[MAX_PATH + 50];
if(fragmentShader) {
glDetachShader(shaderProgram,fragmentShader);
glDeleteShader(fragmentShader);
fragmentShader = 0;
}
if(vertexShader) {
glDetachShader(shaderProgram,vertexShader);
glDeleteShader(vertexShader);
vertexShader = 0;
}
if(shaderProgram) {
glUseProgram(0);
glDeleteProgram(shaderProgram);
shaderProgram = 0;
}
if(glslFileName==NULL || *glslFileName==TEXT('\0'))
return true;
if(!LoadShaderFunctions()) {
MessageBox(NULL, TEXT("Unable to load OpenGL shader functions"), TEXT("Shader Loading Error"),
MB_OK|MB_ICONEXCLAMATION);
if (!glslFileName || !glslShader)
return false;
}
hr = CoCreateInstance(CLSID_DOMDocument,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pXMLDoc));
if(FAILED(hr)) {
MessageBox(NULL, TEXT("Error creating XML Parser"), TEXT("Shader Loading Error"),
MB_OK|MB_ICONEXCLAMATION);
return false;
}
VARIANT fileName;
VARIANT_BOOL ret;
fileName.vt = VT_BSTR;
#ifdef UNICODE
fileName.bstrVal = SysAllocString(glslFileName);
#else
wchar_t tempfilename[MAX_PATH];
MultiByteToWideChar(CP_UTF8,0,glslFileName,-1,tempfilename,MAX_PATH);
fileName.bstrVal = SysAllocString(tempfilename);
#endif
hr = pXMLDoc->load(fileName,&ret);
SysFreeString(fileName.bstrVal);
if(FAILED(hr) || hr==S_FALSE) {
_stprintf(errorMsg,TEXT("Error loading GLSL shader file:\n%s"),glslFileName);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
pXMLDoc->Release();
return false;
}
VARIANT attributeValue;
BSTR attributeName;
hr = pXMLDoc->get_documentElement(&pXDE);
if(FAILED(hr) || hr==S_FALSE) {
_stprintf(errorMsg,TEXT("Error loading root element from file:\n%s"),glslFileName);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
pXMLDoc->Release();
return false;
}
attributeName=SysAllocString(L"language");
pXDE->getAttribute(attributeName,&attributeValue);
SysFreeString(attributeName);
pXDE->Release();
if(attributeValue.vt!=VT_BSTR || lstrcmpiW(attributeValue.bstrVal,L"glsl")) {
_stprintf(errorMsg,TEXT("Shader language is <%s>, expected <GLSL> in file:\n%s"),attributeValue.bstrVal,glslFileName);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
if(attributeValue.vt==VT_BSTR) SysFreeString(attributeValue.bstrVal);
pXMLDoc->Release();
return false;
}
if(attributeValue.vt==VT_BSTR) SysFreeString(attributeValue.bstrVal);
queryString=SysAllocString(L"/shader/fragment");
hr = pXMLDoc->selectSingleNode(queryString,&pXDN);
SysFreeString(queryString);
if(hr == S_OK) {
hr = pXDN->get_text(&nodeContent);
if(hr == S_OK) {
int requiredChars = WideCharToMultiByte(CP_ACP,0,nodeContent,-1,fragment,0,NULL,NULL);
fragment = new char[requiredChars];
WideCharToMultiByte(CP_UTF8,0,nodeContent,-1,fragment,requiredChars,NULL,NULL);
}
SysFreeString(nodeContent);
pXDN->Release();
pXDN = NULL;
}
queryString=SysAllocString(L"/shader/vertex");
hr = pXMLDoc->selectSingleNode(queryString,&pXDN);
SysFreeString(queryString);
if(hr == S_OK) {
hr = pXDN->get_text(&nodeContent);
if(hr == S_OK) {
int requiredChars = WideCharToMultiByte(CP_ACP,0,nodeContent,-1,vertex,0,NULL,NULL);
vertex = new char[requiredChars];
WideCharToMultiByte(CP_UTF8,0,nodeContent,-1,vertex,requiredChars,NULL,NULL);
}
SysFreeString(nodeContent);
pXDN->Release();
pXDN = NULL;
}
pXMLDoc->Release();
if(!fragment && !vertex) {
_stprintf(errorMsg,TEXT("No vertex or fragment program in file:\n%s"),glslFileName);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
return false;
}
shaderProgram = glCreateProgram ();
if(vertex) {
vertexShader = glCreateShader (GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, (const GLchar **)&vertex, NULL);
glCompileShader(vertexShader);
glAttachShader(shaderProgram, vertexShader);
delete[] vertex;
}
if(fragment) {
fragmentShader = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, (const GLchar **)&fragment, NULL);
glCompileShader(fragmentShader);
glAttachShader(shaderProgram, fragmentShader);
delete[] fragment;
}
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
glslShader->destroy();
glslShader->load_shader(_tToChar(glslFileName));
shader_type = OGL_SHADER_GLSL;
return true;
}
bool COpenGL::ShaderAailable()
{
const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
if (!extensions)
return false;
if (strstr(extensions, "fragment_program") ||
strstr(extensions, "fragment_shader"))
{
return true;
}
return false;
}
bool COpenGL::NPOTAvailable()
{
const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
if (!extensions)
return false;
int glVersionMajor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &glVersionMajor);
if (glVersionMajor >= 2)
return true;
if (strstr(extensions, "non_power_of_two") ||
strstr(extensions, "npot"))
{
return true;
}
return false;
}

View File

@ -198,6 +198,7 @@
#include "gl_core_3_1.h"
#include "cgFunctions.h"
#include "CGLCG.h"
#include "../shaders/glsl.h"
#include "wglext.h"
#include "IS9xDisplayOutput.h"
@ -234,6 +235,7 @@ private:
bool cgAvailable;
CGLCG *cgShader;
GLSLShader *glslShader;
GLuint shaderProgram;
GLuint vertexShader;
@ -251,6 +253,8 @@ private:
void DestroyDrawSurface(void);
bool ChangeDrawSurfaceSize(unsigned int scale);
void SetupVertices();
bool ShaderAailable();
bool NPOTAvailable();
public:
COpenGL();

View File

@ -403,6 +403,9 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Unicode|x64'">true</ExcludedFromBuild>
</CustomBuild>
<ClInclude Include="..\msu1.h" />
<ClInclude Include="..\shaders\glsl.h" />
<ClInclude Include="..\shaders\shader_helpers.h" />
<ClInclude Include="..\shaders\shader_platform.h" />
<ClInclude Include="..\statemanager.h" />
<ClInclude Include="..\sha256.h" />
<ClInclude Include="..\bml.h" />
@ -513,6 +516,8 @@
<ClCompile Include="..\seta010.cpp" />
<ClCompile Include="..\seta011.cpp" />
<ClCompile Include="..\seta018.cpp" />
<ClCompile Include="..\shaders\glsl.cpp" />
<ClCompile Include="..\shaders\shader_helpers.cpp" />
<ClCompile Include="..\snapshot.cpp" />
<ClCompile Include="..\snes9x.cpp" />
<ClCompile Include="..\spc7110.cpp" />

View File

@ -40,6 +40,9 @@
<Filter Include="Filter">
<UniqueIdentifier>{1e3e39fd-b2d2-4c3f-83b2-b5731ab8deee}</UniqueIdentifier>
</Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{31758447-7e76-4af0-8561-a377194049e1}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="globals.h">
@ -255,6 +258,15 @@
<ClInclude Include="..\filter\xbrz_tools.h">
<Filter>Filter</Filter>
</ClInclude>
<ClInclude Include="..\shaders\glsl.h">
<Filter>Shaders</Filter>
</ClInclude>
<ClInclude Include="..\shaders\shader_helpers.h">
<Filter>Shaders</Filter>
</ClInclude>
<ClInclude Include="..\shaders\shader_platform.h">
<Filter>Shaders</Filter>
</ClInclude>
<ClInclude Include="gl_core_3_1.h">
<Filter>GUI\VideoDriver</Filter>
</ClInclude>
@ -560,6 +572,12 @@
<ClCompile Include="DumpAtEnd.cpp">
<Filter>GUI</Filter>
</ClCompile>
<ClCompile Include="..\shaders\glsl.cpp">
<Filter>Shaders</Filter>
</ClCompile>
<ClCompile Include="..\shaders\shader_helpers.cpp">
<Filter>Shaders</Filter>
</ClCompile>
<ClCompile Include="gl_core_3_1.c">
<Filter>GUI\VideoDriver</Filter>
</ClCompile>
@ -815,4 +833,4 @@
</CustomBuild>
<CustomBuild Include="snes9x.cfg" />
</ItemGroup>
</Project>
</Project>

View File

@ -7662,7 +7662,7 @@ INT_PTR CALLBACK DlgFunky(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hDlg;
ofn.lpstrFilter = TEXT("Shader Files\0*.shader;*.cg;*.cgp\0All Files\0*.*\0\0");
ofn.lpstrFilter = TEXT("Shader Files\0*.shader;*.cg;*.cgp;*.glsl;*.glslp\0All Files\0*.*\0\0");
ofn.lpstrFile = openFileName;
ofn.lpstrTitle = TEXT("Select Shader");
ofn.lpstrDefExt = TEXT("shader");