Flip output by shader

Problems building or running the engine, queries about how to use features etc.
Post Reply
Transporter
Minaton
Posts: 933
Joined: Mon Mar 05, 2012 11:37 am
Location: Germany
x 110

Flip output by shader

Post by Transporter »

Hi,

I try to learn HLSL and GLSL. For testing purpose, I would like to flip a given texture (compositor) vertically. In my test program I'm using Ogre 2. Depending on the used render system I create the shader programs:

Code: Select all

	Ogre::HighLevelGpuProgramPtr m_VS;								//!< Vertex-Shader
	Ogre::HighLevelGpuProgramPtr m_PS;								//!< Pixel-Shader
DX9 - working

Code: Select all

	m_strShaderLanguage = "hlsl";
	m_strVersionVertexShader = "vs_2_0";
	m_strVersionPixelShader = "ps_2_0";

	// Create vertex shader
	m_VS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNameVertexShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_VERTEX_PROGRAM);
	m_VS->setSource("struct VS_IN\n"
		"{\n"
		"	float4			pos : POSITION;\n"
		"	float2			tex : TEXCOORD0;\n"
		"};\n"
		"\n"
		"struct VS_OUT\n"
		"{\n"
		"	float4			pos : POSITION;\n"
		"	float2			tex : TEXCOORD0;\n"
		"};\n"
		"\n"
		"VS_OUT VS( VS_IN vIn )\n"
		"{\n"
		"	VS_OUT vOut;\n"
		"	vOut.pos = vIn.pos;\n"
		"	vOut.pos.z = 0;\n"
		"	vOut.tex = vIn.tex;\n"
		"	return vIn;\n"
		"}\n");
	m_VS->setParameter("target", m_strVersionVertexShader);
	m_VS->setParameter("entry_point", "VS");

	// Create pixel shader
	m_PS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNamePixelShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_FRAGMENT_PROGRAM);
	m_PS->setSource("uniform extern texture texContent;\n"
		"\n"
		"sampler samContent = sampler_state\n"
		"{\n"
		"	Texture = <texContent>;\n"
		"};\n"
		"\n"
		"\n"
		"struct VS_IN\n"
		"{\n"
		"	float4			pos : POSITION;\n"
		"	float2			tex : TEXCOORD0;\n"
		"};\n"
		"\n"
		"float4 PS( VS_IN vIn ) : COLOR\n"
		"{\n"
		"   float2 flip = { 1.0 - vIn.tex[0], vIn.tex[1] };\n"
		"	return tex2D( samContent, flip );\n"
		"}\n");
	m_PS->setParameter("target", m_strVersionPixelShader);
	m_PS->setParameter("entry_point", "PS");
DX11 - compiling, but not working

Code: Select all

	m_strShaderLanguage = "hlsl";
	m_strVersionVertexShader = "vs_4_0";
	m_strVersionPixelShader = "ps_4_0";

	// Create vertex shader
	m_VS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNameVertexShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_VERTEX_PROGRAM);
	m_VS->setSource("struct VS_IN\n"
		"{\n"
		"	float4			pos : POSITION;\n"
		"	float2			tex : TEXCOORD0;\n"
		"};\n"
		"\n"
		"struct VS_OUT\n"
		"{\n"
		"	float4			pos : POSITION;\n"
		"	float2			tex : TEXCOORD0;\n"
		"};\n"
		"\n"
		"VS_OUT VS( VS_IN vIn )\n"
		"{\n"
		"	VS_OUT vOut;\n"
		"	vOut.pos = vIn.pos;\n"
		"	vOut.pos.z = 0;\n"
		"	vOut.tex = vIn.tex;\n"
		"	return vOut;\n"
		"}\n");
	m_VS->setParameter("target", m_strVersionVertexShader);
	m_VS->setParameter("entry_point", "VS");

	// Create pixel shader
	m_PS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNamePixelShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_FRAGMENT_PROGRAM);
	m_PS->setSource("Texture2D texContent;\n"
		"SamplerState samContent;\n"
		"\n"
		"struct VS_IN\n"
		"{\n"
		"	float4			pos : POSITION;\n"
		"	float2			tex : TEXCOORD0;\n"
		"};\n"
		"\n"
		"float4 PS( VS_IN vIn ) : SV_TARGET\n"
		"{\n"
		"   float2 flip = { 1.0 - vIn.tex[0], vIn.tex[1] };\n"
		"	return texContent.Sample( samContent, flip );\n"
		"}\n");
	m_PS->setParameter("target", m_strVersionPixelShader);
	m_PS->setParameter("entry_point", "PS");
