Vertex shader not compiling after moving to ES 3.0 on IOS - ios

After moving all my shaders to ES3.0 , my compileShader is faulting on the first line.
#version 300
It gives a syntax error:
ERROR: 0:2: '' : syntax error: #version
If it was the wrong version then I would have expected to get unsupported version, not syntax error. So this is baffling why it won't recognize the syntax. I checked the source being passed to the compile shader and it begins clearly with #version 300\n\n\n.
And after setting the context my version is "OpenGL ES 3.0 Apple A7 GPU - 95.16".
This is how I compile it:
GLint status;
const GLchar *source;
source = (GLchar *) [ [ NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil ] UTF8String ];
if (!source)
{
DebugLog(#"Failed to load shader %#", file);
return FALSE;
}
*shader = glCreateShader( type );
glShaderSource( *shader, 1, &source, NULL );
glCompileShader( *shader );

I was able to get around this error by appending core to the #version command. It seems to be an optional parameter but it helped me get beyond the syntax error.
#version core

The correct syntax for OpenGL ES 3.0 is #version 300 es

Related

HLSL min16float fails to compile

I have a shader:
float4 Test_PixelShader(float2 inTex:TEXCOORD,
uniform int mode ):COLOR
{
if(mode) // half
{
min16float2 t=(min16float2)inTex;
min16float2 r=1;
for(int i=0; i<256; i++)
{
r+=t*r;
}
return float4(r.x, r.y, mode, 1);
}else
{
float2 t=inTex;
float2 r=1;
for(int i=0; i<256; i++)
{
r+=t*r;
}
return float4(r.x, r.y, mode, 1);
}
}
#define TECHNIQUE5(name, vs, ps) technique11 name{pass p0{SetVertexShader(CompileShader(vs_5_0, vs)); SetPixelShader(CompileShader(ps_5_0, ps));}}
TECHNIQUE5(Test , Draw_VertexShader(), Test_PixelShader(0));
TECHNIQUE5(Test1, Draw_VertexShader(), Test_PixelShader(1));
Which I'm compiling using:
#define FLAGS_DX11 (D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY|D3DCOMPILE_OPTIMIZATION_LEVEL3|D3DCOMPILE_NO_PRESHADER)
D3DCompile(data.data(), data.elms(), null, d3d_macros.data(), &Include11(src), null, "fx_5_0", FLAGS_DX11, 0, &buffer, &error);
Compilation fails, and the only error message I get is:
warning X4717: Effects deprecated for D3DCompiler_47
No errors, just a warning, yet the shader blob is null.
However if I replace all min16float2 with float2, then compilation works OK.
How to get min16float working?
I've read the https://gpuopen.com/first-steps-implementing-fp16/ article, and it mentions it should work OK.
Do I need to use https://github.com/microsoft/DirectXShaderCompiler instead of D3DCompile from Win10SDK?
I have Windows 10, using latest Windows SDK
Looks like it fails because Microsoft abandoned "fx_*" targets, and have to use "vs/ps" targets instead.

Emscripten: how do I detect webgl context version in runtime?

I use GLFW3 and GLEW wrappers in Emscripten, so I don't call emscripten_webgl_create_context manually and don't set context's properties. The context version is determined only by JS code, which is out of my scope. In my C++ code I need to know whether we run in WebGL1 or WebGL2 context. Is there a document-independent way to do it? Something like:
const auto ctx = emscripten_webgl_get_current_context();
emscripten_webgl_get_context_version(ctx);// Should return 1 or 2.
In C++
const char ES_VERSION_2_0[] = "OpenGL ES 2.0";
const char ES_VERSION_3_0[] = "OpenGL ES 3.0";
const char* version = glGetString(GL_VERSION);
if (strncmp(version, ES_VERSION_2_0, sizeof(ES_VERSION_2_0)) == 0) {
// it's WebGL1
} else if (strncmp(version, ES_VERSION_3_0, sizeof(ES_VERSION_3_0)) == 0) {
// it's WebGL2
} else {
// it's something else
}
Version strings in WebGL have a required non-hardware dependent starting format. See the spec for WebGL2
VERSION: Returns a version or release number of the form WebGL<space>2.0<optional><space><vendor-specific information></optional>.
and for WebGL1
VERSION: Returns a version or release number of the form WebGL<space>1.0<space><vendor-specific information>.
Emscripten also returns fixed strings. See the source
https://github.com/kripken/emscripten/blob/ec764ace634f13bab5ae932912da53fe93ee1b69/src/library_gl.js#L923

compiler error use custom shader on a geometry

I want to do some custom work in custom shader, but get a internal error when run it. compile seems ok, but run will get the error message:
error building shaders : Error Domain=AGXMetalG4P Code=1 "Compiler encountered an internal error" UserInfo={NSLocalizedDescription=Compiler encountered an internal error}
[SCNKit ERROR] display link thread seems stuck
SCNProgram *program = [[SCNProgram alloc] init];
program.fragmentFunctionName = #"myVertex";
program.vertexFunctionName = #"myFragment";
mySceneNode.geometry.program = program
and the shader:
#include <metal_stdlib>
using namespace metal;
#include <SceneKit/scn_metal>
struct MyNodeBuffer {
float4x4 modelTransform;
float4x4 modelViewTransform;
float4x4 normalTransform;
float4x4 modelViewProjectionTransform;
};
typedef struct {
float3 position [[ attribute(SCNVertexSemanticPosition) ]];
} MyVertexInput;
struct SimpleVertex
{
float4 position [[position]];
};
vertex SimpleVertex myVertex(MyVertexInput in [[ stage_in ]],
constant SCNSceneBuffer& scn_frame [[buffer(0)]],
constant MyNodeBuffer& scn_node [[buffer(1)]])
{
SimpleVertex vert;
vert.position = scn_node.modelViewProjectionTransform * float4(in.position, 1.0);
vert.position = float4(in.position,0);
return vert;
}
fragment half4 myFragment(SimpleVertex in [[stage_in]])
{
half4 color;
color = half4(0.0 ,1.0 ,0.0, 1.0);
return color;
}

Reflecting shader uniform names in Metal

Suppose I have the following uniform buffer in my shader:
typedef struct
{
matrix_float4x4 modelview_projection_matrix;
float someValue;
} uniforms_t;
How do I get someValue's location in C++ or Objective-C? I want to do something like this:
void Shader::SetFloat( const char* name, float value )
where name would be 'someValue'.
I came up with a solution by examining BGFX's source code:
NSError* error = NULL;
MTLRenderPipelineReflection* reflectionObj;
MTLPipelineOption option = MTLPipelineOptionBufferTypeInfo | MTLPipelineOptionArgumentInfo;
id <MTLRenderPipelineState> pso = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor options:option reflection:&reflectionObj error:&error];
for (MTLArgument *arg in reflectionObj.vertexArguments)
{
NSLog(#"Found arg: %#\n", arg.name);
if (arg.bufferDataType == MTLDataTypeStruct)
{
for( MTLStructMember* uniform in arg.bufferStructType.members )
{
NSLog(#"uniform: %# type:%lu, location: %lu", uniform.name, (unsigned long)uniform.dataType, (unsigned long)uniform.offset);
}
}
}
Have a look at the Specifying Resources for a Render Command Encoder section of Apple's Metal Programming Guide.
As a very basic explanation...
Declare your uniforms_t structure (which will typically be a single structure containing all the uniforms for a particular shader function) as an argument of a Metal shader function, and associate it with a specific buffer index (eg. [[ buffer(0) ]]) as part of your shader function declaration.
From your app code, copy the contents of your uniforms_t structure into a MTLBuffer, at some offset.
From your app code, invoke the MTLRenderCommandEncoder setVertexBuffer:offset:atIndex: or setFragmentBuffer:offset:atIndex: methods to associate the contents of the MTLBuffer (at the offset where you copied your uniforms_t structure) with the buffer index you declared in your shader function. This basically tells the shader function which MTLBuffer to look in (and where in that buffer) for the value of that function argument.

HLSL error X3086: DX9-style 'compile' syntax is deprecated in strict mode

Hey,
I get this error:
error X3086: DX9-style 'compile' syntax is deprecated in strict mode
When compiling a directx effect with this code:
hr=D3DX11CompileFromFile( TEXT("shaders\\basic.fx"), NULL, NULL, NULL,"fx_5_0", D3DCOMPILE_ENABLE_STRICTNESS, 0, NULL, &pBlob, &pErrorBlob, NULL );
I'm pretty sure it's complaining about this:
technique11 basic
{
pass p0
{
VertexShader = compile vs_5_0 vsMain();
PixelShader = compile ps_5_0 psMain();
}
}
So what am I supposed to use instead of compile?
Try:
technique11 basic
{
pass p0
{
SetVertexShader( CompileShader( vs_5_0, vsMain() ) );
SetPixelShader( CompileShader( ps_5_0, psMain() ) );
}
}

Resources