Output for sample code for an upcoming exam concerning pthread - pthreads

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int token = 2;
int value = 3;
void * red ( void *arg ) {
int myid = * ((int *) arg);
pthread_mutex_lock( &mutex );
while ( myid != token) {
pthread_cond_wait( &cond, &mutex );
}
value = value + (myid + 3);
printf( "RED: id is %d \n", value);
token = (token + 1) % 3;
pthread_cond_broadcast( &cond );
pthread_mutex_unlock( &mutex );
}
void * blue ( void *arg ) {
int myid = * ((int *) arg);
pthread_mutex_lock( &mutex );
while ( myid != token) {
pthread_cond_wait( &cond, &mutex );
}
value = value * (myid + 2);
printf( "BLUE: id is %d \n", value);
token = (token + 1) % 3;
pthread_cond_broadcast( &cond );
pthread_mutex_unlock( &mutex );
}
void * white ( void *arg ) {
int myid = * ((int *) arg);
pthread_mutex_lock( &mutex );
while ( myid != token) {
pthread_cond_wait( &cond, &mutex );
}
value = value * (myid + 1);
printf( "WHITE: id is %d \n", value);
token = (token + 1) % 3;
pthread_cond_broadcast( &cond );
pthread_mutex_unlock( &mutex );
}
main( int argc, char *argv[] ) {
pthread_t tid;
int count = 0;
int id1, id2, id3;
id1 = count;
n = pthread_create( &tid, NULL, red, &id1);
id2 = ++count;
n = pthread_create( &tid, NULL, blue, &id2);
id3 = ++count;
n = pthread_create( &tid, NULL, white, &id3);
if ( n = pthread_join( tid, NULL ) ) {
fprintf( stderr, "pthread_join: %s\n", strerror( n ) );
exit( 1 );
}
}
I am just looking for comments and or notes to what the output would be. THIS IS FOR AN EXAM AND WAS OFFERED AS AN EXAMPLE. THIS IS NOT HOMEWORK OR GOING TO BE USED FOR ANY TYPE OF SUBMISSION. I am looking to understand what is going on. Any help is greatly appreciated.