OpenGL - working

Code: Select all

	m_strShaderLanguage = "glsl";
	m_strVersionVertexShader = "vp40";
	m_strVersionPixelShader = "fp40";

	// Create vertex shader
	m_VS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNameVertexShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_VERTEX_PROGRAM);
	m_VS->setSource("void main(void)\n"
		"{\n"
		"   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
		"   gl_TexCoord[0] = gl_MultiTexCoord0;\n"
		"}\n");
	m_VS->setParameter("target", m_strVersionVertexShader);
	m_VS->setParameter("entry_point", "main");

	// Create pixel shader
	m_PS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNamePixelShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_FRAGMENT_PROGRAM);
	m_PS->setSource("uniform sampler2D texContent;\n"
		"\n"
		"void main(void)\n"
		"{\n"
		"   vec2 inPos = gl_TexCoord[0];"
		"   gl_FragColor = texture2D(texContent, vec2(1.0 - inPos.x, inPos.y));\n"
		"}\n");
	m_PS->setParameter("target", m_strVersionPixelShader);
	m_PS->setParameter("entry_point", "main");
OpenGL3+ - compiling, but not working

Code: Select all

	m_strShaderLanguage = "glsl";
	m_strVersionVertexShader = "glsl420";
	m_strVersionPixelShader = "glsl420";

	// Create vertex shader
	m_VS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNameVertexShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_VERTEX_PROGRAM);
	m_VS->setSource("#version 420\n"
		"\n"
		"layout (location = 0) in vec3 VertexPosition;\n"
		"\n"
		"void main(void)\n"
		"{\n"
		"   gl_Position = vec4(VertexPosition, 1.0);\n"
		"}\n");
	m_VS->setParameter("target", m_strVersionVertexShader);
	m_VS->setParameter("entry_point", "main");

	// Create pixel shader
	m_PS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNamePixelShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_FRAGMENT_PROGRAM);
	m_PS->setSource("#version 420\n"
		"layout(binding=0) uniform sampler2D texContent;\n"
		"out vec4 FragColor;\n"
		"\n"
		"void main(void)\n"
		"{\n"
		"   FragColor = texture2D(texContent, gl_SamplePosition);\n"
		"}\n");
	m_PS->setParameter("target", m_strVersionPixelShader);
	m_PS->setParameter("entry_point", "main");
I can't find the mistakes. It would be nice if someone could help me.

Thanks,
Transporter
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: Flip output by shader

Post by xrgo »

I believe that DX11 and GL3+ support is very bad (or none) before Ogre 2.1, and in the same way DX9 and GL has no support on Ogre 2.1
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5299
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1280
Contact:

Re: Flip output by shader

Post by dark_sylinc »

xrgo is right, GL3+ & DX11 support in 1.x branches is far from ideal.

Your GLSL shader mixes deprecated stuff with new one. I've tried to fix it (may not compile, didn't try it):

Code: Select all

   m_strShaderLanguage = "glsl";
   m_strVersionVertexShader = "glsl420";
   m_strVersionPixelShader = "glsl420";

   // Create vertex shader
   m_VS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNameVertexShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_VERTEX_PROGRAM);
   m_VS->setSource("#version 420\n"
      "\n"
	  "out gl_PerVertex\n"
	  "{\n"
	  "	vec4 gl_Position;\n"
	  "};\n"
      "in vec3 VertexPosition;\n"
	  "in vec2 uv0;\n"
	  "out block\n"
	  "{"
	  "	vec2 uv0\n"
	  "} outVs;"
      "\n"
      "void main(void)\n"
      "{\n"
	  "		outVs.uv0 = uv0;"
      "   gl_Position = vec4(VertexPosition, 1.0);\n"
      "}\n");
   m_VS->setParameter("target", m_strVersionVertexShader);
   m_VS->setParameter("entry_point", "main");

   // Create pixel shader
   m_PS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNamePixelShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_FRAGMENT_PROGRAM);
   m_PS->setSource("#version 420\n"
      "layout(binding=0) uniform sampler2D texContent;\n"
	  "in block\n"
	  "{\n"
	  "	vec2 uv0;\n"
	  "} inPs;\n"
      "out vec4 FragColor;\n"
      "\n"
      "void main(void)\n"
      "{\n"
      "   FragColor = texture(texContent, inPs.uv0);\n"
      "}\n");
   m_PS->setParameter("target", m_strVersionPixelShader);
   m_PS->setParameter("entry_point", "main");
