HLSL Array Indexing Returning Unexpected Values - unity3d

I am executing some HLSL code on the GPU in Unity, but I am having issues with getting values out of an array. Here is my simplified code example.
C#
ComputeBuffer meshVerticesBuffer = new ComputeBuffer(
15 * 1,
sizeof(float) * 3
);
marchingCubesShader.SetBuffer(0, "MeshVertices", meshVerticesBuffer);
marchingCubesShader.Dispatch(0, 1, 1, 1);
Vector3[] meshVertices = new Vector3[15 * 1];
meshVerticesBuffer.GetData(meshVertices);
meshVerticesBuffer.Release();
HLSL
#pragma kernel ApplyMarchingCubes
int EDGE_TABLE[][15] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
...255 more irrelevant entries
};
RWStructuredBuffer<float3> MeshVertices;
[numthreads(4, 4, 4)]
void ApplyMarchingCubes(uint3 id : SV_DispatchThreadID)
{
MeshVertices[0] = float3(0, 0, EDGE_TABLE[0][0]);
}
I am watching meshVertices on the C# side through the debugger, and the first item is always a Vector3(0, 0, 0). I am expecting a result of Vector3(0, 0, -1). What am I doing wrong?

I figured out why the array was not putting out the right values.
In HLSL, when declaring and initializing an array in the same line, you must include the static keyword.
My HLSL code should have been:
#pragma kernel ApplyMarchingCubes
static int EDGE_TABLE[][15] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
...255 more irrelevant entries
};
RWStructuredBuffer<float3> MeshVertices;
[numthreads(4, 4, 4)]
void ApplyMarchingCubes(uint3 id : SV_DispatchThreadID)
{
MeshVertices[0] = float3(0, 0, EDGE_TABLE[0][0]);
}
(Notice static on the third line, it was not there before).

Related

Distribute elements evenly (adding ints values to array of int values)

Say I have an array of Ints and all elements equal zero. It'd look something like this:
let arr: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
There are 11 elements in this array in total. I want three of the elements in this array to be the number one. I want these one values to be distributed evenly throughout the array so that it looks something like this:
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
I want to be able to add however many one's and distribute them evenly (or as close to evenly as possible) no matter how many total elements there are. How could I do this?
Note: For anyone wondering why I need this, I have a collection of strings that when joined together make up a large body of text. Think of the zeroes as the pieces of text and think of the ones as advertisements I am adding in between the text. I wanted to distribute these ads as evenly as possible. I figured this would be a simple way of expressing what I needed.
Maybe you can try this.
var arr: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
let distribution = arr.count / 3 // 3 is the number of 1s
for (index, value) in arr.enumerated() {
arr[index] = (index + 1) % distribution == 0 ? 1 : value
}
print(arr) // [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
Assuming that the value distribution > 1

What should the results of a convolution be?

I'm using convnetjs to build an interactive tutorial (as I learn it myself). I have a simple 9x9 image of an 'X' and a convolutional layer with one of the filters as a 3x3 '\'...
I expected the results to be different. I expected the circled result on the right to be (-1+1+1+1+1+1+1+1+1)/9 = 0.77 instead of 7.1.
What else is happening that results in 7.1? Is this due to biases? I also expected the whole result to show highest numbers along the '\' diagonal, since the filter is that shape which would match the '\' part of the 'X'.
UPDATE: I expected the results to be the following. The biases appear to be an array [0.1,0.1,0.1]. What is the calculation that yeilds the above results (for at least the upper left pixel), instead of the below?
<html>
<head>
<script src="http://cs.stanford.edu/people/karpathy/convnetjs/build/convnet-min.js"></script>
</head>
<body>
<script>
// Initialize an input that is 9x9 and initialized with zeroes.
let inputVol = new convnetjs.Vol(9, 9, 1, 0.0);
// Manually set the input weights from zeroes to a 'X'...
inputVol.w = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
// Define the layers
let layers = [];
layers.push({
type: 'input',
out_sx: 9,
out_sy: 9,
out_depth: 1
});
layers.push({
type: 'conv',
sx: 3,
pad: 0,
filters: 3,
stride: 1,
activation: 'relu'
});
let net = new convnetjs.Net();
net.makeLayers(layers);
let convLayer = net.layers[1];
let convLayerFilters = convLayer.filters;
// Set filters manually
// looks like a '\'
convLayerFilters[0].w = [1, -1, -1, -1, 1, -1, -1, -1, 1];
// looks like a 'X'
convLayerFilters[1].w = [1, -1, 1, -1, 1, -1, 1, -1, 1];
// looks like a '/'
convLayerFilters[2].w = [-1, -1, 1, -1, 1, -1, 1, -1, -1];
// Run the net
net.forward(inputVol);
// Prints '7.1' instead of '0.77'. Why???
console.log(net.layers[1].out_act.w[0]);
</script>
</body>
</html>
Yes it is happening because of biasing so that it stays in the range defined.
Bias is the value that you can add to each element in a convolution result to add additional influence from neighbouring pixels. Since with certain convolutions it is possible to get negative numbers (which are not representable in a 0–255 format), bias prevents the signal from drifting out of range. You can choose to add a bias of 127 or 128 to allow some negative numbers to be representable (with an implicit +127 or +128 in their value).

