error C2146 trying to compile a basic HLSL shader in C++ - c++

i've just started to learn the basics of HLSL using C++, im following the tutorials on a book, the first basic shader is:
float4 VS_Main( float4 pos:POSITION):SV_POSITION
{
return pos;
}
but i get a lot of errors at compile time:
error C2146: syntax error: ';' missing before the identifier 'VS_Main'
error C4430: missing type specifier, int assumed. Note: default-int is no longer supported
error C2146: syntax error : ')' missing before the identifier 'pos'
error C2059: syntax errorlooks like a function definition, but there is no formal parameter list.
error C2059: syntax error: '{'
it really looks like the compiler cant handle HLSL at all...maybe VS2012 express doesnt support HLSL?
thanks in advance

HLSL is not C++. You should compile shaders with shader compiler, and C++ with C++ compiler — do not mix. There are two options for compiling HLSL.
Use command-line utility fxc.exe that is included in DirectX SDK (docs and usage here at MSDN). It generates a file that you should load in runtime by some of the ways described here.
Compile your shader at runtime by using D3DCompileFromFile function.
There are pros and cons about each of the variants. In short, pre-compiling at build time gives you some time gain at runtime, while compiling at runtime is more flexible and comfortable at development stage (no need to remember to recompile it or to use post-build scripts) but is more error-prone. Choose by yourself.

The code looks fine for HLSL. If you want to compile it from within VS2012 set the ".fx" file to build using the HLSL shader compiler. Right click the file select properties. Then select General and Item Type should be set to HLSL compiler.
If you really want to make shaders using C++ you could look into C++AMP to see if it may suit your needs.

it really looks like the compiler cant handle HLSL at all...maybe VS2012 express doesnt support HLSL?
No C++ compiler does. It is not supposed to be handled by the compiler.
You need to turn it into a resource and copy it into the bin directory using post-build scripts, and load the HLSL at runtime.
See also: http://www.directxtutorial.com/Lesson.aspx?lessonid=11-4-5

Related

glbinding, Qt and error "glbinding is not compatible with gl.h"

I'm writing a new project and want to replace GLEW with glbinding.
My setup is like this:
VS 2015, Qt 5.6, glbinding 2.0
I tried to compile quite simple code - only window (QWindow) with OpenGL context (QOpenGLContext) but got an error: glbinding is not compatible with gl.h
When I was tracking this bug I found out that file (which is imported by the glbinding/gl/gl.h) nogl.h is causing this message with the code below:
#ifdef __gl_h_
#error "glbinding is not compatible with gl.h"
#else
#define __gl_h_
#endif
Then I noticed that this is caused by the QtGui/qopenglcontext.h header (which I need for creating OpenGL context...). So when I don't include qopenglcontext.h my program compiles without error. I also noticed that this error message appears only when I include glbinding/gl/gl.h after QtGui/qopenglcontext.h. If I reverse the include order I get a bunch of errors like this:
1>C:\Qt\Qt5.6.0\5.6\msvc2015_64\include\QtGui/qopenglext.h(117): error C2065: 'GLenum': undeclared identifier
1>C:\Qt\Qt5.6.0\5.6\msvc2015_64\include\QtGui/qopenglext.h(117): error C2146: syntax error: missing ')' before identifier 'mode'
1>C:\Qt\Qt5.6.0\5.6\msvc2015_64\include\QtGui/qopenglext.h(118): error C2065: 'GLenum': undeclared identifier
1>C:\Qt\Qt5.6.0\5.6\msvc2015_64\include\QtGui/qopenglext.h(118): error C2146: syntax error: missing ')' before identifier 'target'
After all I still don't know how to solve this and what exactly is causing this error...
glbinding – like practically every other OpenGL loader – has to fiddle with the OpenGL symbol tokens to avoid namespace clashing. To this end it must interact with the OpenGL definitions in a specific way, which means you must not have included any OpenGL header, or something that includes such in turn at the moment you include the glbinding header. The preprocessor fiddling glbinding performs will prevent gl.h getting included in a way that causes trouble.
So what you must do is: Include the glbinding headers in your C++ compilation unit files before everything else (Qt headers, OpenGL helper libraries and so on).

Get errors about redefinition (OpenGL 4.0 Shading Language Cookbook)