Your vertex shader was never outputing UVs to the pixel shader, gl_SamplePosition is in range [0; width) and [0; height) whereas you needed them to be in range [0; 1)
"texture2D" is deprecated, and should be used texture instead.

I didn't find anything weird in your DX11 shader, but you should try RenderDoc, it will probably make debugging your problem a lot easier.
Transporter
Minaton
Posts: 933
Joined: Mon Mar 05, 2012 11:37 am
Location: Germany
x 110

Re: Flip output by shader

Post by Transporter »

Thank you for your help. I'm using Ogre 2.0 at the moment but I would like to switch to 2.1. So I try to get DX11 and GL3+ running, but it's not working.

I fixed the shader code

Code: Select all

void MirrorShaderGenerator::createGlsl4(void)
{
	m_strShaderLanguage = "glsl";
	m_strVersionVertexShader = "glsl420";
	m_strVersionPixelShader = "glsl420";

	// Create vertex shader
	m_VS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNameVertexShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_VERTEX_PROGRAM);
	m_VS->setSource("#version 420\n"
		"\n"
		"in vec3 VertexPosition;\n"
		"in vec2 uv0;\n"
		"\n"
		"out gl_PerVertex\n"
		"{\n"
		"   vec4 gl_Position;\n"
		"};\n"
		"\n"
		"out block\n"
		"{\n"
		"   vec2 uv0;\n"
		"} outVs;\n"
		"\n"
		"void main(void)\n"
		"{\n"
		"   outVs.uv0 = uv0;\n"
		"   gl_Position = vec4(VertexPosition, 1.0);\n"
		"}\n");
	m_VS->setParameter("target", m_strVersionVertexShader);
	m_VS->setParameter("entry_point", "main");

	// Create pixel shader
	m_PS = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram(m_strNamePixelShader, m_strResourceGroup, m_strShaderLanguage, Ogre::GPT_FRAGMENT_PROGRAM);
	m_PS->setSource("#version 420\n"
		"layout(binding=0) uniform sampler2D texContent;\n"
		"in block\n"
		"{\n"
		"   vec2 uv0;\n"
		"} inPs;\n"
		"out vec4 FragColor;\n"
		"\n"
		"void main(void)\n"
		"{\n"
		"   FragColor = texture(texContent, inPs.uv0);\n"
		"}\n");
	m_PS->setParameter("target", m_strVersionPixelShader);
	m_PS->setParameter("entry_point", "main");
}
but I still get a black screen and thousands of messages like this:
Ogre.log wrote:16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 112 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 128 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_DYNAMIC_DRAW) will use SYSTEM HEAP memory as the source for buffer object operations.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 128 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_DYNAMIC_DRAW) has been mapped WRITE_ONLY in SYSTEM HEAP memory (fast).
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 106 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_DYNAMIC_DRAW) will use SYSTEM HEAP memory as the source for buffer object operations.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 106 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_DYNAMIC_DRAW) has been mapped WRITE_ONLY in SYSTEM HEAP memory (fast).
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 107 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_DYNAMIC_DRAW) will use SYSTEM HEAP memory as the source for buffer object operations.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 107 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_DYNAMIC_DRAW) has been mapped WRITE_ONLY in SYSTEM HEAP memory (fast).
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 108 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_DYNAMIC_DRAW) will use SYSTEM HEAP memory as the source for buffer object operations.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 108 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_DYNAMIC_DRAW) has been mapped WRITE_ONLY in SYSTEM HEAP memory (fast).
16:22:07: ERROR: Failed to create separable program.
16:22:07: OpenGL:performance(medium) 131186: Buffer performance warning: Buffer object 78 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) is being copied/moved from VIDEO memory to HOST memory.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 78 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations.
16:22:07: ERROR: Failed to create separable program.
16:22:07: ERROR: Failed to create separable program.
16:22:07: OpenGL:performance(medium) 131186: Buffer performance warning: Buffer object 104 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) is being copied/moved from VIDEO memory to HOST memory.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 104 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations.
16:22:07: ERROR: Failed to create separable program.
16:22:07: ERROR: Failed to create separable program.
16:22:07: OpenGL:performance(medium) 131186: Buffer performance warning: Buffer object 96 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) is being copied/moved from VIDEO memory to HOST memory.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 96 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations.
16:22:07: ERROR: Failed to create separable program.
16:22:07: ERROR: Failed to create separable program.
16:22:07: OpenGL:performance(medium) 131186: Buffer performance warning: Buffer object 86 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) is being copied/moved from VIDEO memory to HOST memory.
16:22:07: OpenGL:message(ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌmessage) 131185: Buffer detailed info: Buffer object 86 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations.
16:22:07: ERROR: Failed to create separable program.
16:22:07: ERROR: Failed to create separable program.
In combination with RenderDoc, I get the following additional message:
RenderDoc for GL3+ wrote:OpenGL. Inactive window.
Context not created via CreateContextAttribs. Capturing disabled.
Only OpenGL 3.2+ contexts are supported.
I also tried to use RenderDoc with DX11, but the application is crashing during the program initialisation