I'm going to assume that you know the function of the locks, condition variables, and the waits. Basically you have three threads that each call Red, Blue, and White. Token is originally 2, and value is originally 3.
Red is called when id1 = 0, but it will stay in the while block calling wait() until the token = 0.
Blue is called when id3 = 1, and will stay in the while block called wait() until the token is 1.
White is called when id2 = 2, and will stay in the while block calling wait() until the token is 2.
So White will enter the critical section first, since it's the only one that won't enter the while loop. So value = 3 * ( 3 ) = 9; token = ( 3 ) % 3 = 0;
Broadcast wakes every waiting thread, but the only one that will enter the critical section is Red. It adds 3 to value for 12; token = ( 1 ) % 3 = 1; Broadcast wakes Blue.
Blue enters the critical section. value = 12 * 3; token = 2 ( but it doesn't matter anymore ).
This would be the order of the threads would execute, which is what I assume the test is really asking. However, what should really come out is just:
White is 9
This is because there is only one pthread_t tid. So after pthread_join( tid, NULL ), it can immediately exit. If you put different pthread_t in each of the pthread_create() then all of them would print.

Related

While there are no MQL4 errors, why there was no GUI drawing produced?

For a learning purpose, I am trying to code a simple MA indicator that changes color when price crosses. Though there are no errors, it draws nothing. Could you review the attached code to show me my mistake?
#property indicator_chart_window
#property indicator_buffers 2
extern int maperiod = 20;
extern int maprice = PRICE_CLOSE;
extern int mamethod = MODE_SMA;
extern color colorup = Green;
extern color colordn = Red;
double mamain[];
double bufferup[];
double bufferdn[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init(){
//--- indicator buffers mapping
SetIndexBuffer( 0, bufferup );
SetIndexStyle( 0, DRAW_LINE, 0, 2, colorup );
SetIndexBuffer( 1, bufferdn );
SetIndexStyle( 1, DRAW_LINE, 0, 2, colordn );
//---
return( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start(){
//---
int counted_bars = IndicatorCounted();
if ( counted_bars < 0 ) return( -1 );
if ( counted_bars > 0 ) counted_bars--;
int limit = Bars - counted_bars;
for ( int i = limit; i >= 0 ; i-- )
{ mamain[i] = iMA( NULL, 0, maperiod, 0, 0, 0, i );
if ( mamain[i] >= iClose( NULL, 0, i ) ) bufferup[i] = mamain[i];
if ( mamain[i] <= iClose( NULL, 0, i ) ) bufferdn[i] = mamain[i];
}
//--- return value of prev_calculated for next call
return( 0 );
}
//+------------------------------------------------------------------+
"Old"-MQL4 may work for some time, but still, get used to new features:
extern ENUM_APPLIED_PRICE MAprice = PRICE_CLOSE; // AVOIDS incompatible values
extern ENUM_MA_METHOD MAmethod = MODE_SMA; // AVOIDS incompatible values
#define MAshift 0 // ADDS code == intent match-robustness
extern int MAperiod = 20;
extern color colorUP = clrGreen;
extern color colorDN = clrRed;
double bufferUP[];
double bufferDN[];
int init(){
ArrayInitialize bufferUP, EMPTY_VALUE );
SetIndexBuffer( 0, bufferUP );
SetIndexStyle( 0, DRAW_LINE, EMPTY, 2, colorUP );
SetIndexLabel( 0, "MA_ABOVE_Close" );
ArrayInitialize bufferDN, EMPTY_VALUE );
SetIndexBuffer( 1, bufferDN );
SetIndexStyle( 1, DRAW_LINE, EMPTY, 2, colorDN );
SetIndexLabel( 1, "MA_UNDER_Close" );
return( 0 );
}
int start(){
int counted_bars = IndicatorCounted();
if ( counted_bars < 0 ) return( -1 );
if ( counted_bars > 0 ) counted_bars--;
for ( int i = Bars - counted_bars; i >= 0 ; i-- ){
double C = iClose( _Symbol, PERIOD_CURRENT, i ),
A = iMA( _Symbol, PERIOD_CURRENT,
MAperiod,
MAshift,
MAmethod,
MAprice,
i
);
if ( A >= C ) bufferUP[i] = A;
if ( A <= C ) bufferDN[i] = A;
}
return( 0 );
}
New-MQL4.56789 uses another call-signature if #property strict:
int OnCalculate( const int rates_total,
const int prev_calculated,
const datetime &time_______ARR[],
const double &open_______ARR[],
const double &high_______ARR[],
const double &low________ARR[],
const double &close______ARR[],
const long &tick_volumeARR[],
const long &volume_____ARR[],
const int &spread_____ARR[]
){
//--- the main loop of calculations
for( int i = prev_calculated - 1;
( i < rates_total
&& !IsStopped() ); // AVOID SHARED (!) solo-THREAD BLOCKING
i++
){
// -----------------------------------------------------------------
double A = iMA( _Symbol, PERIOD_CURRENT,
MAperiod,
MAshift,
MAmethod,
MAprice,
i
);
if ( A >= close______ARR[i] ) bufferUP[i] = A;
if ( A <= close______ARR[i] ) bufferDN[i] = A;
// -----------------------------------------------------------------
}
return( rates_total );
}
your buffer mamain[] is not initialized.
int init(){
IndicatorBuffers(3);
SetIndexBuffer(2,mamain);
}
rates_total and prev_calculated seems preferred but of course you can use IndicatorCounted() but keep in mind the corner situation with the first bar: when you first attach the indicator to the chart, your counted_bars = 0 and limit = Bars, but mamain[] and other indicator buffers have Bars elements only, from 0 to Bars-1. so better to use
int limit = Bars - counted_bars - 1;
About resolving issues - in addition to asking here you can always try to attach your indicator to a chart and see it there's no error (terminal window - Experts folder), that will make delevopment faster

Any MQL4 programmers? What is wrong with this code?

When I try to divide the two doubles in a buffer my indicator blacks out, and the values go extreme in the second window -90000000 and 90000000
#property indicator_separate_window // Îòîáðàæåíèå â îòäåëüíîì îêíå
#property indicator_buffers 3 // Êîëè÷åñòâî áóôåðîâ
#property indicator_color1 Red // Öâåò ïåðâîé ëèíèè
#property indicator_color2 Blue // Öâåò âòîðîé ëèíèè
#property indicator_color3 Green
double FillBuffer[];
double DBuffer[];
double AverageBuffer[];
double H,L;
double point=Point();
int init() // Ñïåöèàëüíàÿ ôóíêöèÿ init()
{
int period = _Period;
string symbol = Symbol();
int digits = _Digits ;
point = _Point ;
if(digits == 5 || digits == 3) { digits = digits - 1 ; point = point * 10 ; }
SetIndexBuffer(0,DBuffer);
SetIndexBuffer(1,FillBuffer);
SetIndexBuffer(2,AverageBuffer);
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,1);
SetIndexLabel(0, "ADR");
return(INIT_SUCCEEDED);
}
int start()
{
int i, limit, counted_bars;
counted_bars = IndicatorCounted();
//---- check for possible errors
if (counted_bars<0) return(-1);
//---- last counted bar will be recounted
if (counted_bars>0) counted_bars--;
limit = Bars - counted_bars;
for (i = limit; i >= 0; i--)
{
double dbuff= iHigh(NULL,0,i)- iLow(NULL,0,i);
double D0 = iHigh(NULL,0,i+1)- iLow(NULL,0,i+1);
double D1 = iHigh(NULL,0,i+2)- iLow(NULL,0,i+2);
double D2 = iHigh(NULL,0,i+3)- iLow(NULL,0,i+3);
double D3 = iHigh(NULL,0,i+4)- iLow(NULL,0,i+4);
double D4 = iHigh(NULL,0,i+5)- iLow(NULL,0,i+5);
double Average = ((D0+D1+D2+D3+D4)/5)/point;
FillBuffer[i]=dbuff/Average;
}
return(0);
When I try to divide the two values in FillBuffer[] my indicator blacks out. But if I just have either the dbuff or Average in the buffer it will show lines but I want the percentage one is of the other to be printed.
There is not much wrong with the code, but it does not paint the line:
Some polishing may help, but the core logic of the MQL4 Custom Indicator is forgotten in the code. If one does not assign a way, how to plot ( paint ) the line on screen, the GUI will remain "blacked-out" even though the values may have gotten calculated.
Performance warning:
Custom Indicators ( all Custom Indicators ) share one common solo-thread (!!), so proper performance tuning is warmly recommended in this type of MQL4-code-execution blocks.
Some further acceleration might be achieved by reducing / avoiding all the repetitive re-averaging via a sliding-window implementation replacement. While this is not so risky at 5-BARs deep re-processing, for deeper TimeSeries convolutions, the effect is significant and impressive in Strategy Tester accelerated mode of computing's run-times ( getting down to minutes instead of hours ). Worth one's time and efforts.
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Green
double FillBuffer[];
double DBuffer[]; // NEVER FILLED IN
double AverageBuffer[]; // NEVER FILLED IN
double point = Point();
int init()
{ int period = _Period; // NEVER CONSUMED
string symbol = Symbol(); // NEVER CONSUMED
int digits = _Digits ; // LOCAL SCOPE ONLY VISIBLE INSIDE init(){...}
point = _Point ;
if ( digits == 5 || digits == 3 ) { digits -= 1;
point *= 10;
}
SetIndexBuffer( 0, DBuffer );
SetIndexBuffer( 1, FillBuffer ); // 1: ASSIGNED, BUT NEVER SET TO HAVE ANY { DRAW_LINE | DRAW_ ... } GUI OUTPUT
SetIndexBuffer( 2, AverageBuffer ); // 2: ASSIGNED, BUT NEVER SET TO HAVE ANY { DRAW_LINE | DRAW_ ... } GUI OUTPUT
SetIndexStyle ( 0, DRAW_LINE, STYLE_SOLID, 1 ); // 0: SET AS DRAW_LINE ( BUT NEVER FILLED IN WITH DATA )
SetIndexLabel( 0, "ADR" );
return( INIT_SUCCEEDED );
}
int start()
{ int i, limit, counted_bars;
counted_bars = IndicatorCounted();
//----check for possible errors
if ( counted_bars < 0 ) return( -1 );
//----last counted bar will be recounted
if ( counted_bars > 0 ) counted_bars--;
limit = ( Bars - 5 ) - counted_bars; // AVOID 1st 5 BARS,
int i0, i1, i2, i3, i4, i5; // WHERE (i+5) WILL OVERFLOW THE TIMESERIES LEFT EDGE
for ( i = limit,
i1 = i + 1,
i2 = i + 2,
i3 = i + 3,
i4 = i + 4,
i5 = i + 5; i >= 0; i--,
i1--,
i2--,
i3--,
i4--,
i5--
)
{ FillBuffer[i] = ( High[i]
- Low[i]
)
/ ( ( High[i1] + High[i2] + High[i3] + High[i4] + High[i5] )
- ( Low[i1] + Low[i2] + Low[i3] + Low[i4] + Low[i5] )
)
/ 5.
/ point;
/* double dbuff = iHigh( NULL, 0, i ) - iLow( NULL, 0, i );
double D0 = iHigh( NULL, 0, i+1 ) - iLow( NULL, 0, i+1 );
double D1 = iHigh( NULL, 0, i+2 ) - iLow( NULL, 0, i+2 );
double D2 = iHigh( NULL, 0, i+3 ) - iLow( NULL, 0, i+3 );
double D3 = iHigh( NULL, 0, i+4 ) - iLow( NULL, 0, i+4 );
double D4 = iHigh( NULL, 0, i+5 ) - iLow( NULL, 0, i+5 );
double Average = ( ( D0 + D1 + D2 + D3 + D4 ) / 5 ) / point;
FillBuffer[i] = dbuff / Average;
*/
}
return( 0 );
}

My First MQL4 EA does not generate any Orders. Why?

I'm trying to build a first EA, code below, but it doesn't execute any trades.
I know it is very simple, but logically, I think this should Buy and Sell.
I'm trying to only use a code that I understand.
I'd appreciate it if anyone had any feedback!
//
extern int sma_short = 10;
extern int sma_long = 20;
extern double fakeout = 0.0005 ;
extern double stoploss = 150;
extern double risk = 1;
extern int slippage = 5;
extern int magicnumber = 12345;
extern bool SignalMail = false;
extern bool UseTrailingStop = true;
extern int TrailingStop = 150;
double sma_short_t3;
double sma_short_t0;
double sma_long_t3;
double sma_long_t0;
double sma_diff_t3;
double sma_diff_t0;
double lots;
double stoplosslevel;
int P = 1;
int ticket, ticket2;
int total = OrdersTotal();
bool OpenLong = false;
bool OpenShort = false;
bool CloseLong = false;
bool CloseShort = false;
bool isYenPair = false;
bool OpenOrder = false;
int OnInit()
{
if ( Digits == 5 || Digits == 3 || Digits == 1 ) P = 10; else P = 1; // To account for 5 digit brokers
if ( Digits == 3 || Digits == 2 ) isYenPair = true; // Adjust for YenPair
return( INIT_SUCCEEDED );
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit( const int reason )
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void Start()
{
sma_short_t3 = iMA( NULL, 0, sma_short, 0, MODE_SMA, PRICE_CLOSE, 3 );
sma_short_t0 = iMA( NULL, 0, sma_short, 0, MODE_SMA, PRICE_CLOSE, 0 );
sma_long_t3 = iMA( NULL, 0, sma_long, 0, MODE_SMA, PRICE_CLOSE, 3 );
sma_long_t0 = iMA( NULL, 0, sma_long, 0, MODE_SMA, PRICE_CLOSE, 0 );
sma_diff_t3 = sma_long_t3 - sma_short_t3;
sma_diff_t0 = sma_long_t0 - sma_short_t0;
if ( OpenOrder )
{
if ( CloseLong || CloseShort )
{
OrderClose( OrderTicket(), OrderLots(), Bid, slippage, MediumSeaGreen );
OpenOrder = False;
CloseLong = False;
CloseShort = False;
}
}
if ( sma_diff_t3 < 0 && sma_diff_t0 > fakeout )
{
OpenLong = True ;
CloseShort = True;
}
if ( sma_diff_t3 > 0 && sma_diff_t0 < -fakeout )
{
OpenShort = True;
CloseLong = True;
}
lots = risk * 0.01 * AccountBalance() / ( MarketInfo( Symbol(), MODE_LOTSIZE ) * stoploss * P * Point ); // Sizing Algo based on account size
if ( isYenPair == true ) lots = lots * 100; // Adjust for Yen Pairs
lots = NormalizeDouble( lots, 2 );
if ( OpenLong )
{
stoplosslevel = Ask - stoploss * Point * P;
OrderSend( Symbol(), OP_BUY, lots, Ask, slippage, stoplosslevel, 0, "Buy(#" + magicnumber + ")", magicnumber, 0, DodgerBlue );
OpenOrder = True;
}
if ( OpenShort )
{
stoplosslevel = Bid + stoploss * Point * P;
OrderSend( Symbol(), OP_SELL, lots, Ask, slippage, stoplosslevel, 0, "Buy(#" + magicnumber + ")", magicnumber, 0, DodgerBlue );
OpenOrder = True ;
}
}
//+------------------------------------------------------------------+
and why do you use (MarketInfo(Symbol(),MODE_LOTSIZE)? what is the idea of that? first try with double lots = 1.00; and if problem still exists - please add a line telling about the reason why ea failed to send. sth like int ticket = OrderSend(***); if(ticket<0)Print("error=",GetLastError()); or more complex telling about the actual prices, lots, stoploss etc.
Few things in MQL4 to rather get used to:
All PriceDOMAIN data has to be NormalizeDouble() before sending to MetaTrader 4 Server.
All EquityDOMAIN data has to follow a set of discrete values,having MathMin( aMinLOT_SIZE + N * aMinLOT_STEP, aMaxLOT_SIZE ). Normalisation of EquityDOMAIN data is broker-specificand instrument-specific, so need not be always 2.
For XTO, OrderSend(), OrderMOdify(), OrderClose(), one ought follow something like this:
if ( OpenLong )
{ stoplosslevel = NormalizeDouble( Ask - stoploss * Point * P, _Digits ); // ALWAYS NormalizeDouble()
int RetCODE = OrderSend( _Symbol,
OP_BUY,
lots,
Ask,
slippage,
stoplosslevel,
0,
"Buy(#" + magicnumber + ")",
magicnumber,
0,
DodgerBlue
);
if ( RetCODE < 0 )
{ Print( "EXC: Tried to go LONG, OrderSend() failed to get confirmed ( Errno: ", GetLastError(), " )" );
}
else
{ OpenOrder = True;
...
}
...
}

pthred_exit return variable static vs global scope

I am seeing different behaviors when variable used to get return values using pthread_join is defined gloabal vs static scope. I have included code_snippet here.
Static variables
int main()
{
static int r1,r2;
pthread_t t1, t2;
int i1[] = {1,2};
int i2[] = {3,4};
r1 = pthread_create( &t1, NULL, myfn, (void*)i1);
r2 = pthread_create( &t2, NULL, myfn, (void*)i2);
pthread_join( t1, (void *)&r1 );
pthread_join( t2, (void *)&r2 );
printf("Thread 1 returns: %d\n",r1);
printf("Thread 2 returns: %d\n",r2);
return 0;
}
void *myfn( void *intarray )
{
pthread_t t=pthread_self();
int *g = (int *) intarray;
int i=0;
int d=1;
for (i=g[0];i<=g[1];i++)
d*=i;
fprintf(stderr, "TID=%u %d\n",t, d);
pthread_exit((void *)d);
}
Return value
TID=3425117952 12
TID=3433510656 2
Thread 1 returns: 2
Thread 2 returns: 12
Global variables
int r1,r2;
int main()
{
same as above
}
void *myfn( void *intarray )
{
same as above
}
Return value
TID=3425117952 12
TID=3433510656 2
Thread 1 returns: 0 <<<<< it returns 0
Thread 2 returns: 12
Could someone please explain why it behaves differently ?
Almost certainly it's because the size of int and void * differ on your platform, so when pthread_join() writes a void * value through the int * pointer you gave it, it overwrites adjacent memory.
The different declaration of r1 and r2 changes the layout of the variables enough to change the effect you see.
Casting an int to void * in order to return it is messy; you're better off either allocating space for a result in the main thread and passing that to the thread when it starts, or have the thread allocate the result and return a pointer to it when it finishes.
However, if you insist on the cast to void method, you can fix it by passing the address of an actual void * object to pthread_join and then casting from that to int:
int main()
{
static int r1,r2;
void *result;
pthread_t t1, t2;
int i1[] = {1,2};
int i2[] = {3,4};
r1 = pthread_create( &t1, NULL, myfn, (void*)i1);
r2 = pthread_create( &t2, NULL, myfn, (void*)i2);
pthread_join( t1, &result );
r1 = (int)result;
pthread_join( t2, &result );
r2 = (int)result;
printf("Thread 1 returns: %d\n",r1);
printf("Thread 2 returns: %d\n",r2);
return 0;
}

SlimDX (DirectX10) - How to change a texel in Texture?

I try to change the texels of a Texture which is already loaded.
My assumption was to use the Texture2D::Map and UnMap functions, but there is no change when I change the data of given DataRectangle.
I need a simple example like, creating a texture of 128x128 with a gradient from black to white from each side.
Thx
ps: A Direct3D 10 C++ example may also help, SlimDX is only a wrapper and has nearly complete the same functions.
This is my D3D10 2D texture loader
bool D3D10Texture::Init( GFXHandler* pHandler, unsigned int usage, unsigned int width, unsigned int height, unsigned int textureType, bool bMipmapped, void* pTextureData )
{
mMipmapped = bMipmapped;
//SetData( pHandler, 0 );
D3D10Handler* pD3DHandler = (D3D10Handler*)pHandler;
ID3D10Device* pDevice = pD3DHandler->GetDevice();
DXGI_SAMPLE_DESC dxgiSampleDesc;
dxgiSampleDesc.Count = 1;
dxgiSampleDesc.Quality = 0;
D3D10_USAGE d3d10Usage;
if ( usage & RU_All_Dynamic ) d3d10Usage = D3D10_USAGE_DYNAMIC;
else d3d10Usage = D3D10_USAGE_DEFAULT;
//unsigned int cpuAccess = D3D10_CPU_ACCESS_WRITE;
//if ( (usage & RU_Buffer_WriteOnly) == 0 ) cpuAccess |= D3D10_CPU_ACCESS_READ;
unsigned int cpuAccess = 0;
if ( !pTextureData )
{
cpuAccess = D3D10_CPU_ACCESS_WRITE;
//if ( (usage & RU_Buffer_WriteOnly) == 0 ) cpuAccess |= D3D10_CPU_ACCESS_READ;
}
unsigned int bindFlags = D3D10_BIND_SHADER_RESOURCE;
if ( usage & RU_Texture_RenderTarget ) bindFlags |= D3D10_BIND_RENDER_TARGET;
unsigned int miscFlags = 0;
if ( usage & RU_Texture_AutoGenMipmap ) miscFlags |= D3D10_RESOURCE_MISC_GENERATE_MIPS;
D3D10_TEXTURE2D_DESC d3d10Texture2DDesc;
d3d10Texture2DDesc.Width = width;
d3d10Texture2DDesc.Height = height;
d3d10Texture2DDesc.MipLevels = GetNumMipMaps( width, height, bMipmapped );
d3d10Texture2DDesc.ArraySize = 1;
d3d10Texture2DDesc.Format = GetD3DFormat( (TextureTypes)textureType );
d3d10Texture2DDesc.SampleDesc = dxgiSampleDesc;
d3d10Texture2DDesc.Usage = d3d10Usage;
d3d10Texture2DDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
d3d10Texture2DDesc.CPUAccessFlags = cpuAccess;
d3d10Texture2DDesc.MiscFlags = miscFlags;
//D3D10_SUBRESOURCE_DATA d3d10SubResourceData;
//d3d10SubResourceData.pSysMem = pTextureData;
//d3d10SubResourceData.SysMemPitch = GetPitch( width, (TextureTypes)textureType );
//d3d10SubResourceData.SysMemSlicePitch = 0;
D3D10_SUBRESOURCE_DATA* pSubResourceData = NULL;
if ( pTextureData )
{
pSubResourceData = new D3D10_SUBRESOURCE_DATA[d3d10Texture2DDesc.MipLevels];
char* pTexPos = (char*)pTextureData;
unsigned int pitch = GetPitch( width, (TextureTypes)textureType );
unsigned int count = 0;
unsigned int max = d3d10Texture2DDesc.MipLevels;
while( count < max )
{
pSubResourceData[count].pSysMem = pTexPos;
pSubResourceData[count].SysMemPitch = pitch;
pSubResourceData[count].SysMemSlicePitch = 0;
pTexPos += pitch * height;
pitch >>= 1;
count++;
}
}
if ( FAILED( pDevice->CreateTexture2D( &d3d10Texture2DDesc, pSubResourceData, &mpTexture ) ) )
{
return false;
}
if ( pSubResourceData )
{
delete[] pSubResourceData;
pSubResourceData = NULL;
}
mWidth = width;
mHeight = height;
mFormat = (TextureTypes)textureType;
mpTexture->AddRef();
mpTexture->Release();
D3D10_SHADER_RESOURCE_VIEW_DESC d3d10ShaderResourceViewDesc;
d3d10ShaderResourceViewDesc.Format = d3d10Texture2DDesc.Format;
d3d10ShaderResourceViewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
d3d10ShaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
d3d10ShaderResourceViewDesc.Texture2D.MipLevels = GetNumMipMaps( width, height, bMipmapped );
if ( FAILED( pDevice->CreateShaderResourceView( mpTexture, &d3d10ShaderResourceViewDesc, &mpView ) ) )
{
return false;
}
ResourceRecorder::Instance()->AddResource( this );
return true;
}
With that function all you need to do is pass in the whit to black texture. For example to write a 256x256 textue with each horizontal line being one brighter than the previous line the following code will work
int* pTexture = new int[256 * 256];
int count = 0;
while( count < 256 )
{
int count2 = 0;
while( count2 < 256 )
{
pTexture[(count * 256) + count2] = 0xff000000 | (count << 16) | (count << 8) | count;
count2++;
}
count++;
}
Make sure you follow the rules in the "Resource Usage Restrictions" section:
MSDN: D3D10_USAGE
public void NewData(byte[] newData)
{
DataRectangle mappedTex = null;
//assign and lock the resource
mappedTex = pTexture.Map(0, D3D10.MapMode.WriteDiscard, D3D10.MapFlags.None);
// if unable to hold texture
if (!mappedTex.Data.CanWrite)
{
throw new ApplicationException("Cannot Write to the Texture");
}
// write new data to the texture
mappedTex.Data.WriteRange<byte>(newData);
// unlock the resource
pTexture.Unmap(0);
if (samplerflag)
temptex = newData;
}
this overwrites the buffer on every new frame, you may want to use a D3D10.MapMode.readwrite or something if ur only trying to write one texel
you will also need to write to the datarectangle in a specific point using one of the other write functions

Resources