Autohotkey Most of numpad keys don't work

Here is my script. When I try it, only the NumpadClear (Num5) key works. I use Windows 7 64bit.
#SingleInstance force
#UseHook
#InstallKeybdHook
*~NumpadIns::MouseMove, 0, 1, 0, R
*~NumpadClear::MouseMove, 0, -1, 0, R
*~NumpadEnd::MouseMove, -1, 0, 0, R
*~NumpadPgDn::MouseMove, 1, 0, 0, R
*~NumpadDown::Click
*~NumpadEnter::Click Right
This AutoHotkey script should achieve what you're looking for.
It will work whether NumLock is turned off or on.
#SingleInstance force
#UseHook
#InstallKeybdHook
NumpadIns::MouseMove, 0, 1, 0, R
NumpadClear::MouseMove, 0, -1, 0, R
NumpadEnd::MouseMove, -1, 0, 0, R
NumpadPgDn::MouseMove, 1, 0, 0, R
NumpadDown::Click
NumpadEnter::Click Right
Numpad0::MouseMove, 0, 1, 0, R
Numpad5::MouseMove, 0, -1, 0, R
Numpad1::MouseMove, -1, 0, 0, R
Numpad3::MouseMove, 1, 0, 0, R
Numpad2::Click
;NumpadEnter::Click Right

GTK3 - Create image object from stock

I want to create a grid which displays error message if the file does not exist:
/* size */
s = get_file_size(recording->filename);
if (s > 0) {
size = g_format_size_full(s, G_FORMAT_SIZE_LONG_FORMAT);
gtk_label_set_text(GTK_LABEL(size_lbl), size);
gtk_widget_hide(error)
g_free(size);
} else {
size = g_strdup(_("Import Errors"));
gtk_widget_show(error)
}
in gtk grid cannot set type of "error" element to display message as in screen shot:
grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
gtk_grid_set_column_spacing(GTK_GRID(grid), 15);
gtk_container_set_border_width(GTK_CONTAINER(grid), 6);
s_lbl = gtk_label_new(Size:);
size_lbl = gtk_label_new("");
error = ?
error_pixmap = gtk_image_new_from_stock(GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_SMALL_TOOLBAR);
gtk_container_add(GTK_CONTAINER(error), error_pixmap);
gtk_grid_attach(GTK_GRID(grid), s_lbl, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), error, 1, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), size_lbl, 2, 0, 1, 1);
For any help, thanks.
Screen shot:
[Size]:
Use a GtkBox with GTK_ORIENTATION_HORIZONTAL, or an adjoining cell in the GtkGrid.

GWT Flex Table Problem

