Where can I find a reference of the CG shader language? - reference

I am new to shaders and I would like to find a list of all (or most/common) functions, variables, availeble to write CG shaders.

This should get you started: http://http.developer.nvidia.com/Cg/index.html

Related

Rcpp::export - exporting only the C++ interface, not the R functions

I do not need roxygen2 and Rcpp to create for me the R functions (or maybe I do?) for the exported C++ functions - is there any way to tell Rcpp::export not to create them? I would be perfectly happy with just .Call-ing them directly.
I went through Writing R Extensions, and Rcpp Attributes and Writing a package that uses Rcpp vignettes, documentation of roxygen2 and multiple threads on SO (like here) but I did not find anything helpful.
If I understand your question correctly, then it is as simple "well if you don't want a stub function created, do no put an [[Rcpp::export]] tag there".
You also confuse what roxygen2 does for documentation with what the compileAttributes() function does for exporting.
To be plain, only the latter has anything to do with creating interfaces between R and C++. And on the margin, you do want them for the free exception handling and RNG setting they give you. But hey, if you'd rather do without, you can, and that is documented.

RcppArmadillo and RcppGSL

I would like to utilize both RcppArmadillo and RcppGSL via sourceCpp. Basically I am interested in modifying the B-spline example
http://dirk.eddelbuettel.com/blog/2012/12/08/
so that the B-splines are functions of R^3 instead of only R^1. This entails working with 3-dimensional arrays which apparently is not supported in GSL (there is an extension here http://savannah.nongnu.org/projects/marray though). However, RcppArmadillo has the arma::cube type which I could use if only I could get RcppArmadillo and RcppGSL to "work together." This I am unfortunately not able to do. I have looked at
Multiple plugins in cxxfunction
but have not succeeded in creating the mentioned combined plugin. Any help is greatly appreciated!
Adam
Edit: It seems like it is actually possible to compile a .cpp file with sourceCpp containing the following sequence of commands at the top:
// [[Rcpp::depends(RcppGSL)]]
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppGSL.h>
#include <gsl/gsl_bspline.h>
Furthermore, it also seems possible to store values like
double gsl_vector_get (const gsl_vector * v, size_t i)
in an arma::cube structure.
RcppArmadillo.h and RcppGSL.h are modelled similarly. They first include RcppCommon.h, then some forward declarations, then they include Rcpp.h that uses those forward declarations, then implementations.
It is definitely possible to use them both if you come up with the right order of includes.
This is definitely an Rcpp question as what is preventing you from using them both is (good or bad) design decisions.
You need to study RcppArmadillo.h and RcppGSL.h and come up with the right include order, or wait for someone to follow these hints and give you the answer. I might not have time to do it myself in the next few days.
Armadillo types and GSL types are not interchangeable.
You could rewrite the GSL algorithm for Armadillo, but it is not automatic but any means. I am also not sure if the theory behind splines extends just like that from the real line to three-dimensions.

Transparency in Progressive Photon Mapping in cuda

I am working on a project, which is based on optix. I need to use progressive photon mapping, hence I am trying to use the Progressive Photon Mapping from the samples, but the transparency material is not implemented.
I've googled a lot and also tried to understand other samples that contains transparency material (e.g. Glass, Tutorial, whitted). At last, I got the solution as follows;
Find the hit point (intersection point) (h below)
Generate another ray from that point
use the color of the new generated points
By following you can also find the code of that part, by I do not understand why I get black color(.0f, .0f, 0.f) for the new generated ray (part 3 above).
optix::Ray ray( h, t, rtpass_ray_type, scene_epsilon );
HitPRD refr_prd;
refr_prd.ray_depth = hit_prd.ray_depth+1;
refr_prd.importance = importance;
rtTrace( top_object, ray, refr_prd );
result += (1.0f - reflection) * refraction_color * refr_prd.attenuation;
Any idea will be appreciated.
Please note that refr_prd.attenuation should contains some colors, after using function rtTrace(). I've mentioned reflection and reflaction_color to help you better understand the procedure. You can simply ignore them.
There are a number of methods to diagnose your problem.
Isolate the contribution of the refracted ray, by removing any contribution of the reflection ray.
Make sure you have a miss program. HitPRD::attenuation needs to be written to by all of your closest hit programs and your miss programs. If you suspect the miss program is being called set your miss color to something obviously bad ([1,0,1] is my favorite).
Use rtPrintf in combination with rtContextSetPrintLaunchIndex or setPrintLaunchIndex to print out the individual values of the product to see which term is zero from a given pixel. If you don't restrict the output to a given launch index you will get too much output. You probably also want to print out the depth as well.

Is D powerful enough for these features?

For the longest time I wanted to design a programming language that married extensibility with efficiency (and safety, ease-of-use, etc.) I recently rediscovered D and I am wondering if D 2.0 is pretty much the language I wanted to make myself. What I love most is the potential of metaprogramming; in theory, could D's traits system enable the following features at compile time?
Run-time reflection: Are the compile-time reflection features sufficient to build a run-time reflection system a la Java/.NET?
Code conversion: Using a metaprogram, create C#/C++/etc. versions of your D program every time you compile it (bonus point if doc comments can be propagated).
Traits. I don't mean the metaprogramming traits built into D, I mean object-oriented traits for class composition. A D program would indicate a set of traits to compose, and a metaprogram would compose them.
Unit inference engine: Given some notation for optionally indicating units, e.g. unit(value), could a D metaprogram examine the following code, infer the correct units, and issue an error message on the last line? (I wrote such a thing for boo so I can assure you this is possible in general, program-wide):
auto mass = kg(2.0);
auto accel = 1.0; // units are strictly optional
auto force = mass*accel;
accel += metresPerSecondSquared(9.81); // units of 'force' and 'accel' are now known
force += pounds(3.0); // unit mismatch detected
Run-time reflection: Are the compile-time reflection features sufficient to build a run-time reflection system a la Java/.NET?
Yes. You can get all the information you need at compile time using __traits and produce the runtime data structures you need for runtime reflection.
Code conversion: Using a metaprogram, create C#/C++/etc. versions of your D program every time you compile it (bonus point if doc comments can be propagated).
No, it simply isn't possible no matter how powerful D is. Some features simply do not transfer over. For example, D has an inline assembler, which is 100% impossible to convert to C#. No language can losslessly convert to all other languages.
Traits. I don't mean the metaprogramming traits built into D, I mean object-oriented traits for class composition. A D program would indicate a set of traits to compose, and a metaprogram would compose them.
You can use template mixins for this, although they don't provide method exclusion.
Unit inference engine: Given some notation for optionally indicating units, e.g. unit(value), could a D metaprogram examine the following code, infer the correct units, and issue an error message on the last line? (I wrote such a thing for boo so I can assure you this is possible in general, program-wide):
Yes, this is straightforward in D. There's at least one implementation already.

SIMD programming languages

In the last couple of years, I've been doing a lot of SIMD programming and most of the time I've been relying on compiler intrinsic functions (such as the ones for SSE programming) or on programming assembly to get to the really nifty stuff. However, up until now I've hardly been able to find any programming language with built-in support for SIMD.
Now obviously there are the shader languages such as HLSL, Cg and GLSL that have native support for this kind of stuff however, I'm looking for something that's able to at least compile to SSE without autovectorization but with built-in support for vector operations. Does such a language exist?
This is an example of (part of) a Cg shader that does a spotlight and in terms of syntax this is probably the closest to what I'm looking for.
float4 pixelfunction(
output_vs IN,
uniform sampler2D texture : TEX0,
uniform sampler2D normals : TEX1,
uniform float3 light,
uniform float3 eye ) : COLOR
{
float4 color = tex2D( texture, IN.uv );
float4 normal = tex2D( normals, IN.uv ) * 2 - 1;
float3 T = normalize(IN.T);
float3 B = normalize(IN.B);
float3 N =
normal.b * normalize(IN.normal) +
normal.r * T +
normal.g * B;
float3 V = normalize(eye - IN.pos.xyz);
float3 L = normalize(light - IN.pos);
float3 H = normalize(L + V);
float4 diffuse = color * saturate( dot(N, L) );
float4 specular = color * pow(saturate(dot(N, H)), 15);
float falloff = dot(L, normalize(light));
return pow(falloff, 5) * (diffuse + specular);
}
Stuff that would be a real must in this language is:
Built in swizzle operators
Vector operations (dot, cross, normalize, saturate, reflect et cetera)
Support for custom data types (structs)
Dynamic branching would be nice (for loops, if statements)
So recently Intel released ISPC which is exactly what I was looking for when asking this question. It's a language that can link with normal C code, has and implicit execution model, and support for all the features mentioned in the start post (swizzle operators, branching, data structs, vector ops, shader like) and compiles for SSE2, SSE4, AVX, AVX2, and Xeon Phi vector instructions.
Your best bet is probably OpenCL. I know it has mostly been hyped as a way to run code on GPUs, but OpenCL kernels can also be compiled and run on CPUs. OpenCL is basically C with a few restrictions:
No function pointers
No recursion
and a bunch of additions. In particular vector types:
float4 x = float4(1.0f, 2.0f, 3.0f, 4.0f);
float4 y = float4(10.0f, 10.0f, 10.0f, 10.0f);
float4 z = y + x.s3210 // add the vector y with a swizzle of x that reverses the element order
On big caveat is that the code has to be cleanly sperable, OpenCL can't call out to arbitrary libraries, etc. But if your compute kernels are reasonably independent then you basically get a vector enhanced C where you don't need to use intrinsics.
Here is a quick reference/cheatsheet with all of the extensions.
It's not really the language itself, but there is a library for Mono (Mono.Simd) that will expose the vectors to you and optimise the operations on them into SSE whenever possible:
It's a library for C++, rather than built into the language, but Eigen is pretty invisible once your variables are declared.
Currently the best solution is to do it myself by creating a back-end for the open-source Cg frontend that Nvidia released, but I'd like to save myself the effort so I'm curious if it's been done before. Preferably I'd start using it right away.
The D programming language also provides access to SIMD in a similar way than Mono.SIMD.
That would be Fortran that you are looking for. If memory serves even the open-source compilers (g95, gfortran) will take advantage of SSE if it's implemented on your hardware.
I know this question is a bit old, but I found myself in a similar predicament and decided I should just make my own.
I haven't gotten very far yet in the slightest, but if you're interested in the directions that I'm exploring it might be worth a look. :)
https://github.com/HappMacDonald/MasterBlaster
MasterBlaster is a functional programming language, but it's going to compile down into a bytecode that is ultimately it's own much simpler stack-based language called Crude. Crude then compiles directly into assembly.
My strategy is a SIMD-first one: unoptimized executables will use almost entirely SIMD, and then one of the potential optimizations will be to simplify code that isn't benefiting from SIMD into using only general registers instead.
Crude is up to the turing-complete stage, but only exists as a few dozen GAS macros presently. I'm working towards a self-contained compiler for it, and building out the iterator/generator features that are the stars of the show when it comes to SIMD acceleration.
No vector-matrix-etc support just yet, but that is on the roadmap and I'll probably bear your description in mind when writing up that syntax. :)

Resources