ABC of compiling shaders - directx

I swear I have been looking for the answer to this question exhaustively, but I found no real solution to my problem. And what problem is that?
Well I am new to DirectX and shaders. There are a few things about shaders that I still don't get.
1 - How to make a shader? Do I have to create an .fx file in the project? Some times it is so, but in some examples I can't find any .fx file. And how do I make this file? My version of Visual Studio can't directly create .fx files; I have to "force" the file to be .fx.
2 - How I compile them? Are they compiled at the same time I compile solution or they have special ways to compile?
3 - Is there a nice tutorial around? I have been looking for a shaders-bible but mostly I found vague and short tutorials explaining few things, and never in a deep way.

1. New to Shader ?
To get the introduction to sharder use the free shazzam shader editor (http://shazzam-tool.com/) for creating the simple shaders by interactive draw tools. Try to play with different option and then compare the automatically generated HLSL(.fx) codes for better understanding. After you got the feel of how the shader code to be written buy a standard book/online tutorial and practice to write your own code according to your requirements.
2. Common Methods for compilation:
a. D3DXCreateEffectFromFile- Write the shader code and save in .fx extension and dynamically compile the code by D3DXCreateEffectFromFile. Compiled code can be used in your core module using effect(ID3DXEffect) interface.
b. Explicit Compilation: Write the shader code and save in .fx extension and explicitly compile the code using fxc.exe (You can find in DirectX SDK Utility folder).
Example:
fxc.exe /Tfx_2_0 /Fo file.fxo file.fx
After binary file is created follow as below
1. Create a buffer and load the generated binary file(.fxo) by the file stream.
2. Call D3DXCreateEffect and give the buffer content as a input parameter.
3. Like "method a" use effect(ID3DXEffect) interface for interacting with the
shader code.
3. Introduction Tutorial:
http://rbwhitaker.wikidot.com/hlsl-tutorials

Shaders are just like normal text files you put your shader code in them. You don't need to add them to your project if you are compiling the shader at runtime with the D3D functions.
There are two ways to do this. One is putting your shader code in .fx files or *.hlsl files and then compiling the shader at runtime using D3D library functions (D3DCompileFromFile). Though Microsoft is not suggesting this anymore because D3DCompileFromFile won't work in Metro style apps. The other way is to use fxc.exe to compile your shaders at build time. Visual Studio 2012 made this process part of the usual build. So you can add your hlsl files into your project and they will be build when you build your project. This will also enable you to see any errors/warnings in the shader at compile time.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb509633(v=vs.85).aspx
Hope that helps.

Related

How to link metal utility shaders with local executable metal library using SPM

The bounty expires in 3 days. Answers to this question are eligible for a +50 reputation bounty.
Affinity wants to draw more attention to this question.
The idea is having a Utilities.metal shader with helper functions (let's say converting color: rgb2hsv(), ... ) that I want to reuse in many projects. I'd like to access from the local project's metal shader files those helper functions while sharing it as a swift package.
Goals:
Having a Utilities.metal shader file or files in a swift package that I can add to other projects.
Calling that rgb2hsv() function from local shaders. I want to access the functions on that Utilities.metal file from a local executable metal library's (the one that you create in the local project with MTLDevice.makeDefaultLibrary() shaders (doing #include "Utilities.h" in local metal shaders?).
I want the package to contain the .metal files so I can update and commit them from any project (as local package in tandem with the app) not just the metallib.
What I don't want:
Making the bundle library MTLDevice.makeDefaultLibrary(bundle:) to use it inside the package or locally as a stand alone library. I want to link the functions and have access to them.
Avoid if possible hacky things like converting the shaders files to strings and using MTLDevide.makeLibrary(source: source options: nil).
Do I need to use MTLDynamicLibrary? The problem is it's not supported on many devices. Any help is welcome.

Compiling multiple .metal files into one .metallib

I’m currently writing some custom Core Image filters using Metal. For the sake of structure I want to put the different kernels into different .metal files with some common includes like you would do with “normal” source files.
However, when the metallib tool bundles the different .air files created by the Metal compiler into one .metallib file, only the kernel functions defined in the first input .air file given to metallib are visible. Functions from the other .air files don’t seem to be included. What’s the reason for this?
I thought (as is the default compilation behavior for Metal files) all Metal sources get compiled into one library that is then used by every custom CIFilter class to instantiate their internal CIKernel with the function they need.
I now ended up compiling a .metallib file for each custom filter with custom build rules and copying all of them them into my framework using a custom build phase. This doesn't seem to be the intended way…

Direct2D Effects

I am trying to follow instructions on creating Custom Direct2D Effects, as described in Custom Effects and Effect Shader Linking. I am getting into number of problems:
The "d2d1effecthelpers.hlsli" file in not found in my Windows SDK. I could only find it here.
When trying to run fxc compiler with model set to lib_4_0_level_9_1_ps_only I get an error that this model is not supported.
When using lib_4_0_level_9_1 instead, I get another compilation error, saying that static variables are not supported for libraries (and this static definition comes from the above HLSLI file, not from my code).
After all my goal is to create a pixel shader for Direct2D, which will be receiving number of textures as input. Maybe something in my environment is missing and I can't follow the examples (I'm using Windows 8.1). Any suggestions are welcome.

27MB IPA with just GStreamer iOS Framework... how do I make much smaller?

I'm very interested in using GStreamer's iOS framework http://docs.gstreamer.com/display/GstSDK/Installing+for+iOS+development for video streaming, but when I add the framework to a blank project and add a few lines of code to take advantage of its powerful features, the final IPA is 27MB. This is just way to big to be adding to my project, what is the best way to go about stripping this down the the bare necessities as I'm sure I'm only using a small percent of the code that is included in the SDK.
Here's a pic showing the package contents of the IPA:
Thanks!
In the gst_ios_main.h you can disable all the plugins that you don't need (make sure to enable linker optimizations so that unused code is removed). If that's not enough, you can build your own stripped down version of the iOS binaries with http://cgit.freedesktop.org/gstreamer/cerbero/ (you need to remove things from the .package and .recipe files to only build what you need). Just disabling things from gst_ios_main.h should be enough in 99% of the cases though.
Note that by default you'll build applications for multiple architectures, as such the resulting application will be rather large. Depending on your use case you can drop some architectures.
On another note, gstreamer.com is providing an completely outdated version of GStreamer and is in no way related to the GStreamer project. The official website is http://gstreamer.freedesktop.org .
SDKs have their source code encapsulated away from you, the user. You get access only to header files. Thus you just can't extract some class from it because you don't have access to the implementation file.
Of course if this library is opensource you can attempt to isolate one class, but sometimes everything is so deeply connected, that it is close to impossible.

Light options for kiwiViewer

Im trying to add light options to kiwiviewer for visualization of medical 3D Models, in my case a surface model of a head.
I cant find a starting point for hours becouse when I delete all glsl shaders I still get the console output "INFO: Compiling shaders:" without error and the View with the 3D Model displaying correctly
I would appreciate any help.
Manuel
At build time, each shader is encoded into a cpp file and compiled into the library named libvesShaders.a. If you modify a shader, but do not recompile the library and relink the application, the app will continue to operate with the previous version of the shader.
If you want to avoid the recompile step, you could create a new shader file and add it to the xcode project so that the file is packaged with the app. Then read the shader source from the file on the iOS filesystem at runtime.
You might want to try the VES mailing list for VES and KiwiViewer questions in the future.

Resources