I just tested the code in chapter 1 (OpenGL 4.0 Shading Language Cookbook)
I have installed Qt5, qmake -tp vc chapter01.pro then open it in vs2010.
Built and got many errors like:
1>D:\APP\Qt5\5.0.0\msvc2010\include\GLES2/gl2.h(38): error C2371: 'GLintptr' : redefinition; different basic types
1> D:\OpenGL\glew-1.5.4\include\GL/glew.h(1615) : see declaration of 'GLintptr'
1>D:\APP\Qt5\5.0.0\msvc2010\include\GLES2/gl2.h(39): error C2371: 'GLsizeiptr' : redefinition; different basic types
1> D:\OpenGL\glew-1.5.4\include\GL/glew.h(1614) : see declaration of 'GLsizeiptr'
1>D:\APP\Qt5\5.0.0\msvc2010\include\GLES2/gl2.h(96): warning C4005: 'GL_BLEND_EQUATION_RGB' : macro redefinition
1> D:\OpenGL\glew-1.5.4\include\GL/glew.h(1666) : see previous definition of 'GL_BLEND_EQUATION_RGB'
1>D:\APP\Qt5\5.0.0\msvc2010\include\GLES2/gl2.h(474): error C2365: '__glewActiveTexture' : redefinition; previous definition was 'data variable'
1> D:\OpenGL\glew-1.5.4\include\GL/glew.h(12027) : see declaration of '__glewActiveTexture'
1>D:\APP\Qt5\5.0.0\msvc2010\include\GLES2/gl2.h(475): error C2365: '__glewAttachShader' : redefinition; previous definition was 'data variable'
How to solve these problems? (The code link is here)
Short answer: only include GLEW's header file, and don't include any OpenGL headers.
Explanation: GLEW's header file effectively includes everything in the OpenGL header files (I've never tried with GLES, but I suspect it's the same), redefining all of the OpenGL entry points through C preprocessor macros. This is why you're seeing the multiply-defined symbols and types. Additional information can be found here.
Not a best answer for sure, but I've just checked the book.
It seems that book is outdated when you get to Qt's version:
For example, in recent versions of Qt (at least version 4.7)
So, for the sake of easy reading the book, you may consider to downgrade your Qt to 4.x (4.8, for example) family, as there may be a lot of changes in the OpenGl requirements, especially if Qt build you are using is built with different OpenGl libraries.
Another solution is to use updated source code which doesn't require Qt at all:
This is the example programs from the OpenGL 4.0 Shading Language Cookbook, by David Wolff. The source code has been updated to work with MS Visual Studio, and no longer requires Qt.

Unable to compile CUDA C sources. Simple version is provided

Here is the problem...
For school project I need to write parallel application using CUDA C. Even the most simple example will not compile. I'm using Windows7 and MS visual studio. The code is taken from the book: CUDA by example. An introduction to general purpose GPU computing.
#include<iostream>
#include<cuda.h>
using namespace std;
__global__ void kernel(void){
}
int main(){
kernel<<<1, 1>>>();
cout << "Hello world" << endl;
return 0;
}
Here are the errors:
1>c:\users\administrator\documents\visualstudio2010\projects\test\test\test.cpp(6): error C2144: syntax error : 'void' should be preceded by ';'
1>c:\users\administrator\documents\visualstudio2010\projects\test\test\test.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\administrator\documents\visualstudio2010\projects\test\test\test.cpp(10): error C2059: syntax error : '<'
Do I need to set nvcc.exe as default compiler instead of cl.exe? If that is the case, how to do it?
Any help is much appreciated!
CUDA code needs to be written in a .cu file and compiled with the NVCC compiler. You are seeing the above errors because you have written your code in a .c or .cpp file and are trying to compile it with a C++ compiler (the Visual C++ compiler).
You have chosen the right book to learn CUDA from. However, you are not following all the steps given in the book. Please have a look at the details of compilation in the book :-)

Very Mysterious/Random C++ WDK STL 7 Error: iosfwd(202): error C2144: syntax error