I am creating a flex table dynamically with the following code.
for (int CurrentRow=1;CurrentRow<2;CurrentRow++)
{
Label lblGettingName = new Label("Getting Name...");
View.getMainFlex().setWidget(CurrentRow, 0, lblGettingName);
Button btnViewDetails = new Button("View Details");
View.getMainFlex().setWidget(CurrentRow, 1, btnViewDetails);
Label lblGettingBid = new Label("Getting Bid...");
View.getMainFlex().setWidget(CurrentRow, 2, lblGettingBid);
View.getMainFlex().getFlexCellFormatter().setStyleName(CurrentRow, 2, "BackNormalNotBold");
Label lblGettingBidDesription = new Label("Getting Bid Desription...");
lblGettingBidDesription.setStyleName("BidDesc");
View.getMainFlex().setWidget(CurrentRow, 3, lblGettingBidDesription);
View.getMainFlex().getCellFormatter().setWidth(CurrentRow, 3, "40");
View.getMainFlex().getFlexCellFormatter().setStylePrimaryName(CurrentRow, 3, ".BidDesc");
Label lblCalculating = new Label("Calculating..");
Label lblCalculatingTime = new Label("Calculating Time...");
View.getMainFlex().setWidget(CurrentRow, 4, lblCalculatingTime);
View.getMainFlex().getFlexCellFormatter().setStyleName(1,4, "BackNormalNotBold");
TextBox textBox = new TextBox();
View.getMainFlex().setWidget(CurrentRow+1, 3, textBox);
View.getMainFlex().getCellFormatter().setWidth(CurrentRow+1, 3, "40");
View.getMainFlex().getFlexCellFormatter().setStyleName(CurrentRow, 0, "BackNormalNotBold");
View.getMainFlex().getFlexCellFormatter().setStyleName(CurrentRow, 1, "BackNormalNotBold");
View.getMainFlex().getFlexCellFormatter().setStyleName(CurrentRow, 2, "BackNormalNotBold");
View.getMainFlex().getFlexCellFormatter().setStyleName(CurrentRow+1, 3, "BackNormalNotBold");
View.getMainFlex().getFlexCellFormatter().setRowSpan(CurrentRow, 4, 3);
View.getMainFlex().getFlexCellFormatter().setRowSpan(CurrentRow, 2, 3);
View.getMainFlex().getFlexCellFormatter().setRowSpan(CurrentRow, 1, 3);
View.getMainFlex().getFlexCellFormatter().setRowSpan(CurrentRow, 0, 3);
View.getMainFlex().getFlexCellFormatter().setColSpan(CurrentRow+1, 3, 2);
View.getMainFlex().getFlexCellFormatter().setColSpan(CurrentRow, 3, 2);
View.getMainFlex().getFlexCellFormatter().setColSpan(CurrentRow-1, 3, 2);
View.getMainFlex().getCellFormatter().setHorizontalAlignment(CurrentRow, 1, HasHorizontalAlignment.ALIGN_CENTER);
View.getMainFlex().getCellFormatter().setVerticalAlignment(CurrentRow, 1, HasVerticalAlignment.ALIGN_MIDDLE);
View.getMainFlex().getCellFormatter().setVerticalAlignment(CurrentRow, 0, HasVerticalAlignment.ALIGN_MIDDLE);
View.getMainFlex().getCellFormatter().setHorizontalAlignment(CurrentRow, 0, HasHorizontalAlignment.ALIGN_CENTER);
View.getMainFlex().getCellFormatter().setHorizontalAlignment(CurrentRow, 2, HasHorizontalAlignment.ALIGN_CENTER);
View.getMainFlex().getCellFormatter().setHorizontalAlignment(CurrentRow+1, 3, HasHorizontalAlignment.ALIGN_CENTER);
View.getMainFlex().getCellFormatter().setHorizontalAlignment(CurrentRow, 3, HasHorizontalAlignment.ALIGN_CENTER);
Button btnPlaceBid = new Button("Bid!");
View.getMainFlex().setWidget(CurrentRow+2, 3, btnPlaceBid);
View.getMainFlex().getCellFormatter().setWidth(CurrentRow+2, 3, "20");
btnPlaceBid.setSize("66px", "26px");
ToggleButton tglbtnAutomate = new ToggleButton("Automate");
View.getMainFlex().setWidget(3, 4, tglbtnAutomate);
View.getMainFlex().getCellFormatter().setWidth(3, 4, "20");
tglbtnAutomate.getDownHoveringFace().setText("TurnOFF");
tglbtnAutomate.getUpHoveringFace().setText("TurnON");
tglbtnAutomate.getDownDisabledFace().setText("Enable");
tglbtnAutomate.setHTML("Auto:OFF");
tglbtnAutomate.getUpFace().setHTML("Auto:OFF");
tglbtnAutomate.getDownFace().setHTML("Auto:ON");
tglbtnAutomate.setSize("54px", "18px");
View.getMainFlex().getCellFormatter().setHorizontalAlignment(CurrentRow+2, 3, HasHorizontalAlignment.ALIGN_RIGHT);
View.getMainFlex().getCellFormatter().setHorizontalAlignment(CurrentRow-1, 4, HasHorizontalAlignment.ALIGN_CENTER);
View.getMainFlex().getCellFormatter().setHorizontalAlignment(CurrentRow, 4, HasHorizontalAlignment.ALIGN_CENTER);
}
FlexTableHelper.fixRowSpan(View.getMainFlex());
When the loop executes only once, the correct layout is generated but when i try to create more than 1 row, the layout degerate
Most likely problems with flextable are caused by setRowSpan/setColSpan methods which can easily wreak havoc in layout. Instead of using those methods you can create composite widget/Html and place it in cells so Flextable will have less amount of rows/columns.
FlexTable uses old element attributes of td like width, height, align etc.
It is not yet adapted to use CSS instead even in the newest release. IE11 does no longer support <td align="..."> at all, for example.