Code: Select all

mWindow = mRoot->initialise(true, "Test Render Window");
RenderDoc for DX11 wrote:RENDERDOC: [16:47:10] core.cpp( 239) - Log - RenderDoc v0.24 (fc831982ebd4fdfea085d24f26783a497fa21bb6-official) capturing application
RENDERDOC: [16:47:12] win32_libentry.cpp( 73) - Log - Loading into F:\Compositor\bin\Debug-x86\ShaderTest_d.exe
RENDERDOC: [16:47:12] hooks.cpp( 49) - Log - Loaded and hooked into d3d11.dll, PID 7784
RENDERDOC: [16:47:12] hooks.cpp( 49) - Log - Loaded and hooked into d3d9.dll, PID 7784
RENDERDOC: [16:47:12] hooks.cpp( 49) - Log - Loaded and hooked into dxgi.dll, PID 7784
RENDERDOC: [16:47:12] hooks.cpp( 49) - Log - Loaded and hooked into opengl32.dll, PID 7784
RENDERDOC: [16:47:12] hooks.cpp( 49) - Log - Loaded and hooked into kernel32.dll, PID 7784
RENDERDOC: [16:47:12] entry_points.cpp( 218) - Log - Using logfile C:\Users\schweig\AppData\Local\Temp\ShaderTest_d_2015.05.04_16.47.10.rdc
RENDERDOC: [16:47:12] entry_points.cpp( 225) - Log - Setting capture options
RENDERDOC: [16:47:15] d3d11_device.cpp( 654) - Warning - Returning a dummy ID3D11InfoQueue that does nothing. This ID3D11InfoQueue will not work!
RENDERDOC: [16:47:15] dxgi_wrapped.cpp( 683) - Warning - Querying IDXGIDevice1 for interface: GUID {f74ee86f-7270-48e8-9d63-38af75f22d57}
RENDERDOC: [16:47:15] dxgi_wrapped.cpp( 542) - Warning - Querying IDXGIObject for interface: GUID {f74ee86f-7270-48e8-9d63-38af75f22d57}
RENDERDOC: [16:47:15] d3d11_manager.cpp( 50) - Error - UnwrapResource(): Unexpected non-wrapped resource
RENDERDOC: [16:47:15] d3d11_resources.cpp(1466) - Error - Unknown type for ptr 0x002CAA10
RENDERDOC: [16:47:15] d3d11_resources.cpp(1466) - Error - Unknown type for ptr 0x002CAA10
RENDERDOC: [16:47:15] d3d11_device_wrap.cpp( 924) - Error - Assertion failed: 'record'
without RenderDoc it's crashing later on

Code: Select all

mRoot->startRendering();
Post Reply