I have the following trivial file named Temp.cpp:
#include <string>
int main() { return 0; }
and I'm trying to compile it with the following command-line in the Windows XP Free Build Environment, using WDK 7.1:
cl.exe /Iinc\api\crt\stl70 /Iinc\crt C:\Temp.cpp
and I'm getting really random errors like:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.207 for 80x86
C:\WinDDK\7600.16385.1\inc\api\crt\stl70\iosfwd(202) :
error C2144: syntax error : 'int' should be preceded by ';'
The error goes away if I use stl60 instead of stl70, but that doesn't solve the problem.
What's the cause of the problem?
Update: I tried uninstalling and installing the WDK again, but nothing changed. :(
Update 2: Okay, apparently the error is screaming out at the header file itself: _SCL_INSECURE_DEPRECATE is the cause. Does anybody know how to turn it off correctly? (If I just comment out the lines, I get a ton more errors regarding a bunch of other macros.)
Found the answer myself, through modifying the headers and guess'n'checking:
I need to have _STL70_ defined.
Which cl.exe are you picking up? If your path happens to have an older (VC6) compiler before the WDK one, you'd expect these errors. VC6 can't compile the STL as shipped with VC7
apparently the error is screaming out at the header file itself: _SCL_INSECURE_DEPRECATE is the cause. Does anybody know how to turn it off correctly?
If you're having problems with _SCL_INSECURE_DEPRECATE, try setting:
/D_SCL_SECURE_NO_DEPRECATE
But given the error message you're seeing it sounds like you're the compiling headers with a a compiler that's older than the headers support (so this might not get you very far anyway).

Trouble porting OpenGL app to Windows

I am trying to move an OpenGL app to Windows.
It was my understanding that Windows had a decent OpenGL implementation. But I'm starting to think that it doesn't...
Specifically, I use array buffers and glDrawArrays.
When I tried to compile my code in Visual Studio 2008 Pro, I received the following errors:
vertexbuffers.cpp(31) : error C3861: 'glGenBuffers': identifier not found
vertexbuffers.cpp(32) : error C2065: 'GL_ARRAY_BUFFER' : undeclared identifier
vertexbuffers.cpp(32) : error C3861: 'glBindBuffer': identifier not found
vertexbuffers.cpp(33) : error C2065: 'GL_ARRAY_BUFFER' : undeclared identifier
vertexbuffers.cpp(33) : error C2065: 'GL_STATIC_DRAW' : undeclared identifier
vertexbuffers.cpp(33) : error C3861: 'glBufferData': identifier not found
When I examined <GL\gl.h> (contained in C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include\gl), I saw:
/* ClientArrayType */
/* GL_VERTEX_ARRAY */
/* GL_NORMAL_ARRAY */
/* GL_COLOR_ARRAY */
Update but it would seem that those contants get defined elsewhere.
How am I supposed to generate buffers if I don't have access to those functions?
The documentation doesn't say that those array types are disabled. How do I get access to the real implementation on OpenGL on Windows?
You might give GLEW a shot:
http://glew.sourceforge.net/
I'm pretty sure I used it at some time in the past, and makes this sort of thing a little easier and more portable.
The #defines are commented out in the header file whenever they would otherwise be repeated. Look at line 1054 of gl.h:
/* vertex_array */
#define GL_VERTEX_ARRAY 0x8074
If this #define is actually missing then you should probably replace the file with a fresh copy.
If you look at the documentation for glGenBuffers you will see that it is only available in OpenGL 1.5 and higher. The header file for Windows only comes with OpenGL 1.2 and you should use the extension mechanism to access the newer functionality. If you call wglGetProcAddress with the function name, e.g.
void (__stdcall *glGenBuffers)(GLsizei,GLuint*) =
wglGetProcAddress("glGenBuffers");
then you have a pointer to the function.
It would seem that the buffer functions are only available on Windows as extension methods.
OpenGL provides glext.h that declares pointers to all of these functions. It is then up to my app to use wglGetProcAddress to get pointers to the functions.
For example:
PFNGLGENBUFFERSPROC myglBindBuffers =
(PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffersARB");
Thankfully, I only have to do it for about 4 functions. Unfortunately, I now have to add platform-dependent code to my app.
Microsoft's support for OpenGL stretches only as far as OpenGL-1.1 up to Windows XP, and OpenGL-1.4 starting with Vista. Any OpenGL functionality beyond those must be delivered and supported by the installable client driver (ICD), i.e. the GPU driver's OpenGL implmenentation. To access the advanced functionality, OpenGL provides the so called Extension System, formed by wglGetProcAddress, which is kind of like GetProcAddress for DLLs, but gives access to functions of the OpenGL implementation (=driver).
To make things easier, nice wrapper libraries like GLEW have been developed, which do all the grunt work initializing all the available OpenGL extensions, providing them to the end user.